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