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 "core/inspector/InjectedScriptManager.h" |
| 33 |
| 34 #include "bindings/core/v8/ScriptValue.h" |
| 35 #include "core/inspector/InjectedScript.h" |
| 36 #include "core/inspector/InjectedScriptHost.h" |
| 37 #include "core/inspector/JSONParser.h" |
| 38 #include "platform/JSONValues.h" |
| 39 #include "public/platform/Platform.h" |
| 40 #include "public/platform/WebData.h" |
| 41 #include "wtf/PassOwnPtr.h" |
| 42 |
| 43 namespace blink { |
| 44 |
| 45 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage() |
| 46 { |
| 47 return adoptPtr(new InjectedScriptManager); |
| 48 } |
| 49 |
| 50 InjectedScriptManager::InjectedScriptManager() |
| 51 : m_nextInjectedScriptId(1) |
| 52 , m_injectedScriptHost(InjectedScriptHost::create()) |
| 53 { |
| 54 } |
| 55 |
| 56 InjectedScriptManager::~InjectedScriptManager() |
| 57 { |
| 58 } |
| 59 |
| 60 void InjectedScriptManager::disconnect() |
| 61 { |
| 62 m_injectedScriptHost->disconnect(); |
| 63 m_injectedScriptHost.clear(); |
| 64 } |
| 65 |
| 66 InjectedScriptHost* InjectedScriptManager::injectedScriptHost() |
| 67 { |
| 68 return m_injectedScriptHost.get(); |
| 69 } |
| 70 |
| 71 InjectedScript InjectedScriptManager::injectedScriptForId(int id) |
| 72 { |
| 73 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); |
| 74 if (it != m_idToInjectedScript.end()) |
| 75 return it->value; |
| 76 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { |
| 77 if (it->value == id) |
| 78 return injectedScriptFor(it->key.get()); |
| 79 } |
| 80 return InjectedScript(); |
| 81 } |
| 82 |
| 83 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState) |
| 84 { |
| 85 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState); |
| 86 if (it != m_scriptStateToId.end()) |
| 87 return it->value; |
| 88 int id = m_nextInjectedScriptId++; |
| 89 m_scriptStateToId.set(scriptState, id); |
| 90 return id; |
| 91 } |
| 92 |
| 93 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& ob
jectId) |
| 94 { |
| 95 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId); |
| 96 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) { |
| 97 long injectedScriptId = 0; |
| 98 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId",
&injectedScriptId); |
| 99 if (success) |
| 100 return m_idToInjectedScript.get(injectedScriptId); |
| 101 } |
| 102 return InjectedScript(); |
| 103 } |
| 104 |
| 105 void InjectedScriptManager::discardInjectedScripts() |
| 106 { |
| 107 m_idToInjectedScript.clear(); |
| 108 m_scriptStateToId.clear(); |
| 109 } |
| 110 |
| 111 void InjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window) |
| 112 { |
| 113 if (m_scriptStateToId.isEmpty()) |
| 114 return; |
| 115 |
| 116 Vector<long> idsToRemove; |
| 117 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end(); |
| 118 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it !
= end; ++it) { |
| 119 ScriptState* scriptState = it->value.scriptState(); |
| 120 if (window != scriptState->domWindow()) |
| 121 continue; |
| 122 m_scriptStateToId.remove(scriptState); |
| 123 idsToRemove.append(it->key); |
| 124 } |
| 125 m_idToInjectedScript.removeAll(idsToRemove); |
| 126 |
| 127 // Now remove script states that have id but no injected script. |
| 128 Vector<ScriptState*> scriptStatesToRemove; |
| 129 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scrip
tStateToId.end(); ++it) { |
| 130 ScriptState* scriptState = it->key.get(); |
| 131 if (window == scriptState->domWindow()) |
| 132 scriptStatesToRemove.append(scriptState); |
| 133 } |
| 134 m_scriptStateToId.removeAll(scriptStatesToRemove); |
| 135 } |
| 136 |
| 137 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup) |
| 138 { |
| 139 Vector<int> keys; |
| 140 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.k
eys().end()); |
| 141 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) { |
| 142 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k); |
| 143 if (s != m_idToInjectedScript.end()) |
| 144 s->value.releaseObjectGroup(objectGroup); // m_idToInjectedScript ma
y change here. |
| 145 } |
| 146 } |
| 147 |
| 148 String InjectedScriptManager::injectedScriptSource() |
| 149 { |
| 150 const blink::WebData& injectedScriptSourceResource = blink::Platform::curren
t()->loadResource("InjectedScriptSource.js"); |
| 151 return String(injectedScriptSourceResource.data(), injectedScriptSourceResou
rce.size()); |
| 152 } |
| 153 |
| 154 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedSc
riptState) |
| 155 { |
| 156 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState); |
| 157 if (it != m_scriptStateToId.end()) { |
| 158 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->valu
e); |
| 159 if (it1 != m_idToInjectedScript.end()) |
| 160 return it1->value; |
| 161 } |
| 162 |
| 163 int id = injectedScriptIdFor(inspectedScriptState); |
| 164 ScriptValue injectedScriptValue = createInjectedScript(injectedScriptSource(
), inspectedScriptState, id); |
| 165 InjectedScript result(injectedScriptValue); |
| 166 m_idToInjectedScript.set(id, result); |
| 167 return result; |
| 168 } |
| 169 |
| 170 } // namespace blink |
| 171 |
OLD | NEW |