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