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