Home | History | Annotate | Download | only in inspector
      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 
     15 namespace v8_inspector {
     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,
     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   v8::Isolate* isolate = m_inspector->isolate();
     45   info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex),
     46                                 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();
     51   v8::Local<v8::Object> console =
     52       V8Console::createConsole(this, info.hasMemoryOnConsole);
     53   if (!global
     54            ->Set(info.context, toV8StringInternalized(isolate, "console"),
     55                  console)
     56            .FromMaybe(false))
     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()));
     67   }
     68 }
     69 
     70 // static
     71 int InspectedContext::contextId(v8::Local<v8::Context> context) {
     72   v8::Local<v8::Value> data =
     73       context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex));
     74   if (data.IsEmpty() || !data->IsInt32()) return 0;
     75   return static_cast<int>(data.As<v8::Int32>()->Value());
     76 }
     77 
     78 v8::Local<v8::Context> InspectedContext::context() const {
     79   return m_context.Get(isolate());
     80 }
     81 
     82 v8::Isolate* InspectedContext::isolate() const {
     83   return m_inspector->isolate();
     84 }
     85 
     86 bool InspectedContext::createInjectedScript() {
     87   DCHECK(!m_injectedScript);
     88   std::unique_ptr<InjectedScript> injectedScript = InjectedScript::create(this);
     89   // InjectedScript::create can destroy |this|.
     90   if (!injectedScript) return false;
     91   m_injectedScript = std::move(injectedScript);
     92   return true;
     93 }
     94 
     95 void InspectedContext::discardInjectedScript() { m_injectedScript.reset(); }
     96 
     97 }  // namespace v8_inspector
     98