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 3// found in the LICENSE file. 4 5#include "src/inspector/inspected-context.h" 6 7#include "src/inspector/injected-script.h" 8#include "src/inspector/string-util.h" 9#include "src/inspector/v8-console.h" 10#include "src/inspector/v8-inspector-impl.h" 11#include "src/inspector/v8-value-copier.h" 12 13#include "include/v8-inspector.h" 14 15namespace v8_inspector { 16 17namespace { 18 19void 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 34InspectedContext::InspectedContext(V8InspectorImpl* inspector, 35 const V8ContextInfo& info, int contextId) 36 : m_inspector(inspector), 37 m_context(info.context->GetIsolate(), info.context), 38 m_contextId(contextId), 39 m_contextGroupId(info.contextGroupId), 40 m_origin(toString16(info.origin)), 41 m_humanReadableName(toString16(info.humanReadableName)), 42 m_auxData(toString16(info.auxData)), 43 m_reported(false) { 44 m_context.SetWeak(&m_context, &clearContext, 45 v8::WeakCallbackType::kParameter); 46 47 v8::Isolate* isolate = m_inspector->isolate(); 48 v8::Local<v8::Object> global = info.context->Global(); 49 v8::Local<v8::Object> console = 50 V8Console::createConsole(this, info.hasMemoryOnConsole); 51 if (!global 52 ->Set(info.context, toV8StringInternalized(isolate, "console"), 53 console) 54 .FromMaybe(false)) 55 return; 56 m_console.Reset(isolate, console); 57 m_console.SetWeak(); 58} 59 60InspectedContext::~InspectedContext() { 61 if (!m_console.IsEmpty()) { 62 v8::HandleScope scope(isolate()); 63 V8Console::clearInspectedContextIfNeeded(context(), 64 m_console.Get(isolate())); 65 } 66} 67 68v8::Local<v8::Context> InspectedContext::context() const { 69 return m_context.Get(isolate()); 70} 71 72v8::Isolate* InspectedContext::isolate() const { 73 return m_inspector->isolate(); 74} 75 76bool InspectedContext::createInjectedScript() { 77 DCHECK(!m_injectedScript); 78 std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this); 79 // InjectedScript::create can destroy |this|. 80 if (!injectedScript) return false; 81 m_injectedScript = std::move(injectedScript); 82 return true; 83} 84 85void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); } 86 87} // namespace v8_inspector 88