2011-02-09 Pavel Feldman <pfeldman@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / InspectorInstrumentation.cpp
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "InspectorInstrumentation.h"
33
34 #if ENABLE(INSPECTOR)
35
36 #include "DOMWindow.h"
37 #include "Database.h"
38 #include "DocumentLoader.h"
39 #include "Event.h"
40 #include "EventContext.h"
41 #include "InspectorAgent.h"
42 #include "InspectorApplicationCacheAgent.h"
43 #include "InspectorBrowserDebuggerAgent.h"
44 #include "InspectorConsoleAgent.h"
45 #include "InspectorDOMAgent.h"
46 #include "InspectorDebuggerAgent.h"
47 #include "InspectorProfilerAgent.h"
48 #include "InspectorResourceAgent.h"
49 #include "InspectorTimelineAgent.h"
50 #include "ScriptArguments.h"
51 #include "ScriptCallStack.h"
52 #include "XMLHttpRequest.h"
53 #include <wtf/text/CString.h>
54
55 namespace WebCore {
56
57 static const char* const listenerEventCategoryType = "listener";
58 static const char* const instrumentationEventCategoryType = "instrumentation";
59
60 static const char* const setTimerEventName = "setTimer";
61 static const char* const clearTimerEventName = "clearTimer";
62 static const char* const timerFiredEventName = "timerFired";
63
64 HashMap<Page*, InspectorAgent*>& InspectorInstrumentation::inspectorAgents()
65 {
66     static HashMap<Page*, InspectorAgent*>& agents = *new HashMap<Page*, InspectorAgent*>;
67     return agents;
68 }
69
70 int InspectorInstrumentation::s_frontendCounter = 0;
71
72 static bool eventHasListeners(const AtomicString& eventType, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
73 {
74     if (window && window->hasEventListeners(eventType))
75         return true;
76
77     if (node->hasEventListeners(eventType))
78         return true;
79
80     for (size_t i = 0; i < ancestors.size(); i++) {
81         Node* ancestor = ancestors[i].node();
82         if (ancestor->hasEventListeners(eventType))
83             return true;
84     }
85
86     return false;
87 }
88
89 void InspectorInstrumentation::didClearWindowObjectInWorldImpl(InspectorAgent* inspectorAgent, Frame* frame, DOMWrapperWorld* world)
90 {
91     inspectorAgent->didClearWindowObjectInWorld(frame, world);
92 }
93
94 void InspectorInstrumentation::inspectedPageDestroyedImpl(InspectorAgent* inspectorAgent)
95 {
96     inspectorAgent->inspectedPageDestroyed();
97 }
98
99 void InspectorInstrumentation::willInsertDOMNodeImpl(InspectorAgent* inspectorAgent, Node* node, Node* parent)
100 {
101 #if ENABLE(JAVASCRIPT_DEBUGGER)
102     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
103         browserDebuggerAgent->willInsertDOMNode(node, parent);
104 #endif
105 }
106
107 void InspectorInstrumentation::didInsertDOMNodeImpl(InspectorAgent* inspectorAgent, Node* node)
108 {
109     if (InspectorDOMAgent* domAgent = inspectorAgent->domAgent())
110         domAgent->didInsertDOMNode(node);
111 #if ENABLE(JAVASCRIPT_DEBUGGER)
112     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
113         browserDebuggerAgent->didInsertDOMNode(node);
114 #endif
115 }
116
117 void InspectorInstrumentation::willRemoveDOMNodeImpl(InspectorAgent* inspectorAgent, Node* node)
118 {
119 #if ENABLE(JAVASCRIPT_DEBUGGER)
120     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
121         browserDebuggerAgent->willRemoveDOMNode(node);
122 #endif
123 }
124
125 void InspectorInstrumentation::didRemoveDOMNodeImpl(InspectorAgent* inspectorAgent, Node* node)
126 {
127 #if ENABLE(JAVASCRIPT_DEBUGGER)
128     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
129         browserDebuggerAgent->didRemoveDOMNode(node);
130 #endif
131     if (InspectorDOMAgent* domAgent = inspectorAgent->domAgent())
132         domAgent->didRemoveDOMNode(node);
133 }
134
135 void InspectorInstrumentation::willModifyDOMAttrImpl(InspectorAgent* inspectorAgent, Element* element)
136 {
137 #if ENABLE(JAVASCRIPT_DEBUGGER)
138     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
139         browserDebuggerAgent->willModifyDOMAttr(element);
140 #endif
141 }
142
143 void InspectorInstrumentation::didModifyDOMAttrImpl(InspectorAgent* inspectorAgent, Element* element)
144 {
145     if (InspectorDOMAgent* domAgent = inspectorAgent->domAgent())
146         domAgent->didModifyDOMAttr(element);
147 }
148
149 void InspectorInstrumentation::mouseDidMoveOverElementImpl(InspectorAgent* inspectorAgent, const HitTestResult& result, unsigned modifierFlags)
150 {
151     inspectorAgent->mouseDidMoveOverElement(result, modifierFlags);
152 }
153
154 bool InspectorInstrumentation::handleMousePressImpl(InspectorAgent* inspectorAgent)
155 {
156     return inspectorAgent->handleMousePress();
157 }
158
159 void InspectorInstrumentation::characterDataModifiedImpl(InspectorAgent* inspectorAgent, CharacterData* characterData)
160 {
161     if (InspectorDOMAgent* domAgent = inspectorAgent->domAgent())
162         domAgent->characterDataModified(characterData);
163 }
164
165 void InspectorInstrumentation::willSendXMLHttpRequestImpl(InspectorAgent* inspectorAgent, const String& url)
166 {
167 #if ENABLE(JAVASCRIPT_DEBUGGER)
168     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
169         browserDebuggerAgent->willSendXMLHttpRequest(url);
170 #endif
171 }
172
173 void InspectorInstrumentation::didScheduleResourceRequestImpl(InspectorAgent* inspectorAgent, const String& url)
174 {
175     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
176         timelineAgent->didScheduleResourceRequest(url);
177 }
178
179 void InspectorInstrumentation::didInstallTimerImpl(InspectorAgent* inspectorAgent, int timerId, int timeout, bool singleShot)
180 {
181     pauseOnNativeEventIfNeeded(inspectorAgent, instrumentationEventCategoryType, setTimerEventName, true);
182     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
183         timelineAgent->didInstallTimer(timerId, timeout, singleShot);
184 }
185
186 void InspectorInstrumentation::didRemoveTimerImpl(InspectorAgent* inspectorAgent, int timerId)
187 {
188     pauseOnNativeEventIfNeeded(inspectorAgent, instrumentationEventCategoryType, clearTimerEventName, true);
189     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
190         timelineAgent->didRemoveTimer(timerId);
191 }
192
193 InspectorInstrumentationCookie InspectorInstrumentation::willCallFunctionImpl(InspectorAgent* inspectorAgent, const String& scriptName, int scriptLine)
194 {
195     int timelineAgentId = 0;
196     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
197     if (timelineAgent) {
198         timelineAgent->willCallFunction(scriptName, scriptLine);
199         timelineAgentId = timelineAgent->id();
200     }
201     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
202 }
203
204 void InspectorInstrumentation::didCallFunctionImpl(const InspectorInstrumentationCookie& cookie)
205 {
206     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
207         timelineAgent->didCallFunction();
208 }
209
210 InspectorInstrumentationCookie InspectorInstrumentation::willChangeXHRReadyStateImpl(InspectorAgent* inspectorAgent, XMLHttpRequest* request)
211 {
212     int timelineAgentId = 0;
213     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
214     if (timelineAgent && request->hasEventListeners(eventNames().readystatechangeEvent)) {
215         timelineAgent->willChangeXHRReadyState(request->url().string(), request->readyState());
216         timelineAgentId = timelineAgent->id();
217     }
218     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
219 }
220
221 void InspectorInstrumentation::didChangeXHRReadyStateImpl(const InspectorInstrumentationCookie& cookie)
222 {
223     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
224         timelineAgent->didChangeXHRReadyState();
225 }
226
227 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventImpl(InspectorAgent* inspectorAgent, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
228 {
229     pauseOnNativeEventIfNeeded(inspectorAgent, listenerEventCategoryType, event.type(), false);
230
231     int timelineAgentId = 0;
232     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
233     if (timelineAgent && eventHasListeners(event.type(), window, node, ancestors)) {
234         timelineAgent->willDispatchEvent(event);
235         timelineAgentId = timelineAgent->id();
236     }
237     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
238 }
239
240 void InspectorInstrumentation::didDispatchEventImpl(const InspectorInstrumentationCookie& cookie)
241 {
242     cancelPauseOnNativeEvent(cookie.first);
243
244     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
245         timelineAgent->didDispatchEvent();
246 }
247
248 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventOnWindowImpl(InspectorAgent* inspectorAgent, const Event& event, DOMWindow* window)
249 {
250     pauseOnNativeEventIfNeeded(inspectorAgent, listenerEventCategoryType, event.type(), false);
251
252     int timelineAgentId = 0;
253     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
254     if (timelineAgent && window->hasEventListeners(event.type())) {
255         timelineAgent->willDispatchEvent(event);
256         timelineAgentId = timelineAgent->id();
257     }
258     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
259 }
260
261 void InspectorInstrumentation::didDispatchEventOnWindowImpl(const InspectorInstrumentationCookie& cookie)
262 {
263     cancelPauseOnNativeEvent(cookie.first);
264
265     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
266         timelineAgent->didDispatchEvent();
267 }
268
269 InspectorInstrumentationCookie InspectorInstrumentation::willEvaluateScriptImpl(InspectorAgent* inspectorAgent, const String& url, int lineNumber)
270 {
271     int timelineAgentId = 0;
272     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
273     if (timelineAgent) {
274         timelineAgent->willEvaluateScript(url, lineNumber);
275         timelineAgentId = timelineAgent->id();
276     }
277     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
278 }
279
280 void InspectorInstrumentation::didEvaluateScriptImpl(const InspectorInstrumentationCookie& cookie)
281 {
282     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
283         timelineAgent->didEvaluateScript();
284 }
285
286 InspectorInstrumentationCookie InspectorInstrumentation::willFireTimerImpl(InspectorAgent* inspectorAgent, int timerId)
287 {
288     pauseOnNativeEventIfNeeded(inspectorAgent, instrumentationEventCategoryType, timerFiredEventName, false);
289
290     int timelineAgentId = 0;
291     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
292     if (timelineAgent) {
293         timelineAgent->willFireTimer(timerId);
294         timelineAgentId = timelineAgent->id();
295     }
296     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
297 }
298
299 void InspectorInstrumentation::didFireTimerImpl(const InspectorInstrumentationCookie& cookie)
300 {
301     cancelPauseOnNativeEvent(cookie.first);
302
303     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
304         timelineAgent->didFireTimer();
305 }
306
307 InspectorInstrumentationCookie InspectorInstrumentation::willLayoutImpl(InspectorAgent* inspectorAgent)
308 {
309     int timelineAgentId = 0;
310     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
311     if (timelineAgent) {
312         timelineAgent->willLayout();
313         timelineAgentId = timelineAgent->id();
314     }
315     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
316 }
317
318 void InspectorInstrumentation::didLayoutImpl(const InspectorInstrumentationCookie& cookie)
319 {
320     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
321         timelineAgent->didLayout();
322 }
323
324 InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHRImpl(InspectorAgent* inspectorAgent, XMLHttpRequest* request)
325 {
326     int timelineAgentId = 0;
327     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
328     if (timelineAgent && request->hasEventListeners(eventNames().loadEvent)) {
329         timelineAgent->willLoadXHR(request->url());
330         timelineAgentId = timelineAgent->id();
331     }
332     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
333 }
334
335 void InspectorInstrumentation::didLoadXHRImpl(const InspectorInstrumentationCookie& cookie)
336 {
337     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
338         timelineAgent->didLoadXHR();
339 }
340
341 InspectorInstrumentationCookie InspectorInstrumentation::willPaintImpl(InspectorAgent* inspectorAgent, const IntRect& rect)
342 {
343     int timelineAgentId = 0;
344     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
345     if (timelineAgent) {
346         timelineAgent->willPaint(rect);
347         timelineAgentId = timelineAgent->id();
348     }
349     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
350 }
351
352 void InspectorInstrumentation::didPaintImpl(const InspectorInstrumentationCookie& cookie)
353 {
354     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
355         timelineAgent->didPaint();
356 }
357
358 InspectorInstrumentationCookie InspectorInstrumentation::willRecalculateStyleImpl(InspectorAgent* inspectorAgent)
359 {
360     int timelineAgentId = 0;
361     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
362     if (timelineAgent) {
363         timelineAgent->willRecalculateStyle();
364         timelineAgentId = timelineAgent->id();
365     }
366     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
367 }
368
369 void InspectorInstrumentation::didRecalculateStyleImpl(const InspectorInstrumentationCookie& cookie)
370 {
371     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
372         timelineAgent->didRecalculateStyle();
373 }
374
375 void InspectorInstrumentation::identifierForInitialRequestImpl(InspectorAgent* inspectorAgent, unsigned long identifier, DocumentLoader* loader, const ResourceRequest& request)
376 {
377     if (!inspectorAgent->enabled())
378         return;
379
380     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
381         resourceAgent->identifierForInitialRequest(identifier, request.url(), loader);
382 }
383
384 void InspectorInstrumentation::applyUserAgentOverrideImpl(InspectorAgent* inspectorAgent, String* userAgent)
385 {
386     inspectorAgent->applyUserAgentOverride(userAgent);
387 }
388
389 void InspectorInstrumentation::willSendRequestImpl(InspectorAgent* inspectorAgent, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse)
390 {
391     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
392         timelineAgent->willSendResourceRequest(identifier, request);
393     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
394         resourceAgent->willSendRequest(identifier, request, redirectResponse);
395 }
396
397 void InspectorInstrumentation::markResourceAsCachedImpl(InspectorAgent* inspectorAgent, unsigned long identifier)
398 {
399     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
400         resourceAgent->markResourceAsCached(identifier);
401 }
402
403 void InspectorInstrumentation::didLoadResourceFromMemoryCacheImpl(InspectorAgent* inspectorAgent, DocumentLoader* loader, const CachedResource* cachedResource)
404 {
405     if (!inspectorAgent->enabled())
406         return;
407     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
408         resourceAgent->didLoadResourceFromMemoryCache(loader, cachedResource);
409 }
410
411 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceDataImpl(InspectorAgent* inspectorAgent, unsigned long identifier)
412 {
413     int timelineAgentId = 0;
414     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
415     if (timelineAgent) {
416         timelineAgent->willReceiveResourceData(identifier);
417         timelineAgentId = timelineAgent->id();
418     }
419     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
420 }
421
422 void InspectorInstrumentation::didReceiveResourceDataImpl(const InspectorInstrumentationCookie& cookie)
423 {
424     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
425         timelineAgent->didReceiveResourceData();
426 }
427
428 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceResponseImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const ResourceResponse& response)
429 {
430     int timelineAgentId = 0;
431     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
432     if (timelineAgent) {
433         timelineAgent->willReceiveResourceResponse(identifier, response);
434         timelineAgentId = timelineAgent->id();
435     }
436     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
437 }
438
439 void InspectorInstrumentation::didReceiveResourceResponseImpl(const InspectorInstrumentationCookie& cookie, unsigned long identifier, DocumentLoader* loader, const ResourceResponse& response)
440 {
441     InspectorAgent* inspectorAgent = cookie.first;
442     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
443         resourceAgent->didReceiveResponse(identifier, loader, response);
444     inspectorAgent->consoleAgent()->didReceiveResponse(identifier, response);
445     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
446         timelineAgent->didReceiveResourceResponse();
447 }
448
449 void InspectorInstrumentation::didReceiveContentLengthImpl(InspectorAgent* inspectorAgent, unsigned long identifier, int lengthReceived)
450 {
451     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
452         resourceAgent->didReceiveContentLength(identifier, lengthReceived);
453 }
454
455 void InspectorInstrumentation::didFinishLoadingImpl(InspectorAgent* inspectorAgent, unsigned long identifier, double finishTime)
456 {
457     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
458         timelineAgent->didFinishLoadingResource(identifier, false, finishTime);
459     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
460         resourceAgent->didFinishLoading(identifier, finishTime);
461 }
462
463 void InspectorInstrumentation::didFailLoadingImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const ResourceError& error)
464 {
465     inspectorAgent->consoleAgent()->didFailLoading(identifier, error);
466     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent))
467         timelineAgent->didFinishLoadingResource(identifier, true, 0);
468     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
469         resourceAgent->didFailLoading(identifier, error);
470 }
471
472 void InspectorInstrumentation::resourceRetrievedByXMLHttpRequestImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber)
473 {
474     inspectorAgent->consoleAgent()->resourceRetrievedByXMLHttpRequest(url, sendURL, sendLineNumber);
475     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
476         resourceAgent->setInitialContent(identifier, sourceString, "XHR");
477 }
478
479 void InspectorInstrumentation::scriptImportedImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const String& sourceString)
480 {
481     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
482         resourceAgent->setInitialContent(identifier, sourceString, "Script");
483 }
484
485 void InspectorInstrumentation::mainResourceFiredLoadEventImpl(InspectorAgent* inspectorAgent, Frame* frame, const KURL& url)
486 {
487     inspectorAgent->mainResourceFiredLoadEvent(frame->loader()->documentLoader(), url);
488 }
489
490 void InspectorInstrumentation::mainResourceFiredDOMContentEventImpl(InspectorAgent* inspectorAgent, Frame* frame, const KURL& url)
491 {
492     inspectorAgent->mainResourceFiredDOMContentEvent(frame->loader()->documentLoader(), url);
493 }
494
495 void InspectorInstrumentation::frameDetachedFromParentImpl(InspectorAgent* inspectorAgent, Frame* frame)
496 {
497     if (InspectorResourceAgent* resourceAgent = retrieveResourceAgent(inspectorAgent))
498         resourceAgent->frameDetachedFromParent(frame);
499 }
500
501 void InspectorInstrumentation::didCommitLoadImpl(InspectorAgent* inspectorAgent, DocumentLoader* loader)
502 {
503     inspectorAgent->didCommitLoad(loader);
504 }
505
506 InspectorInstrumentationCookie InspectorInstrumentation::willWriteHTMLImpl(InspectorAgent* inspectorAgent, unsigned int length, unsigned int startLine)
507 {
508     int timelineAgentId = 0;
509     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent);
510     if (timelineAgent) {
511         timelineAgent->willWriteHTML(length, startLine);
512         timelineAgentId = timelineAgent->id();
513     }
514     return InspectorInstrumentationCookie(inspectorAgent, timelineAgentId);
515 }
516
517 void InspectorInstrumentation::didWriteHTMLImpl(const InspectorInstrumentationCookie& cookie, unsigned int endLine)
518 {
519     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
520         timelineAgent->didWriteHTML(endLine);
521 }
522
523 void InspectorInstrumentation::addMessageToConsoleImpl(InspectorAgent* inspectorAgent, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
524 {
525     inspectorAgent->consoleAgent()->addMessageToConsole(source, type, level, message, arguments, callStack);
526 }
527
528 void InspectorInstrumentation::addMessageToConsoleImpl(InspectorAgent* inspectorAgent, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceID)
529 {
530     inspectorAgent->consoleAgent()->addMessageToConsole(source, type, level, message, lineNumber, sourceID);
531 }
532
533 void InspectorInstrumentation::consoleCountImpl(InspectorAgent* inspectorAgent, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> stack)
534 {
535     inspectorAgent->consoleAgent()->count(arguments, stack);
536 }
537
538 void InspectorInstrumentation::startConsoleTimingImpl(InspectorAgent* inspectorAgent, const String& title)
539 {
540     inspectorAgent->consoleAgent()->startTiming(title);
541 }
542
543 void InspectorInstrumentation::stopConsoleTimingImpl(InspectorAgent* inspectorAgent, const String& title, PassRefPtr<ScriptCallStack> stack)
544 {
545     inspectorAgent->consoleAgent()->stopTiming(title, stack);
546 }
547
548 void InspectorInstrumentation::consoleMarkTimelineImpl(InspectorAgent* inspectorAgent, PassRefPtr<ScriptArguments> arguments)
549 {
550     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(inspectorAgent)) {
551         String message;
552         arguments->getFirstArgumentAsString(message);
553         timelineAgent->didMarkTimeline(message);
554      }
555 }
556
557 #if ENABLE(JAVASCRIPT_DEBUGGER)
558 void InspectorInstrumentation::addStartProfilingMessageToConsoleImpl(InspectorAgent* inspectorAgent, const String& title, unsigned lineNumber, const String& sourceURL)
559 {
560     if (InspectorProfilerAgent* profilerAgent = inspectorAgent->profilerAgent())
561         profilerAgent->addStartProfilingMessageToConsole(title, lineNumber, sourceURL);
562 }
563
564 void InspectorInstrumentation::addProfileImpl(InspectorAgent* inspectorAgent, RefPtr<ScriptProfile> profile, PassRefPtr<ScriptCallStack> callStack)
565 {
566     if (InspectorProfilerAgent* profilerAgent = inspectorAgent->profilerAgent()) {
567         const ScriptCallFrame& lastCaller = callStack->at(0);
568         profilerAgent->addProfile(profile, lastCaller.lineNumber(), lastCaller.sourceURL());
569     }
570 }
571
572 String InspectorInstrumentation::getCurrentUserInitiatedProfileNameImpl(InspectorAgent* inspectorAgent, bool incrementProfileNumber)
573 {
574     if (InspectorProfilerAgent* profilerAgent = inspectorAgent->profilerAgent())
575         return profilerAgent->getCurrentUserInitiatedProfileName(incrementProfileNumber);
576     return "";
577 }
578
579 bool InspectorInstrumentation::profilerEnabledImpl(InspectorAgent* inspectorAgent)
580 {
581     return inspectorAgent->profilerEnabled();
582 }
583 #endif
584
585 #if ENABLE(DATABASE)
586 void InspectorInstrumentation::didOpenDatabaseImpl(InspectorAgent* inspectorAgent, PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
587 {
588     inspectorAgent->didOpenDatabase(database, domain, name, version);
589 }
590 #endif
591
592 #if ENABLE(DOM_STORAGE)
593 void InspectorInstrumentation::didUseDOMStorageImpl(InspectorAgent* inspectorAgent, StorageArea* storageArea, bool isLocalStorage, Frame* frame)
594 {
595     inspectorAgent->didUseDOMStorage(storageArea, isLocalStorage, frame);
596 }
597 #endif
598
599 #if ENABLE(WORKERS)
600 void InspectorInstrumentation::didCreateWorkerImpl(InspectorAgent* inspectorAgent, intptr_t id, const String& url, bool isSharedWorker)
601 {
602     inspectorAgent->didCreateWorker(id, url, isSharedWorker);
603 }
604
605 void InspectorInstrumentation::didDestroyWorkerImpl(InspectorAgent* inspectorAgent, intptr_t id)
606 {
607     inspectorAgent->didDestroyWorker(id);
608 }
609 #endif
610
611 #if ENABLE(WEB_SOCKETS)
612 void InspectorInstrumentation::didCreateWebSocketImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const KURL& requestURL, const KURL& documentURL)
613 {
614     inspectorAgent->didCreateWebSocket(identifier, requestURL, documentURL);
615 }
616
617 void InspectorInstrumentation::willSendWebSocketHandshakeRequestImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const WebSocketHandshakeRequest& request)
618 {
619     inspectorAgent->willSendWebSocketHandshakeRequest(identifier, request);
620 }
621
622 void InspectorInstrumentation::didReceiveWebSocketHandshakeResponseImpl(InspectorAgent* inspectorAgent, unsigned long identifier, const WebSocketHandshakeResponse& response)
623 {
624     inspectorAgent->didReceiveWebSocketHandshakeResponse(identifier, response);
625 }
626
627 void InspectorInstrumentation::didCloseWebSocketImpl(InspectorAgent* inspectorAgent, unsigned long identifier)
628 {
629     inspectorAgent->didCloseWebSocket(identifier);
630 }
631 #endif
632
633 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
634 void InspectorInstrumentation::networkStateChangedImpl(InspectorAgent* inspectorAgent)
635 {
636     if (InspectorApplicationCacheAgent* applicationCacheAgent = inspectorAgent->applicationCacheAgent())
637         applicationCacheAgent->networkStateChanged();
638 }
639
640 void InspectorInstrumentation::updateApplicationCacheStatusImpl(InspectorAgent* inspectorAgent, Frame* frame)
641 {
642     if (InspectorApplicationCacheAgent* applicationCacheAgent = inspectorAgent->applicationCacheAgent())
643         applicationCacheAgent->updateApplicationCacheStatus(frame);
644 }
645 #endif
646
647 bool InspectorInstrumentation::hasFrontend(InspectorAgent* inspectorAgent)
648 {
649     return inspectorAgent->hasFrontend();
650 }
651
652 void InspectorInstrumentation::pauseOnNativeEventIfNeeded(InspectorAgent* inspectorAgent, const String& categoryType, const String& eventName, bool synchronous)
653 {
654 #if ENABLE(JAVASCRIPT_DEBUGGER)
655     if (InspectorBrowserDebuggerAgent* browserDebuggerAgent = inspectorAgent->browserDebuggerAgent())
656         browserDebuggerAgent->pauseOnNativeEventIfNeeded(categoryType, eventName, synchronous);
657 #endif
658 }
659
660 void InspectorInstrumentation::cancelPauseOnNativeEvent(InspectorAgent* inspectorAgent)
661 {
662 #if ENABLE(JAVASCRIPT_DEBUGGER)
663     if (InspectorDebuggerAgent* debuggerAgent = inspectorAgent->debuggerAgent())
664         debuggerAgent->cancelPauseOnNextStatement();
665 #endif
666 }
667
668 InspectorTimelineAgent* InspectorInstrumentation::retrieveTimelineAgent(InspectorAgent* inspectorAgent)
669 {
670     return inspectorAgent->timelineAgent();
671 }
672
673 InspectorTimelineAgent* InspectorInstrumentation::retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
674 {
675     InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie.first);
676     if (timelineAgent && timelineAgent->id() == cookie.second)
677         return timelineAgent;
678     return 0;
679 }
680
681 InspectorResourceAgent* InspectorInstrumentation::retrieveResourceAgent(InspectorAgent* inspectorAgent)
682 {
683     return inspectorAgent->resourceAgent();
684 }
685
686 } // namespace WebCore
687
688 #endif // !ENABLE(INSPECTOR)