Allow creation of V8 wrapper objects on any thread with a V8 isolate (issue #451).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@967 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2012-12-30 21:16:59 +00:00
parent e33ea0aff4
commit e574751caa
4 changed files with 213 additions and 169 deletions

View File

@ -10,10 +10,12 @@
#include "include/cef_v8.h"
#include "libcef/common/tracker.h"
#include "libcef/renderer/thread_util.h"
#include "v8/include/v8.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop_proxy.h"
class CefTrackNode;
@ -52,25 +54,50 @@ class CefV8ContextState : public base::RefCounted<CefV8ContextState> {
CefTrackManager track_manager_;
};
// Use this template in conjuction with RefCountedThreadSafe to ensure that a
// V8 object is deleted on the correct thread.
struct CefV8DeleteOnMessageLoopThread {
template<typename T>
static void Destruct(const T* x) {
if (x->message_loop_proxy_->BelongsToCurrentThread()) {
delete x;
} else {
if (!x->message_loop_proxy_->DeleteSoon(FROM_HERE, x)) {
#if defined(UNIT_TEST)
// Only logged under unit testing because leaks at shutdown
// are acceptable under normal circumstances.
LOG(ERROR) << "DeleteSoon failed on thread " << thread;
#endif // UNIT_TEST
}
}
}
};
// Base class for V8 Handle types.
class CefV8HandleBase :
public base::RefCountedThreadSafe<CefV8HandleBase,
CefDeleteOnRenderThread> {
CefV8DeleteOnMessageLoopThread> {
public:
virtual ~CefV8HandleBase() {}
virtual ~CefV8HandleBase();
// Returns true if there is no underlying context or if the underlying context
// is valid.
bool IsValid() {
bool IsValid() const {
return (!context_state_.get() || context_state_->IsValid());
}
bool BelongsToCurrentThread() const;
protected:
// |context| is the context that owns this handle. If empty the current
// context will be used.
explicit CefV8HandleBase(v8::Handle<v8::Context> context);
protected:
friend struct CefV8DeleteOnMessageLoopThread;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
scoped_refptr<CefV8ContextState> context_state_;
};