1 /*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "config.h"
31 #include "InspectorFrontend.h"
32
33 #include "ConsoleMessage.h"
34 #include "Frame.h"
35 #include "InspectorController.h" // TODO(pfeldman): Extract SpecialPanels to remove include.
36 #include "Node.h"
37 #include "ScriptFunctionCall.h"
38 #include "ScriptObject.h"
39 #include "ScriptObjectQuarantine.h"
40 #include "ScriptState.h"
41 #include "ScriptString.h"
42 #include <wtf/OwnPtr.h>
43
44 #if ENABLE(JAVASCRIPT_DEBUGGER)
45 #include <parser/SourceCode.h>
46 #include <runtime/JSValue.h>
47 #include <runtime/UString.h>
48 #endif
49
50 namespace WebCore {
51
InspectorFrontend(ScriptState * scriptState,ScriptObject webInspector)52 InspectorFrontend::InspectorFrontend(ScriptState* scriptState, ScriptObject webInspector)
53 : m_scriptState(scriptState)
54 , m_webInspector(webInspector)
55 {
56 }
57
~InspectorFrontend()58 InspectorFrontend::~InspectorFrontend()
59 {
60 m_webInspector = ScriptObject();
61 }
62
newScriptArray()63 ScriptArray InspectorFrontend::newScriptArray()
64 {
65 return ScriptArray::createNew(m_scriptState);
66 }
67
newScriptObject()68 ScriptObject InspectorFrontend::newScriptObject()
69 {
70 return ScriptObject::createNew(m_scriptState);
71 }
72
addMessageToConsole(const ScriptObject & messageObj,const Vector<ScriptString> & frames,const Vector<ScriptValue> wrappedArguments,const String & message)73 void InspectorFrontend::addMessageToConsole(const ScriptObject& messageObj, const Vector<ScriptString>& frames, const Vector<ScriptValue> wrappedArguments, const String& message)
74 {
75 OwnPtr<ScriptFunctionCall> function(newFunctionCall("addMessageToConsole"));
76 function->appendArgument(messageObj);
77 if (!frames.isEmpty()) {
78 for (unsigned i = 0; i < frames.size(); ++i)
79 function->appendArgument(frames[i]);
80 } else if (!wrappedArguments.isEmpty()) {
81 for (unsigned i = 0; i < wrappedArguments.size(); ++i)
82 function->appendArgument(wrappedArguments[i]);
83 } else
84 function->appendArgument(message);
85 function->call();
86 }
87
addResource(long long identifier,const ScriptObject & resourceObj)88 bool InspectorFrontend::addResource(long long identifier, const ScriptObject& resourceObj)
89 {
90 OwnPtr<ScriptFunctionCall> function(newFunctionCall("addResource"));
91 function->appendArgument(identifier);
92 function->appendArgument(resourceObj);
93 bool hadException = false;
94 function->call(hadException);
95 return !hadException;
96 }
97
updateResource(long long identifier,const ScriptObject & resourceObj)98 bool InspectorFrontend::updateResource(long long identifier, const ScriptObject& resourceObj)
99 {
100 OwnPtr<ScriptFunctionCall> function(newFunctionCall("updateResource"));
101 function->appendArgument(identifier);
102 function->appendArgument(resourceObj);
103 bool hadException = false;
104 function->call(hadException);
105 return !hadException;
106 }
107
removeResource(long long identifier)108 void InspectorFrontend::removeResource(long long identifier)
109 {
110 OwnPtr<ScriptFunctionCall> function(newFunctionCall("removeResource"));
111 function->appendArgument(identifier);
112 function->call();
113 }
114
updateFocusedNode(Node * node)115 void InspectorFrontend::updateFocusedNode(Node* node)
116 {
117 ScriptObject quarantinedNode;
118 if (!getQuarantinedScriptObject(node, quarantinedNode))
119 return;
120
121 OwnPtr<ScriptFunctionCall> function(newFunctionCall("updateFocusedNode"));
122 function->appendArgument(quarantinedNode);
123 function->call();
124 }
125
setAttachedWindow(bool attached)126 void InspectorFrontend::setAttachedWindow(bool attached)
127 {
128 OwnPtr<ScriptFunctionCall> function(newFunctionCall("setAttachedWindow"));
129 function->appendArgument(attached);
130 function->call();
131 }
132
inspectedWindowScriptObjectCleared(Frame * frame)133 void InspectorFrontend::inspectedWindowScriptObjectCleared(Frame* frame)
134 {
135 ScriptObject domWindow;
136 if (!getQuarantinedScriptObject(frame->domWindow(), domWindow))
137 return;
138
139 OwnPtr<ScriptFunctionCall> function(newFunctionCall("inspectedWindowCleared"));
140 function->appendArgument(domWindow);
141 function->call();
142 }
143
showPanel(int panel)144 void InspectorFrontend::showPanel(int panel)
145 {
146 const char* showFunctionName;
147 switch (panel) {
148 case InspectorController::ConsolePanel:
149 showFunctionName = "showConsole";
150 break;
151 case InspectorController::DatabasesPanel:
152 showFunctionName = "showDatabasesPanel";
153 break;
154 case InspectorController::ElementsPanel:
155 showFunctionName = "showElementsPanel";
156 break;
157 case InspectorController::ProfilesPanel:
158 showFunctionName = "showProfilesPanel";
159 break;
160 case InspectorController::ResourcesPanel:
161 showFunctionName = "showResourcesPanel";
162 break;
163 case InspectorController::ScriptsPanel:
164 showFunctionName = "showScriptsPanel";
165 break;
166 default:
167 ASSERT_NOT_REACHED();
168 showFunctionName = 0;
169 }
170
171 if (showFunctionName)
172 callSimpleFunction(showFunctionName);
173 }
174
populateInterface()175 void InspectorFrontend::populateInterface()
176 {
177 callSimpleFunction("populateInterface");
178 }
179
reset()180 void InspectorFrontend::reset()
181 {
182 callSimpleFunction("reset");
183 }
184
resourceTrackingWasEnabled()185 void InspectorFrontend::resourceTrackingWasEnabled()
186 {
187 callSimpleFunction("resourceTrackingWasEnabled");
188 }
189
resourceTrackingWasDisabled()190 void InspectorFrontend::resourceTrackingWasDisabled()
191 {
192 callSimpleFunction("resourceTrackingWasDisabled");
193 }
194
195 #if ENABLE(JAVASCRIPT_DEBUGGER)
attachDebuggerWhenShown()196 void InspectorFrontend::attachDebuggerWhenShown()
197 {
198 callSimpleFunction("attachDebuggerWhenShown");
199 }
200
debuggerWasEnabled()201 void InspectorFrontend::debuggerWasEnabled()
202 {
203 callSimpleFunction("debuggerWasEnabled");
204 }
205
debuggerWasDisabled()206 void InspectorFrontend::debuggerWasDisabled()
207 {
208 callSimpleFunction("debuggerWasDisabled");
209 }
210
profilerWasEnabled()211 void InspectorFrontend::profilerWasEnabled()
212 {
213 callSimpleFunction("profilerWasEnabled");
214 }
215
profilerWasDisabled()216 void InspectorFrontend::profilerWasDisabled()
217 {
218 callSimpleFunction("profilerWasDisabled");
219 }
220
parsedScriptSource(const JSC::SourceCode & source)221 void InspectorFrontend::parsedScriptSource(const JSC::SourceCode& source)
222 {
223 OwnPtr<ScriptFunctionCall> function(newFunctionCall("parsedScriptSource"));
224 function->appendArgument(JSC::UString(JSC::UString::from(source.provider()->asID())));
225 function->appendArgument(source.provider()->url());
226 function->appendArgument(JSC::UString(source.data(), source.length()));
227 function->appendArgument(source.firstLine());
228 function->call();
229 }
230
failedToParseScriptSource(const JSC::SourceCode & source,int errorLine,const JSC::UString & errorMessage)231 void InspectorFrontend::failedToParseScriptSource(const JSC::SourceCode& source, int errorLine, const JSC::UString& errorMessage)
232 {
233 OwnPtr<ScriptFunctionCall> function(newFunctionCall("failedToParseScriptSource"));
234 function->appendArgument(source.provider()->url());
235 function->appendArgument(JSC::UString(source.data(), source.length()));
236 function->appendArgument(source.firstLine());
237 function->appendArgument(errorLine);
238 function->appendArgument(errorMessage);
239 function->call();
240 }
241
addProfile(const JSC::JSValue & profile)242 void InspectorFrontend::addProfile(const JSC::JSValue& profile)
243 {
244 OwnPtr<ScriptFunctionCall> function(newFunctionCall("addProfile"));
245 function->appendArgument(profile);
246 function->call();
247 }
248
setRecordingProfile(bool isProfiling)249 void InspectorFrontend::setRecordingProfile(bool isProfiling)
250 {
251 OwnPtr<ScriptFunctionCall> function(newFunctionCall("setRecordingProfile"));
252 function->appendArgument(isProfiling);
253 function->call();
254 }
255
pausedScript()256 void InspectorFrontend::pausedScript()
257 {
258 callSimpleFunction("pausedScript");
259 }
260
resumedScript()261 void InspectorFrontend::resumedScript()
262 {
263 callSimpleFunction("resumedScript");
264 }
265 #endif
266
267 #if ENABLE(DATABASE)
addDatabase(const ScriptObject & dbObject)268 bool InspectorFrontend::addDatabase(const ScriptObject& dbObject)
269 {
270 OwnPtr<ScriptFunctionCall> function(newFunctionCall("addDatabase"));
271 function->appendArgument(dbObject);
272 bool hadException = false;
273 function->call(hadException);
274 return !hadException;
275 }
276 #endif
277
278 #if ENABLE(DOM_STORAGE)
addDOMStorage(const ScriptObject & domStorageObj)279 bool InspectorFrontend::addDOMStorage(const ScriptObject& domStorageObj)
280 {
281 OwnPtr<ScriptFunctionCall> function(newFunctionCall("addDOMStorage"));
282 function->appendArgument(domStorageObj);
283 bool hadException = false;
284 function->call(hadException);
285 return !hadException;
286 }
287 #endif
288
setDocumentElement(const ScriptObject & root)289 void InspectorFrontend::setDocumentElement(const ScriptObject& root)
290 {
291 OwnPtr<ScriptFunctionCall> function(newFunctionCall("setDocumentElement"));
292 function->appendArgument(root);
293 function->call();
294 }
295
setChildNodes(int parentId,const ScriptArray & nodes)296 void InspectorFrontend::setChildNodes(int parentId, const ScriptArray& nodes)
297 {
298 OwnPtr<ScriptFunctionCall> function(newFunctionCall("setChildNodes"));
299 function->appendArgument(parentId);
300 function->appendArgument(nodes);
301 function->call();
302 }
303
hasChildrenUpdated(int id,bool newValue)304 void InspectorFrontend::hasChildrenUpdated(int id, bool newValue)
305 {
306 OwnPtr<ScriptFunctionCall> function(newFunctionCall("hasChildrenUpdated"));
307 function->appendArgument(id);
308 function->appendArgument(newValue);
309 function->call();
310 }
311
childNodeInserted(int parentId,int prevId,const ScriptObject & node)312 void InspectorFrontend::childNodeInserted(int parentId, int prevId, const ScriptObject& node)
313 {
314 OwnPtr<ScriptFunctionCall> function(newFunctionCall("childNodeInserted"));
315 function->appendArgument(parentId);
316 function->appendArgument(prevId);
317 function->appendArgument(node);
318 function->call();
319 }
320
childNodeRemoved(int parentId,int id)321 void InspectorFrontend::childNodeRemoved(int parentId, int id)
322 {
323 OwnPtr<ScriptFunctionCall> function(newFunctionCall("childNodeRemoved"));
324 function->appendArgument(parentId);
325 function->appendArgument(id);
326 function->call();
327 }
328
attributesUpdated(int id,const ScriptArray & attributes)329 void InspectorFrontend::attributesUpdated(int id, const ScriptArray& attributes)
330 {
331 OwnPtr<ScriptFunctionCall> function(newFunctionCall("attributesUpdated"));
332 function->appendArgument(id);
333 function->appendArgument(attributes);
334 function->call();
335 }
336
didGetChildNodes(int callId)337 void InspectorFrontend::didGetChildNodes(int callId)
338 {
339 OwnPtr<ScriptFunctionCall> function(newFunctionCall("didGetChildNodes"));
340 function->appendArgument(callId);
341 function->call();
342 }
343
didApplyDomChange(int callId,bool success)344 void InspectorFrontend::didApplyDomChange(int callId, bool success)
345 {
346 OwnPtr<ScriptFunctionCall> function(newFunctionCall("didApplyDomChange"));
347 function->appendArgument(callId);
348 function->appendArgument(success);
349 function->call();
350 }
351
newFunctionCall(const String & functionName)352 PassOwnPtr<ScriptFunctionCall> InspectorFrontend::newFunctionCall(const String& functionName)
353 {
354 ScriptFunctionCall* function = new ScriptFunctionCall(m_scriptState, m_webInspector, "dispatch");
355 function->appendArgument(functionName);
356 return function;
357 }
358
callSimpleFunction(const String & functionName)359 void InspectorFrontend::callSimpleFunction(const String& functionName)
360 {
361 ScriptFunctionCall function(m_scriptState, m_webInspector, "dispatch");
362 function.appendArgument(functionName);
363 function.call();
364 }
365
366 } // namespace WebCore
367