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