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