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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
32 #include "core/inspector/InjectedScriptManager.h"
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"
45 PassOwnPtrWillBeRawPtr<InjectedScriptManager> InjectedScriptManager::createForPage()
47 return adoptPtrWillBeNoop(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWindow));
50 PassOwnPtrWillBeRawPtr<InjectedScriptManager> InjectedScriptManager::createForWorker()
52 return adoptPtrWillBeNoop(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWorkerGlobalScope));
55 InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessCheck)
56 : m_nextInjectedScriptId(1)
57 , m_injectedScriptHost(InjectedScriptHost::create())
58 , m_inspectedStateAccessCheck(accessCheck)
62 InjectedScriptManager::~InjectedScriptManager()
66 void InjectedScriptManager::trace(Visitor* visitor)
68 visitor->trace(m_injectedScriptHost);
71 void InjectedScriptManager::disconnect()
73 m_injectedScriptHost->disconnect();
74 m_injectedScriptHost.clear();
77 InjectedScriptHost* InjectedScriptManager::injectedScriptHost()
79 return m_injectedScriptHost.get();
82 InjectedScript InjectedScriptManager::injectedScriptForId(int id)
84 IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id);
85 if (it != m_idToInjectedScript.end())
87 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
89 return injectedScriptFor(it->key.get());
91 return InjectedScript();
94 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState)
96 ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState);
97 if (it != m_scriptStateToId.end())
99 int id = m_nextInjectedScriptId++;
100 m_scriptStateToId.set(scriptState, id);
104 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId)
106 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId);
107 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) {
108 long injectedScriptId = 0;
109 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
111 return m_idToInjectedScript.get(injectedScriptId);
113 return InjectedScript();
116 void InjectedScriptManager::discardInjectedScripts()
118 m_idToInjectedScript.clear();
119 m_scriptStateToId.clear();
122 void InjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window)
124 if (m_scriptStateToId.isEmpty())
127 Vector<long> idsToRemove;
128 IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
129 for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != end; ++it) {
130 ScriptState* scriptState = it->value.scriptState();
131 if (window != scriptState->domWindow())
133 m_scriptStateToId.remove(scriptState);
134 idsToRemove.append(it->key);
136 m_idToInjectedScript.removeAll(idsToRemove);
138 // Now remove script states that have id but no injected script.
139 Vector<ScriptState*> scriptStatesToRemove;
140 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
141 ScriptState* scriptState = it->key.get();
142 if (window == scriptState->domWindow())
143 scriptStatesToRemove.append(scriptState);
145 m_scriptStateToId.removeAll(scriptStatesToRemove);
148 bool InjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState*)
153 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
156 keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.keys().end());
157 for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) {
158 IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k);
159 if (s != m_idToInjectedScript.end())
160 s->value.releaseObjectGroup(objectGroup); // m_idToInjectedScript may change here.
164 String InjectedScriptManager::injectedScriptSource()
166 const blink::WebData& injectedScriptSourceResource = blink::Platform::current()->loadResource("InjectedScriptSource.js");
167 return String(injectedScriptSourceResource.data(), injectedScriptSourceResource.size());
170 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedScriptState)
172 ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState);
173 if (it != m_scriptStateToId.end()) {
174 IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->value);
175 if (it1 != m_idToInjectedScript.end())
179 if (!m_inspectedStateAccessCheck(inspectedScriptState))
180 return InjectedScript();
182 int id = injectedScriptIdFor(inspectedScriptState);
183 ScriptValue injectedScriptValue = createInjectedScript(injectedScriptSource(), inspectedScriptState, id);
184 InjectedScript result(injectedScriptValue, m_inspectedStateAccessCheck);
185 m_idToInjectedScript.set(id, result);