0bc16f4940713e71cf8e8831e3d02d13bf5b5c78
[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
33 #if ENABLE(INSPECTOR)
34
35 #include "InspectorInstrumentation.h"
36
37 #include "CSSRule.h"
38 #include "CSSStyleRule.h"
39 #include "DOMWindow.h"
40 #include "Database.h"
41 #include "DocumentLoader.h"
42 #include "Event.h"
43 #include "EventContext.h"
44 #include "InspectorAgent.h"
45 #include "InspectorApplicationCacheAgent.h"
46 #include "InspectorDOMDebuggerAgent.h"
47 #include "InspectorCSSAgent.h"
48 #include "InspectorConsoleAgent.h"
49 #include "InspectorController.h"
50 #include "WorkerInspectorController.h"
51 #include "InspectorDatabaseAgent.h"
52 #include "InspectorDOMAgent.h"
53 #include "InspectorDOMStorageAgent.h"
54 #include "InspectorDebuggerAgent.h"
55 #include "InspectorPageAgent.h"
56 #include "InspectorProfilerAgent.h"
57 #include "InspectorResourceAgent.h"
58 #include "InspectorRuntimeAgent.h"
59 #include "InspectorTimelineAgent.h"
60 #include "InspectorWorkerAgent.h"
61 #include "InstrumentingAgents.h"
62 #include "PageRuntimeAgent.h"
63 #include "ScriptArguments.h"
64 #include "ScriptCallStack.h"
65 #include "ScriptProfile.h"
66 #include "StyleRule.h"
67 #include "WorkerContext.h"
68 #include "WorkerThread.h"
69 #include "XMLHttpRequest.h"
70 #include <wtf/StdLibExtras.h>
71 #include <wtf/ThreadSpecific.h>
72 #include <wtf/text/CString.h>
73
74 namespace WebCore {
75
76 static const char* const requestAnimationFrameEventName = "requestAnimationFrame";
77 static const char* const cancelAnimationFrameEventName = "cancelAnimationFrame";
78 static const char* const animationFrameFiredEventName = "animationFrameFired";
79 static const char* const setTimerEventName = "setTimer";
80 static const char* const clearTimerEventName = "clearTimer";
81 static const char* const timerFiredEventName = "timerFired";
82
83 namespace {
84 static HashSet<InstrumentingAgents*>* instrumentingAgentsSet = 0;
85 }
86
87 int InspectorInstrumentation::s_frontendCounter = 0;
88
89 static bool eventHasListeners(const AtomicString& eventType, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
90 {
91     if (window && window->hasEventListeners(eventType))
92         return true;
93
94     if (node->hasEventListeners(eventType))
95         return true;
96
97     for (size_t i = 0; i < ancestors.size(); i++) {
98         Node* ancestor = ancestors[i].node();
99         if (ancestor->hasEventListeners(eventType))
100             return true;
101     }
102
103     return false;
104 }
105
106 static Frame* frameForScriptExecutionContext(ScriptExecutionContext* context)
107 {
108     Frame* frame = 0;
109     if (context->isDocument())
110         frame = static_cast<Document*>(context)->frame();
111     return frame;
112 }
113
114 void InspectorInstrumentation::didClearWindowObjectInWorldImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, DOMWrapperWorld* world)
115 {
116     InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent();
117     if (pageAgent)
118         pageAgent->didClearWindowObjectInWorld(frame, world);
119     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
120         inspectorAgent->didClearWindowObjectInWorld(frame, world);
121 #if ENABLE(JAVASCRIPT_DEBUGGER)
122     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) {
123         if (pageAgent && world == mainThreadNormalWorld() && frame == pageAgent->mainFrame())
124             debuggerAgent->didClearMainFrameWindowObject();
125     }
126 #endif
127     if (PageRuntimeAgent* pageRuntimeAgent = instrumentingAgents->pageRuntimeAgent()) {
128         if (world == mainThreadNormalWorld())
129             pageRuntimeAgent->didClearWindowObject(frame);
130     }
131 }
132
133 bool InspectorInstrumentation::isDebuggerPausedImpl(InstrumentingAgents* instrumentingAgents)
134 {
135 #if ENABLE(JAVASCRIPT_DEBUGGER)
136     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
137         return debuggerAgent->isPaused();
138 #endif
139     return false;
140 }
141
142 void InspectorInstrumentation::willInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* parent)
143 {
144 #if ENABLE(JAVASCRIPT_DEBUGGER)
145     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
146         domDebuggerAgent->willInsertDOMNode(parent);
147 #endif
148 }
149
150 void InspectorInstrumentation::didInsertDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
151 {
152     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
153         domAgent->didInsertDOMNode(node);
154 #if ENABLE(JAVASCRIPT_DEBUGGER)
155     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
156         domDebuggerAgent->didInsertDOMNode(node);
157 #endif
158 }
159
160 void InspectorInstrumentation::willRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
161 {
162 #if ENABLE(JAVASCRIPT_DEBUGGER)
163     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
164         domDebuggerAgent->willRemoveDOMNode(node);
165 #endif
166 }
167
168 void InspectorInstrumentation::didRemoveDOMNodeImpl(InstrumentingAgents* instrumentingAgents, Node* node)
169 {
170 #if ENABLE(JAVASCRIPT_DEBUGGER)
171     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
172         domDebuggerAgent->didRemoveDOMNode(node);
173 #endif
174     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
175         domAgent->didRemoveDOMNode(node);
176 }
177
178 void InspectorInstrumentation::willModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element, const AtomicString& oldValue, const AtomicString& newValue)
179 {
180 #if ENABLE(JAVASCRIPT_DEBUGGER)
181     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
182         domDebuggerAgent->willModifyDOMAttr(element);
183     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
184         domAgent->willModifyDOMAttr(element, oldValue, newValue);
185 #endif
186 }
187
188 void InspectorInstrumentation::didModifyDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element, const AtomicString& name, const AtomicString& value)
189 {
190     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
191         domAgent->didModifyDOMAttr(element, name, value);
192 }
193
194 void InspectorInstrumentation::didRemoveDOMAttrImpl(InstrumentingAgents* instrumentingAgents, Element* element, const AtomicString& name)
195 {
196     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
197         domAgent->didRemoveDOMAttr(element, name);
198 }
199
200 void InspectorInstrumentation::didInvalidateStyleAttrImpl(InstrumentingAgents* instrumentingAgents, Node* node)
201 {
202     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
203         domAgent->didInvalidateStyleAttr(node);
204 #if ENABLE(JAVASCRIPT_DEBUGGER)
205     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
206         domDebuggerAgent->didInvalidateStyleAttr(node);
207 #endif
208 }
209
210 void InspectorInstrumentation::frameWindowDiscardedImpl(InstrumentingAgents* instrumentingAgents, DOMWindow* window)
211 {
212     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
213         consoleAgent->frameWindowDiscarded(window);
214 }
215
216 void InspectorInstrumentation::mediaQueryResultChangedImpl(InstrumentingAgents* instrumentingAgents)
217 {
218     if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
219         cssAgent->mediaQueryResultChanged();
220 }
221
222 void InspectorInstrumentation::didPushShadowRootImpl(InstrumentingAgents* instrumentingAgents, Element* host, ShadowRoot* root)
223 {
224     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
225         domAgent->didPushShadowRoot(host, root);
226 }
227
228 void InspectorInstrumentation::willPopShadowRootImpl(InstrumentingAgents* instrumentingAgents, Element* host, ShadowRoot* root)
229 {
230     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
231         domAgent->willPopShadowRoot(host, root);
232 }
233
234 void InspectorInstrumentation::mouseDidMoveOverElementImpl(InstrumentingAgents* instrumentingAgents, const HitTestResult& result, unsigned modifierFlags)
235 {
236     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
237         domAgent->mouseDidMoveOverElement(result, modifierFlags);
238 }
239
240 bool InspectorInstrumentation::handleMousePressImpl(InstrumentingAgents* instrumentingAgents)
241 {
242     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
243         return domAgent->handleMousePress();
244     return false;
245 }
246
247 bool InspectorInstrumentation::forcePseudoStateImpl(InstrumentingAgents* instrumentingAgents, Element* element, CSSSelector::PseudoType pseudoState)
248 {
249     if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
250         return cssAgent->forcePseudoState(element, pseudoState);
251     return false;
252 }
253
254 void InspectorInstrumentation::characterDataModifiedImpl(InstrumentingAgents* instrumentingAgents, CharacterData* characterData)
255 {
256     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
257         domAgent->characterDataModified(characterData);
258 }
259
260 void InspectorInstrumentation::willSendXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url)
261 {
262 #if ENABLE(JAVASCRIPT_DEBUGGER)
263     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
264         domDebuggerAgent->willSendXMLHttpRequest(url);
265 #endif
266 }
267
268 void InspectorInstrumentation::didScheduleResourceRequestImpl(InstrumentingAgents* instrumentingAgents, const String& url, Frame* frame)
269 {
270     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
271         timelineAgent->didScheduleResourceRequest(url, frame);
272 }
273
274 void InspectorInstrumentation::didInstallTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId, int timeout, bool singleShot, ScriptExecutionContext* context)
275 {
276     pauseOnNativeEventIfNeeded(instrumentingAgents, false, setTimerEventName, true);
277     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
278         timelineAgent->didInstallTimer(timerId, timeout, singleShot, frameForScriptExecutionContext(context));
279 }
280
281 void InspectorInstrumentation::didRemoveTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId, ScriptExecutionContext* context)
282 {
283     pauseOnNativeEventIfNeeded(instrumentingAgents, false, clearTimerEventName, true);
284     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
285         timelineAgent->didRemoveTimer(timerId, frameForScriptExecutionContext(context));
286 }
287
288 InspectorInstrumentationCookie InspectorInstrumentation::willCallFunctionImpl(InstrumentingAgents* instrumentingAgents, const String& scriptName, int scriptLine, ScriptExecutionContext* context)
289 {
290     int timelineAgentId = 0;
291     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
292         timelineAgent->willCallFunction(scriptName, scriptLine, frameForScriptExecutionContext(context));
293         timelineAgentId = timelineAgent->id();
294     }
295     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
296 }
297
298 void InspectorInstrumentation::didCallFunctionImpl(const InspectorInstrumentationCookie& cookie)
299 {
300     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
301         timelineAgent->didCallFunction();
302 }
303
304 InspectorInstrumentationCookie InspectorInstrumentation::willChangeXHRReadyStateImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request, ScriptExecutionContext* context)
305 {
306     int timelineAgentId = 0;
307     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
308     if (timelineAgent && request->hasEventListeners(eventNames().readystatechangeEvent)) {
309         timelineAgent->willChangeXHRReadyState(request->url().string(), request->readyState(), frameForScriptExecutionContext(context));
310         timelineAgentId = timelineAgent->id();
311     }
312     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
313 }
314
315 void InspectorInstrumentation::didChangeXHRReadyStateImpl(const InspectorInstrumentationCookie& cookie)
316 {
317     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
318         timelineAgent->didChangeXHRReadyState();
319 }
320
321 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors, Document* document)
322 {
323     int timelineAgentId = 0;
324     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
325     if (timelineAgent && eventHasListeners(event.type(), window, node, ancestors)) {
326         timelineAgent->willDispatchEvent(event, document->frame());
327         timelineAgentId = timelineAgent->id();
328     }
329     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
330 }
331
332 InspectorInstrumentationCookie InspectorInstrumentation::willHandleEventImpl(InstrumentingAgents* instrumentingAgents, Event* event)
333 {
334     pauseOnNativeEventIfNeeded(instrumentingAgents, true, event->type(), false);
335     return InspectorInstrumentationCookie(instrumentingAgents, 0);
336 }
337
338 void InspectorInstrumentation::didHandleEventImpl(const InspectorInstrumentationCookie& cookie)
339 {
340     cancelPauseOnNativeEvent(cookie.first);
341 }
342
343 void InspectorInstrumentation::didDispatchEventImpl(const InspectorInstrumentationCookie& cookie)
344 {
345     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
346         timelineAgent->didDispatchEvent();
347 }
348
349 InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventOnWindowImpl(InstrumentingAgents* instrumentingAgents, const Event& event, DOMWindow* window)
350 {
351     int timelineAgentId = 0;
352     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
353     if (timelineAgent && window->hasEventListeners(event.type())) {
354         timelineAgent->willDispatchEvent(event, window ? window->frame() : 0);
355         timelineAgentId = timelineAgent->id();
356     }
357     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
358 }
359
360 void InspectorInstrumentation::didDispatchEventOnWindowImpl(const InspectorInstrumentationCookie& cookie)
361 {
362     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
363         timelineAgent->didDispatchEvent();
364 }
365
366 InspectorInstrumentationCookie InspectorInstrumentation::willEvaluateScriptImpl(InstrumentingAgents* instrumentingAgents, const String& url, int lineNumber, Frame* frame)
367 {
368     int timelineAgentId = 0;
369     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
370         timelineAgent->willEvaluateScript(url, lineNumber, frame);
371         timelineAgentId = timelineAgent->id();
372     }
373     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
374 }
375
376 void InspectorInstrumentation::didEvaluateScriptImpl(const InspectorInstrumentationCookie& cookie)
377 {
378     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
379         timelineAgent->didEvaluateScript();
380 }
381
382 void InspectorInstrumentation::didCreateIsolatedContextImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, ScriptState* scriptState, SecurityOrigin* origin)
383 {
384     if (PageRuntimeAgent* runtimeAgent = instrumentingAgents->pageRuntimeAgent())
385         runtimeAgent->didCreateIsolatedContext(frame, scriptState, origin);
386 }
387
388 InspectorInstrumentationCookie InspectorInstrumentation::willFireTimerImpl(InstrumentingAgents* instrumentingAgents, int timerId, ScriptExecutionContext* context)
389 {
390     pauseOnNativeEventIfNeeded(instrumentingAgents, false, timerFiredEventName, false);
391
392     int timelineAgentId = 0;
393     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
394         timelineAgent->willFireTimer(timerId, frameForScriptExecutionContext(context));
395         timelineAgentId = timelineAgent->id();
396     }
397     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
398 }
399
400 void InspectorInstrumentation::didFireTimerImpl(const InspectorInstrumentationCookie& cookie)
401 {
402     cancelPauseOnNativeEvent(cookie.first);
403
404     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
405         timelineAgent->didFireTimer();
406 }
407
408 void InspectorInstrumentation::didBeginFrameImpl(InstrumentingAgents* instrumentingAgents)
409 {
410     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
411         timelineAgent->didBeginFrame();
412 }
413
414 void InspectorInstrumentation::didCancelFrameImpl(InstrumentingAgents* instrumentingAgents)
415 {
416     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
417         timelineAgent->didCancelFrame();
418 }
419
420 InspectorInstrumentationCookie InspectorInstrumentation::willLayoutImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
421 {
422     int timelineAgentId = 0;
423     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
424         timelineAgent->willLayout(frame);
425         timelineAgentId = timelineAgent->id();
426     }
427     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
428 }
429
430 void InspectorInstrumentation::didLayoutImpl(const InspectorInstrumentationCookie& cookie)
431 {
432     if (!cookie.first)
433         return;
434
435     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
436         timelineAgent->didLayout();
437
438     if (InspectorPageAgent* pageAgent = cookie.first->inspectorPageAgent())
439         pageAgent->didLayout();
440 }
441
442 InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHRImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request, ScriptExecutionContext* context)
443 {
444     int timelineAgentId = 0;
445     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
446     if (timelineAgent && request->hasEventListeners(eventNames().loadEvent)) {
447         timelineAgent->willLoadXHR(request->url(), frameForScriptExecutionContext(context));
448         timelineAgentId = timelineAgent->id();
449     }
450     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
451 }
452
453 void InspectorInstrumentation::didLoadXHRImpl(const InspectorInstrumentationCookie& cookie)
454 {
455     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
456         timelineAgent->didLoadXHR();
457 }
458
459 InspectorInstrumentationCookie InspectorInstrumentation::willPaintImpl(InstrumentingAgents* instrumentingAgents, GraphicsContext* context, const LayoutRect& rect, Frame* frame)
460 {
461     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
462         pageAgent->willPaint(context, rect);
463
464     int timelineAgentId = 0;
465     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
466         timelineAgent->willPaint(rect, frame);
467         timelineAgentId = timelineAgent->id();
468     }
469     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
470 }
471
472 void InspectorInstrumentation::didPaintImpl(const InspectorInstrumentationCookie& cookie)
473 {
474     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
475         timelineAgent->didPaint();
476     if (InspectorPageAgent* pageAgent = cookie.first ? cookie.first->inspectorPageAgent() : 0)
477         pageAgent->didPaint();
478 }
479
480 void InspectorInstrumentation::willCompositeImpl(InstrumentingAgents* instrumentingAgents)
481 {
482     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
483         timelineAgent->willComposite();
484 }
485
486 void InspectorInstrumentation::didCompositeImpl(InstrumentingAgents* instrumentingAgents)
487 {
488     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
489         timelineAgent->didComposite();
490 }
491
492 InspectorInstrumentationCookie InspectorInstrumentation::willRecalculateStyleImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
493 {
494     int timelineAgentId = 0;
495     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
496         timelineAgent->willRecalculateStyle(frame);
497         timelineAgentId = timelineAgent->id();
498     }
499     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
500         resourceAgent->willRecalculateStyle();
501     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
502 }
503
504 void InspectorInstrumentation::didRecalculateStyleImpl(const InspectorInstrumentationCookie& cookie)
505 {
506     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
507         timelineAgent->didRecalculateStyle();
508     InstrumentingAgents* instrumentingAgents = cookie.first;
509     if (!instrumentingAgents)
510         return;
511     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
512         resourceAgent->didRecalculateStyle();
513 }
514
515 void InspectorInstrumentation::didScheduleStyleRecalculationImpl(InstrumentingAgents* instrumentingAgents, Document* document)
516 {
517     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
518         resourceAgent->didScheduleStyleRecalculation(document);
519 }
520
521 InspectorInstrumentationCookie InspectorInstrumentation::willMatchRuleImpl(InstrumentingAgents* instrumentingAgents, const StyleRule* rule)
522 {
523     InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent();
524     if (cssAgent) {
525         RefPtr<CSSRule> cssRule = rule->createCSSOMWrapper();
526         cssAgent->willMatchRule(static_cast<CSSStyleRule*>(cssRule.get()));
527         return InspectorInstrumentationCookie(instrumentingAgents, 1);
528     }
529
530     return InspectorInstrumentationCookie();
531 }
532
533 void InspectorInstrumentation::didMatchRuleImpl(const InspectorInstrumentationCookie& cookie, bool matched)
534 {
535     InspectorCSSAgent* cssAgent = cookie.first->inspectorCSSAgent();
536     if (cssAgent)
537         cssAgent->didMatchRule(matched);
538 }
539
540 InspectorInstrumentationCookie InspectorInstrumentation::willProcessRuleImpl(InstrumentingAgents* instrumentingAgents, const StyleRule* rule)
541 {
542     InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent();
543     if (cssAgent) {
544         RefPtr<CSSRule> cssRule = rule->createCSSOMWrapper();
545         cssAgent->willProcessRule(static_cast<CSSStyleRule*>(cssRule.get()));
546         return InspectorInstrumentationCookie(instrumentingAgents, 1);
547     }
548
549     return InspectorInstrumentationCookie();
550 }
551
552 void InspectorInstrumentation::didProcessRuleImpl(const InspectorInstrumentationCookie& cookie)
553 {
554     InspectorCSSAgent* cssAgent = cookie.first->inspectorCSSAgent();
555     if (cssAgent)
556         cssAgent->didProcessRule();
557 }
558
559 void InspectorInstrumentation::willProcessTaskImpl(InstrumentingAgents* instrumentingAgents)
560 {
561     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
562         timelineAgent->willProcessTask();
563 }
564
565 void InspectorInstrumentation::didProcessTaskImpl(InstrumentingAgents* instrumentingAgents)
566 {
567     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
568         timelineAgent->didProcessTask();
569 }
570
571 void InspectorInstrumentation::applyUserAgentOverrideImpl(InstrumentingAgents* instrumentingAgents, String* userAgent)
572 {
573     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
574         resourceAgent->applyUserAgentOverride(userAgent);
575 }
576
577 void InspectorInstrumentation::applyScreenWidthOverrideImpl(InstrumentingAgents* instrumentingAgents, long* width)
578 {
579     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
580         pageAgent->applyScreenWidthOverride(width);
581 }
582
583 void InspectorInstrumentation::applyScreenHeightOverrideImpl(InstrumentingAgents* instrumentingAgents, long* height)
584 {
585     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
586         pageAgent->applyScreenHeightOverride(height);
587 }
588
589 bool InspectorInstrumentation::shouldApplyScreenWidthOverrideImpl(InstrumentingAgents* instrumentingAgents)
590 {
591     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent()) {
592         long width = 0;
593         pageAgent->applyScreenWidthOverride(&width);
594         return !!width;
595     }
596     return false;
597 }
598
599 bool InspectorInstrumentation::shouldApplyScreenHeightOverrideImpl(InstrumentingAgents* instrumentingAgents)
600 {
601     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent()) {
602         long height = 0;
603         pageAgent->applyScreenHeightOverride(&height);
604         return !!height;
605     }
606     return false;
607 }
608
609 void InspectorInstrumentation::willSendRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
610 {
611     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
612         timelineAgent->willSendResourceRequest(identifier, request, loader->frame());
613     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
614         resourceAgent->willSendRequest(identifier, loader, request, redirectResponse);
615 }
616
617 void InspectorInstrumentation::continueAfterPingLoaderImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& response)
618 {
619     willSendRequestImpl(instrumentingAgents, identifier, loader, request, response);
620 }
621
622 void InspectorInstrumentation::markResourceAsCachedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
623 {
624     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
625         resourceAgent->markResourceAsCached(identifier);
626 }
627
628 void InspectorInstrumentation::didLoadResourceFromMemoryCacheImpl(InstrumentingAgents* instrumentingAgents, DocumentLoader* loader, CachedResource* cachedResource)
629 {
630     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
631     if (!inspectorAgent || !inspectorAgent->developerExtrasEnabled())
632         return;
633     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
634         resourceAgent->didLoadResourceFromMemoryCache(loader, cachedResource);
635 }
636
637 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, Frame* frame, int length)
638 {
639     int timelineAgentId = 0;
640     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
641         timelineAgent->willReceiveResourceData(identifier, frame, length);
642         timelineAgentId = timelineAgent->id();
643     }
644     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
645 }
646
647 void InspectorInstrumentation::didReceiveResourceDataImpl(const InspectorInstrumentationCookie& cookie)
648 {
649     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
650         timelineAgent->didReceiveResourceData();
651 }
652
653 InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const ResourceResponse& response, Frame* frame)
654 {
655     int timelineAgentId = 0;
656     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
657     if (timelineAgent) {
658         timelineAgent->willReceiveResourceResponse(identifier, response, frame);
659         timelineAgentId = timelineAgent->id();
660     }
661     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
662 }
663
664 void InspectorInstrumentation::didReceiveResourceResponseImpl(const InspectorInstrumentationCookie& cookie, unsigned long identifier, DocumentLoader* loader, const ResourceResponse& response)
665 {
666     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
667         timelineAgent->didReceiveResourceResponse();
668     if (!loader)
669         return;
670     InstrumentingAgents* instrumentingAgents = cookie.first;
671     if (!instrumentingAgents)
672         return;
673     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
674         resourceAgent->didReceiveResponse(identifier, loader, response);
675     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
676         consoleAgent->didReceiveResponse(identifier, response); // This should come AFTER resource notification, front-end relies on this.
677 }
678
679 void InspectorInstrumentation::didReceiveResourceResponseButCanceledImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
680 {
681     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceResponse(frame, identifier, r);
682     InspectorInstrumentation::didReceiveResourceResponse(cookie, identifier, loader, r);
683 }
684
685 void InspectorInstrumentation::continueAfterXFrameOptionsDeniedImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
686 {
687     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
688 }
689
690 void InspectorInstrumentation::continueWithPolicyDownloadImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
691 {
692     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
693 }
694
695 void InspectorInstrumentation::continueWithPolicyIgnoreImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
696 {
697     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
698 }
699
700 void InspectorInstrumentation::didReceiveDataImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
701 {
702     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
703         resourceAgent->didReceiveData(identifier, data, dataLength, encodedDataLength);
704 }
705
706 void InspectorInstrumentation::didFinishLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, double monotonicFinishTime)
707 {
708     InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent();
709     InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent();
710     if (!timelineAgent && !resourceAgent)
711         return;
712
713     double finishTime = 0.0;
714     // FIXME: Expose all of the timing details to inspector and have it calculate finishTime.
715     if (monotonicFinishTime)
716         finishTime = loader->timing()->convertMonotonicTimeToDocumentTime(monotonicFinishTime);
717
718     if (timelineAgent)
719         timelineAgent->didFinishLoadingResource(identifier, false, finishTime, loader->frame());
720     if (resourceAgent)
721         resourceAgent->didFinishLoading(identifier, loader, finishTime);
722 }
723
724 void InspectorInstrumentation::didFailLoadingImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, const ResourceError& error)
725 {
726     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
727         timelineAgent->didFinishLoadingResource(identifier, true, 0, loader->frame());
728     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
729         resourceAgent->didFailLoading(identifier, loader, error);
730     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
731         consoleAgent->didFailLoading(identifier, error); // This should come AFTER resource notification, front-end relies on this.
732 }
733
734 void InspectorInstrumentation::resourceRetrievedByXMLHttpRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber)
735 {
736     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
737         consoleAgent->resourceRetrievedByXMLHttpRequest(identifier, url, sendURL, sendLineNumber);
738     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
739         resourceAgent->setInitialXHRContent(identifier, sourceString);
740 }
741
742 void InspectorInstrumentation::didReceiveXHRResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
743 {
744     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
745         resourceAgent->didReceiveXHRResponse(identifier);
746 }
747
748 void InspectorInstrumentation::willLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
749 {
750     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
751         resourceAgent->willLoadXHRSynchronously();
752 }
753
754 void InspectorInstrumentation::didLoadXHRSynchronouslyImpl(InstrumentingAgents* instrumentingAgents)
755 {
756     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
757         resourceAgent->didLoadXHRSynchronously();
758 }
759
760 void InspectorInstrumentation::scriptImportedImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& sourceString)
761 {
762     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
763         resourceAgent->setInitialScriptContent(identifier, sourceString);
764 }
765
766 void InspectorInstrumentation::didReceiveScriptResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
767 {
768     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
769         resourceAgent->didReceiveScriptResponse(identifier);
770 }
771
772 void InspectorInstrumentation::domContentLoadedEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
773 {
774     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
775         timelineAgent->didMarkDOMContentEvent(frame);
776
777     if (frame->page()->mainFrame() != frame)
778         return;
779
780     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
781         inspectorAgent->domContentLoadedEventFired();
782
783     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
784         domAgent->mainFrameDOMContentLoaded();
785
786     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
787         pageAgent->domContentEventFired();
788 }
789
790 void InspectorInstrumentation::loadEventFiredImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
791 {
792     if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
793         domAgent->loadEventFired(frame->document());
794
795     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
796         timelineAgent->didMarkLoadEvent(frame);
797
798     if (frame->page()->mainFrame() != frame)
799         return;
800
801     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
802         pageAgent->loadEventFired();
803 }
804
805 void InspectorInstrumentation::frameDetachedFromParentImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
806 {
807     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
808         pageAgent->frameDetached(frame);
809 }
810
811 void InspectorInstrumentation::didCommitLoadImpl(InstrumentingAgents* instrumentingAgents, Page* page, DocumentLoader* loader)
812 {
813     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
814     if (!inspectorAgent || !inspectorAgent->developerExtrasEnabled())
815         return;
816
817     Frame* mainFrame = page->mainFrame();
818     if (loader->frame() == mainFrame) {
819         if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
820             consoleAgent->reset();
821
822         if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
823             resourceAgent->mainFrameNavigated(loader);
824 #if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
825         if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
826             profilerAgent->resetState();
827 #endif
828         if (InspectorCSSAgent* cssAgent = instrumentingAgents->inspectorCSSAgent())
829             cssAgent->reset();
830 #if ENABLE(SQL_DATABASE)
831         if (InspectorDatabaseAgent* databaseAgent = instrumentingAgents->inspectorDatabaseAgent())
832             databaseAgent->clearResources();
833 #endif
834         if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
835             domStorageAgent->clearResources();
836         if (InspectorDOMAgent* domAgent = instrumentingAgents->inspectorDOMAgent())
837             domAgent->setDocument(mainFrame->document());
838
839         inspectorAgent->didCommitLoad();
840     }
841     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
842         pageAgent->frameNavigated(loader);
843 }
844
845 void InspectorInstrumentation::loaderDetachedFromFrameImpl(InstrumentingAgents* instrumentingAgents, DocumentLoader* loader)
846 {
847     if (InspectorPageAgent* inspectorPageAgent = instrumentingAgents->inspectorPageAgent())
848         inspectorPageAgent->loaderDetachedFromFrame(loader);
849 }
850
851 void InspectorInstrumentation::willDestroyCachedResourceImpl(CachedResource* cachedResource)
852 {
853     if (!instrumentingAgentsSet)
854         return;
855     HashSet<InstrumentingAgents*>::iterator end = instrumentingAgentsSet->end();
856     for (HashSet<InstrumentingAgents*>::iterator it = instrumentingAgentsSet->begin(); it != end; ++it) {
857         InstrumentingAgents* instrumentingAgents = *it;
858         if (InspectorResourceAgent* inspectorResourceAgent = instrumentingAgents->inspectorResourceAgent())
859             inspectorResourceAgent->willDestroyCachedResource(cachedResource);
860     }
861 }
862
863 InspectorInstrumentationCookie InspectorInstrumentation::willWriteHTMLImpl(InstrumentingAgents* instrumentingAgents, unsigned int length, unsigned int startLine, Frame* frame)
864 {
865     int timelineAgentId = 0;
866     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
867         timelineAgent->willWriteHTML(length, startLine, frame);
868         timelineAgentId = timelineAgent->id();
869     }
870     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
871 }
872
873 void InspectorInstrumentation::didWriteHTMLImpl(const InspectorInstrumentationCookie& cookie, unsigned int endLine)
874 {
875     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
876         timelineAgent->didWriteHTML(endLine);
877 }
878
879 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
880 {
881     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
882         consoleAgent->addMessageToConsole(source, type, level, message, arguments, callStack);
883 }
884
885 void InspectorInstrumentation::addMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, MessageSource source, MessageType type, MessageLevel level, const String& message, const String& scriptId, unsigned lineNumber)
886 {
887     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
888         consoleAgent->addMessageToConsole(source, type, level, message, scriptId, lineNumber);
889 }
890
891 void InspectorInstrumentation::consoleCountImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> stack)
892 {
893     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
894         consoleAgent->count(arguments, stack);
895 }
896
897 void InspectorInstrumentation::startConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title)
898 {
899     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
900         timelineAgent->time(title);
901     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
902         consoleAgent->startTiming(title);
903 }
904
905 void InspectorInstrumentation::stopConsoleTimingImpl(InstrumentingAgents* instrumentingAgents, const String& title, PassRefPtr<ScriptCallStack> stack)
906 {
907     if (InspectorConsoleAgent* consoleAgent = instrumentingAgents->inspectorConsoleAgent())
908         consoleAgent->stopTiming(title, stack);
909     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
910         timelineAgent->timeEnd(title);
911 }
912
913 void InspectorInstrumentation::consoleTimeStampImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<ScriptArguments> arguments)
914 {
915     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
916         String message;
917         arguments->getFirstArgumentAsString(message);
918         timelineAgent->didTimeStamp(message);
919      }
920 }
921
922 #if ENABLE(JAVASCRIPT_DEBUGGER)
923 void InspectorInstrumentation::addStartProfilingMessageToConsoleImpl(InstrumentingAgents* instrumentingAgents, const String& title, unsigned lineNumber, const String& sourceURL)
924 {
925     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
926         profilerAgent->addStartProfilingMessageToConsole(title, lineNumber, sourceURL);
927 }
928
929 void InspectorInstrumentation::addProfileImpl(InstrumentingAgents* instrumentingAgents, RefPtr<ScriptProfile> profile, PassRefPtr<ScriptCallStack> callStack)
930 {
931     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) {
932         const ScriptCallFrame& lastCaller = callStack->at(0);
933         profilerAgent->addProfile(profile, lastCaller.lineNumber(), lastCaller.sourceURL());
934     }
935 }
936
937 String InspectorInstrumentation::getCurrentUserInitiatedProfileNameImpl(InstrumentingAgents* instrumentingAgents, bool incrementProfileNumber)
938 {
939     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
940         return profilerAgent->getCurrentUserInitiatedProfileName(incrementProfileNumber);
941     return "";
942 }
943
944 bool InspectorInstrumentation::profilerEnabledImpl(InstrumentingAgents* instrumentingAgents)
945 {
946     if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent())
947         return profilerAgent->enabled();
948     return false;
949 }
950 #endif
951
952 #if ENABLE(SQL_DATABASE)
953 void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents* instrumentingAgents, PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
954 {
955     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
956     if (!inspectorAgent || !inspectorAgent->developerExtrasEnabled())
957         return;
958     if (InspectorDatabaseAgent* dbAgent = instrumentingAgents->inspectorDatabaseAgent())
959         dbAgent->didOpenDatabase(database, domain, name, version);
960 }
961 #endif
962
963 void InspectorInstrumentation::didUseDOMStorageImpl(InstrumentingAgents* instrumentingAgents, StorageArea* storageArea, bool isLocalStorage, Frame* frame)
964 {
965     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
966     if (!inspectorAgent || !inspectorAgent->developerExtrasEnabled())
967         return;
968     if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
969         domStorageAgent->didUseDOMStorage(storageArea, isLocalStorage, frame);
970 }
971
972 void InspectorInstrumentation::didDispatchDOMStorageEventImpl(InstrumentingAgents* instrumentingAgents, const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Page* page)
973 {
974     if (InspectorDOMStorageAgent* domStorageAgent = instrumentingAgents->inspectorDOMStorageAgent())
975         domStorageAgent->didDispatchDOMStorageEvent(key, oldValue, newValue, storageType, securityOrigin, page);
976 }
977
978 #if ENABLE(WORKERS)
979 bool InspectorInstrumentation::shouldPauseDedicatedWorkerOnStartImpl(InstrumentingAgents* instrumentingAgents)
980 {
981     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
982         return workerAgent->shouldPauseDedicatedWorkerOnStart();
983     return false;
984 }
985
986 void InspectorInstrumentation::didStartWorkerContextImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* workerContextProxy, const KURL& url)
987 {
988     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
989         workerAgent->didStartWorkerContext(workerContextProxy, url);
990 }
991
992 void InspectorInstrumentation::willEvaluateWorkerScript(WorkerContext* workerContext, int workerThreadStartMode)
993 {
994     if (workerThreadStartMode != PauseWorkerContextOnStart)
995         return;
996     InstrumentingAgents* instrumentingAgents = instrumentationForWorkerContext(workerContext);
997     if (!instrumentingAgents)
998         return;
999 #if ENABLE(JAVASCRIPT_DEBUGGER)
1000     if (InspectorRuntimeAgent* runtimeAgent = instrumentingAgents->inspectorRuntimeAgent())
1001         runtimeAgent->pauseWorkerContext(workerContext);
1002 #endif
1003 }
1004
1005 void InspectorInstrumentation::workerContextTerminatedImpl(InstrumentingAgents* instrumentingAgents, WorkerContextProxy* proxy)
1006 {
1007     if (InspectorWorkerAgent* workerAgent = instrumentingAgents->inspectorWorkerAgent())
1008         workerAgent->workerContextTerminated(proxy);
1009 }
1010 #endif
1011
1012 #if ENABLE(WEB_SOCKETS)
1013 void InspectorInstrumentation::didCreateWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const KURL& requestURL, const KURL&)
1014 {
1015     InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent();
1016     if (!inspectorAgent || !inspectorAgent->developerExtrasEnabled())
1017         return;
1018     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1019         resourceAgent->didCreateWebSocket(identifier, requestURL);
1020 }
1021
1022 void InspectorInstrumentation::willSendWebSocketHandshakeRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeRequest& request)
1023 {
1024     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1025         resourceAgent->willSendWebSocketHandshakeRequest(identifier, request);
1026 }
1027
1028 void InspectorInstrumentation::didReceiveWebSocketHandshakeResponseImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketHandshakeResponse& response)
1029 {
1030     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1031         resourceAgent->didReceiveWebSocketHandshakeResponse(identifier, response);
1032 }
1033
1034 void InspectorInstrumentation::didCloseWebSocketImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier)
1035 {
1036     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1037         resourceAgent->didCloseWebSocket(identifier);
1038 }
1039 void InspectorInstrumentation::didReceiveWebSocketFrameImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketFrame& frame)
1040 {
1041     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1042         resourceAgent->didReceiveWebSocketFrame(identifier, frame);
1043 }
1044 void InspectorInstrumentation::didReceiveWebSocketFrameErrorImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const String& errorMessage)
1045 {
1046     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1047         resourceAgent->didReceiveWebSocketFrameError(identifier, errorMessage);
1048 }
1049 void InspectorInstrumentation::didSendWebSocketFrameImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, const WebSocketFrame& frame)
1050 {
1051     if (InspectorResourceAgent* resourceAgent = instrumentingAgents->inspectorResourceAgent())
1052         resourceAgent->didSendWebSocketFrame(identifier, frame);
1053 }
1054 #endif
1055
1056 void InspectorInstrumentation::networkStateChangedImpl(InstrumentingAgents* instrumentingAgents)
1057 {
1058     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
1059         applicationCacheAgent->networkStateChanged();
1060 }
1061
1062 void InspectorInstrumentation::updateApplicationCacheStatusImpl(InstrumentingAgents* instrumentingAgents, Frame* frame)
1063 {
1064     if (InspectorApplicationCacheAgent* applicationCacheAgent = instrumentingAgents->inspectorApplicationCacheAgent())
1065         applicationCacheAgent->updateApplicationCacheStatus(frame);
1066 }
1067
1068 bool InspectorInstrumentation::collectingHTMLParseErrors(InstrumentingAgents* instrumentingAgents)
1069 {
1070     if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent())
1071         return inspectorAgent->hasFrontend();
1072     return false;
1073 }
1074
1075 bool InspectorInstrumentation::hasFrontendForScriptContext(ScriptExecutionContext* scriptExecutionContext)
1076 {
1077     if (!scriptExecutionContext)
1078         return false;
1079
1080 #if ENABLE(WORKERS)
1081     if (scriptExecutionContext->isWorkerContext()) {
1082         WorkerContext* workerContext = static_cast<WorkerContext*>(scriptExecutionContext);
1083         WorkerInspectorController* workerInspectorController = workerContext->workerInspectorController();
1084         return workerInspectorController && workerInspectorController->hasFrontend();
1085     }
1086 #endif
1087
1088     ASSERT(scriptExecutionContext->isDocument());
1089     Document* document = static_cast<Document*>(scriptExecutionContext);
1090     Page* page = document->page();
1091     return page && page->inspectorController()->hasFrontend();
1092 }
1093
1094 void InspectorInstrumentation::pauseOnNativeEventIfNeeded(InstrumentingAgents* instrumentingAgents, bool isDOMEvent, const String& eventName, bool synchronous)
1095 {
1096 #if ENABLE(JAVASCRIPT_DEBUGGER)
1097     if (InspectorDOMDebuggerAgent* domDebuggerAgent = instrumentingAgents->inspectorDOMDebuggerAgent())
1098         domDebuggerAgent->pauseOnNativeEventIfNeeded(isDOMEvent, eventName, synchronous);
1099 #endif
1100 }
1101
1102 void InspectorInstrumentation::cancelPauseOnNativeEvent(InstrumentingAgents* instrumentingAgents)
1103 {
1104 #if ENABLE(JAVASCRIPT_DEBUGGER)
1105     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
1106         debuggerAgent->cancelPauseOnNextStatement();
1107 #endif
1108 }
1109
1110 void InspectorInstrumentation::didRequestAnimationFrameImpl(InstrumentingAgents* instrumentingAgents, int callbackId, Frame* frame)
1111 {
1112     pauseOnNativeEventIfNeeded(instrumentingAgents, false, requestAnimationFrameEventName, true);
1113
1114     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
1115         timelineAgent->didRequestAnimationFrame(callbackId, frame);
1116 }
1117
1118 void InspectorInstrumentation::didCancelAnimationFrameImpl(InstrumentingAgents* instrumentingAgents, int callbackId, Frame* frame)
1119 {
1120     pauseOnNativeEventIfNeeded(instrumentingAgents, false, cancelAnimationFrameEventName, true);
1121
1122     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())
1123         timelineAgent->didCancelAnimationFrame(callbackId, frame);
1124 }
1125
1126 InspectorInstrumentationCookie InspectorInstrumentation::willFireAnimationFrameImpl(InstrumentingAgents* instrumentingAgents, int callbackId, Frame* frame)
1127 {
1128     pauseOnNativeEventIfNeeded(instrumentingAgents, false, animationFrameFiredEventName, false);
1129
1130     int timelineAgentId = 0;
1131     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent()) {
1132         timelineAgent->willFireAnimationFrame(callbackId, frame);
1133         timelineAgentId = timelineAgent->id();
1134     }
1135     return InspectorInstrumentationCookie(instrumentingAgents, timelineAgentId);
1136 }
1137
1138 void InspectorInstrumentation::didFireAnimationFrameImpl(const InspectorInstrumentationCookie& cookie)
1139 {
1140     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
1141         timelineAgent->didFireAnimationFrame();
1142 }
1143
1144 InspectorTimelineAgent* InspectorInstrumentation::timelineAgentForOrphanEvents()
1145 {
1146     return *threadSpecificTimelineAgentForOrphanEvents();
1147 }
1148
1149 void InspectorInstrumentation::setTimelineAgentForOrphanEvents(InspectorTimelineAgent* inspectorTimelineAgent)
1150 {
1151     *threadSpecificTimelineAgentForOrphanEvents() = inspectorTimelineAgent;
1152 }
1153
1154 WTF::ThreadSpecific<InspectorTimelineAgent*>& InspectorInstrumentation::threadSpecificTimelineAgentForOrphanEvents()
1155 {
1156     AtomicallyInitializedStatic(WTF::ThreadSpecific<InspectorTimelineAgent*>*, instance = new WTF::ThreadSpecific<InspectorTimelineAgent*>());
1157     return *instance;
1158 }
1159
1160 void InspectorInstrumentation::registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
1161 {
1162     if (!instrumentingAgentsSet)
1163         instrumentingAgentsSet = new HashSet<InstrumentingAgents*>();
1164     instrumentingAgentsSet->add(instrumentingAgents);
1165 }
1166
1167 void InspectorInstrumentation::unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
1168 {
1169     if (!instrumentingAgentsSet)
1170         return;
1171     instrumentingAgentsSet->remove(instrumentingAgents);
1172     if (!instrumentingAgentsSet->size())
1173         delete instrumentingAgentsSet;
1174 }
1175
1176 InspectorTimelineAgent* InspectorInstrumentation::retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
1177 {
1178     if (!cookie.first)
1179         return 0;
1180     InspectorTimelineAgent* timelineAgent = cookie.first->inspectorTimelineAgent();
1181     if (timelineAgent && timelineAgent->id() == cookie.second)
1182         return timelineAgent;
1183     return 0;
1184 }
1185
1186 InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForPage(Page* page)
1187 {
1188     if (!page)
1189         return 0;
1190     return instrumentationForPage(page);
1191 }
1192
1193 #if ENABLE(WORKERS)
1194 InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForWorkerContext(WorkerContext* workerContext)
1195 {
1196     if (!workerContext)
1197         return 0;
1198     return instrumentationForWorkerContext(workerContext);
1199 }
1200
1201 InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForNonDocumentContext(ScriptExecutionContext* context)
1202 {
1203     if (context->isWorkerContext())
1204         return instrumentationForWorkerContext(static_cast<WorkerContext*>(context));
1205     return 0;
1206 }
1207 #endif
1208
1209 #if ENABLE(GEOLOCATION)
1210 GeolocationPosition* InspectorInstrumentation::overrideGeolocationPositionImpl(InstrumentingAgents* instrumentingAgents, GeolocationPosition* position)
1211 {
1212     if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
1213         position = pageAgent->overrideGeolocationPosition(position);
1214     return position;
1215 }
1216 #endif
1217
1218 } // namespace WebCore
1219
1220 #endif // !ENABLE(INSPECTOR)