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