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