// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #ifndef _V8_IMPL_H #define _V8_IMPL_H #include "include/cef.h" #include "v8/include/v8.h" class CefTrackObject; namespace WebKit { class WebFrame; }; // Template for V8 Handle types. This class is used to ensure that V8 objects // are only released on the UI thread. template class CefReleaseV8HandleOnUIThread: public base::RefCountedThreadSafe, CefThread::DeleteOnUIThread> { public: typedef v8::Handle handleType; typedef v8::Persistent persistentType; typedef CefReleaseV8HandleOnUIThread superType; CefReleaseV8HandleOnUIThread(handleType v) { v8_handle_ = persistentType::New(v); } virtual ~CefReleaseV8HandleOnUIThread() { } handleType GetHandle() { return v8_handle_; } persistentType v8_handle_; }; // Special class for a v8::Context to ensure that it is deleted from the UI // thread. class CefV8ContextHandle : public CefReleaseV8HandleOnUIThread { public: CefV8ContextHandle(handleType context): superType(context) { } // Context handles are disposed rather than makeweak. ~CefV8ContextHandle() { v8_handle_.Dispose(); v8_handle_.Clear(); } }; class CefV8ContextImpl : public CefThreadSafeBase { public: CefV8ContextImpl(v8::Handle context); virtual ~CefV8ContextImpl(); virtual CefRefPtr GetBrowser(); virtual CefRefPtr GetFrame(); virtual CefRefPtr GetGlobal(); v8::Local GetContext(); WebKit::WebFrame* GetWebFrame(); protected: scoped_refptr v8_context_; }; // Special class for a v8::Value to ensure that it is deleted from the UI // thread. class CefV8ValueHandle: public CefReleaseV8HandleOnUIThread { public: CefV8ValueHandle(handleType value, CefTrackObject* tracker) : superType(value), tracker_(tracker) { } // Destructor implementation is provided in v8_impl.cc. ~CefV8ValueHandle(); private: // For Object and Function types, we need to hold on to a reference to their // internal data or function handler objects that are reference counted. CefTrackObject *tracker_; }; class CefV8ValueImpl : public CefThreadSafeBase { public: CefV8ValueImpl(v8::Handle value, CefTrackObject* tracker = NULL); virtual ~CefV8ValueImpl(); virtual bool IsUndefined(); virtual bool IsNull(); virtual bool IsBool(); virtual bool IsInt(); virtual bool IsDouble(); virtual bool IsString(); virtual bool IsObject(); virtual bool IsArray(); virtual bool IsFunction(); virtual bool GetBoolValue(); virtual int GetIntValue(); virtual double GetDoubleValue(); virtual CefString GetStringValue(); virtual bool HasValue(const CefString& key); virtual bool HasValue(int index); virtual bool DeleteValue(const CefString& key); virtual bool DeleteValue(int index); virtual CefRefPtr GetValue(const CefString& key); virtual CefRefPtr GetValue(int index); virtual bool SetValue(const CefString& key, CefRefPtr value); virtual bool SetValue(int index, CefRefPtr value); virtual bool GetKeys(std::vector& keys); virtual CefRefPtr GetUserData(); virtual int GetArrayLength(); virtual CefString GetFunctionName(); virtual CefRefPtr GetFunctionHandler(); virtual bool ExecuteFunction(CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); virtual bool ExecuteFunctionWithContext( CefRefPtr context, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); inline v8::Handle GetHandle() { DCHECK(v8_value_.get()); return v8_value_->GetHandle(); } bool IsReservedKey(const CefString& key); protected: scoped_refptr v8_value_; }; #endif //_V8_IMPL_H