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 | |
34 InspectedContext::InspectedContext(V8InspectorImpl* inspector, | 17 InspectedContext::InspectedContext(V8InspectorImpl* inspector, |
35 const V8ContextInfo& info, int contextId) | 18 const V8ContextInfo& info, int contextId) |
36 : m_inspector(inspector), | 19 : m_inspector(inspector), |
37 m_context(info.context->GetIsolate(), info.context), | 20 m_context(info.context->GetIsolate(), info.context), |
38 m_contextId(contextId), | 21 m_contextId(contextId), |
39 m_contextGroupId(info.contextGroupId), | 22 m_contextGroupId(info.contextGroupId), |
40 m_origin(toString16(info.origin)), | 23 m_origin(toString16(info.origin)), |
41 m_humanReadableName(toString16(info.humanReadableName)), | 24 m_humanReadableName(toString16(info.humanReadableName)), |
42 m_auxData(toString16(info.auxData)), | 25 m_auxData(toString16(info.auxData)), |
43 m_reported(false) { | 26 m_reported(false) { |
44 v8::Isolate* isolate = m_inspector->isolate(); | 27 v8::Isolate* isolate = m_inspector->isolate(); |
45 info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex), | 28 info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex), |
46 v8::Int32::New(isolate, contextId)); | 29 v8::Int32::New(isolate, contextId)); |
47 m_context.SetWeak(&m_context, &clearContext, | |
48 v8::WeakCallbackType::kParameter); | |
49 | |
50 v8::Local<v8::Object> global = info.context->Global(); | 30 v8::Local<v8::Object> global = info.context->Global(); |
51 v8::Local<v8::Object> console = | 31 v8::Local<v8::Object> console = |
52 V8Console::createConsole(this, info.hasMemoryOnConsole); | 32 V8Console::createConsole(this, info.hasMemoryOnConsole); |
53 if (!global | 33 if (!global |
54 ->Set(info.context, toV8StringInternalized(isolate, "console"), | 34 ->Set(info.context, toV8StringInternalized(isolate, "console"), |
55 console) | 35 console) |
56 .FromMaybe(false)) | 36 .FromMaybe(false)) { |
57 return; | 37 return; |
58 m_console.Reset(isolate, console); | 38 } |
59 m_console.SetWeak(); | |
60 } | 39 } |
61 | 40 |
62 InspectedContext::~InspectedContext() { | 41 InspectedContext::~InspectedContext() { |
63 if (!m_console.IsEmpty()) { | |
64 v8::HandleScope scope(isolate()); | |
65 V8Console::clearInspectedContextIfNeeded(context(), | |
66 m_console.Get(isolate())); | |
67 } | |
68 } | 42 } |
69 | 43 |
70 // static | 44 // static |
71 int InspectedContext::contextId(v8::Local<v8::Context> context) { | 45 int InspectedContext::contextId(v8::Local<v8::Context> context) { |
72 v8::Local<v8::Value> data = | 46 v8::Local<v8::Value> data = |
73 context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex)); | 47 context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex)); |
74 if (data.IsEmpty() || !data->IsInt32()) return 0; | 48 if (data.IsEmpty() || !data->IsInt32()) return 0; |
75 return static_cast<int>(data.As<v8::Int32>()->Value()); | 49 return static_cast<int>(data.As<v8::Int32>()->Value()); |
76 } | 50 } |
77 | 51 |
(...skipping 10 matching lines...) Expand all Loading... |
88 std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this); | 62 std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this); |
89 // InjectedScript::create can destroy |this|. | 63 // InjectedScript::create can destroy |this|. |
90 if (!injectedScript) return false; | 64 if (!injectedScript) return false; |
91 m_injectedScript = std::move(injectedScript); | 65 m_injectedScript = std::move(injectedScript); |
92 return true; | 66 return true; |
93 } | 67 } |
94 | 68 |
95 void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); } | 69 void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); } |
96 | 70 |
97 } // namespace v8_inspector | 71 } // namespace v8_inspector |
OLD | NEW |