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