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