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