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