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