Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: sky/engine/bindings-dart/core/dart/DartInjectedScriptManager.cpp

Issue 875013003: Import Dart bindings as of Blink r188698. This merely copies the files over and does not attach any… (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> 3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2012 Google Inc. All rights reserved. 4 * Copyright (C) 2012 Google Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
(...skipping 10 matching lines...) Expand all
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "sky/engine/config.h" 31 #include "config.h"
32 #include "sky/engine/v8_inspector/InjectedScriptManager.h" 32 #include "bindings/core/dart/DartInjectedScriptManager.h"
33 33
34 #include "sky/engine/bindings/core/v8/ScriptValue.h" 34 #include "bindings/common/ScriptValue.h"
35 #include "sky/engine/core/inspector/InjectedScriptHost.h" 35 #include "bindings/core/dart/DartInjectedScript.h"
36 #include "sky/engine/platform/JSONValues.h" 36 #include "bindings/core/dart/DartScriptState.h"
37 #include "sky/engine/v8_inspector/InjectedScript.h" 37 #include "core/inspector/InjectedScriptHost.h"
38 #include "sky/engine/v8_inspector/JSONParser.h" 38 #include "core/inspector/InjectedScriptManager.h"
39 #include "sky/engine/v8_inspector/read_from_source_tree.h" 39 #include "core/inspector/JSONParser.h"
40 #include "sky/engine/wtf/PassOwnPtr.h" 40 #include "platform/JSONValues.h"
41 #include "public/platform/Platform.h"
42 #include "public/platform/WebData.h"
43 #include "wtf/PassOwnPtr.h"
41 44
42 namespace blink { 45 namespace blink {
43 46
44 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage() 47 DartInjectedScriptManager::DartInjectedScriptManager(InspectedStateAccessCheck a ccessCheck, InjectedScriptManager* javaScriptInjectedScriptManager)
45 { 48 : m_nextInjectedScriptId(1000000000) // Elegant design so that Dart and Java Script ids don't overlap.
46 return adoptPtr(new InjectedScriptManager); 49 , m_inspectedStateAccessCheck(accessCheck)
47 } 50 , m_javaScriptInjectedScriptManager(javaScriptInjectedScriptManager)
48
49 InjectedScriptManager::InjectedScriptManager()
50 : m_nextInjectedScriptId(1)
51 , m_injectedScriptHost(InjectedScriptHost::create())
52 { 51 {
53 } 52 }
54 53
55 InjectedScriptManager::~InjectedScriptManager() 54 DartInjectedScriptManager::~DartInjectedScriptManager()
56 { 55 {
57 } 56 }
58 57
59 void InjectedScriptManager::disconnect() 58 InjectedScriptHost* DartInjectedScriptManager::injectedScriptHost()
60 { 59 {
61 m_injectedScriptHost->disconnect(); 60 return m_javaScriptInjectedScriptManager->injectedScriptHost();
62 m_injectedScriptHost.clear();
63 } 61 }
64 62
65 InjectedScriptHost* InjectedScriptManager::injectedScriptHost() 63 DartInjectedScript* DartInjectedScriptManager::injectedScriptForId(int id)
66 {
67 return m_injectedScriptHost.get();
68 }
69
70 InjectedScript InjectedScriptManager::injectedScriptForId(int id)
71 { 64 {
72 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); 65 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id);
73 if (it != m_idToInjectedScript.end()) 66 if (it != m_idToInjectedScript.end())
74 return it->value; 67 return it->value;
75 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip tStateToId.end(); ++it) { 68 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip tStateToId.end(); ++it) {
76 if (it->value == id) 69 if (it->value == id)
77 return injectedScriptFor(it->key.get()); 70 return injectedScriptFor(it->key.get());
78 } 71 }
79 return InjectedScript(); 72 return 0;
80 } 73 }
81 74
82 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState) 75 int DartInjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState)
83 { 76 {
84 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState); 77 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState);
85 if (it != m_scriptStateToId.end()) 78 if (it != m_scriptStateToId.end())
86 return it->value; 79 return it->value;
87 int id = m_nextInjectedScriptId++; 80 int id = m_nextInjectedScriptId++;
88 m_scriptStateToId.set(scriptState, id); 81 m_scriptStateToId.set(scriptState, id);
89 return id; 82 return id;
90 } 83 }
91 84
92 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& ob jectId) 85 DartInjectedScript* DartInjectedScriptManager::injectedScriptForObjectId(const S tring& objectId)
93 { 86 {
94 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId); 87 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId);
95 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) { 88 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) {
96 long injectedScriptId = 0; 89 long injectedScriptId = 0;
97 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId); 90 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
98 if (success) 91 if (success) {
99 return m_idToInjectedScript.get(injectedScriptId); 92 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(inject edScriptId);
93 if (s != m_idToInjectedScript.end())
94 return s->value;
95 }
100 } 96 }
101 return InjectedScript(); 97 return 0;
102 } 98 }
103 99
104 void InjectedScriptManager::discardInjectedScripts() 100 void DartInjectedScriptManager::discardInjectedScripts()
105 { 101 {
106 m_idToInjectedScript.clear(); 102 m_idToInjectedScript.clear();
107 m_scriptStateToId.clear(); 103 m_scriptStateToId.clear();
108 } 104 }
109 105
110 void InjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window) 106 void DartInjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window )
111 { 107 {
112 if (m_scriptStateToId.isEmpty()) 108 if (m_scriptStateToId.isEmpty())
113 return; 109 return;
114 110
115 Vector<long> idsToRemove; 111 Vector<long> idsToRemove;
116 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end(); 112 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
117 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it ! = end; ++it) { 113 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it ! = end; ++it) {
118 ScriptState* scriptState = it->value.scriptState(); 114 ScriptState* scriptState = it->value->scriptState();
119 if (window != scriptState->domWindow()) 115 if (window != scriptState->domWindow())
120 continue; 116 continue;
121 m_scriptStateToId.remove(scriptState); 117 m_scriptStateToId.remove(scriptState);
122 idsToRemove.append(it->key); 118 idsToRemove.append(it->key);
123 } 119 }
120 for (size_t i = 0; i < idsToRemove.size(); i++)
121 delete m_idToInjectedScript.get(idsToRemove[i]);
124 m_idToInjectedScript.removeAll(idsToRemove); 122 m_idToInjectedScript.removeAll(idsToRemove);
125 123
124
126 // Now remove script states that have id but no injected script. 125 // Now remove script states that have id but no injected script.
127 Vector<ScriptState*> scriptStatesToRemove; 126 Vector<ScriptState*> scriptStatesToRemove;
128 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip tStateToId.end(); ++it) { 127 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip tStateToId.end(); ++it) {
129 ScriptState* scriptState = it->key.get(); 128 ScriptState* scriptState = it->key.get();
130 if (window == scriptState->domWindow()) 129 if (window == scriptState->domWindow())
131 scriptStatesToRemove.append(scriptState); 130 scriptStatesToRemove.append(scriptState);
132 } 131 }
133 m_scriptStateToId.removeAll(scriptStatesToRemove); 132 m_scriptStateToId.removeAll(scriptStatesToRemove);
134 } 133 }
135 134
136 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup) 135 bool DartInjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState* )
136 {
137 return true;
138 }
139
140 void DartInjectedScriptManager::releaseObjectGroup(const String& objectGroup)
137 { 141 {
138 Vector<int> keys; 142 Vector<int> keys;
139 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k eys().end()); 143 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k eys().end());
140 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) { 144 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) {
141 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k); 145 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k);
142 if (s != m_idToInjectedScript.end()) 146 if (s != m_idToInjectedScript.end())
143 s->value.releaseObjectGroup(objectGroup); // m_idToInjectedScript ma y change here. 147 s->value->releaseObjectGroup(objectGroup); // m_idToInjectedScript m ay change here.
144 } 148 }
145 } 149 }
146 150
147 String InjectedScriptManager::injectedScriptSource() 151 DartInjectedScript* DartInjectedScriptManager::injectedScriptFor(ScriptState* in spectedScriptState)
148 { 152 {
149 std::string buffer; 153 if (!inspectedScriptState)
150 inspector::ReadFileFromSourceTree("InjectedScriptSource.js", &buffer); 154 return 0;
151 return String::fromUTF8(buffer);
152 }
153
154 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedSc riptState)
155 {
156 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState); 155 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState);
157 if (it != m_scriptStateToId.end()) { 156 if (it != m_scriptStateToId.end()) {
158 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu e); 157 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu e);
159 if (it1 != m_idToInjectedScript.end()) 158 if (it1 != m_idToInjectedScript.end())
160 return it1->value; 159 return it1->value;
161 } 160 }
162 161
162 if (!m_inspectedStateAccessCheck(inspectedScriptState))
163 return 0;
164
163 int id = injectedScriptIdFor(inspectedScriptState); 165 int id = injectedScriptIdFor(inspectedScriptState);
164 ScriptValue injectedScriptValue = createInjectedScript(injectedScriptSource( ), inspectedScriptState, id); 166
165 InjectedScript result(injectedScriptValue); 167 DartInjectedScript* result;
168 ASSERT(!inspectedScriptState->isJavaScript());
169 result = new DartInjectedScript(static_cast<DartScriptState*>(inspectedScrip tState), m_inspectedStateAccessCheck, id, injectedScriptHost(), m_javaScriptInje ctedScriptManager);
166 m_idToInjectedScript.set(id, result); 170 m_idToInjectedScript.set(id, result);
167
168 // TODO(yurys): InjecedScript should be available as a regular module rather that using a global variable.
169 v8::Isolate* isolate = inspectedScriptState->isolate();
170 v8::Local<v8::Object> global = inspectedScriptState->context()->Global();
171 global->Set(v8::String::NewFromUtf8(isolate, "injectedScript"), injectedScri ptValue.v8ValueUnsafe());
172 return result; 171 return result;
173 } 172 }
174 173
175 } // namespace blink 174 } // namespace blink
176 175
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698