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