| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> | |
| 4 * Copyright (C) 2012 Google Inc. All rights reserved. | |
| 5 * | |
| 6 * Redistribution and use in source and binary forms, with or without | |
| 7 * modification, are permitted provided that the following conditions | |
| 8 * are met: | |
| 9 * | |
| 10 * 1. Redistributions of source code must retain the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer. | |
| 12 * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 * notice, this list of conditions and the following disclaimer in the | |
| 14 * documentation and/or other materials provided with the distribution. | |
| 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
| 16 * its contributors may be used to endorse or promote products derived | |
| 17 * from this software without specific prior written permission. | |
| 18 * | |
| 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 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 | |
| 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 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 | |
| 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 | |
| 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "bindings/core/dart/DartInjectedScriptManager.h" | |
| 33 | |
| 34 #include "bindings/common/ScriptValue.h" | |
| 35 #include "bindings/core/dart/DartInjectedScript.h" | |
| 36 #include "bindings/core/dart/DartScriptState.h" | |
| 37 #include "core/inspector/InjectedScriptHost.h" | |
| 38 #include "core/inspector/InjectedScriptManager.h" | |
| 39 #include "core/inspector/JSONParser.h" | |
| 40 #include "platform/JSONValues.h" | |
| 41 #include "public/platform/Platform.h" | |
| 42 #include "public/platform/WebData.h" | |
| 43 #include "wtf/PassOwnPtr.h" | |
| 44 | |
| 45 namespace blink { | |
| 46 | |
| 47 DartInjectedScriptManager::DartInjectedScriptManager(InspectedStateAccessCheck a
ccessCheck, InjectedScriptManager* javaScriptInjectedScriptManager) | |
| 48 : m_nextInjectedScriptId(1000000000) // Elegant design so that Dart and Java
Script ids don't overlap. | |
| 49 , m_inspectedStateAccessCheck(accessCheck) | |
| 50 , m_javaScriptInjectedScriptManager(javaScriptInjectedScriptManager) | |
| 51 { | |
| 52 } | |
| 53 | |
| 54 DartInjectedScriptManager::~DartInjectedScriptManager() | |
| 55 { | |
| 56 } | |
| 57 | |
| 58 InjectedScriptHost* DartInjectedScriptManager::injectedScriptHost() | |
| 59 { | |
| 60 return m_javaScriptInjectedScriptManager->injectedScriptHost(); | |
| 61 } | |
| 62 | |
| 63 DartInjectedScript* DartInjectedScriptManager::injectedScriptForId(int id) | |
| 64 { | |
| 65 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); | |
| 66 if (it != m_idToInjectedScript.end()) | |
| 67 return it->value; | |
| 68 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { | |
| 69 if (it->value == id) | |
| 70 return injectedScriptFor(it->key.get()); | |
| 71 } | |
| 72 return 0; | |
| 73 } | |
| 74 | |
| 75 int DartInjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState) | |
| 76 { | |
| 77 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState); | |
| 78 if (it != m_scriptStateToId.end()) | |
| 79 return it->value; | |
| 80 int id = m_nextInjectedScriptId++; | |
| 81 m_scriptStateToId.set(scriptState, id); | |
| 82 return id; | |
| 83 } | |
| 84 | |
| 85 DartInjectedScript* DartInjectedScriptManager::injectedScriptForObjectId(const S
tring& objectId) | |
| 86 { | |
| 87 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId); | |
| 88 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) { | |
| 89 long injectedScriptId = 0; | |
| 90 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId",
&injectedScriptId); | |
| 91 if (success) { | |
| 92 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(inject
edScriptId); | |
| 93 if (s != m_idToInjectedScript.end()) | |
| 94 return s->value; | |
| 95 } | |
| 96 } | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 void DartInjectedScriptManager::discardInjectedScripts() | |
| 101 { | |
| 102 m_idToInjectedScript.clear(); | |
| 103 m_scriptStateToId.clear(); | |
| 104 } | |
| 105 | |
| 106 void DartInjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window
) | |
| 107 { | |
| 108 if (m_scriptStateToId.isEmpty()) | |
| 109 return; | |
| 110 | |
| 111 Vector<long> idsToRemove; | |
| 112 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end(); | |
| 113 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it !
= end; ++it) { | |
| 114 ScriptState* scriptState = it->value->scriptState(); | |
| 115 if (window != scriptState->domWindow()) | |
| 116 continue; | |
| 117 m_scriptStateToId.remove(scriptState); | |
| 118 idsToRemove.append(it->key); | |
| 119 } | |
| 120 for (size_t i = 0; i < idsToRemove.size(); i++) | |
| 121 delete m_idToInjectedScript.get(idsToRemove[i]); | |
| 122 m_idToInjectedScript.removeAll(idsToRemove); | |
| 123 | |
| 124 | |
| 125 // Now remove script states that have id but no injected script. | |
| 126 Vector<ScriptState*> scriptStatesToRemove; | |
| 127 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { | |
| 128 ScriptState* scriptState = it->key.get(); | |
| 129 if (window == scriptState->domWindow()) | |
| 130 scriptStatesToRemove.append(scriptState); | |
| 131 } | |
| 132 m_scriptStateToId.removeAll(scriptStatesToRemove); | |
| 133 } | |
| 134 | |
| 135 bool DartInjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState*
) | |
| 136 { | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 void DartInjectedScriptManager::releaseObjectGroup(const String& objectGroup) | |
| 141 { | |
| 142 Vector<int> keys; | |
| 143 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k
eys().end()); | |
| 144 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) { | |
| 145 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k); | |
| 146 if (s != m_idToInjectedScript.end()) | |
| 147 s->value->releaseObjectGroup(objectGroup); // m_idToInjectedScript m
ay change here. | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 DartInjectedScript* DartInjectedScriptManager::injectedScriptFor(ScriptState* in
spectedScriptState) | |
| 152 { | |
| 153 if (!inspectedScriptState) | |
| 154 return 0; | |
| 155 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState); | |
| 156 if (it != m_scriptStateToId.end()) { | |
| 157 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu
e); | |
| 158 if (it1 != m_idToInjectedScript.end()) | |
| 159 return it1->value; | |
| 160 } | |
| 161 | |
| 162 if (!m_inspectedStateAccessCheck(inspectedScriptState)) | |
| 163 return 0; | |
| 164 | |
| 165 int id = injectedScriptIdFor(inspectedScriptState); | |
| 166 | |
| 167 DartInjectedScript* result; | |
| 168 ASSERT(!inspectedScriptState->isJavaScript()); | |
| 169 result = new DartInjectedScript(static_cast<DartScriptState*>(inspectedScrip
tState), m_inspectedStateAccessCheck, id, injectedScriptHost(), m_javaScriptInje
ctedScriptManager); | |
| 170 m_idToInjectedScript.set(id, result); | |
| 171 return result; | |
| 172 } | |
| 173 | |
| 174 } // namespace blink | |
| 175 | |
| OLD | NEW |