OLD | NEW |
| (Empty) |
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 "bindings/core/v8/ScriptState.h" | |
6 | |
7 #include "bindings/core/v8/V8Binding.h" | |
8 #include "core/dom/ExecutionContext.h" | |
9 | |
10 namespace blink { | |
11 | |
12 PassRefPtr<ScriptState> ScriptState::Create(v8::Local<v8::Context> context, | |
13 PassRefPtr<DOMWrapperWorld> world) { | |
14 RefPtr<ScriptState> script_state = | |
15 AdoptRef(new ScriptState(context, std::move(world))); | |
16 // This ref() is for keeping this ScriptState alive as long as the v8::Context | |
17 // is alive. This is deref()ed in the weak callback of the v8::Context. | |
18 script_state->Ref(); | |
19 return script_state; | |
20 } | |
21 | |
22 static void DerefCallback(const v8::WeakCallbackInfo<ScriptState>& data) { | |
23 data.GetParameter()->Deref(); | |
24 } | |
25 | |
26 static void ContextCollectedCallback( | |
27 const v8::WeakCallbackInfo<ScriptState>& data) { | |
28 data.GetParameter()->ClearContext(); | |
29 data.SetSecondPassCallback(DerefCallback); | |
30 } | |
31 | |
32 ScriptState::ScriptState(v8::Local<v8::Context> context, | |
33 PassRefPtr<DOMWrapperWorld> world) | |
34 : isolate_(context->GetIsolate()), | |
35 context_(isolate_, context), | |
36 world_(std::move(world)), | |
37 per_context_data_(V8PerContextData::Create(context)) { | |
38 DCHECK(world_); | |
39 context_.SetWeak(this, &ContextCollectedCallback); | |
40 context->SetAlignedPointerInEmbedderData(kV8ContextPerContextDataIndex, this); | |
41 } | |
42 | |
43 ScriptState::~ScriptState() { | |
44 DCHECK(!per_context_data_); | |
45 DCHECK(context_.IsEmpty()); | |
46 } | |
47 | |
48 void ScriptState::DetachGlobalObject() { | |
49 DCHECK(!context_.IsEmpty()); | |
50 GetContext()->DetachGlobal(); | |
51 } | |
52 | |
53 void ScriptState::DisposePerContextData() { | |
54 per_context_data_ = nullptr; | |
55 } | |
56 | |
57 } // namespace blink | |
OLD | NEW |