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.
6 #include "bindings/v8/ScriptState.h"
8 #include "bindings/v8/V8Binding.h"
9 #include "core/frame/LocalFrame.h"
13 PassRefPtr<ScriptState> ScriptState::create(v8::Handle<v8::Context> context, PassRefPtr<DOMWrapperWorld> world)
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.
22 static void weakCallback(const v8::WeakCallbackData<v8::Context, ScriptState>& data)
24 data.GetValue()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
25 data.GetParameter()->clearContext();
26 data.GetParameter()->deref();
29 ScriptState::ScriptState(v8::Handle<v8::Context> context, PassRefPtr<DOMWrapperWorld> world)
30 : m_isolate(context->GetIsolate())
31 , m_context(m_isolate, context)
33 , m_perContextData(V8PerContextData::create(context))
36 m_context.setWeak(this, &weakCallback);
37 context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this);
40 ScriptState::~ScriptState()
42 ASSERT(!m_perContextData);
43 ASSERT(m_context.isEmpty());
46 bool ScriptState::evalEnabled() const
48 v8::HandleScope handleScope(m_isolate);
49 return context()->IsCodeGenerationFromStringsAllowed();
52 void ScriptState::setEvalEnabled(bool enabled)
54 v8::HandleScope handleScope(m_isolate);
55 return context()->AllowCodeGenerationFromStrings(enabled);
58 ExecutionContext* ScriptState::executionContext() const
60 v8::HandleScope scope(m_isolate);
61 return toExecutionContext(context());
64 DOMWindow* ScriptState::domWindow() const
66 v8::HandleScope scope(m_isolate);
67 return toDOMWindow(context());
70 ScriptState* ScriptState::forMainWorld(LocalFrame* frame)
72 v8::Isolate* isolate = toIsolate(frame);
73 v8::HandleScope handleScope(isolate);
74 return ScriptState::from(toV8Context(isolate, frame, DOMWrapperWorld::mainWorld()));