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