2 * Copyright (C) 2009, 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "ScriptState.h"
37 #include "ScriptController.h"
38 #include "V8DOMWindow.h"
39 #include "V8HiddenPropertyName.h"
41 #include "WorkerContext.h"
42 #include "WorkerContextExecutionProxy.h"
43 #include "WorkerScriptController.h"
46 #include <wtf/Assertions.h>
50 ScriptState::ScriptState(v8::Handle<v8::Context> context)
51 : m_context(v8::Persistent<v8::Context>::New(context))
53 m_context.MakeWeak(this, &ScriptState::weakReferenceCallback);
56 ScriptState::~ScriptState()
62 DOMWindow* ScriptState::domWindow() const
64 v8::HandleScope handleScope;
65 v8::Handle<v8::Object> v8RealGlobal = v8::Handle<v8::Object>::Cast(m_context->Global()->GetPrototype());
66 return V8DOMWindow::toNative(v8RealGlobal);
69 ScriptState* ScriptState::forContext(v8::Local<v8::Context> context)
71 v8::Context::Scope contextScope(context);
73 v8::Local<v8::Object> global = context->Global();
74 // Skip proxy object. The proxy object will survive page navigation while we need
75 // an object whose lifetime consides with that of the inspected context.
76 global = v8::Local<v8::Object>::Cast(global->GetPrototype());
78 v8::Handle<v8::String> key = V8HiddenPropertyName::scriptState();
79 v8::Local<v8::Value> val = global->GetHiddenValue(key);
80 if (!val.IsEmpty() && val->IsExternal())
81 return static_cast<ScriptState*>(v8::External::Cast(*val)->Value());
83 ScriptState* state = new ScriptState(context);
84 global->SetHiddenValue(key, v8::External::New(state));
88 ScriptState* ScriptState::current()
90 v8::HandleScope handleScope;
91 v8::Local<v8::Context> context = v8::Context::GetCurrent();
92 if (context.IsEmpty()) {
96 return ScriptState::forContext(context);
99 void ScriptState::weakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
101 ScriptState* scriptState = static_cast<ScriptState*>(parameter);
105 DOMWindow* domWindowFromScriptState(ScriptState* scriptState)
107 return scriptState->domWindow();
110 ScriptState* mainWorldScriptState(Frame* frame)
112 v8::HandleScope handleScope;
113 V8Proxy* proxy = frame->script()->proxy();
114 return ScriptState::forContext(proxy->mainWorldContext());
117 ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node* node)
119 // This should be never reached with V8 bindings (WebKit only uses it
120 // for non-JS bindings)
121 ASSERT_NOT_REACHED();
125 ScriptState* scriptStateFromPage(DOMWrapperWorld*, Page* page)
127 // This should be only reached with V8 bindings from single process layout tests.
128 return mainWorldScriptState(page->mainFrame());
132 ScriptState* scriptStateFromWorkerContext(WorkerContext* workerContext)
134 WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
138 v8::HandleScope handleScope;
139 v8::Local<v8::Context> context = proxy->context();
140 return ScriptState::forContext(context);