diff options
Diffstat (limited to 'WebCore/bindings/v8/ScriptState.cpp')
-rw-r--r-- | WebCore/bindings/v8/ScriptState.cpp | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/WebCore/bindings/v8/ScriptState.cpp b/WebCore/bindings/v8/ScriptState.cpp index 68593e7..fac1d26 100644 --- a/WebCore/bindings/v8/ScriptState.cpp +++ b/WebCore/bindings/v8/ScriptState.cpp @@ -35,22 +35,17 @@ #include "Node.h" #include "Page.h" #include "ScriptController.h" +#include "V8HiddenPropertyName.h" +#include <v8.h> #include <wtf/Assertions.h> -#include <wtf/StdLibExtras.h> namespace WebCore { -ScriptState::ScriptState(Frame* frame) - : m_frame(frame) - , m_context(v8::Persistent<v8::Context>::New(V8Proxy::mainWorldContext(frame))) -{ -} - -ScriptState::ScriptState(Frame* frame, v8::Handle<v8::Context> context) - : m_frame(frame) - , m_context(v8::Persistent<v8::Context>::New(context)) +ScriptState::ScriptState(v8::Handle<v8::Context> context) + : m_context(v8::Persistent<v8::Context>::New(context)) { + m_context.MakeWeak(this, &ScriptState::weakReferenceCallback); } ScriptState::~ScriptState() @@ -59,6 +54,49 @@ ScriptState::~ScriptState() m_context.Clear(); } +ScriptState* ScriptState::forContext(v8::Local<v8::Context> context) +{ + v8::Context::Scope contextScope(context); + + v8::Local<v8::Object> global = context->Global(); + // Skip proxy object. The proxy object will survive page navigation while we need + // an object whose lifetime consides with that of the inspected context. + global = v8::Local<v8::Object>::Cast(global->GetPrototype()); + + v8::Handle<v8::String> key = V8HiddenPropertyName::scriptState(); + v8::Local<v8::Value> val = global->GetHiddenValue(key); + if (!val.IsEmpty() && val->IsExternal()) + return static_cast<ScriptState*>(v8::External::Cast(*val)->Value()); + + ScriptState* state = new ScriptState(context); + global->SetHiddenValue(key, v8::External::New(state)); + return state; +} + +ScriptState* ScriptState::current() +{ + v8::HandleScope handleScope; + v8::Local<v8::Context> context = v8::Context::GetCurrent(); + if (context.IsEmpty()) { + ASSERT_NOT_REACHED(); + return 0; + } + return ScriptState::forContext(context); +} + +void ScriptState::weakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter) +{ + ScriptState* scriptState = static_cast<ScriptState*>(parameter); + delete scriptState; +} + +ScriptState* mainWorldScriptState(Frame* frame) +{ + v8::HandleScope handleScope; + V8Proxy* proxy = frame->script()->proxy(); + return ScriptState::forContext(proxy->mainWorldContext()); +} + ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node* node) { // This should be never reached with V8 bindings (WebKit only uses it @@ -70,14 +108,7 @@ ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node* node) ScriptState* scriptStateFromPage(DOMWrapperWorld*, Page* page) { // This should be only reached with V8 bindings from single process layout tests. - return page->mainFrame()->script()->mainWorldScriptState(); -} - -// FIXME: Stub method so we compile. Currently called from FrameLoader.cpp. -DOMWrapperWorld* mainThreadNormalWorld() -{ - DEFINE_STATIC_LOCAL(DOMWrapperWorld, oneWorld, ()); - return &oneWorld; + return mainWorldScriptState(page->mainFrame()); } } |