| OLD | NEW |
| 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 14 matching lines...) Expand all Loading... |
| 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 "config.h" | 31 #include "config.h" |
| 32 #include "core/inspector/InjectedScriptManager.h" | 32 #include "core/inspector/InjectedScriptManager.h" |
| 33 | 33 |
| 34 #include "InjectedScriptSource.h" | 34 #include "InjectedScriptSource.h" |
| 35 #include "bindings/dart/DartInjectedScript.h" |
| 36 #include "bindings/dart/DartScriptState.h" |
| 35 #include "bindings/v8/ScriptObject.h" | 37 #include "bindings/v8/ScriptObject.h" |
| 36 #include "core/inspector/InjectedScript.h" | 38 #include "core/inspector/InjectedScript.h" |
| 37 #include "core/inspector/InjectedScriptHost.h" | 39 #include "core/inspector/InjectedScriptHost.h" |
| 38 #include "core/inspector/JSONParser.h" | 40 #include "core/inspector/JSONParser.h" |
| 39 #include "platform/JSONValues.h" | 41 #include "platform/JSONValues.h" |
| 40 #include "wtf/PassOwnPtr.h" | 42 #include "wtf/PassOwnPtr.h" |
| 41 | 43 |
| 42 namespace WebCore { | 44 namespace WebCore { |
| 43 | 45 |
| 44 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage() | 46 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage() |
| 45 { | 47 { |
| 46 return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessI
nspectedWindow)); | 48 return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessI
nspectedWindow)); |
| 47 } | 49 } |
| 48 | 50 |
| 49 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForWorker() | 51 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForWorker() |
| 50 { | 52 { |
| 51 return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessI
nspectedWorkerGlobalScope)); | 53 return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessI
nspectedWorkerGlobalScope)); |
| 52 } | 54 } |
| 53 | 55 |
| 54 InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessChe
ck) | 56 InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessChe
ck) |
| 55 : m_nextInjectedScriptId(1) | 57 : m_nextInjectedScriptId(1) |
| 56 , m_injectedScriptHost(InjectedScriptHost::create()) | 58 , m_injectedScriptHost(InjectedScriptHost::create()) |
| 57 , m_inspectedStateAccessCheck(accessCheck) | 59 , m_inspectedStateAccessCheck(accessCheck) |
| 60 , m_placeholderInjectedScript(0) |
| 58 { | 61 { |
| 59 } | 62 } |
| 60 | 63 |
| 61 InjectedScriptManager::~InjectedScriptManager() | 64 InjectedScriptManager::~InjectedScriptManager() |
| 62 { | 65 { |
| 66 delete m_placeholderInjectedScript; |
| 63 } | 67 } |
| 64 | 68 |
| 65 void InjectedScriptManager::disconnect() | 69 void InjectedScriptManager::disconnect() |
| 66 { | 70 { |
| 67 m_injectedScriptHost->disconnect(); | 71 m_injectedScriptHost->disconnect(); |
| 68 m_injectedScriptHost.clear(); | 72 m_injectedScriptHost.clear(); |
| 69 } | 73 } |
| 70 | 74 |
| 75 InjectedScript& InjectedScriptManager::placeholderInjectedScript() |
| 76 { |
| 77 if (!m_placeholderInjectedScript) |
| 78 m_placeholderInjectedScript = new V8InjectedScript(); |
| 79 return *m_placeholderInjectedScript; |
| 80 } |
| 81 |
| 71 InjectedScriptHost* InjectedScriptManager::injectedScriptHost() | 82 InjectedScriptHost* InjectedScriptManager::injectedScriptHost() |
| 72 { | 83 { |
| 73 return m_injectedScriptHost.get(); | 84 return m_injectedScriptHost.get(); |
| 74 } | 85 } |
| 75 | 86 |
| 76 InjectedScript InjectedScriptManager::injectedScriptForId(int id) | 87 InjectedScript& InjectedScriptManager::injectedScriptForId(int id) |
| 77 { | 88 { |
| 78 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); | 89 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); |
| 79 if (it != m_idToInjectedScript.end()) | 90 if (it != m_idToInjectedScript.end()) |
| 80 return it->value; | 91 return *(it->value); |
| 81 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { | 92 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { |
| 82 if (it->value == id) | 93 if (it->value == id) |
| 83 return injectedScriptFor(it->key.get()); | 94 return injectedScriptFor(it->key.get()); |
| 84 } | 95 } |
| 85 return InjectedScript(); | 96 return placeholderInjectedScript(); |
| 86 } | 97 } |
| 87 | 98 |
| 88 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState) | 99 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState) |
| 89 { | 100 { |
| 90 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState); | 101 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState); |
| 91 if (it != m_scriptStateToId.end()) | 102 if (it != m_scriptStateToId.end()) |
| 92 return it->value; | 103 return it->value; |
| 93 int id = m_nextInjectedScriptId++; | 104 int id = m_nextInjectedScriptId++; |
| 94 m_scriptStateToId.set(scriptState, id); | 105 m_scriptStateToId.set(scriptState, id); |
| 95 return id; | 106 return id; |
| 96 } | 107 } |
| 97 | 108 |
| 98 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& ob
jectId) | 109 InjectedScript& InjectedScriptManager::injectedScriptForObjectId(const String& o
bjectId) |
| 99 { | 110 { |
| 100 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId); | 111 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId); |
| 101 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) { | 112 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) { |
| 102 long injectedScriptId = 0; | 113 long injectedScriptId = 0; |
| 103 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId",
&injectedScriptId); | 114 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId",
&injectedScriptId); |
| 104 if (success) | 115 if (success) { |
| 105 return m_idToInjectedScript.get(injectedScriptId); | 116 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(inject
edScriptId); |
| 117 if (s != m_idToInjectedScript.end()) |
| 118 return *(s->value); |
| 119 } |
| 106 } | 120 } |
| 107 return InjectedScript(); | 121 return placeholderInjectedScript(); |
| 108 } | 122 } |
| 109 | 123 |
| 110 void InjectedScriptManager::discardInjectedScripts() | 124 void InjectedScriptManager::discardInjectedScripts() |
| 111 { | 125 { |
| 112 m_idToInjectedScript.clear(); | 126 m_idToInjectedScript.clear(); |
| 113 m_scriptStateToId.clear(); | 127 m_scriptStateToId.clear(); |
| 114 } | 128 } |
| 115 | 129 |
| 116 void InjectedScriptManager::discardInjectedScriptsFor(DOMWindow* window) | 130 void InjectedScriptManager::discardInjectedScriptsFor(DOMWindow* window) |
| 117 { | 131 { |
| 118 if (m_scriptStateToId.isEmpty()) | 132 if (m_scriptStateToId.isEmpty()) |
| 119 return; | 133 return; |
| 120 | 134 |
| 121 Vector<long> idsToRemove; | 135 Vector<long> idsToRemove; |
| 122 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end(); | 136 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end(); |
| 123 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it !
= end; ++it) { | 137 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it !
= end; ++it) { |
| 124 ScriptState* scriptState = it->value.scriptState(); | 138 ScriptState* scriptState = it->value->scriptState(); |
| 125 if (window != scriptState->domWindow()) | 139 if (window != scriptState->domWindow()) |
| 126 continue; | 140 continue; |
| 127 m_scriptStateToId.remove(scriptState); | 141 m_scriptStateToId.remove(scriptState); |
| 128 idsToRemove.append(it->key); | 142 idsToRemove.append(it->key); |
| 129 } | 143 } |
| 144 for (size_t i = 0; i < idsToRemove.size(); i++) |
| 145 delete m_idToInjectedScript.get(idsToRemove[i]); |
| 130 m_idToInjectedScript.removeAll(idsToRemove); | 146 m_idToInjectedScript.removeAll(idsToRemove); |
| 131 | 147 |
| 148 |
| 132 // Now remove script states that have id but no injected script. | 149 // Now remove script states that have id but no injected script. |
| 133 Vector<ScriptState*> scriptStatesToRemove; | 150 Vector<ScriptState*> scriptStatesToRemove; |
| 134 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { | 151 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { |
| 135 ScriptState* scriptState = it->key.get(); | 152 ScriptState* scriptState = it->key.get(); |
| 136 if (window == scriptState->domWindow()) | 153 if (window == scriptState->domWindow()) |
| 137 scriptStatesToRemove.append(scriptState); | 154 scriptStatesToRemove.append(scriptState); |
| 138 } | 155 } |
| 139 m_scriptStateToId.removeAll(scriptStatesToRemove); | 156 m_scriptStateToId.removeAll(scriptStatesToRemove); |
| 140 } | 157 } |
| 141 | 158 |
| 142 bool InjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState*) | 159 bool InjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState*) |
| 143 { | 160 { |
| 144 return true; | 161 return true; |
| 145 } | 162 } |
| 146 | 163 |
| 147 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup) | 164 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup) |
| 148 { | 165 { |
| 149 Vector<int> keys; | 166 Vector<int> keys; |
| 150 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k
eys().end()); | 167 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k
eys().end()); |
| 151 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) { | 168 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) { |
| 152 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k); | 169 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k); |
| 153 if (s != m_idToInjectedScript.end()) | 170 if (s != m_idToInjectedScript.end()) |
| 154 s->value.releaseObjectGroup(objectGroup); // m_idToInjectedScript ma
y change here. | 171 s->value->releaseObjectGroup(objectGroup); // m_idToInjectedScript m
ay change here. |
| 155 } | 172 } |
| 156 } | 173 } |
| 157 | 174 |
| 158 String InjectedScriptManager::injectedScriptSource() | 175 String InjectedScriptManager::injectedScriptSource() |
| 159 { | 176 { |
| 160 return String(reinterpret_cast<const char*>(InjectedScriptSource_js), sizeof
(InjectedScriptSource_js)); | 177 return String(reinterpret_cast<const char*>(InjectedScriptSource_js), sizeof
(InjectedScriptSource_js)); |
| 161 } | 178 } |
| 162 | 179 |
| 163 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedSc
riptState) | 180 InjectedScript& InjectedScriptManager::injectedScriptFor(ScriptState* inspectedS
criptState) |
| 164 { | 181 { |
| 165 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState); | 182 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState); |
| 166 if (it != m_scriptStateToId.end()) { | 183 if (it != m_scriptStateToId.end()) { |
| 167 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu
e); | 184 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu
e); |
| 168 if (it1 != m_idToInjectedScript.end()) | 185 if (it1 != m_idToInjectedScript.end()) |
| 169 return it1->value; | 186 return *(it1->value); |
| 170 } | 187 } |
| 171 | 188 |
| 172 if (!m_inspectedStateAccessCheck(inspectedScriptState)) | 189 if (!m_inspectedStateAccessCheck(inspectedScriptState)) |
| 173 return InjectedScript(); | 190 return placeholderInjectedScript(); |
| 174 | 191 |
| 175 int id = injectedScriptIdFor(inspectedScriptState); | 192 int id = injectedScriptIdFor(inspectedScriptState); |
| 176 ScriptObject injectedScriptObject = createInjectedScript(injectedScriptSourc
e(), inspectedScriptState, id); | 193 |
| 177 InjectedScript result(injectedScriptObject, m_inspectedStateAccessCheck); | 194 InjectedScript* result; |
| 195 if (inspectedScriptState->isJavaScript()) { |
| 196 ScriptObject injectedScriptObject = createInjectedScript(injectedScriptS
ource(), inspectedScriptState, id); |
| 197 result = new V8InjectedScript(injectedScriptObject, m_inspectedStateAcce
ssCheck); |
| 198 } else { |
| 199 result = new DartInjectedScript(static_cast<DartScriptState*>(inspectedS
criptState), m_inspectedStateAccessCheck, id, injectedScriptHost()); |
| 200 } |
| 178 m_idToInjectedScript.set(id, result); | 201 m_idToInjectedScript.set(id, result); |
| 179 return result; | 202 return *result; |
| 180 } | 203 } |
| 181 | 204 |
| 182 } // namespace WebCore | 205 } // namespace WebCore |
| 183 | 206 |
| OLD | NEW |