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