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