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