2012-01-19 19:52:59 +01:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
2010-10-03 23:04:50 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#ifndef CEF_LIBCEF_V8_IMPL_H_
|
|
|
|
#define CEF_LIBCEF_V8_IMPL_H_
|
|
|
|
#pragma once
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <vector>
|
2012-05-23 21:01:04 +02:00
|
|
|
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/cef_v8.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "v8/include/v8.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/cef_thread.h"
|
|
|
|
#include "base/memory/ref_counted.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
class CefTrackNode;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
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 v8class>
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefReleaseV8HandleOnUIThread
|
|
|
|
: public base::RefCountedThreadSafe<CefReleaseV8HandleOnUIThread<v8class>,
|
|
|
|
CefThread::DeleteOnUIThread> {
|
|
|
|
public:
|
2011-02-21 23:44:06 +01:00
|
|
|
typedef v8::Handle<v8class> handleType;
|
|
|
|
typedef v8::Persistent<v8class> persistentType;
|
|
|
|
typedef CefReleaseV8HandleOnUIThread<v8class> superType;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
explicit CefReleaseV8HandleOnUIThread(handleType v) {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8_handle_ = persistentType::New(v);
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual ~CefReleaseV8HandleOnUIThread() {
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
handleType GetHandle() {
|
2011-02-21 23:44:06 +01:00
|
|
|
return v8_handle_;
|
|
|
|
}
|
|
|
|
|
|
|
|
persistentType v8_handle_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Special class for a v8::Context to ensure that it is deleted from the UI
|
|
|
|
// thread.
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefV8ContextHandle : public CefReleaseV8HandleOnUIThread<v8::Context> {
|
|
|
|
public:
|
|
|
|
explicit CefV8ContextHandle(handleType context)
|
|
|
|
: superType(context) {
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Context handles are disposed rather than makeweak.
|
2012-01-10 00:46:23 +01:00
|
|
|
~CefV8ContextHandle() {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8_handle_.Dispose();
|
|
|
|
v8_handle_.Clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefV8ContextImpl : public CefV8Context {
|
|
|
|
public:
|
|
|
|
explicit CefV8ContextImpl(v8::Handle<v8::Context> context);
|
2011-02-21 23:44:06 +01:00
|
|
|
virtual ~CefV8ContextImpl();
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual CefRefPtr<CefBrowser> GetBrowser() OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefFrame> GetFrame() OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefV8Value> GetGlobal() OVERRIDE;
|
|
|
|
virtual bool Enter() OVERRIDE;
|
|
|
|
virtual bool Exit() OVERRIDE;
|
2011-11-21 22:21:55 +01:00
|
|
|
virtual bool IsSame(CefRefPtr<CefV8Context> that) OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual bool Eval(const CefString& code,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
|
|
|
CefRefPtr<CefV8Exception>& exception) OVERRIDE;
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::Local<v8::Context> GetContext();
|
|
|
|
WebKit::WebFrame* GetWebFrame();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-02-21 23:44:06 +01:00
|
|
|
scoped_refptr<CefV8ContextHandle> v8_context_;
|
2011-04-07 03:58:49 +02:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2011-04-07 03:58:49 +02:00
|
|
|
// Used in debug builds to catch missing Exits in destructor.
|
|
|
|
int enter_count_;
|
|
|
|
#endif
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefV8ContextImpl);
|
2011-02-21 23:44:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Special class for a v8::Value to ensure that it is deleted from the UI
|
|
|
|
// thread.
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefV8ValueHandle: public CefReleaseV8HandleOnUIThread<v8::Value> {
|
|
|
|
public:
|
2012-01-19 19:52:59 +01:00
|
|
|
CefV8ValueHandle(handleType value, CefTrackNode* tracker)
|
2012-01-10 00:46:23 +01:00
|
|
|
: superType(value),
|
|
|
|
tracker_(tracker) {
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
// Destructor implementation is provided in v8_impl.cc.
|
|
|
|
~CefV8ValueHandle();
|
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
CefTrackNode* GetTracker() {
|
|
|
|
return tracker_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
2011-02-21 23:44:06 +01:00
|
|
|
// 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.
|
2012-01-19 19:52:59 +01:00
|
|
|
CefTrackNode* tracker_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8ValueHandle);
|
2011-02-21 23:44:06 +01:00
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefV8ValueImpl : public CefV8Value {
|
|
|
|
public:
|
2012-01-19 19:52:59 +01:00
|
|
|
CefV8ValueImpl(v8::Handle<v8::Value> value, CefTrackNode* tracker = NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual ~CefV8ValueImpl();
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool IsUndefined() OVERRIDE;
|
|
|
|
virtual bool IsNull() OVERRIDE;
|
|
|
|
virtual bool IsBool() OVERRIDE;
|
|
|
|
virtual bool IsInt() OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual bool IsUInt() OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool IsDouble() OVERRIDE;
|
2011-05-23 19:43:53 +02:00
|
|
|
virtual bool IsDate() OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool IsString() OVERRIDE;
|
|
|
|
virtual bool IsObject() OVERRIDE;
|
|
|
|
virtual bool IsArray() OVERRIDE;
|
|
|
|
virtual bool IsFunction() OVERRIDE;
|
|
|
|
virtual bool IsSame(CefRefPtr<CefV8Value> value) OVERRIDE;
|
|
|
|
virtual bool GetBoolValue() OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual int32 GetIntValue() OVERRIDE;
|
|
|
|
virtual uint32 GetUIntValue() OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual double GetDoubleValue() OVERRIDE;
|
2011-05-23 19:43:53 +02:00
|
|
|
virtual CefTime GetDateValue() OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual CefString GetStringValue() OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual bool IsUserCreated() OVERRIDE;
|
|
|
|
virtual bool HasException() OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefV8Exception> GetException() OVERRIDE;
|
|
|
|
virtual bool ClearException() OVERRIDE;
|
|
|
|
virtual bool WillRethrowExceptions() OVERRIDE;
|
|
|
|
virtual bool SetRethrowExceptions(bool rethrow) OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool HasValue(const CefString& key) OVERRIDE;
|
2011-12-08 11:22:15 +01:00
|
|
|
virtual bool HasValue(int index) OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual bool DeleteValue(const CefString& key) OVERRIDE;
|
2011-12-08 11:22:15 +01:00
|
|
|
virtual bool DeleteValue(int index) OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual CefRefPtr<CefV8Value> GetValue(const CefString& key) OVERRIDE;
|
2011-12-08 11:22:15 +01:00
|
|
|
virtual CefRefPtr<CefV8Value> GetValue(int index) OVERRIDE;
|
2011-11-04 20:34:14 +01:00
|
|
|
virtual bool SetValue(const CefString& key, CefRefPtr<CefV8Value> value,
|
|
|
|
PropertyAttribute attribute) OVERRIDE;
|
2011-12-08 11:22:15 +01:00
|
|
|
virtual bool SetValue(int index, CefRefPtr<CefV8Value> value) OVERRIDE;
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual bool SetValue(const CefString& key, AccessControl settings,
|
2011-05-20 16:42:25 +02:00
|
|
|
PropertyAttribute attribute) OVERRIDE;
|
|
|
|
virtual bool GetKeys(std::vector<CefString>& keys) OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual bool SetUserData(CefRefPtr<CefBase> user_data) OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual CefRefPtr<CefBase> GetUserData() OVERRIDE;
|
2012-01-19 19:52:59 +01:00
|
|
|
virtual int GetExternallyAllocatedMemory() OVERRIDE;
|
|
|
|
virtual int AdjustExternallyAllocatedMemory(int change_in_bytes) OVERRIDE;
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual int GetArrayLength() OVERRIDE;
|
|
|
|
virtual CefString GetFunctionName() OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefV8Handler> GetFunctionHandler() OVERRIDE;
|
2012-05-23 21:01:04 +02:00
|
|
|
virtual CefRefPtr<CefV8Value> ExecuteFunction(
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefV8Value> ExecuteFunctionWithContext(
|
|
|
|
CefRefPtr<CefV8Context> context,
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) OVERRIDE;
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
inline v8::Handle<v8::Value> GetHandle() {
|
2011-02-21 23:44:06 +01:00
|
|
|
DCHECK(v8_value_.get());
|
|
|
|
return v8_value_->GetHandle();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2012-05-23 21:01:04 +02:00
|
|
|
// Test for and record any exception.
|
|
|
|
bool HasCaught(v8::TryCatch& try_catch);
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
scoped_refptr<CefV8ValueHandle> v8_value_;
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Exception> last_exception_;
|
|
|
|
bool rethrow_exceptions_;
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefV8ValueImpl);
|
2012-01-19 19:52:59 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8ValueImpl);
|
2010-10-03 23:04:50 +02:00
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // CEF_LIBCEF_V8_IMPL_H_
|