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