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