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