initial import
[vuplus_webkit] / Source / WebCore / bindings / v8 / ScriptState.cpp
1 /*
2  * Copyright (C) 2009, 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "ScriptState.h"
33
34 #include "Frame.h"
35 #include "Node.h"
36 #include "Page.h"
37 #include "ScriptController.h"
38 #include "V8DOMWindow.h"
39 #include "V8HiddenPropertyName.h"
40
41 #include "WorkerContext.h"
42 #include "WorkerContextExecutionProxy.h"
43 #include "WorkerScriptController.h"
44
45 #include <v8.h>
46 #include <wtf/Assertions.h>
47
48 namespace WebCore {
49
50 ScriptState::ScriptState(v8::Handle<v8::Context> context)
51     : m_context(v8::Persistent<v8::Context>::New(context))
52 {
53     m_context.MakeWeak(this, &ScriptState::weakReferenceCallback);
54 }
55
56 ScriptState::~ScriptState()
57 {
58     m_context.Dispose();
59     m_context.Clear();
60 }
61
62 DOMWindow* ScriptState::domWindow() const
63 {
64     v8::HandleScope handleScope;
65     v8::Handle<v8::Object> v8RealGlobal = v8::Handle<v8::Object>::Cast(m_context->Global()->GetPrototype());
66     return V8DOMWindow::toNative(v8RealGlobal);
67 }
68
69 ScriptState* ScriptState::forContext(v8::Local<v8::Context> context)
70 {
71     v8::Context::Scope contextScope(context);
72
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());
77
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());
82
83     ScriptState* state = new ScriptState(context);
84     global->SetHiddenValue(key, v8::External::New(state));
85     return state;
86 }
87
88 ScriptState* ScriptState::current()
89 {
90     v8::HandleScope handleScope;
91     v8::Local<v8::Context> context = v8::Context::GetCurrent();
92     if (context.IsEmpty()) {
93         ASSERT_NOT_REACHED();
94         return 0;
95     }
96     return ScriptState::forContext(context);
97 }
98
99 void ScriptState::weakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
100 {
101     ScriptState* scriptState = static_cast<ScriptState*>(parameter);
102     delete scriptState;
103 }
104
105 DOMWindow* domWindowFromScriptState(ScriptState* scriptState)
106 {
107     return scriptState->domWindow();
108 }
109
110 ScriptState* mainWorldScriptState(Frame* frame)
111 {
112     v8::HandleScope handleScope;
113     V8Proxy* proxy = frame->script()->proxy();
114     return ScriptState::forContext(proxy->mainWorldContext());
115 }
116
117 ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node* node)
118 {
119     // This should be never reached with V8 bindings (WebKit only uses it
120     // for non-JS bindings)
121     ASSERT_NOT_REACHED();
122     return 0;
123 }
124
125 ScriptState* scriptStateFromPage(DOMWrapperWorld*, Page* page)
126 {
127     // This should be only reached with V8 bindings from single process layout tests.
128     return mainWorldScriptState(page->mainFrame());
129 }
130
131 #if ENABLE(WORKERS)
132 ScriptState* scriptStateFromWorkerContext(WorkerContext* workerContext)
133 {
134     WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
135     if (!proxy)
136         return 0;
137
138     v8::HandleScope handleScope;
139     v8::Local<v8::Context> context = proxy->context();
140     return ScriptState::forContext(context);
141 }
142 #endif
143
144 }