LCOV - code coverage report
Current view: top level - src/inspector - injected-script-native.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 38 38 100.0 %
Date: 2017-04-26 Functions: 10 10 100.0 %

          Line data    Source code
       1             : // Copyright 2015 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/injected-script-native.h"
       6             : 
       7             : namespace v8_inspector {
       8             : 
       9        4103 : InjectedScriptNative::InjectedScriptNative(v8::Isolate* isolate)
      10       16412 :     : m_lastBoundObjectId(1), m_isolate(isolate) {}
      11             : 
      12             : static const char privateKeyName[] = "v8-inspector#injectedScript";
      13             : 
      14        8206 : InjectedScriptNative::~InjectedScriptNative() {}
      15             : 
      16        4103 : void InjectedScriptNative::setOnInjectedScriptHost(
      17             :     v8::Local<v8::Object> injectedScriptHost) {
      18        4103 :   v8::HandleScope handleScope(m_isolate);
      19        4103 :   v8::Local<v8::External> external = v8::External::New(m_isolate, this);
      20             :   v8::Local<v8::Private> privateKey = v8::Private::ForApi(
      21             :       m_isolate, v8::String::NewFromUtf8(m_isolate, privateKeyName,
      22        4103 :                                          v8::NewStringType::kInternalized)
      23        8206 :                      .ToLocalChecked());
      24             :   injectedScriptHost->SetPrivate(m_isolate->GetCurrentContext(), privateKey,
      25        8206 :                                  external);
      26        4103 : }
      27             : 
      28     3780245 : InjectedScriptNative* InjectedScriptNative::fromInjectedScriptHost(
      29             :     v8::Isolate* isolate, v8::Local<v8::Object> injectedScriptObject) {
      30     3780245 :   v8::HandleScope handleScope(isolate);
      31     3780245 :   v8::Local<v8::Context> context = isolate->GetCurrentContext();
      32             :   v8::Local<v8::Private> privateKey = v8::Private::ForApi(
      33             :       isolate, v8::String::NewFromUtf8(isolate, privateKeyName,
      34             :                                        v8::NewStringType::kInternalized)
      35     7560490 :                    .ToLocalChecked());
      36             :   v8::Local<v8::Value> value =
      37     3780245 :       injectedScriptObject->GetPrivate(context, privateKey).ToLocalChecked();
      38             :   DCHECK(value->IsExternal());
      39             :   v8::Local<v8::External> external = value.As<v8::External>();
      40     3780245 :   return static_cast<InjectedScriptNative*>(external->Value());
      41             : }
      42             : 
      43     3780245 : int InjectedScriptNative::bind(v8::Local<v8::Value> value,
      44             :                                const String16& groupName) {
      45     3780245 :   if (m_lastBoundObjectId <= 0) m_lastBoundObjectId = 1;
      46     3780245 :   int id = m_lastBoundObjectId++;
      47             :   m_idToWrappedObject.insert(
      48     7560490 :       std::make_pair(id, v8::Global<v8::Value>(m_isolate, value)));
      49     3780245 :   addObjectToGroup(id, groupName);
      50     3780245 :   return id;
      51             : }
      52             : 
      53           6 : void InjectedScriptNative::unbind(int id) {
      54             :   m_idToWrappedObject.erase(id);
      55             :   m_idToObjectGroupName.erase(id);
      56           6 : }
      57             : 
      58      107328 : v8::Local<v8::Value> InjectedScriptNative::objectForId(int id) {
      59             :   auto iter = m_idToWrappedObject.find(id);
      60      107328 :   return iter != m_idToWrappedObject.end() ? iter->second.Get(m_isolate)
      61      321984 :                                            : v8::Local<v8::Value>();
      62             : }
      63             : 
      64     3780245 : void InjectedScriptNative::addObjectToGroup(int objectId,
      65             :                                             const String16& groupName) {
      66     3780245 :   if (groupName.isEmpty()) return;
      67     3769492 :   if (objectId <= 0) return;
      68             :   m_idToObjectGroupName[objectId] = groupName;
      69             :   m_nameToObjectGroup[groupName].push_back(
      70     3769492 :       objectId);  // Creates an empty vector if key is not there
      71             : }
      72             : 
      73       64476 : void InjectedScriptNative::releaseObjectGroup(const String16& groupName) {
      74       64476 :   if (groupName.isEmpty()) return;
      75             :   NameToObjectGroup::iterator groupIt = m_nameToObjectGroup.find(groupName);
      76       64476 :   if (groupIt == m_nameToObjectGroup.end()) return;
      77     2999909 :   for (int id : groupIt->second) unbind(id);
      78             :   m_nameToObjectGroup.erase(groupIt);
      79             : }
      80             : 
      81      107298 : String16 InjectedScriptNative::groupName(int objectId) const {
      82      107298 :   if (objectId <= 0) return String16();
      83             :   IdToObjectGroupName::const_iterator iterator =
      84             :       m_idToObjectGroupName.find(objectId);
      85             :   return iterator != m_idToObjectGroupName.end() ? iterator->second
      86      107298 :                                                  : String16();
      87             : }
      88             : 
      89             : }  // namespace v8_inspector

Generated by: LCOV version 1.10