• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "V8HiddenPropertyName.h"
39 
40 #include "WorkerContext.h"
41 #include "WorkerContextExecutionProxy.h"
42 #include "WorkerScriptController.h"
43 
44 #include <v8.h>
45 #include <wtf/Assertions.h>
46 
47 namespace WebCore {
48 
ScriptState(v8::Handle<v8::Context> context)49 ScriptState::ScriptState(v8::Handle<v8::Context> context)
50     : m_context(v8::Persistent<v8::Context>::New(context))
51 {
52     m_context.MakeWeak(this, &ScriptState::weakReferenceCallback);
53 }
54 
~ScriptState()55 ScriptState::~ScriptState()
56 {
57     m_context.Dispose();
58     m_context.Clear();
59 }
60 
forContext(v8::Local<v8::Context> context)61 ScriptState* ScriptState::forContext(v8::Local<v8::Context> context)
62 {
63     v8::Context::Scope contextScope(context);
64 
65     v8::Local<v8::Object> global = context->Global();
66     // Skip proxy object. The proxy object will survive page navigation while we need
67     // an object whose lifetime consides with that of the inspected context.
68     global = v8::Local<v8::Object>::Cast(global->GetPrototype());
69 
70     v8::Handle<v8::String> key = V8HiddenPropertyName::scriptState();
71     v8::Local<v8::Value> val = global->GetHiddenValue(key);
72     if (!val.IsEmpty() && val->IsExternal())
73         return static_cast<ScriptState*>(v8::External::Cast(*val)->Value());
74 
75     ScriptState* state = new ScriptState(context);
76     global->SetHiddenValue(key, v8::External::New(state));
77     return state;
78 }
79 
current()80 ScriptState* ScriptState::current()
81 {
82     v8::HandleScope handleScope;
83     v8::Local<v8::Context> context = v8::Context::GetCurrent();
84     if (context.IsEmpty()) {
85         ASSERT_NOT_REACHED();
86         return 0;
87     }
88     return ScriptState::forContext(context);
89 }
90 
weakReferenceCallback(v8::Persistent<v8::Value> object,void * parameter)91 void ScriptState::weakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
92 {
93     ScriptState* scriptState = static_cast<ScriptState*>(parameter);
94     delete scriptState;
95 }
96 
mainWorldScriptState(Frame * frame)97 ScriptState* mainWorldScriptState(Frame* frame)
98 {
99     v8::HandleScope handleScope;
100     V8Proxy* proxy = frame->script()->proxy();
101     return ScriptState::forContext(proxy->mainWorldContext());
102 }
103 
scriptStateFromNode(DOMWrapperWorld *,Node * node)104 ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node* node)
105 {
106     // This should be never reached with V8 bindings (WebKit only uses it
107     // for non-JS bindings)
108     ASSERT_NOT_REACHED();
109     return 0;
110 }
111 
scriptStateFromPage(DOMWrapperWorld *,Page * page)112 ScriptState* scriptStateFromPage(DOMWrapperWorld*, Page* page)
113 {
114     // This should be only reached with V8 bindings from single process layout tests.
115     return mainWorldScriptState(page->mainFrame());
116 }
117 
118 #if ENABLE(WORKERS)
scriptStateFromWorkerContext(WorkerContext * workerContext)119 ScriptState* scriptStateFromWorkerContext(WorkerContext* workerContext)
120 {
121     WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
122     if (!proxy)
123         return 0;
124 
125     v8::HandleScope handleScope;
126     v8::Local<v8::Context> context = proxy->context();
127     return ScriptState::forContext(context);
128 }
129 #endif
130 
131 }
132