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