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