OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/inspected-context.h" | 5 #include "src/inspector/inspected-context.h" |
6 | 6 |
7 #include "src/inspector/injected-script.h" | 7 #include "src/inspector/injected-script.h" |
8 #include "src/inspector/string-util.h" | 8 #include "src/inspector/string-util.h" |
9 #include "src/inspector/v8-console.h" | 9 #include "src/inspector/v8-console.h" |
10 #include "src/inspector/v8-inspector-impl.h" | 10 #include "src/inspector/v8-inspector-impl.h" |
11 #include "src/inspector/v8-value-copier.h" | 11 #include "src/inspector/v8-value-copier.h" |
12 | 12 |
13 #include "include/v8-inspector.h" | 13 #include "include/v8-inspector.h" |
14 | 14 |
15 namespace v8_inspector { | 15 namespace v8_inspector { |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 void clearContext(const v8::WeakCallbackInfo<v8::Global<v8::Context>>& data) { |
| 20 // Inspected context is created in V8InspectorImpl::contextCreated method |
| 21 // and destroyed in V8InspectorImpl::contextDestroyed. |
| 22 // Both methods takes valid v8::Local<v8::Context> handle to the same context, |
| 23 // it means that context is created before InspectedContext constructor and is |
| 24 // always destroyed after InspectedContext destructor therefore this callback |
| 25 // should be never called. |
| 26 // It's possible only if inspector client doesn't call contextDestroyed which |
| 27 // is considered an error. |
| 28 CHECK(false); |
| 29 data.GetParameter()->Reset(); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
17 InspectedContext::InspectedContext(V8InspectorImpl* inspector, | 34 InspectedContext::InspectedContext(V8InspectorImpl* inspector, |
18 const V8ContextInfo& info, int contextId) | 35 const V8ContextInfo& info, int contextId) |
19 : m_inspector(inspector), | 36 : m_inspector(inspector), |
20 m_context(info.context->GetIsolate(), info.context), | 37 m_context(info.context->GetIsolate(), info.context), |
21 m_contextId(contextId), | 38 m_contextId(contextId), |
22 m_contextGroupId(info.contextGroupId), | 39 m_contextGroupId(info.contextGroupId), |
23 m_origin(toString16(info.origin)), | 40 m_origin(toString16(info.origin)), |
24 m_humanReadableName(toString16(info.humanReadableName)), | 41 m_humanReadableName(toString16(info.humanReadableName)), |
25 m_auxData(toString16(info.auxData)), | 42 m_auxData(toString16(info.auxData)), |
26 m_reported(false) { | 43 m_reported(false) { |
27 v8::Isolate* isolate = m_inspector->isolate(); | 44 v8::Isolate* isolate = m_inspector->isolate(); |
28 info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex), | 45 info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex), |
29 v8::Int32::New(isolate, contextId)); | 46 v8::Int32::New(isolate, contextId)); |
| 47 m_context.SetWeak(&m_context, &clearContext, |
| 48 v8::WeakCallbackType::kParameter); |
| 49 |
30 v8::Local<v8::Object> global = info.context->Global(); | 50 v8::Local<v8::Object> global = info.context->Global(); |
31 v8::Local<v8::Object> console = | 51 v8::Local<v8::Object> console = |
32 V8Console::createConsole(this, info.hasMemoryOnConsole); | 52 V8Console::createConsole(this, info.hasMemoryOnConsole); |
33 if (!global | 53 if (!global |
34 ->Set(info.context, toV8StringInternalized(isolate, "console"), | 54 ->Set(info.context, toV8StringInternalized(isolate, "console"), |
35 console) | 55 console) |
36 .FromMaybe(false)) { | 56 .FromMaybe(false)) |
37 return; | 57 return; |
| 58 m_console.Reset(isolate, console); |
| 59 m_console.SetWeak(); |
| 60 } |
| 61 |
| 62 InspectedContext::~InspectedContext() { |
| 63 if (!m_console.IsEmpty()) { |
| 64 v8::HandleScope scope(isolate()); |
| 65 V8Console::clearInspectedContextIfNeeded(context(), |
| 66 m_console.Get(isolate())); |
38 } | 67 } |
39 } | 68 } |
40 | 69 |
41 InspectedContext::~InspectedContext() { | |
42 } | |
43 | |
44 // static | 70 // static |
45 int InspectedContext::contextId(v8::Local<v8::Context> context) { | 71 int InspectedContext::contextId(v8::Local<v8::Context> context) { |
46 v8::Local<v8::Value> data = | 72 v8::Local<v8::Value> data = |
47 context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex)); | 73 context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex)); |
48 if (data.IsEmpty() || !data->IsInt32()) return 0; | 74 if (data.IsEmpty() || !data->IsInt32()) return 0; |
49 return static_cast<int>(data.As<v8::Int32>()->Value()); | 75 return static_cast<int>(data.As<v8::Int32>()->Value()); |
50 } | 76 } |
51 | 77 |
52 v8::Local<v8::Context> InspectedContext::context() const { | 78 v8::Local<v8::Context> InspectedContext::context() const { |
53 return m_context.Get(isolate()); | 79 return m_context.Get(isolate()); |
54 } | 80 } |
55 | 81 |
56 v8::Isolate* InspectedContext::isolate() const { | 82 v8::Isolate* InspectedContext::isolate() const { |
57 return m_inspector->isolate(); | 83 return m_inspector->isolate(); |
58 } | 84 } |
59 | 85 |
60 bool InspectedContext::createInjectedScript() { | 86 bool InspectedContext::createInjectedScript() { |
61 DCHECK(!m_injectedScript); | 87 DCHECK(!m_injectedScript); |
62 std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this); | 88 std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this); |
63 // InjectedScript::create can destroy |this|. | 89 // InjectedScript::create can destroy |this|. |
64 if (!injectedScript) return false; | 90 if (!injectedScript) return false; |
65 m_injectedScript = std::move(injectedScript); | 91 m_injectedScript = std::move(injectedScript); |
66 return true; | 92 return true; |
67 } | 93 } |
68 | 94 |
69 void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); } | 95 void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); } |
70 | 96 |
71 } // namespace v8_inspector | 97 } // namespace v8_inspector |
OLD | NEW |