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;
|
|
|
|
};
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
// Call to detach all handles associated with the specified contxt.
|
|
|
|
void CefV8ReleaseContext(v8::Handle<v8::Context> context);
|
|
|
|
|
|
|
|
// Used to detach handles when the associated context is released.
|
|
|
|
class CefV8ContextState : public base::RefCounted<CefV8ContextState> {
|
|
|
|
public:
|
|
|
|
CefV8ContextState() : valid_(true) {}
|
|
|
|
virtual ~CefV8ContextState() {}
|
|
|
|
|
|
|
|
bool IsValid() { return valid_; }
|
|
|
|
void Detach() { valid_ = false; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool valid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Base class for V8 Handle types.
|
|
|
|
class CefV8HandleBase :
|
|
|
|
public base::RefCountedThreadSafe<CefV8HandleBase,
|
|
|
|
CefThread::DeleteOnUIThread> {
|
|
|
|
public:
|
|
|
|
virtual ~CefV8HandleBase() {}
|
|
|
|
|
|
|
|
// Returns true if there is no underlying context or if the underlying context
|
|
|
|
// is valid.
|
|
|
|
bool IsValid() {
|
|
|
|
return (!context_state_.get() || context_state_->IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// |context| is the context that owns this handle. If empty the current
|
|
|
|
// context will be used.
|
|
|
|
explicit CefV8HandleBase(v8::Handle<v8::Context> context);
|
|
|
|
|
|
|
|
private:
|
|
|
|
scoped_refptr<CefV8ContextState> context_state_;
|
|
|
|
};
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
// Template for V8 Handle types. This class is used to ensure that V8 objects
|
|
|
|
// are only released on the UI thread.
|
2012-07-25 11:45:49 +02:00
|
|
|
template <typename v8class>
|
2012-10-29 22:54:57 +01:00
|
|
|
class CefV8Handle : public CefV8HandleBase {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
2011-02-21 23:44:06 +01:00
|
|
|
typedef v8::Handle<v8class> handleType;
|
|
|
|
typedef v8::Persistent<v8class> persistentType;
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
CefV8Handle(v8::Handle<v8::Context> context, handleType v)
|
|
|
|
: CefV8HandleBase(context),
|
|
|
|
handle_(persistentType::New(v)) {
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual ~CefV8Handle() {
|
2012-07-25 11:45:49 +02:00
|
|
|
handle_.Dispose();
|
|
|
|
handle_.Clear();
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
handleType GetHandle() {
|
|
|
|
DCHECK(IsValid());
|
|
|
|
return handle_;
|
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
protected:
|
|
|
|
persistentType handle_;
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8Handle);
|
|
|
|
};
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
// Specialization for v8::Value with empty implementation to avoid incorrect
|
|
|
|
// usage.
|
|
|
|
template <>
|
|
|
|
class CefV8Handle<v8::Value> {
|
2011-02-21 23:44:06 +01:00
|
|
|
};
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
|
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();
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual bool IsValid() OVERRIDE;
|
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-07-25 11:45:49 +02:00
|
|
|
v8::Handle<v8::Context> GetHandle() { return handle_->GetHandle(); }
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2012-07-25 11:45:49 +02:00
|
|
|
typedef CefV8Handle<v8::Context> Handle;
|
|
|
|
scoped_refptr<Handle> handle_;
|
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);
|
2012-07-25 11:45:49 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8ContextImpl);
|
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();
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual bool IsValid() OVERRIDE;
|
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-07-25 11:45:49 +02:00
|
|
|
v8::Handle<v8::Value> GetHandle() { return handle_->GetHandle(); }
|
2011-02-21 23:44:06 +01:00
|
|
|
|
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);
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
class Handle : public CefV8HandleBase {
|
2012-07-25 11:45:49 +02:00
|
|
|
public:
|
|
|
|
typedef v8::Handle<v8::Value> handleType;
|
|
|
|
typedef v8::Persistent<v8::Value> persistentType;
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
Handle(v8::Handle<v8::Context> context, handleType v, CefTrackNode* tracker)
|
|
|
|
: CefV8HandleBase(context),
|
|
|
|
handle_(persistentType::New(v)),
|
2012-07-25 11:45:49 +02:00
|
|
|
tracker_(tracker) {
|
|
|
|
}
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual ~Handle();
|
2012-07-25 11:45:49 +02:00
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
handleType GetHandle() {
|
|
|
|
DCHECK(IsValid());
|
|
|
|
return handle_;
|
|
|
|
}
|
2012-07-25 11:45:49 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
persistentType handle_;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
CefTrackNode* tracker_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Handle);
|
|
|
|
};
|
|
|
|
scoped_refptr<Handle> handle_;
|
|
|
|
|
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-07-25 11:45:49 +02:00
|
|
|
class CefV8StackTraceImpl : public CefV8StackTrace {
|
|
|
|
public:
|
|
|
|
explicit CefV8StackTraceImpl(v8::Handle<v8::StackTrace> handle);
|
|
|
|
virtual ~CefV8StackTraceImpl();
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual bool IsValid() OVERRIDE;
|
2012-07-25 11:45:49 +02:00
|
|
|
virtual int GetFrameCount() OVERRIDE;
|
|
|
|
virtual CefRefPtr<CefV8StackFrame> GetFrame(int index) OVERRIDE;
|
|
|
|
|
|
|
|
v8::Handle<v8::StackTrace> GetHandle() { return handle_->GetHandle(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef CefV8Handle<v8::StackTrace> Handle;
|
|
|
|
scoped_refptr<Handle> handle_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefV8StackTraceImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8StackTraceImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
class CefV8StackFrameImpl : public CefV8StackFrame {
|
|
|
|
public:
|
|
|
|
explicit CefV8StackFrameImpl(v8::Handle<v8::StackFrame> handle);
|
|
|
|
virtual ~CefV8StackFrameImpl();
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
virtual bool IsValid() OVERRIDE;
|
2012-07-25 11:45:49 +02:00
|
|
|
virtual CefString GetScriptName() OVERRIDE;
|
|
|
|
virtual CefString GetScriptNameOrSourceURL() OVERRIDE;
|
|
|
|
virtual CefString GetFunctionName() OVERRIDE;
|
|
|
|
virtual int GetLineNumber() OVERRIDE;
|
|
|
|
virtual int GetColumn() OVERRIDE;
|
|
|
|
virtual bool IsEval() OVERRIDE;
|
|
|
|
virtual bool IsConstructor() OVERRIDE;
|
|
|
|
|
|
|
|
v8::Handle<v8::StackFrame> GetHandle() { return handle_->GetHandle(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef CefV8Handle<v8::StackFrame> Handle;
|
|
|
|
scoped_refptr<Handle> handle_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefV8StackFrameImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefV8StackFrameImpl);
|
|
|
|
};
|
|
|
|
|
2012-10-12 18:38:24 +02:00
|
|
|
// Used to catch global exceptions.
|
|
|
|
void CefV8MessageHandler(v8::Handle<v8::Message> message,
|
|
|
|
v8::Handle<v8::Value> data);
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // CEF_LIBCEF_V8_IMPL_H_
|