1/*
2 * Copyright (C) 2007-2017 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 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 "InjectedScriptManager.h"
33
34#include "CatchScope.h"
35#include "Completion.h"
36#include "InjectedScriptHost.h"
37#include "InjectedScriptSource.h"
38#include "JSCInlines.h"
39#include "JSInjectedScriptHost.h"
40#include "JSLock.h"
41#include "ScriptObject.h"
42#include "SourceCode.h"
43#include <wtf/JSONValues.h>
44
45using namespace JSC;
46
47namespace Inspector {
48
49InjectedScriptManager::InjectedScriptManager(InspectorEnvironment& environment, Ref<InjectedScriptHost>&& injectedScriptHost)
50 : m_environment(environment)
51 , m_injectedScriptHost(WTFMove(injectedScriptHost))
52 , m_nextInjectedScriptId(1)
53{
54}
55
56InjectedScriptManager::~InjectedScriptManager()
57{
58}
59
60void InjectedScriptManager::disconnect()
61{
62 discardInjectedScripts();
63}
64
65void InjectedScriptManager::discardInjectedScripts()
66{
67 m_injectedScriptHost->clearAllWrappers();
68 m_idToInjectedScript.clear();
69 m_scriptStateToId.clear();
70}
71
72InjectedScriptHost& InjectedScriptManager::injectedScriptHost()
73{
74 return m_injectedScriptHost.get();
75}
76
77InjectedScript InjectedScriptManager::injectedScriptForId(int id)
78{
79 auto it = m_idToInjectedScript.find(id);
80 if (it != m_idToInjectedScript.end())
81 return it->value;
82
83 for (auto it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
84 if (it->value == id)
85 return injectedScriptFor(it->key);
86 }
87
88 return InjectedScript();
89}
90
91int InjectedScriptManager::injectedScriptIdFor(ExecState* scriptState)
92{
93 auto it = m_scriptStateToId.find(scriptState);
94 if (it != m_scriptStateToId.end())
95 return it->value;
96
97 int id = m_nextInjectedScriptId++;
98 m_scriptStateToId.set(scriptState, id);
99 return id;
100}
101
102InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId)
103{
104 RefPtr<JSON::Value> parsedObjectId;
105 if (!JSON::Value::parseJSON(objectId, parsedObjectId))
106 return InjectedScript();
107
108 RefPtr<JSON::Object> resultObject;
109 if (!parsedObjectId->asObject(resultObject))
110 return InjectedScript();
111
112 long injectedScriptId = 0;
113 if (!resultObject->getInteger("injectedScriptId"_s, injectedScriptId))
114 return InjectedScript();
115
116 return m_idToInjectedScript.get(injectedScriptId);
117}
118
119void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
120{
121 for (auto& injectedScript : m_idToInjectedScript.values())
122 injectedScript.releaseObjectGroup(objectGroup);
123}
124
125void InjectedScriptManager::clearEventValue()
126{
127 for (auto& injectedScript : m_idToInjectedScript.values())
128 injectedScript.clearEventValue();
129}
130
131void InjectedScriptManager::clearExceptionValue()
132{
133 for (auto& injectedScript : m_idToInjectedScript.values())
134 injectedScript.clearExceptionValue();
135}
136
137String InjectedScriptManager::injectedScriptSource()
138{
139 return StringImpl::createWithoutCopying(InjectedScriptSource_js, sizeof(InjectedScriptSource_js));
140}
141
142JSC::JSObject* InjectedScriptManager::createInjectedScript(const String& source, ExecState* scriptState, int id)
143{
144 VM& vm = scriptState->vm();
145 JSLockHolder lock(vm);
146 auto scope = DECLARE_CATCH_SCOPE(vm);
147
148 SourceCode sourceCode = makeSource(source, { });
149 JSGlobalObject* globalObject = scriptState->lexicalGlobalObject();
150 JSValue globalThisValue = scriptState->globalThisValue();
151
152 NakedPtr<Exception> evaluationException;
153 InspectorEvaluateHandler evaluateHandler = m_environment.evaluateHandler();
154 JSValue functionValue = evaluateHandler(scriptState, sourceCode, globalThisValue, evaluationException);
155 if (evaluationException)
156 return nullptr;
157
158 CallData callData;
159 CallType callType = getCallData(vm, functionValue, callData);
160 if (callType == CallType::None)
161 return nullptr;
162
163 MarkedArgumentBuffer args;
164 args.append(m_injectedScriptHost->wrapper(scriptState, globalObject));
165 args.append(globalThisValue);
166 args.append(jsNumber(id));
167 ASSERT(!args.hasOverflowed());
168
169 JSValue result = JSC::call(scriptState, functionValue, callType, callData, globalThisValue, args);
170 scope.clearException();
171 return result.getObject();
172}
173
174InjectedScript InjectedScriptManager::injectedScriptFor(ExecState* inspectedExecState)
175{
176 auto it = m_scriptStateToId.find(inspectedExecState);
177 if (it != m_scriptStateToId.end()) {
178 auto it1 = m_idToInjectedScript.find(it->value);
179 if (it1 != m_idToInjectedScript.end())
180 return it1->value;
181 }
182
183 if (!m_environment.canAccessInspectedScriptState(inspectedExecState))
184 return InjectedScript();
185
186 int id = injectedScriptIdFor(inspectedExecState);
187 auto injectedScriptObject = createInjectedScript(injectedScriptSource(), inspectedExecState, id);
188 if (!injectedScriptObject) {
189 WTFLogAlways("Failed to parse/execute InjectedScriptSource.js!");
190 WTFLogAlways("%s\n", injectedScriptSource().ascii().data());
191 RELEASE_ASSERT_NOT_REACHED();
192 }
193
194 InjectedScript result({ inspectedExecState, injectedScriptObject }, &m_environment);
195 m_idToInjectedScript.set(id, result);
196 didCreateInjectedScript(result);
197 return result;
198}
199
200void InjectedScriptManager::didCreateInjectedScript(const InjectedScript&)
201{
202 // Intentionally empty. This allows for subclasses to inject additional scripts.
203}
204
205} // namespace Inspector
206
207