Support implicit detachment of CEF V8 references when the associated context is released (issue #484).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@881 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2012-10-29 21:46:02 +00:00
parent 95b29d590d
commit 0cea9668fe
25 changed files with 505 additions and 31 deletions

View File

@ -20,25 +20,65 @@ namespace WebKit {
class WebFrame;
};
// 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,
CefDeleteOnRenderThread> {
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_;
};
// Template for V8 Handle types. This class is used to ensure that V8 objects
// are only released on the render thread.
template <typename v8class>
class CefV8Handle :
public base::RefCountedThreadSafe<CefV8Handle<v8class>,
CefDeleteOnRenderThread> {
class CefV8Handle : public CefV8HandleBase {
public:
typedef v8::Handle<v8class> handleType;
typedef v8::Persistent<v8class> persistentType;
CefV8Handle(handleType v)
: handle_(persistentType::New(v)) {
CefV8Handle(v8::Handle<v8::Context> context, handleType v)
: CefV8HandleBase(context),
handle_(persistentType::New(v)) {
}
~CefV8Handle() {
virtual ~CefV8Handle() {
handle_.Dispose();
handle_.Clear();
}
handleType GetHandle() { return handle_; }
handleType GetHandle() {
DCHECK(IsValid());
return handle_;
}
protected:
persistentType handle_;
@ -58,6 +98,7 @@ class CefV8ContextImpl : public CefV8Context {
explicit CefV8ContextImpl(v8::Handle<v8::Context> context);
virtual ~CefV8ContextImpl();
virtual bool IsValid() OVERRIDE;
virtual CefRefPtr<CefBrowser> GetBrowser() OVERRIDE;
virtual CefRefPtr<CefFrame> GetFrame() OVERRIDE;
virtual CefRefPtr<CefV8Value> GetGlobal() OVERRIDE;
@ -91,6 +132,7 @@ class CefV8ValueImpl : public CefV8Value {
CefV8ValueImpl(v8::Handle<v8::Value> value, CefTrackNode* tracker = NULL);
virtual ~CefV8ValueImpl();
virtual bool IsValid() OVERRIDE;
virtual bool IsUndefined() OVERRIDE;
virtual bool IsNull() OVERRIDE;
virtual bool IsBool() OVERRIDE;
@ -148,19 +190,22 @@ class CefV8ValueImpl : public CefV8Value {
// Test for and record any exception.
bool HasCaught(v8::TryCatch& try_catch);
class Handle :
public base::RefCountedThreadSafe<Handle, CefDeleteOnRenderThread> {
class Handle : public CefV8HandleBase {
public:
typedef v8::Handle<v8::Value> handleType;
typedef v8::Persistent<v8::Value> persistentType;
Handle(handleType v, CefTrackNode* tracker)
: handle_(persistentType::New(v)),
Handle(v8::Handle<v8::Context> context, handleType v, CefTrackNode* tracker)
: CefV8HandleBase(context),
handle_(persistentType::New(v)),
tracker_(tracker) {
}
~Handle();
virtual ~Handle();
handleType GetHandle() { return handle_; }
handleType GetHandle() {
DCHECK(IsValid());
return handle_;
}
private:
persistentType handle_;
@ -185,6 +230,7 @@ class CefV8StackTraceImpl : public CefV8StackTrace {
explicit CefV8StackTraceImpl(v8::Handle<v8::StackTrace> handle);
virtual ~CefV8StackTraceImpl();
virtual bool IsValid() OVERRIDE;
virtual int GetFrameCount() OVERRIDE;
virtual CefRefPtr<CefV8StackFrame> GetFrame(int index) OVERRIDE;
@ -203,6 +249,7 @@ class CefV8StackFrameImpl : public CefV8StackFrame {
explicit CefV8StackFrameImpl(v8::Handle<v8::StackFrame> handle);
virtual ~CefV8StackFrameImpl();
virtual bool IsValid() OVERRIDE;
virtual CefString GetScriptName() OVERRIDE;
virtual CefString GetScriptNameOrSourceURL() OVERRIDE;
virtual CefString GetFunctionName() OVERRIDE;