2011-07-04 Vsevolod Vlasov <vsevik@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / InspectorInstrumentation.cpp
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "InspectorInstrumentation.h"
33
34 #if ENABLE(INSPECTOR)
35
36 #include "DOMWindow.h"
37 #include "Database.h"
38 #include "DocumentLoader.h"
39 #include "Event.h"
40 #include "EventContext.h"
41 #include "InspectorAgent.h"
42 #include "InspectorApplicationCacheAgent.h"
43 #include "InspectorDOMDebuggerAgent.h"
44 #include "InspectorCSSAgent.h"
45 #include "InspectorConsoleAgent.h"
46 #include "InspectorDatabaseAgent.h"
47 #include "InspectorDOMAgent.h"
48 #include "InspectorDOMStorageAgent.h"
49 #include "InspectorDebuggerAgent.h"
50 #include "InspectorPageAgent.h"
51 #include "InspectorProfilerAgent.h"
52 #include "InspectorResourceAgent.h"
53 #include "InspectorTimelineAgent.h"
54 #include "InspectorWorkerAgent.h"
55 #include "InstrumentingAgents.h"
56 #include "ScriptArguments.h"
57 #include "ScriptCallStack.h"
58 #include "ScriptProfile.h"
59 #include "XMLHttpRequest.h"
60 #include <wtf/StdLibExtras.h>
61 #include <wtf/text/CString.h>
62
63 namespace WebCore {
64
65 static const char* const listenerEventCategoryType = "listener";
66 static const char* const instrumentationEventCategoryType = "instrumentation";
67
68 static const char* const setTimerEventName = "setTimer";
69 static const char* const clearTimerEventName = "clearTimer";
70 static const char* const timerFiredEventName = "timerFired";
71
72 InspectorInstrumentation::InstrumentingAgentsMap& InspectorInstrumentation::instrumentingAgents()
73 {
74     DEFINE_STATIC_LOCAL(InstrumentingAgentsMap, agents, ());
75     return agents;
76 }
77
78 int InspectorInstrumentation::s_frontendCounter = 0;
79
80 static bool eventHasListeners(const AtomicString& eventType, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
81 {
82     if (window && window->hasEventListeners(eventType))
83         return true;
84
85     if (node->hasEventListeners(eventType))
86         return true;
87
88     for (size_t i = 0; i < ancestors.size(); i++) {
89         Node* ancestor = ancestors[i].node();
90         if (ancestor->hasEventListeners(eventType))
91             return true;
92     }
93
94     return false;
95 }
96
97 void InspectorInstrumentation::didClearWindowObjectInWorldImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, DOMWrapperWorld* world)
98 {
99     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
100         pageAgent->didClearWindowObjectInWorld(frame, world);
101     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
102         inspectorAgent->didClearWindowObjectInWorld(frame, world);
103 }
104
105 void InspectorInstrumentation::inspectedPageDestroyedImpl(InstrumentingAgents* instrumentingAgents)
106 {
107     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
108         inspectorAgent->inspectedPageDestroyed();
109 }
110
111 void InspectorInstrumentation::willInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node, Node* parent)
112 {
113 #if ENABLE(JAVASCRIPT_DEBUGGER)
114     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
115         domDebuggerAgent->willInsertDOMNode(node, parent);
116 #endif
117 }
118
119 void InspectorInstrumentation::didInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
120 {
121     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
122         domAgent->didInsertDOMNode(node);
123 #if ENABLE(JAVASCRIPT_DEBUGGER)
124     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
125         domDebuggerAgent->didInsertDOMNode(node);
126 #endif
127 }
128
129 void InspectorInstrumentation::willRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
130 {
131 #if ENABLE(JAVASCRIPT_DEBUGGER)
132     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
133         domDebuggerAgent->willRemoveDOMNode(node);
134 #endif
135 }
136
137 void InspectorInstrumentation::didRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
138 {
139 #if ENABLE(JAVASCRIPT_DEBUGGER)
140     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
141         domDebuggerAgent->didRemoveDOMNode(node);
142 #endif
143     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
144         domAgent->didRemoveDOMNode(node);
145 }
146
147 void InspectorInstrumentation::willModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element)
148 {
149 #if ENABLE(JAVASCRIPT_DEBUGGER)
150     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
151         domDebuggerAgent->willModifyDOMAttr(element);
152 #endif
153 }
154
155 void InspectorInstrumentation::didModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element)
156 {
157     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
158         domAgent->didModifyDOMAttr(element);
159 }
160
161 void InspectorInstrumentation::didInvalidateStyleAttrImpl(InstrumentingAgents* instrumentingAgents, Node* node)
162 {
163     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
164         domAgent->didInvalidateStyleAttr(node);
165     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
166         domDebuggerAgent->didInvalidateStyleAttr(node);
167 }
168
169 void InspectorInstrumentation::frameWindowDiscardedImpl(InstrumentingAgents* instrumentingAgents, DOMWindow* window)
170 {
171     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
172         consoleAgent->frameWindowDiscarded(window);
173 }
174
175 void InspectorInstrumentation::mouseDidMoveOverElementImpl(InstrumentingAgents* instrumentingAgents, const HitTestResult& result, unsigned modifierFlags)
176 {
177     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
178         domAgent->mouseDidMoveOverElement(result, modifierFlags);
179 }
180
181 bool InspectorInstrumentation::handleMousePressImpl(InstrumentingAgents* instrumentingAgents)
182 {
183     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
184         return domAgent->handleMousePress();
185     return false;
186 }
187
188 void InspectorInstrumentation::characterDataModifiedImpl(InstrumentingAgents* instrumentingAgents, CharacterData* characterData)
189 {
190     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
191         domAgent->characterDataModified(characterData);
192 }
193
194 void InspectorInstrumentation::willSendXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url)
195 {
196 #if ENABLE(JAVASCRIPT_DEBUGGER)
197     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
198         domDebuggerAgent->willSendXMLHttpRequest(url);
199 #endif
200 }
201
202 void InspectorInstrumentation::didScheduleResourceRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url)
203 {
204     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
205         timelineAgent->didScheduleResourceRequest(url);
206 }
207
208 void InspectorInstrumentation::didInstallTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId, int timeout, bool singleShot)
209 {
210     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, setTimerEventName, true);
211     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
212         timelineAgent->didInstallTimer(timerId, timeout, singleShot);
213 }
214
215 void InspectorInstrumentation::didRemoveTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId)
216 {
217     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, clearTimerEventName, true);
218     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
219         timelineAgent->didRemoveTimer(timerId);
220 }
221
222 InspectorInstrumentationCookie InspectorInstrumentation::willCallFunctionImpl(InstrumentingAgents* instrumentingAgents, const String& scriptName, int scriptLine)
223 {
224     int timelineAgentId = 0;
225     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
226         timelineAgent->willCallFunction(scriptName, scriptLine);
227         timelineAgentId = timelineAgent->id();
228     }
229     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
230 }
231
232 void InspectorInstrumentation::didCallFunctionImpl(const InspectorInstrumentationCookie& cookie)
233 {
234     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
235         timelineAgent->didCallFunction();
236 }
237
238 InspectorInstrumentationCookie InspectorInstrumentation::willChangeXHRReadyStateImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request)
239 {
240     int timelineAgentId = 0;
241     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
242     if (timelineAgent && request->hasEventListeners(eventNames().readystatechangeEvent)) {
243         timelineAgent->willChangeXHRReadyState(request->url().string(), request->readyState());
244         timelineAgentId = timelineAgent->id();
245     }
246     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
247 }
248
249 void InspectorInstrumentation::didChangeXHRReadyStateImpl(const InspectorInstrumentationCookie& cookie)
250 {
251     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
252         timelineAgent->didChangeXHRReadyState();
253 }
254
255 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
256 {
257     pauseOnNativeEventIfNeeded(instrumentingAgents, listenerEventCategoryType, event.type(), false);
258
259     int timelineAgentId = 0;
260     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
261     if (timelineAgent && eventHasListeners(event.type(), window, node, ancestors)) {
262         timelineAgent->willDispatchEvent(event);
263         timelineAgentId = timelineAgent->id();
264     }
265     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
266 }
267
268 void InspectorInstrumentation::didDispatchEventImpl(const InspectorInstrumentationCookie& cookie)
269 {
270     cancelPauseOnNativeEvent(cookie.first);
271
272     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
273         timelineAgent->didDispatchEvent();
274 }
275
276 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventOnWindowImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window)
277 {
278     pauseOnNativeEventIfNeeded(instrumentingAgents, listenerEventCategoryType, event.type(), false);
279
280     int timelineAgentId = 0;
281     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
282     if (timelineAgent && window->hasEventListeners(event.type())) {
283         timelineAgent->willDispatchEvent(event);
284         timelineAgentId = timelineAgent->id();
285     }
286     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
287 }
288
289 void InspectorInstrumentation::didDispatchEventOnWindowImpl(const InspectorInstrumentationCookie& cookie)
290 {
291     cancelPauseOnNativeEvent(cookie.first);
292
293     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
294         timelineAgent->didDispatchEvent();
295 }
296
297 InspectorInstrumentationCookie InspectorInstrumentation::willEvaluateScriptImpl(InstrumentingAgents* instrumentingAgents, const String& url, int lineNumber)
298 {
299     int timelineAgentId = 0;
300     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
301         timelineAgent->willEvaluateScript(url, lineNumber);
302         timelineAgentId = timelineAgent->id();
303     }
304     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
305 }
306
307 void InspectorInstrumentation::didEvaluateScriptImpl(const InspectorInstrumentationCookie& cookie)
308 {
309     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
310         timelineAgent->didEvaluateScript();
311 }
312
313 InspectorInstrumentationCookie InspectorInstrumentation::willFireTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId)
314 {
315     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, timerFiredEventName, false);
316
317     int timelineAgentId = 0;
318     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
319         timelineAgent->willFireTimer(timerId);
320         timelineAgentId = timelineAgent->id();
321     }
322     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
323 }
324
325 void InspectorInstrumentation::didFireTimerImpl(const InspectorInstrumentationCookie& cookie)
326 {
327     cancelPauseOnNativeEvent(cookie.first);
328
329     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
330         timelineAgent->didFireTimer();
331 }
332
333 InspectorInstrumentationCookie InspectorInstrumentation::willLayoutImpl(InstrumentingAgents* instrumentingAgents)
334 {
335     int timelineAgentId = 0;
336     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
337         timelineAgent->willLayout();
338         timelineAgentId = timelineAgent->id();
339     }
340     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
341 }
342
343 void InspectorInstrumentation::didLayoutImpl(const InspectorInstrumentationCookie& cookie)
344 {
345     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
346         timelineAgent->didLayout();
347 }
348
349 InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHRImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request)
350 {
351     int timelineAgentId = 0;
352     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
353     if (timelineAgent && request->hasEventListeners(eventNames().loadEvent)) {
354         timelineAgent->willLoadXHR(request->url());
355         timelineAgentId = timelineAgent->id();
356     }
357     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
358 }
359
360 void InspectorInstrumentation::didLoadXHRImpl(const InspectorInstrumentationCookie& cookie)
361 {
362     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
363         timelineAgent->didLoadXHR();
364 }
365
366 InspectorInstrumentationCookie InspectorInstrumentation::willPaintImpl(InstrumentingAgents* instrumentingAgents, const IntRect& rect)
367 {
368     int timelineAgentId = 0;
369     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
370         timelineAgent->willPaint(rect);
371         timelineAgentId = timelineAgent->id();
372     }
373     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
374 }
375
376 void InspectorInstrumentation::didPaintImpl(const InspectorInstrumentationCookie& cookie)
377 {
378     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
379         timelineAgent->didPaint();
380 }
381
382 InspectorInstrumentationCookie InspectorInstrumentation::willRecalculateStyleImpl(InstrumentingAgents* instrumentingAgents)
383 {
384     int timelineAgentId = 0;
385     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
386         timelineAgent->willRecalculateStyle();
387         timelineAgentId = timelineAgent->id();
388     }
389     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
390 }
391
392 void InspectorInstrumentation::didRecalculateStyleImpl(const InspectorInstrumentationCookie& cookie)
393 {
394     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
395         timelineAgent->didRecalculateStyle();
396 }
397
398 void InspectorInstrumentation::applyUserAgentOverrideImpl(InstrumentingAgents* instrumentingAgents, String* userAgent)
399 {
400     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
401         resourceAgent->applyUserAgentOverride(userAgent);
402 }
403
404 void InspectorInstrumentation::willSendRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
405 {
406     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
407         timelineAgent->willSendResourceRequest(identifier, request);
408     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
409         resourceAgent->willSendRequest(identifier, loader, request, redirectResponse);
410 }
411
412 void InspectorInstrumentation::continueAfterPingLoaderImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& response)
413 {
414     willSendRequestImpl(instrumentingAgents, identifier, loader, request, response);
415 }
416
417 void InspectorInstrumentation::markResourceAsCachedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
418 {
419     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
420         resourceAgent->markResourceAsCached(identifier);
421 }
422
423 void InspectorInstrumentation::didLoadResourceFromMemoryCacheImpl(InstrumentingAgents* instrumentingAgents, DocumentLoader* loader, const CachedResource* cachedResource)
424 {
425     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
426     if (!inspectorAgent || !inspectorAgent->enabled())
427         return;
428     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
429         resourceAgent->didLoadResourceFromMemoryCache(loader, cachedResource);
430 }
431
432 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
433 {
434     int timelineAgentId = 0;
435     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
436         timelineAgent->willReceiveResourceData(identifier);
437         timelineAgentId = timelineAgent->id();
438     }
439     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
440 }
441
442 void InspectorInstrumentation::didReceiveResourceDataImpl(const InspectorInstrumentationCookie& cookie)
443 {
444     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
445         timelineAgent->didReceiveResourceData();
446 }
447
448 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const ResourceResponse& response)
449 {
450     int timelineAgentId = 0;
451     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
452     if (timelineAgent) {
453         timelineAgent->willReceiveResourceResponse(identifier, response);
454         timelineAgentId = timelineAgent->id();
455     }
456     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
457 }
458
459 void InspectorInstrumentation::didReceiveResourceResponseImpl(const InspectorInstrumentationCookie& cookie, unsigned long identifier, DocumentLoader* loader, const ResourceResponse& response)
460 {
461     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
462         timelineAgent->didReceiveResourceResponse();
463     if (!loader)
464         return;
465     InstrumentingAgents* instrumentingAgents = cookie.first;
466     if (!instrumentingAgents)
467         return;
468     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
469         resourceAgent->didReceiveResponse(identifier, loader, response);
470     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
471         consoleAgent->didReceiveResponse(identifier, response); // This should come AFTER resource notification, front-end relies on this.
472 }
473
474 void InspectorInstrumentation::didReceiveResourceResponseButCanceledImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
475 {
476     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceResponse(frame, identifier, r);
477     InspectorInstrumentation::didReceiveResourceResponse(cookie, identifier, loader, r);
478 }
479
480 void InspectorInstrumentation::continueAfterXFrameOptionsDeniedImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
481 {
482     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
483 }
484
485 void InspectorInstrumentation::continueWithPolicyDownloadImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
486 {
487     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
488 }
489
490 void InspectorInstrumentation::continueWithPolicyIgnoreImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
491 {
492     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
493 }
494
495 void InspectorInstrumentation::didReceiveDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
496 {
497     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
498         resourceAgent->didReceiveData(identifier, data, dataLength, encodedDataLength);
499 }
500
501 void InspectorInstrumentation::didFinishLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, double finishTime)
502 {
503     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
504         timelineAgent->didFinishLoadingResource(identifier, false, finishTime);
505     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
506         resourceAgent->didFinishLoading(identifier, loader, finishTime);
507 }
508
509 void InspectorInstrumentation::didFailLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, const ResourceError& error)
510 {
511     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
512         timelineAgent->didFinishLoadingResource(identifier, true, 0);
513     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
514         resourceAgent->didFailLoading(identifier, loader, error);
515     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
516         consoleAgent->didFailLoading(identifier, error); // This should come AFTER resource notification, front-end relies on this.
517 }
518
519 void InspectorInstrumentation::resourceRetrievedByXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber)
520 {
521     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
522         consoleAgent->resourceRetrievedByXMLHttpRequest(url, sendURL, sendLineNumber);
523     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
524         resourceAgent->setInitialXHRContent(identifier, sourceString);
525 }
526
527 void InspectorInstrumentation::didReceiveXHRResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
528 {
529     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
530         resourceAgent->didReceiveXHRResponse(identifier);
531 }
532
533 void InspectorInstrumentation::willLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
534 {
535     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
536         resourceAgent->willLoadXHRSynchronously();
537 }
538
539 void InspectorInstrumentation::didLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
540 {
541     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
542         resourceAgent->didLoadXHRSynchronously();
543 }
544
545 void InspectorInstrumentation::scriptImportedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString)
546 {
547     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
548         resourceAgent->setInitialScriptContent(identifier, sourceString);
549 }
550
551 void InspectorInstrumentation::domContentLoadedEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, const KURL& url)
552 {
553     DocumentLoader* documentLoader = frame->loader()->documentLoader();
554     ASSERT(documentLoader);
555
556     if (frame->page()->mainFrame() != frame || url != documentLoader->requestURL())
557         return;
558
559     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
560         inspectorAgent->domContentLoadedEventFired();
561
562     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
563         domAgent->mainFrameDOMContentLoaded();
564
565     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
566         timelineAgent->didMarkDOMContentEvent();
567
568     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
569         pageAgent->domContentEventFired();
570 }
571
572 void InspectorInstrumentation::loadEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, const KURL& url)
573 {
574     DocumentLoader* documentLoader = frame->loader()->documentLoader();
575     ASSERT(documentLoader);
576
577     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
578         domAgent->loadEventFired(documentLoader->frame()->document());
579
580     if (frame->page()->mainFrame() != frame || url != documentLoader->requestURL())
581         return;
582
583     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
584         timelineAgent->didMarkLoadEvent();
585
586     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
587         pageAgent->loadEventFired();
588 }
589
590 void InspectorInstrumentation::frameDetachedFromParentImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
591 {
592     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
593         pageAgent->frameDetached(frame);
594 }
595
596 void InspectorInstrumentation::didCommitLoadImpl(InstrumentingAgents* instrumentingAgents, Page* page, DocumentLoader* loader)
597 {
598     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
599     if (!inspectorAgent || !inspectorAgent->enabled())
600         return;
601
602     Frame* mainFrame = page->mainFrame();
603     if (loader->frame() == mainFrame) {
604         if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
605             consoleAgent->reset();
606
607         if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
608             resourceAgent->mainFrameNavigated(loader);
609
610 #if ENABLE(JAVASCRIPT_DEBUGGER)
611         if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) {
612             KURL url = inspectorAgent->inspectedURLWithoutFragment();
613             debuggerAgent->inspectedURLChanged(url);
614         }
615 #endif
616 #if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
617         if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) {
618             profilerAgent->stopUserInitiatedProfiling(true);
619             profilerAgent->resetState();
620         }
621 #endif
622         if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
623             cssAgent->reset();
624 #if ENABLE(DATABASE)
625         if (InspectorDatabaseAgent* databaseAgent = instrumentingAgents->inspectorDatabaseAgent())
626             databaseAgent->clearResources();
627 #endif
628 #if ENABLE(DOM_STORAGE)
629         if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
630             domStorageAgent->clearResources();
631 #endif
632         if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
633             domAgent->setDocument(mainFrame->document());
634
635         inspectorAgent->didCommitLoad();
636     }
637     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
638         pageAgent->frameNavigated(loader);
639 }
640
641 void InspectorInstrumentation::frameDestroyedImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
642 {
643     if (InspectorPageAgent* inspectorPageAgent = instrumentingAgents->inspectorPageAgent())
644         inspectorPageAgent->frameDestroyed(frame);
645 }
646
647 InspectorInstrumentationCookie InspectorInstrumentation::willWriteHTMLImpl(InstrumentingAgents* instrumentingAgents, unsigned int length, unsigned int startLine)
648 {
649     int timelineAgentId = 0;
650     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
651         timelineAgent->willWriteHTML(length, startLine);
652         timelineAgentId = timelineAgent->id();
653     }
654     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
655 }
656
657 void InspectorInstrumentation::didWriteHTMLImpl(const InspectorInstrumentationCookie& cookie, unsigned int endLine)
658 {
659     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
660         timelineAgent->didWriteHTML(endLine);
661 }
662
663 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
664 {
665     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
666         consoleAgent->addMessageToConsole(source, type, level, message, arguments, callStack);
667 }
668
669 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceId)
670 {
671     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
672         consoleAgent->addMessageToConsole(source, type, level, message, lineNumber, sourceId);
673 }
674
675 void InspectorInstrumentation::consoleCountImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> stack)
676 {
677     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
678         consoleAgent->count(arguments, stack);
679 }
680
681 void InspectorInstrumentation::startConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title)
682 {
683     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
684         consoleAgent->startTiming(title);
685 }
686
687 void InspectorInstrumentation::stopConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title, PassRefPtr<ScriptCallStack> stack)
688 {
689     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
690         consoleAgent->stopTiming(title, stack);
691 }
692
693 void InspectorInstrumentation::consoleMarkTimelineImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments)
694 {
695     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
696         String message;
697         arguments->getFirstArgumentAsString(message);
698         timelineAgent->didMarkTimeline(message);
699      }
700 }
701
702 #if ENABLE(JAVASCRIPT_DEBUGGER)
703 void InspectorInstrumentation::addStartProfilingMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, const String& title, unsigned lineNumber, const String& sourceURL)
704 {
705     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
706         profilerAgent->addStartProfilingMessageToConsole(title, lineNumber, sourceURL);
707 }
708
709 void InspectorInstrumentation::addProfileImpl(InstrumentingAgents* instrumentingAgents, RefPtr<ScriptProfile> profile, PassRefPtr<ScriptCallStack> callStack)
710 {
711     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) {
712         const ScriptCallFrame& lastCaller = callStack->at(0);
713         profilerAgent->addProfile(profile, lastCaller.lineNumber(), lastCaller.sourceURL());
714     }
715 }
716
717 String InspectorInstrumentation::getCurrentUserInitiatedProfileNameImpl(InstrumentingAgents* instrumentingAgents, bool incrementProfileNumber)
718 {
719     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
720         return profilerAgent->getCurrentUserInitiatedProfileName(incrementProfileNumber);
721     return "";
722 }
723
724 bool InspectorInstrumentation::profilerEnabledImpl(InstrumentingAgents* instrumentingAgents)
725 {
726     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
727         return profilerAgent->enabled();
728     return false;
729 }
730 #endif
731
732 #if ENABLE(DATABASE)
733 void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
734 {
735     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
736     if (!inspectorAgent || !inspectorAgent->enabled())
737         return;
738     if (InspectorDatabaseAgent* dbAgent = instrumentingAgents->inspectorDatabaseAgent())
739         dbAgent->didOpenDatabase(database, domain, name, version);
740 }
741 #endif
742
743 #if ENABLE(DOM_STORAGE)
744 void InspectorInstrumentation::didUseDOMStorageImpl(InstrumentingAgents* instrumentingAgents, StorageArea* storageArea, bool isLocalStorage, Frame* frame)
745 {
746     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
747     if (!inspectorAgent || !inspectorAgent->enabled())
748         return;
749     if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
750         domStorageAgent->didUseDOMStorage(storageArea, isLocalStorage, frame);
751 }
752 #endif
753
754 #if ENABLE(WORKERS)
755 void InspectorInstrumentation::didStartWorkerContextImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* workerContextProxy, const KURL& url)
756 {
757     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
758         workerAgent->didStartWorkerContext(workerContextProxy, url);
759 }
760
761 void InspectorInstrumentation::didCreateWorkerImpl(InstrumentingAgents* instrumentingAgents, intptr_t id, const String& url, bool isSharedWorker)
762 {
763     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
764         inspectorAgent->didCreateWorker(id, url, isSharedWorker);
765 }
766
767 void InspectorInstrumentation::didDestroyWorkerImpl(InstrumentingAgents* instrumentingAgents, intptr_t id)
768 {
769     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
770         inspectorAgent->didDestroyWorker(id);
771 }
772
773 void InspectorInstrumentation::workerContextTerminatedImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* proxy)
774 {
775     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
776         workerAgent->workerContextTerminated(proxy);
777 }
778 #endif
779
780 #if ENABLE(WEB_SOCKETS)
781 void InspectorInstrumentation::didCreateWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const KURL& requestURL, const KURL&)
782 {
783     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
784     if (!inspectorAgent || !inspectorAgent->enabled())
785         return;
786     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
787         resourceAgent->didCreateWebSocket(identifier, requestURL);
788 }
789
790 void InspectorInstrumentation::willSendWebSocketHandshakeRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeRequest& request)
791 {
792     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
793         resourceAgent->willSendWebSocketHandshakeRequest(identifier, request);
794 }
795
796 void InspectorInstrumentation::didReceiveWebSocketHandshakeResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeResponse& response)
797 {
798     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
799         resourceAgent->didReceiveWebSocketHandshakeResponse(identifier, response);
800 }
801
802 void InspectorInstrumentation::didCloseWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
803 {
804     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
805         resourceAgent->didCloseWebSocket(identifier);
806 }
807 #endif
808
809 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
810 void InspectorInstrumentation::networkStateChangedImpl(InstrumentingAgents* instrumentingAgents)
811 {
812     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
813         applicationCacheAgent->networkStateChanged();
814 }
815
816 void InspectorInstrumentation::updateApplicationCacheStatusImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
817 {
818     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
819         applicationCacheAgent->updateApplicationCacheStatus(frame);
820 }
821 #endif
822
823 bool InspectorInstrumentation::collectingHTMLParseErrors(InstrumentingAgents* instrumentingAgents)
824 {
825     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
826         return inspectorAgent->hasFrontend();
827     return false;
828 }
829
830 void InspectorInstrumentation::pauseOnNativeEventIfNeeded(InstrumentingAgents* instrumentingAgents, const String& categoryType, const String& eventName, bool synchronous)
831 {
832 #if ENABLE(JAVASCRIPT_DEBUGGER)
833     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
834         domDebuggerAgent->pauseOnNativeEventIfNeeded(categoryType, eventName, synchronous);
835 #endif
836 }
837
838 void InspectorInstrumentation::cancelPauseOnNativeEvent(InstrumentingAgents* instrumentingAgents)
839 {
840 #if ENABLE(JAVASCRIPT_DEBUGGER)
841     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
842         debuggerAgent->cancelPauseOnNextStatement();
843 #endif
844 }
845
846 InspectorTimelineAgent* InspectorInstrumentation::retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
847 {
848     if (!cookie.first)
849         return 0;
850     InspectorTimelineAgent* timelineAgent = cookie.first->inspectorTimelineAgent();
851     if (timelineAgent && timelineAgent->id() == cookie.second)
852         return timelineAgent;
853     return 0;
854 }
855
856 } // namespace WebCore
857
858 #endif // !ENABLE(INSPECTOR)