7b97f6ce900d1585679c7c7f8b5e474e712a3a95
[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 int InspectorInstrumentation::s_frontendCounter = 0;
73
74 static bool eventHasListeners(const AtomicString& eventType, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
75 {
76     if (window && window->hasEventListeners(eventType))
77         return true;
78
79     if (node->hasEventListeners(eventType))
80         return true;
81
82     for (size_t i = 0; i < ancestors.size(); i++) {
83         Node* ancestor = ancestors[i].node();
84         if (ancestor->hasEventListeners(eventType))
85             return true;
86     }
87
88     return false;
89 }
90
91 void InspectorInstrumentation::didClearWindowObjectInWorldImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, DOMWrapperWorld* world)
92 {
93     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
94         pageAgent->didClearWindowObjectInWorld(frame, world);
95     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
96         inspectorAgent->didClearWindowObjectInWorld(frame, world);
97 }
98
99 void InspectorInstrumentation::inspectedPageDestroyedImpl(InstrumentingAgents* instrumentingAgents)
100 {
101     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
102         inspectorAgent->inspectedPageDestroyed();
103 }
104
105 void InspectorInstrumentation::willInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node, Node* parent)
106 {
107 #if ENABLE(JAVASCRIPT_DEBUGGER)
108     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
109         domDebuggerAgent->willInsertDOMNode(node, parent);
110 #endif
111 }
112
113 void InspectorInstrumentation::didInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
114 {
115     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
116         domAgent->didInsertDOMNode(node);
117 #if ENABLE(JAVASCRIPT_DEBUGGER)
118     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
119         domDebuggerAgent->didInsertDOMNode(node);
120 #endif
121 }
122
123 void InspectorInstrumentation::willRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
124 {
125 #if ENABLE(JAVASCRIPT_DEBUGGER)
126     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
127         domDebuggerAgent->willRemoveDOMNode(node);
128 #endif
129 }
130
131 void InspectorInstrumentation::didRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
132 {
133 #if ENABLE(JAVASCRIPT_DEBUGGER)
134     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
135         domDebuggerAgent->didRemoveDOMNode(node);
136 #endif
137     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
138         domAgent->didRemoveDOMNode(node);
139 }
140
141 void InspectorInstrumentation::willModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element)
142 {
143 #if ENABLE(JAVASCRIPT_DEBUGGER)
144     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
145         domDebuggerAgent->willModifyDOMAttr(element);
146 #endif
147 }
148
149 void InspectorInstrumentation::didModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element)
150 {
151     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
152         domAgent->didModifyDOMAttr(element);
153 }
154
155 void InspectorInstrumentation::didInvalidateStyleAttrImpl(InstrumentingAgents* instrumentingAgents, Node* node)
156 {
157     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
158         domAgent->didInvalidateStyleAttr(node);
159 #if ENABLE(JAVASCRIPT_DEBUGGER)
160     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
161         domDebuggerAgent->didInvalidateStyleAttr(node);
162 #endif
163 }
164
165 void InspectorInstrumentation::frameWindowDiscardedImpl(InstrumentingAgents* instrumentingAgents, DOMWindow* window)
166 {
167     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
168         consoleAgent->frameWindowDiscarded(window);
169 }
170
171 void InspectorInstrumentation::mouseDidMoveOverElementImpl(InstrumentingAgents* instrumentingAgents, const HitTestResult& result, unsigned modifierFlags)
172 {
173     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
174         domAgent->mouseDidMoveOverElement(result, modifierFlags);
175 }
176
177 bool InspectorInstrumentation::handleMousePressImpl(InstrumentingAgents* instrumentingAgents)
178 {
179     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
180         return domAgent->handleMousePress();
181     return false;
182 }
183
184 bool InspectorInstrumentation::forcePseudoStateImpl(InstrumentingAgents* instrumentingAgents, Element* element, CSSSelector::PseudoType pseudoState)
185 {
186     if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
187         return cssAgent->forcePseudoState(element, pseudoState);
188     return false;
189 }
190
191 void InspectorInstrumentation::characterDataModifiedImpl(InstrumentingAgents* instrumentingAgents, CharacterData* characterData)
192 {
193     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
194         domAgent->characterDataModified(characterData);
195 }
196
197 void InspectorInstrumentation::willSendXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url)
198 {
199 #if ENABLE(JAVASCRIPT_DEBUGGER)
200     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
201         domDebuggerAgent->willSendXMLHttpRequest(url);
202 #endif
203 }
204
205 void InspectorInstrumentation::didScheduleResourceRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url)
206 {
207     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
208         timelineAgent->didScheduleResourceRequest(url);
209 }
210
211 void InspectorInstrumentation::didInstallTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId, int timeout, bool singleShot)
212 {
213     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, setTimerEventName, true);
214     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
215         timelineAgent->didInstallTimer(timerId, timeout, singleShot);
216 }
217
218 void InspectorInstrumentation::didRemoveTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId)
219 {
220     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, clearTimerEventName, true);
221     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
222         timelineAgent->didRemoveTimer(timerId);
223 }
224
225 InspectorInstrumentationCookie InspectorInstrumentation::willCallFunctionImpl(InstrumentingAgents* instrumentingAgents, const String& scriptName, int scriptLine)
226 {
227     int timelineAgentId = 0;
228     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
229         timelineAgent->willCallFunction(scriptName, scriptLine);
230         timelineAgentId = timelineAgent->id();
231     }
232     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
233 }
234
235 void InspectorInstrumentation::didCallFunctionImpl(const InspectorInstrumentationCookie& cookie)
236 {
237     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
238         timelineAgent->didCallFunction();
239 }
240
241 InspectorInstrumentationCookie InspectorInstrumentation::willChangeXHRReadyStateImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request)
242 {
243     int timelineAgentId = 0;
244     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
245     if (timelineAgent && request->hasEventListeners(eventNames().readystatechangeEvent)) {
246         timelineAgent->willChangeXHRReadyState(request->url().string(), request->readyState());
247         timelineAgentId = timelineAgent->id();
248     }
249     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
250 }
251
252 void InspectorInstrumentation::didChangeXHRReadyStateImpl(const InspectorInstrumentationCookie& cookie)
253 {
254     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
255         timelineAgent->didChangeXHRReadyState();
256 }
257
258 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
259 {
260     pauseOnNativeEventIfNeeded(instrumentingAgents, listenerEventCategoryType, event.type(), false);
261
262     int timelineAgentId = 0;
263     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
264     if (timelineAgent && eventHasListeners(event.type(), window, node, ancestors)) {
265         timelineAgent->willDispatchEvent(event);
266         timelineAgentId = timelineAgent->id();
267     }
268     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
269 }
270
271 void InspectorInstrumentation::didDispatchEventImpl(const InspectorInstrumentationCookie& cookie)
272 {
273     cancelPauseOnNativeEvent(cookie.first);
274
275     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
276         timelineAgent->didDispatchEvent();
277 }
278
279 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventOnWindowImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window)
280 {
281     pauseOnNativeEventIfNeeded(instrumentingAgents, listenerEventCategoryType, event.type(), false);
282
283     int timelineAgentId = 0;
284     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
285     if (timelineAgent && window->hasEventListeners(event.type())) {
286         timelineAgent->willDispatchEvent(event);
287         timelineAgentId = timelineAgent->id();
288     }
289     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
290 }
291
292 void InspectorInstrumentation::didDispatchEventOnWindowImpl(const InspectorInstrumentationCookie& cookie)
293 {
294     cancelPauseOnNativeEvent(cookie.first);
295
296     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
297         timelineAgent->didDispatchEvent();
298 }
299
300 InspectorInstrumentationCookie InspectorInstrumentation::willEvaluateScriptImpl(InstrumentingAgents* instrumentingAgents, const String& url, int lineNumber)
301 {
302     int timelineAgentId = 0;
303     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
304         timelineAgent->willEvaluateScript(url, lineNumber);
305         timelineAgentId = timelineAgent->id();
306     }
307     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
308 }
309
310 void InspectorInstrumentation::didEvaluateScriptImpl(const InspectorInstrumentationCookie& cookie)
311 {
312     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
313         timelineAgent->didEvaluateScript();
314 }
315
316 InspectorInstrumentationCookie InspectorInstrumentation::willFireTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId)
317 {
318     pauseOnNativeEventIfNeeded(instrumentingAgents, instrumentationEventCategoryType, timerFiredEventName, false);
319
320     int timelineAgentId = 0;
321     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
322         timelineAgent->willFireTimer(timerId);
323         timelineAgentId = timelineAgent->id();
324     }
325     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
326 }
327
328 void InspectorInstrumentation::didFireTimerImpl(const InspectorInstrumentationCookie& cookie)
329 {
330     cancelPauseOnNativeEvent(cookie.first);
331
332     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
333         timelineAgent->didFireTimer();
334 }
335
336 InspectorInstrumentationCookie InspectorInstrumentation::willLayoutImpl(InstrumentingAgents* instrumentingAgents)
337 {
338     int timelineAgentId = 0;
339     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
340         timelineAgent->willLayout();
341         timelineAgentId = timelineAgent->id();
342     }
343     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
344 }
345
346 void InspectorInstrumentation::didLayoutImpl(const InspectorInstrumentationCookie& cookie)
347 {
348     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
349         timelineAgent->didLayout();
350 }
351
352 InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHRImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request)
353 {
354     int timelineAgentId = 0;
355     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
356     if (timelineAgent && request->hasEventListeners(eventNames().loadEvent)) {
357         timelineAgent->willLoadXHR(request->url());
358         timelineAgentId = timelineAgent->id();
359     }
360     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
361 }
362
363 void InspectorInstrumentation::didLoadXHRImpl(const InspectorInstrumentationCookie& cookie)
364 {
365     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
366         timelineAgent->didLoadXHR();
367 }
368
369 InspectorInstrumentationCookie InspectorInstrumentation::willPaintImpl(InstrumentingAgents* instrumentingAgents, const LayoutRect& rect)
370 {
371     int timelineAgentId = 0;
372     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
373         timelineAgent->willPaint(rect);
374         timelineAgentId = timelineAgent->id();
375     }
376     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
377 }
378
379 void InspectorInstrumentation::didPaintImpl(const InspectorInstrumentationCookie& cookie)
380 {
381     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
382         timelineAgent->didPaint();
383 }
384
385 InspectorInstrumentationCookie InspectorInstrumentation::willRecalculateStyleImpl(InstrumentingAgents* instrumentingAgents)
386 {
387     int timelineAgentId = 0;
388     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
389         timelineAgent->willRecalculateStyle();
390         timelineAgentId = timelineAgent->id();
391     }
392     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
393         resourceAgent->willRecalculateStyle();
394     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
395 }
396
397 void InspectorInstrumentation::didRecalculateStyleImpl(const InspectorInstrumentationCookie& cookie)
398 {
399     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
400         timelineAgent->didRecalculateStyle();
401     InstrumentingAgents* instrumentingAgents = cookie.first;
402     if (!instrumentingAgents)
403         return;
404     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
405         resourceAgent->didRecalculateStyle();
406 }
407
408 void InspectorInstrumentation::didScheduleStyleRecalculationImpl(InstrumentingAgents* instrumentingAgents, Document* document)
409 {
410     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
411         resourceAgent->didScheduleStyleRecalculation(document);
412 }
413
414 void InspectorInstrumentation::applyUserAgentOverrideImpl(InstrumentingAgents* instrumentingAgents, String* userAgent)
415 {
416     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
417         resourceAgent->applyUserAgentOverride(userAgent);
418 }
419
420 void InspectorInstrumentation::willSendRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
421 {
422     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
423         timelineAgent->willSendResourceRequest(identifier, request);
424     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
425         resourceAgent->willSendRequest(identifier, loader, request, redirectResponse);
426 }
427
428 void InspectorInstrumentation::continueAfterPingLoaderImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& response)
429 {
430     willSendRequestImpl(instrumentingAgents, identifier, loader, request, response);
431 }
432
433 void InspectorInstrumentation::markResourceAsCachedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
434 {
435     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
436         resourceAgent->markResourceAsCached(identifier);
437 }
438
439 void InspectorInstrumentation::didLoadResourceFromMemoryCacheImpl(InstrumentingAgents* instrumentingAgents, DocumentLoader* loader, CachedResource* cachedResource)
440 {
441     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
442     if (!inspectorAgent || !inspectorAgent->enabled())
443         return;
444     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
445         resourceAgent->didLoadResourceFromMemoryCache(loader, cachedResource);
446 }
447
448 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
449 {
450     int timelineAgentId = 0;
451     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
452         timelineAgent->willReceiveResourceData(identifier);
453         timelineAgentId = timelineAgent->id();
454     }
455     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
456 }
457
458 void InspectorInstrumentation::didReceiveResourceDataImpl(const InspectorInstrumentationCookie& cookie)
459 {
460     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
461         timelineAgent->didReceiveResourceData();
462 }
463
464 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const ResourceResponse& response)
465 {
466     int timelineAgentId = 0;
467     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
468     if (timelineAgent) {
469         timelineAgent->willReceiveResourceResponse(identifier, response);
470         timelineAgentId = timelineAgent->id();
471     }
472     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
473 }
474
475 void InspectorInstrumentation::didReceiveResourceResponseImpl(const InspectorInstrumentationCookie& cookie, unsigned long identifier, DocumentLoader* loader, const ResourceResponse& response)
476 {
477     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
478         timelineAgent->didReceiveResourceResponse();
479     if (!loader)
480         return;
481     InstrumentingAgents* instrumentingAgents = cookie.first;
482     if (!instrumentingAgents)
483         return;
484     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
485         resourceAgent->didReceiveResponse(identifier, loader, response);
486     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
487         consoleAgent->didReceiveResponse(identifier, response); // This should come AFTER resource notification, front-end relies on this.
488 }
489
490 void InspectorInstrumentation::didReceiveResourceResponseButCanceledImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
491 {
492     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceResponse(frame, identifier, r);
493     InspectorInstrumentation::didReceiveResourceResponse(cookie, identifier, loader, r);
494 }
495
496 void InspectorInstrumentation::continueAfterXFrameOptionsDeniedImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
497 {
498     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
499 }
500
501 void InspectorInstrumentation::continueWithPolicyDownloadImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
502 {
503     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
504 }
505
506 void InspectorInstrumentation::continueWithPolicyIgnoreImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
507 {
508     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
509 }
510
511 void InspectorInstrumentation::didReceiveDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
512 {
513     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
514         resourceAgent->didReceiveData(identifier, data, dataLength, encodedDataLength);
515 }
516
517 void InspectorInstrumentation::didFinishLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, double finishTime)
518 {
519     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
520         timelineAgent->didFinishLoadingResource(identifier, false, finishTime);
521     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
522         resourceAgent->didFinishLoading(identifier, loader, finishTime);
523 }
524
525 void InspectorInstrumentation::didFailLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, const ResourceError& error)
526 {
527     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
528         timelineAgent->didFinishLoadingResource(identifier, true, 0);
529     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
530         resourceAgent->didFailLoading(identifier, loader, error);
531     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
532         consoleAgent->didFailLoading(identifier, error); // This should come AFTER resource notification, front-end relies on this.
533 }
534
535 void InspectorInstrumentation::resourceRetrievedByXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber)
536 {
537     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
538         consoleAgent->resourceRetrievedByXMLHttpRequest(url, sendURL, sendLineNumber);
539     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
540         resourceAgent->setInitialXHRContent(identifier, sourceString);
541 }
542
543 void InspectorInstrumentation::didReceiveXHRResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
544 {
545     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
546         resourceAgent->didReceiveXHRResponse(identifier);
547 }
548
549 void InspectorInstrumentation::willLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
550 {
551     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
552         resourceAgent->willLoadXHRSynchronously();
553 }
554
555 void InspectorInstrumentation::didLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
556 {
557     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
558         resourceAgent->didLoadXHRSynchronously();
559 }
560
561 void InspectorInstrumentation::scriptImportedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString)
562 {
563     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
564         resourceAgent->setInitialScriptContent(identifier, sourceString);
565 }
566
567 void InspectorInstrumentation::didReceiveScriptResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
568 {
569     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
570         resourceAgent->didReceiveScriptResponse(identifier);
571 }
572
573 void InspectorInstrumentation::domContentLoadedEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
574 {
575     if (frame->page()->mainFrame() != frame)
576         return;
577
578     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
579         inspectorAgent->domContentLoadedEventFired();
580
581     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
582         domAgent->mainFrameDOMContentLoaded();
583
584     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
585         timelineAgent->didMarkDOMContentEvent();
586
587     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
588         pageAgent->domContentEventFired();
589 }
590
591 void InspectorInstrumentation::loadEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
592 {
593     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
594         domAgent->loadEventFired(frame->document());
595
596     if (frame->page()->mainFrame() != frame)
597         return;
598
599     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
600         timelineAgent->didMarkLoadEvent();
601
602     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
603         pageAgent->loadEventFired();
604 }
605
606 void InspectorInstrumentation::frameDetachedFromParentImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
607 {
608     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
609         pageAgent->frameDetached(frame);
610 }
611
612 void InspectorInstrumentation::didCommitLoadImpl(InstrumentingAgents* instrumentingAgents, Page* page, DocumentLoader* loader)
613 {
614     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
615     if (!inspectorAgent || !inspectorAgent->enabled())
616         return;
617
618     Frame* mainFrame = page->mainFrame();
619     if (loader->frame() == mainFrame) {
620         if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
621             consoleAgent->reset();
622
623         if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
624             resourceAgent->mainFrameNavigated(loader);
625
626 #if ENABLE(JAVASCRIPT_DEBUGGER)
627         if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) {
628             KURL url = inspectorAgent->inspectedURLWithoutFragment();
629             debuggerAgent->inspectedURLChanged(url);
630         }
631 #endif
632 #if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
633         if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) {
634             profilerAgent->stopUserInitiatedProfiling(true);
635             profilerAgent->resetState();
636         }
637 #endif
638         if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
639             cssAgent->reset();
640 #if ENABLE(SQL_DATABASE)
641         if (InspectorDatabaseAgent* databaseAgent = instrumentingAgents->inspectorDatabaseAgent())
642             databaseAgent->clearResources();
643 #endif
644 #if ENABLE(DOM_STORAGE)
645         if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
646             domStorageAgent->clearResources();
647 #endif
648         if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
649             domAgent->setDocument(mainFrame->document());
650
651         inspectorAgent->didCommitLoad();
652     }
653     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
654         pageAgent->frameNavigated(loader);
655 }
656
657 void InspectorInstrumentation::loaderDetachedFromFrameImpl(InstrumentingAgents* instrumentingAgents, DocumentLoader* loader)
658 {
659     if (InspectorPageAgent* inspectorPageAgent = instrumentingAgents->inspectorPageAgent())
660         inspectorPageAgent->loaderDetachedFromFrame(loader);
661 }
662
663 InspectorInstrumentationCookie InspectorInstrumentation::willWriteHTMLImpl(InstrumentingAgents* instrumentingAgents, unsigned int length, unsigned int startLine)
664 {
665     int timelineAgentId = 0;
666     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
667         timelineAgent->willWriteHTML(length, startLine);
668         timelineAgentId = timelineAgent->id();
669     }
670     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
671 }
672
673 void InspectorInstrumentation::didWriteHTMLImpl(const InspectorInstrumentationCookie& cookie, unsigned int endLine)
674 {
675     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
676         timelineAgent->didWriteHTML(endLine);
677 }
678
679 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
680 {
681     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
682         consoleAgent->addMessageToConsole(source, type, level, message, arguments, callStack);
683 }
684
685 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& scriptId)
686 {
687     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
688         consoleAgent->addMessageToConsole(source, type, level, message, lineNumber, scriptId);
689 }
690
691 void InspectorInstrumentation::consoleCountImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> stack)
692 {
693     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
694         consoleAgent->count(arguments, stack);
695 }
696
697 void InspectorInstrumentation::startConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title)
698 {
699     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
700         consoleAgent->startTiming(title);
701 }
702
703 void InspectorInstrumentation::stopConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title, PassRefPtr<ScriptCallStack> stack)
704 {
705     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
706         consoleAgent->stopTiming(title, stack);
707 }
708
709 void InspectorInstrumentation::consoleTimeStampImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments)
710 {
711     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
712         String message;
713         arguments->getFirstArgumentAsString(message);
714         timelineAgent->didTimeStamp(message);
715      }
716 }
717
718 #if ENABLE(JAVASCRIPT_DEBUGGER)
719 void InspectorInstrumentation::addStartProfilingMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, const String& title, unsigned lineNumber, const String& sourceURL)
720 {
721     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
722         profilerAgent->addStartProfilingMessageToConsole(title, lineNumber, sourceURL);
723 }
724
725 void InspectorInstrumentation::addProfileImpl(InstrumentingAgents* instrumentingAgents, RefPtr<ScriptProfile> profile, PassRefPtr<ScriptCallStack> callStack)
726 {
727     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) {
728         const ScriptCallFrame& lastCaller = callStack->at(0);
729         profilerAgent->addProfile(profile, lastCaller.lineNumber(), lastCaller.sourceURL());
730     }
731 }
732
733 String InspectorInstrumentation::getCurrentUserInitiatedProfileNameImpl(InstrumentingAgents* instrumentingAgents, bool incrementProfileNumber)
734 {
735     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
736         return profilerAgent->getCurrentUserInitiatedProfileName(incrementProfileNumber);
737     return "";
738 }
739
740 bool InspectorInstrumentation::profilerEnabledImpl(InstrumentingAgents* instrumentingAgents)
741 {
742     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
743         return profilerAgent->enabled();
744     return false;
745 }
746 #endif
747
748 #if ENABLE(SQL_DATABASE)
749 void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
750 {
751     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
752     if (!inspectorAgent || !inspectorAgent->enabled())
753         return;
754     if (InspectorDatabaseAgent* dbAgent = instrumentingAgents->inspectorDatabaseAgent())
755         dbAgent->didOpenDatabase(database, domain, name, version);
756 }
757 #endif
758
759 #if ENABLE(DOM_STORAGE)
760 void InspectorInstrumentation::didUseDOMStorageImpl(InstrumentingAgents* instrumentingAgents, StorageArea* storageArea, bool isLocalStorage, Frame* frame)
761 {
762     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
763     if (!inspectorAgent || !inspectorAgent->enabled())
764         return;
765     if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
766         domStorageAgent->didUseDOMStorage(storageArea, isLocalStorage, frame);
767 }
768 #endif
769
770 #if ENABLE(WORKERS)
771 void InspectorInstrumentation::didStartWorkerContextImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* workerContextProxy, const KURL& url)
772 {
773     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
774         workerAgent->didStartWorkerContext(workerContextProxy, url);
775 }
776
777 void InspectorInstrumentation::didCreateWorkerImpl(InstrumentingAgents* instrumentingAgents, intptr_t id, const String& url, bool isSharedWorker)
778 {
779     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
780         inspectorAgent->didCreateWorker(id, url, isSharedWorker);
781 }
782
783 void InspectorInstrumentation::didDestroyWorkerImpl(InstrumentingAgents* instrumentingAgents, intptr_t id)
784 {
785     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
786         inspectorAgent->didDestroyWorker(id);
787 }
788
789 void InspectorInstrumentation::workerContextTerminatedImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* proxy)
790 {
791     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
792         workerAgent->workerContextTerminated(proxy);
793 }
794 #endif
795
796 #if ENABLE(WEB_SOCKETS)
797 void InspectorInstrumentation::didCreateWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const KURL& requestURL, const KURL&)
798 {
799     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
800     if (!inspectorAgent || !inspectorAgent->enabled())
801         return;
802     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
803         resourceAgent->didCreateWebSocket(identifier, requestURL);
804 }
805
806 void InspectorInstrumentation::willSendWebSocketHandshakeRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeRequest& request)
807 {
808     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
809         resourceAgent->willSendWebSocketHandshakeRequest(identifier, request);
810 }
811
812 void InspectorInstrumentation::didReceiveWebSocketHandshakeResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeResponse& response)
813 {
814     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
815         resourceAgent->didReceiveWebSocketHandshakeResponse(identifier, response);
816 }
817
818 void InspectorInstrumentation::didCloseWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
819 {
820     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
821         resourceAgent->didCloseWebSocket(identifier);
822 }
823 #endif
824
825 void InspectorInstrumentation::networkStateChangedImpl(InstrumentingAgents* instrumentingAgents)
826 {
827     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
828         applicationCacheAgent->networkStateChanged();
829 }
830
831 void InspectorInstrumentation::updateApplicationCacheStatusImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
832 {
833     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
834         applicationCacheAgent->updateApplicationCacheStatus(frame);
835 }
836
837 bool InspectorInstrumentation::collectingHTMLParseErrors(InstrumentingAgents* instrumentingAgents)
838 {
839     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
840         return inspectorAgent->hasFrontend();
841     return false;
842 }
843
844 void InspectorInstrumentation::pauseOnNativeEventIfNeeded(InstrumentingAgents* instrumentingAgents, const String& categoryType, const String& eventName, bool synchronous)
845 {
846 #if ENABLE(JAVASCRIPT_DEBUGGER)
847     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
848         domDebuggerAgent->pauseOnNativeEventIfNeeded(categoryType, eventName, synchronous);
849 #endif
850 }
851
852 void InspectorInstrumentation::cancelPauseOnNativeEvent(InstrumentingAgents* instrumentingAgents)
853 {
854 #if ENABLE(JAVASCRIPT_DEBUGGER)
855     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
856         debuggerAgent->cancelPauseOnNextStatement();
857 #endif
858 }
859
860 void InspectorInstrumentation::didRegisterAnimationFrameCallbackImpl(InstrumentingAgents* instrumentingAgents, int callbackId)
861 {
862     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
863         timelineAgent->didRegisterAnimationFrameCallback(callbackId);
864 }
865
866 void InspectorInstrumentation::didCancelAnimationFrameCallbackImpl(InstrumentingAgents* instrumentingAgents, int callbackId)
867 {
868     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
869         timelineAgent->didCancelAnimationFrameCallback(callbackId);
870 }
871
872 InspectorInstrumentationCookie InspectorInstrumentation::willFireAnimationFrameEventImpl(InstrumentingAgents* instrumentingAgents, int callbackId)
873 {
874     int timelineAgentId = 0;
875     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
876         timelineAgent->willFireAnimationFrameEvent(callbackId);
877         timelineAgentId = timelineAgent->id();
878     }
879     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
880 }
881
882 void InspectorInstrumentation::didFireAnimationFrameEventImpl(const InspectorInstrumentationCookie& cookie)
883 {
884     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
885         timelineAgent->didFireAnimationFrameEvent();
886 }
887
888 InspectorTimelineAgent* InspectorInstrumentation::retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
889 {
890     if (!cookie.first)
891         return 0;
892     InspectorTimelineAgent* timelineAgent = cookie.first->inspectorTimelineAgent();
893     if (timelineAgent && timelineAgent->id() == cookie.second)
894         return timelineAgent;
895     return 0;
896 }
897
898 InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForPage(Page* page)
899 {
900     if (!page)
901         return 0;
902     return instrumentationForPage(page);
903 }
904
905 } // namespace WebCore
906
907 #endif // !ENABLE(INSPECTOR)