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