OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium 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 "core/inspector/InjectedScriptNative.h" | |
6 | |
7 #include "platform/JSONValues.h" | |
8 #include "wtf/Vector.h" | |
9 #include "wtf/text/WTFString.h" | |
10 | |
11 namespace blink { | |
12 | |
13 InjectedScriptNative::InjectedScriptNative(v8::Isolate* isolate) | |
14 : m_lastBoundObjectId(1) | |
15 , m_isolate(isolate) | |
16 , m_idToWrappedObject(m_isolate) | |
17 { | |
18 } | |
19 | |
20 static const char privateKeyName[] = "v8-inspector#injectedScript"; | |
21 | |
22 InjectedScriptNative::~InjectedScriptNative() { } | |
23 | |
24 void InjectedScriptNative::setOnInjectedScriptHost(v8::Local<v8::Object> injecte
dScriptHost) | |
25 { | |
26 v8::HandleScope handleScope(m_isolate); | |
27 v8::Local<v8::External> external = v8::External::New(m_isolate, this); | |
28 v8::Local<v8::Private> privateKey = v8::Private::ForApi(m_isolate, v8::Strin
g::NewFromUtf8(m_isolate, privateKeyName)); | |
29 injectedScriptHost->SetPrivate(m_isolate->GetCurrentContext(), privateKey, e
xternal); | |
30 } | |
31 | |
32 InjectedScriptNative* InjectedScriptNative::fromInjectedScriptHost(v8::Local<v8:
:Object> injectedScriptObject) | |
33 { | |
34 v8::Isolate* isolate = injectedScriptObject->GetIsolate(); | |
35 v8::HandleScope handleScope(isolate); | |
36 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
37 v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, v8::String:
:NewFromUtf8(isolate, privateKeyName)); | |
38 v8::Local<v8::Value> value; | |
39 RELEASE_ASSERT(injectedScriptObject->GetPrivate(context, privateKey).ToLocal
(&value)); | |
40 v8::Local<v8::External> external = value.As<v8::External>(); | |
41 void* ptr = external->Value(); | |
42 ASSERT(ptr); | |
43 return static_cast<InjectedScriptNative*>(ptr); | |
44 } | |
45 | |
46 int InjectedScriptNative::bind(v8::Local<v8::Value> value, const String& groupNa
me) | |
47 { | |
48 if (m_lastBoundObjectId <= 0) | |
49 m_lastBoundObjectId = 1; | |
50 int id = m_lastBoundObjectId++; | |
51 m_idToWrappedObject.Set(id, value); | |
52 addObjectToGroup(id, groupName); | |
53 return id; | |
54 } | |
55 | |
56 void InjectedScriptNative::unbind(int id) | |
57 { | |
58 m_idToWrappedObject.Remove(id); | |
59 m_idToObjectGroupName.remove(id); | |
60 } | |
61 | |
62 v8::Local<v8::Value> InjectedScriptNative::objectForId(int id) | |
63 { | |
64 return m_idToWrappedObject.Get(id); | |
65 } | |
66 | |
67 void InjectedScriptNative::addObjectToGroup(int objectId, const String& groupNam
e) | |
68 { | |
69 if (groupName.isEmpty()) | |
70 return; | |
71 if (objectId <= 0) | |
72 return; | |
73 m_idToObjectGroupName.set(objectId, groupName); | |
74 NameToObjectGroup::iterator groupIt = m_nameToObjectGroup.find(groupName); | |
75 if (groupIt == m_nameToObjectGroup.end()) | |
76 m_nameToObjectGroup.set(groupName, Vector<int>()).storedValue->value.app
end(objectId); | |
77 else | |
78 groupIt->value.append(objectId); | |
79 } | |
80 | |
81 void InjectedScriptNative::releaseObjectGroup(const String& groupName) | |
82 { | |
83 if (groupName.isEmpty()) | |
84 return; | |
85 NameToObjectGroup::iterator groupIt = m_nameToObjectGroup.find(groupName); | |
86 if (groupIt == m_nameToObjectGroup.end()) | |
87 return; | |
88 for (int id : groupIt->value) | |
89 unbind(id); | |
90 m_nameToObjectGroup.remove(groupIt); | |
91 } | |
92 | |
93 String InjectedScriptNative::groupName(int objectId) const | |
94 { | |
95 if (objectId <= 0) | |
96 return String(); | |
97 return m_idToObjectGroupName.get(objectId); | |
98 } | |
99 | |
100 } // namespace blink | |
101 | |
OLD | NEW |