Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptState.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "bindings/v8/ScriptState.h"
7
8 #include "bindings/v8/V8Binding.h"
9 #include "core/frame/LocalFrame.h"
10
11 namespace WebCore {
12
13 PassRefPtr<ScriptState> ScriptState::create(v8::Handle<v8::Context> context, PassRefPtr<DOMWrapperWorld> world)
14 {
15     RefPtr<ScriptState> scriptState = adoptRef(new ScriptState(context, world));
16     // This ref() is for keeping this ScriptState alive as long as the v8::Context is alive.
17     // This is deref()ed in the weak callback of the v8::Context.
18     scriptState->ref();
19     return scriptState;
20 }
21
22 static void weakCallback(const v8::WeakCallbackData<v8::Context, ScriptState>& data)
23 {
24     data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
25     data.GetParameter()->clearContext();
26     data.GetParameter()->deref();
27 }
28
29 ScriptState::ScriptState(v8::Handle<v8::Context> context, PassRefPtr<DOMWrapperWorld> world)
30     : m_isolate(context->GetIsolate())
31     , m_context(m_isolate, context)
32     , m_world(world)
33     , m_perContextData(V8PerContextData::create(context))
34 {
35     ASSERT(m_world);
36     m_context.setWeak(this, &weakCallback);
37     context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this);
38 }
39
40 ScriptState::~ScriptState()
41 {
42     ASSERT(!m_perContextData);
43     ASSERT(m_context.isEmpty());
44 }
45
46 bool ScriptState::evalEnabled() const
47 {
48     v8::HandleScope handleScope(m_isolate);
49     return context()->IsCodeGenerationFromStringsAllowed();
50 }
51
52 void ScriptState::setEvalEnabled(bool enabled)
53 {
54     v8::HandleScope handleScope(m_isolate);
55     return context()->AllowCodeGenerationFromStrings(enabled);
56 }
57
58 ExecutionContext* ScriptState::executionContext() const
59 {
60     v8::HandleScope scope(m_isolate);
61     return toExecutionContext(context());
62 }
63
64 DOMWindow* ScriptState::domWindow() const
65 {
66     v8::HandleScope scope(m_isolate);
67     return toDOMWindow(context());
68 }
69
70 ScriptState* ScriptState::forMainWorld(LocalFrame* frame)
71 {
72     v8::Isolate* isolate = toIsolate(frame);
73     v8::HandleScope handleScope(isolate);
74     return ScriptState::from(toV8Context(isolate, frame, DOMWrapperWorld::mainWorld()));
75 }
76
77 }