1/*
2* Copyright (C) 2010 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#ifndef InspectorInstrumentation_h
32#define InspectorInstrumentation_h
33
34#include "Console.h"
35#include "Frame.h"
36#include "Page.h"
37#include "ScriptExecutionContext.h"
38#include <wtf/HashMap.h>
39
40namespace WebCore {
41
42class CharacterData;
43class DOMWrapperWorld;
44class Database;
45class Document;
46class Element;
47class EventContext;
48class DocumentLoader;
49class HitTestResult;
50class InspectorAgent;
51class InspectorPageAgent;
52class InspectorResourceAgent;
53class InspectorTimelineAgent;
54class KURL;
55class Node;
56class ResourceRequest;
57class ResourceResponse;
58class ScriptArguments;
59class ScriptCallStack;
60class ScriptExecutionContext;
61class StorageArea;
62class XMLHttpRequest;
63
64#if ENABLE(WEB_SOCKETS)
65class WebSocketHandshakeRequest;
66class WebSocketHandshakeResponse;
67#endif
68
69typedef pair<InspectorAgent*, int> InspectorInstrumentationCookie;
70
71class InspectorInstrumentation {
72public:
73    static void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld*);
74    static void inspectedPageDestroyed(Page*);
75
76    static void willInsertDOMNode(Document*, Node*, Node* parent);
77    static void didInsertDOMNode(Document*, Node*);
78    static void willRemoveDOMNode(Document*, Node*);
79    static void willModifyDOMAttr(Document*, Element*);
80    static void didModifyDOMAttr(Document*, Element*);
81    static void characterDataModified(Document*, CharacterData*);
82    static void didInvalidateStyleAttr(Document*, Node*);
83
84    static void mouseDidMoveOverElement(Page*, const HitTestResult&, unsigned modifierFlags);
85    static bool handleMousePress(Page*);
86
87    static void willSendXMLHttpRequest(ScriptExecutionContext*, const String& url);
88    static void didScheduleResourceRequest(Document*, const String& url);
89    static void didInstallTimer(ScriptExecutionContext*, int timerId, int timeout, bool singleShot);
90    static void didRemoveTimer(ScriptExecutionContext*, int timerId);
91
92    static InspectorInstrumentationCookie willCallFunction(Frame*, const String& scriptName, int scriptLine);
93    static void didCallFunction(const InspectorInstrumentationCookie&);
94    static InspectorInstrumentationCookie willChangeXHRReadyState(ScriptExecutionContext*, XMLHttpRequest* request);
95    static void didChangeXHRReadyState(const InspectorInstrumentationCookie&);
96    static InspectorInstrumentationCookie willDispatchEvent(Document*, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors);
97    static void didDispatchEvent(const InspectorInstrumentationCookie&);
98    static InspectorInstrumentationCookie willDispatchEventOnWindow(Frame*, const Event& event, DOMWindow* window);
99    static void didDispatchEventOnWindow(const InspectorInstrumentationCookie&);
100    static InspectorInstrumentationCookie willEvaluateScript(Frame*, const String& url, int lineNumber);
101    static void didEvaluateScript(const InspectorInstrumentationCookie&);
102    static InspectorInstrumentationCookie willFireTimer(ScriptExecutionContext*, int timerId);
103    static void didFireTimer(const InspectorInstrumentationCookie&);
104    static InspectorInstrumentationCookie willLayout(Frame*);
105    static void didLayout(const InspectorInstrumentationCookie&);
106    static InspectorInstrumentationCookie willLoadXHR(ScriptExecutionContext*, XMLHttpRequest*);
107    static void didLoadXHR(const InspectorInstrumentationCookie&);
108    static InspectorInstrumentationCookie willPaint(Frame*, const IntRect& rect);
109    static void didPaint(const InspectorInstrumentationCookie&);
110    static InspectorInstrumentationCookie willRecalculateStyle(Document*);
111    static void didRecalculateStyle(const InspectorInstrumentationCookie&);
112
113    static void applyUserAgentOverride(Frame*, String*);
114    static void willSendRequest(Frame*, unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
115    static void markResourceAsCached(Page*, unsigned long identifier);
116    static void didLoadResourceFromMemoryCache(Page*, DocumentLoader*, const CachedResource*);
117    static InspectorInstrumentationCookie willReceiveResourceData(Frame*, unsigned long identifier);
118    static void didReceiveResourceData(const InspectorInstrumentationCookie&);
119    static InspectorInstrumentationCookie willReceiveResourceResponse(Frame*, unsigned long identifier, const ResourceResponse&);
120    static void didReceiveResourceResponse(const InspectorInstrumentationCookie&, unsigned long identifier, DocumentLoader*, const ResourceResponse&);
121    static void continueAfterXFrameOptionsDenied(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
122    static void continueWithPolicyDownload(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
123    static void continueWithPolicyIgnore(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
124    static void didReceiveContentLength(Frame*, unsigned long identifier, int dataLength, int encodedDataLength);
125    static void didFinishLoading(Frame*, unsigned long identifier, double finishTime);
126    static void didFailLoading(Frame*, unsigned long identifier, const ResourceError&);
127    static void resourceRetrievedByXMLHttpRequest(ScriptExecutionContext*, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber);
128    static void scriptImported(ScriptExecutionContext*, unsigned long identifier, const String& sourceString);
129    static void domContentLoadedEventFired(Frame*, const KURL&);
130    static void loadEventFired(Frame*, const KURL&);
131    static void frameDetachedFromParent(Frame*);
132    static void didCommitLoad(Frame*, DocumentLoader*);
133
134    static InspectorInstrumentationCookie willWriteHTML(Document*, unsigned int length, unsigned int startLine);
135    static void didWriteHTML(const InspectorInstrumentationCookie&, unsigned int endLine);
136
137    static void addMessageToConsole(Page*, MessageSource, MessageType, MessageLevel, const String& message, PassRefPtr<ScriptArguments>, PassRefPtr<ScriptCallStack>);
138    static void addMessageToConsole(Page*, MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String&);
139    static void consoleCount(Page*, PassRefPtr<ScriptArguments>, PassRefPtr<ScriptCallStack>);
140    static void startConsoleTiming(Page*, const String& title);
141    static void stopConsoleTiming(Page*, const String& title, PassRefPtr<ScriptCallStack>);
142    static void consoleMarkTimeline(Page*, PassRefPtr<ScriptArguments>);
143
144#if ENABLE(JAVASCRIPT_DEBUGGER)
145    static void addStartProfilingMessageToConsole(Page*, const String& title, unsigned lineNumber, const String& sourceURL);
146    static void addProfile(Page*, RefPtr<ScriptProfile>, PassRefPtr<ScriptCallStack>);
147    static String getCurrentUserInitiatedProfileName(Page*, bool incrementProfileNumber);
148    static bool profilerEnabled(Page*);
149#endif
150
151#if ENABLE(DATABASE)
152    static void didOpenDatabase(ScriptExecutionContext*, PassRefPtr<Database>, const String& domain, const String& name, const String& version);
153#endif
154
155#if ENABLE(DOM_STORAGE)
156    static void didUseDOMStorage(Page*, StorageArea*, bool isLocalStorage, Frame*);
157#endif
158
159#if ENABLE(WORKERS)
160    static void didCreateWorker(ScriptExecutionContext*, intptr_t id, const String& url, bool isSharedWorker);
161    static void didDestroyWorker(ScriptExecutionContext*, intptr_t id);
162#endif
163
164#if ENABLE(WEB_SOCKETS)
165    static void didCreateWebSocket(ScriptExecutionContext*, unsigned long identifier, const KURL& requestURL, const KURL& documentURL);
166    static void willSendWebSocketHandshakeRequest(ScriptExecutionContext*, unsigned long identifier, const WebSocketHandshakeRequest&);
167    static void didReceiveWebSocketHandshakeResponse(ScriptExecutionContext*, unsigned long identifier, const WebSocketHandshakeResponse&);
168    static void didCloseWebSocket(ScriptExecutionContext*, unsigned long identifier);
169#endif
170
171    static void networkStateChanged(Page*);
172
173#if ENABLE(OFFLINE_WEB_APPLICATIONS)
174    static void updateApplicationCacheStatus(Frame*);
175#endif
176
177#if ENABLE(INSPECTOR)
178    static void bindInspectorAgent(Page* page, InspectorAgent* inspectorAgent) { inspectorAgents().set(page, inspectorAgent); }
179    static void unbindInspectorAgent(Page* page) { inspectorAgents().remove(page); }
180    static void frontendCreated() { s_frontendCounter += 1; }
181    static void frontendDeleted() { s_frontendCounter -= 1; }
182    static bool hasFrontends() { return s_frontendCounter; }
183    static bool hasFrontend(Page*);
184#else
185    static bool hasFrontends() { return false; }
186    static bool hasFrontend(Page*) { return false; }
187#endif
188
189private:
190#if ENABLE(INSPECTOR)
191    static void didClearWindowObjectInWorldImpl(InspectorAgent*, Frame*, DOMWrapperWorld*);
192    static void inspectedPageDestroyedImpl(InspectorAgent*);
193
194    static void willInsertDOMNodeImpl(InspectorAgent*, Node* node, Node* parent);
195    static void didInsertDOMNodeImpl(InspectorAgent*, Node*);
196    static void willRemoveDOMNodeImpl(InspectorAgent*, Node*);
197    static void didRemoveDOMNodeImpl(InspectorAgent*, Node*);
198    static void willModifyDOMAttrImpl(InspectorAgent*, Element*);
199    static void didModifyDOMAttrImpl(InspectorAgent*, Element*);
200    static void characterDataModifiedImpl(InspectorAgent*, CharacterData*);
201    static void didInvalidateStyleAttrImpl(InspectorAgent*, Node*);
202
203    static void mouseDidMoveOverElementImpl(InspectorAgent*, const HitTestResult&, unsigned modifierFlags);
204    static bool handleMousePressImpl(InspectorAgent*);
205
206    static void willSendXMLHttpRequestImpl(InspectorAgent*, const String& url);
207    static void didScheduleResourceRequestImpl(InspectorAgent*, const String& url);
208    static void didInstallTimerImpl(InspectorAgent*, int timerId, int timeout, bool singleShot);
209    static void didRemoveTimerImpl(InspectorAgent*, int timerId);
210
211    static InspectorInstrumentationCookie willCallFunctionImpl(InspectorAgent*, const String& scriptName, int scriptLine);
212    static void didCallFunctionImpl(const InspectorInstrumentationCookie&);
213    static InspectorInstrumentationCookie willChangeXHRReadyStateImpl(InspectorAgent*, XMLHttpRequest* request);
214    static void didChangeXHRReadyStateImpl(const InspectorInstrumentationCookie&);
215    static InspectorInstrumentationCookie willDispatchEventImpl(InspectorAgent*, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors);
216    static void didDispatchEventImpl(const InspectorInstrumentationCookie&);
217    static InspectorInstrumentationCookie willDispatchEventOnWindowImpl(InspectorAgent*, const Event& event, DOMWindow* window);
218    static void didDispatchEventOnWindowImpl(const InspectorInstrumentationCookie&);
219    static InspectorInstrumentationCookie willEvaluateScriptImpl(InspectorAgent*, const String& url, int lineNumber);
220    static void didEvaluateScriptImpl(const InspectorInstrumentationCookie&);
221    static InspectorInstrumentationCookie willFireTimerImpl(InspectorAgent*, int timerId);
222    static void didFireTimerImpl(const InspectorInstrumentationCookie&);
223    static InspectorInstrumentationCookie willLayoutImpl(InspectorAgent*);
224    static void didLayoutImpl(const InspectorInstrumentationCookie&);
225    static InspectorInstrumentationCookie willLoadXHRImpl(InspectorAgent*, XMLHttpRequest* request);
226    static void didLoadXHRImpl(const InspectorInstrumentationCookie&);
227    static InspectorInstrumentationCookie willPaintImpl(InspectorAgent*, const IntRect& rect);
228    static void didPaintImpl(const InspectorInstrumentationCookie&);
229    static InspectorInstrumentationCookie willRecalculateStyleImpl(InspectorAgent*);
230    static void didRecalculateStyleImpl(const InspectorInstrumentationCookie&);
231
232    static void applyUserAgentOverrideImpl(InspectorAgent*, String*);
233    static void willSendRequestImpl(InspectorAgent*, unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
234    static void markResourceAsCachedImpl(InspectorAgent*, unsigned long identifier);
235    static void didLoadResourceFromMemoryCacheImpl(InspectorAgent*, DocumentLoader*, const CachedResource*);
236    static InspectorInstrumentationCookie willReceiveResourceDataImpl(InspectorAgent*, unsigned long identifier);
237    static void didReceiveResourceDataImpl(const InspectorInstrumentationCookie&);
238    static InspectorInstrumentationCookie willReceiveResourceResponseImpl(InspectorAgent*, unsigned long identifier, const ResourceResponse&);
239    static void didReceiveResourceResponseImpl(const InspectorInstrumentationCookie&, unsigned long identifier, DocumentLoader*, const ResourceResponse&);
240    static void didReceiveResourceResponseButCanceledImpl(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
241    static void continueAfterXFrameOptionsDeniedImpl(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
242    static void continueWithPolicyDownloadImpl(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
243    static void continueWithPolicyIgnoreImpl(Frame*, DocumentLoader*, unsigned long identifier, const ResourceResponse&);
244    static void didReceiveContentLengthImpl(InspectorAgent*, unsigned long identifier, int dataLength, int encodedDataLength);
245    static void didFinishLoadingImpl(InspectorAgent*, unsigned long identifier, double finishTime);
246    static void didFailLoadingImpl(InspectorAgent*, unsigned long identifier, const ResourceError&);
247    static void resourceRetrievedByXMLHttpRequestImpl(InspectorAgent*, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber);
248    static void scriptImportedImpl(InspectorAgent*, unsigned long identifier, const String& sourceString);
249    static void domContentLoadedEventFiredImpl(InspectorAgent*, Frame*, const KURL&);
250    static void loadEventFiredImpl(InspectorAgent*, Frame*, const KURL&);
251    static void frameDetachedFromParentImpl(InspectorAgent*, Frame*);
252    static void didCommitLoadImpl(Page*, InspectorAgent*, DocumentLoader*);
253
254    static InspectorInstrumentationCookie willWriteHTMLImpl(InspectorAgent*, unsigned int length, unsigned int startLine);
255    static void didWriteHTMLImpl(const InspectorInstrumentationCookie&, unsigned int endLine);
256
257    static void addMessageToConsoleImpl(InspectorAgent*, MessageSource, MessageType, MessageLevel, const String& message, PassRefPtr<ScriptArguments>, PassRefPtr<ScriptCallStack>);
258    static void addMessageToConsoleImpl(InspectorAgent*, MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceID);
259    static void consoleCountImpl(InspectorAgent*, PassRefPtr<ScriptArguments>, PassRefPtr<ScriptCallStack>);
260    static void startConsoleTimingImpl(InspectorAgent*, const String& title);
261    static void stopConsoleTimingImpl(InspectorAgent*, const String& title, PassRefPtr<ScriptCallStack>);
262    static void consoleMarkTimelineImpl(InspectorAgent*, PassRefPtr<ScriptArguments>);
263
264#if ENABLE(JAVASCRIPT_DEBUGGER)
265    static void addStartProfilingMessageToConsoleImpl(InspectorAgent*, const String& title, unsigned lineNumber, const String& sourceURL);
266    static void addProfileImpl(InspectorAgent*, RefPtr<ScriptProfile>, PassRefPtr<ScriptCallStack>);
267    static String getCurrentUserInitiatedProfileNameImpl(InspectorAgent*, bool incrementProfileNumber);
268    static bool profilerEnabledImpl(InspectorAgent*);
269#endif
270
271#if ENABLE(DATABASE)
272    static void didOpenDatabaseImpl(InspectorAgent*, PassRefPtr<Database>, const String& domain, const String& name, const String& version);
273#endif
274
275#if ENABLE(DOM_STORAGE)
276    static void didUseDOMStorageImpl(InspectorAgent*, StorageArea*, bool isLocalStorage, Frame*);
277#endif
278
279#if ENABLE(WORKERS)
280    static void didCreateWorkerImpl(InspectorAgent*, intptr_t id, const String& url, bool isSharedWorker);
281    static void didDestroyWorkerImpl(InspectorAgent*, intptr_t id);
282#endif
283
284#if ENABLE(WEB_SOCKETS)
285    static void didCreateWebSocketImpl(InspectorAgent*, unsigned long identifier, const KURL& requestURL, const KURL& documentURL);
286    static void willSendWebSocketHandshakeRequestImpl(InspectorAgent*, unsigned long identifier, const WebSocketHandshakeRequest&);
287    static void didReceiveWebSocketHandshakeResponseImpl(InspectorAgent*, unsigned long identifier, const WebSocketHandshakeResponse&);
288    static void didCloseWebSocketImpl(InspectorAgent*, unsigned long identifier);
289#endif
290
291#if ENABLE(OFFLINE_WEB_APPLICATIONS)
292    static void networkStateChangedImpl(InspectorAgent*);
293    static void updateApplicationCacheStatusImpl(InspectorAgent*, Frame*);
294#endif
295
296    static InspectorAgent* inspectorAgentForFrame(Frame*);
297    static InspectorAgent* inspectorAgentForContext(ScriptExecutionContext*);
298    static InspectorAgent* inspectorAgentForPage(Page*);
299    static InspectorAgent* inspectorAgentWithFrontendForContext(ScriptExecutionContext*);
300    static InspectorAgent* inspectorAgentWithFrontendForDocument(Document*);
301    static InspectorAgent* inspectorAgentWithFrontendForFrame(Frame*);
302    static InspectorAgent* inspectorAgentWithFrontendForPage(Page*);
303
304    static bool hasFrontend(InspectorAgent*);
305    static void pauseOnNativeEventIfNeeded(InspectorAgent*, const String& categoryType, const String& eventName, bool synchronous);
306    static void cancelPauseOnNativeEvent(InspectorAgent*);
307    static InspectorTimelineAgent* retrieveTimelineAgent(InspectorAgent*);
308    static InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie&);
309    static InspectorResourceAgent* retrieveResourceAgent(InspectorAgent*);
310    static InspectorPageAgent* retrievePageAgent(InspectorAgent*);
311
312    static HashMap<Page*, InspectorAgent*>& inspectorAgents();
313    static int s_frontendCounter;
314#endif
315};
316
317inline void InspectorInstrumentation::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
318{
319#if ENABLE(INSPECTOR)
320    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
321        didClearWindowObjectInWorldImpl(inspectorAgent, frame, world);
322#endif
323}
324
325inline void InspectorInstrumentation::inspectedPageDestroyed(Page* page)
326{
327#if ENABLE(INSPECTOR)
328    if (InspectorAgent* inspectorAgent = inspectorAgentForPage(page))
329        inspectedPageDestroyedImpl(inspectorAgent);
330#endif
331}
332
333inline void InspectorInstrumentation::willInsertDOMNode(Document* document, Node* node, Node* parent)
334{
335#if ENABLE(INSPECTOR)
336    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
337        willInsertDOMNodeImpl(inspectorAgent, node, parent);
338#endif
339}
340
341inline void InspectorInstrumentation::didInsertDOMNode(Document* document, Node* node)
342{
343#if ENABLE(INSPECTOR)
344    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
345        didInsertDOMNodeImpl(inspectorAgent, node);
346#endif
347}
348
349inline void InspectorInstrumentation::willRemoveDOMNode(Document* document, Node* node)
350{
351#if ENABLE(INSPECTOR)
352    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document)) {
353        willRemoveDOMNodeImpl(inspectorAgent, node);
354        didRemoveDOMNodeImpl(inspectorAgent, node);
355    }
356#endif
357}
358
359inline void InspectorInstrumentation::willModifyDOMAttr(Document* document, Element* element)
360{
361#if ENABLE(INSPECTOR)
362    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
363        willModifyDOMAttrImpl(inspectorAgent, element);
364#endif
365}
366
367inline void InspectorInstrumentation::didModifyDOMAttr(Document* document, Element* element)
368{
369#if ENABLE(INSPECTOR)
370    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
371        didModifyDOMAttrImpl(inspectorAgent, element);
372#endif
373}
374
375inline void InspectorInstrumentation::didInvalidateStyleAttr(Document* document, Node* node)
376{
377#if ENABLE(INSPECTOR)
378    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
379        didInvalidateStyleAttrImpl(inspectorAgent, node);
380#endif
381}
382
383inline void InspectorInstrumentation::mouseDidMoveOverElement(Page* page, const HitTestResult& result, unsigned modifierFlags)
384{
385#if ENABLE(INSPECTOR)
386    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForPage(page))
387        mouseDidMoveOverElementImpl(inspectorAgent, result, modifierFlags);
388#endif
389}
390
391inline bool InspectorInstrumentation::handleMousePress(Page* page)
392{
393#if ENABLE(INSPECTOR)
394    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForPage(page))
395        return handleMousePressImpl(inspectorAgent);
396#endif
397    return false;
398}
399
400inline void InspectorInstrumentation::characterDataModified(Document* document, CharacterData* characterData)
401{
402#if ENABLE(INSPECTOR)
403    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
404        characterDataModifiedImpl(inspectorAgent, characterData);
405#endif
406}
407
408inline void InspectorInstrumentation::willSendXMLHttpRequest(ScriptExecutionContext* context, const String& url)
409{
410#if ENABLE(INSPECTOR)
411    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
412        willSendXMLHttpRequestImpl(inspectorAgent, url);
413#endif
414}
415
416inline void InspectorInstrumentation::didScheduleResourceRequest(Document* document, const String& url)
417{
418#if ENABLE(INSPECTOR)
419    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
420        didScheduleResourceRequestImpl(inspectorAgent, url);
421#endif
422}
423
424inline void InspectorInstrumentation::didInstallTimer(ScriptExecutionContext* context, int timerId, int timeout, bool singleShot)
425{
426#if ENABLE(INSPECTOR)
427    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
428        didInstallTimerImpl(inspectorAgent, timerId, timeout, singleShot);
429#endif
430}
431
432inline void InspectorInstrumentation::didRemoveTimer(ScriptExecutionContext* context, int timerId)
433{
434#if ENABLE(INSPECTOR)
435    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
436        didRemoveTimerImpl(inspectorAgent, timerId);
437#endif
438}
439
440
441inline InspectorInstrumentationCookie InspectorInstrumentation::willCallFunction(Frame* frame, const String& scriptName, int scriptLine)
442{
443#if ENABLE(INSPECTOR)
444    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
445        return willCallFunctionImpl(inspectorAgent, scriptName, scriptLine);
446#endif
447    return InspectorInstrumentationCookie();
448}
449
450inline void InspectorInstrumentation::didCallFunction(const InspectorInstrumentationCookie& cookie)
451{
452#if ENABLE(INSPECTOR)
453    if (hasFrontends() && cookie.first)
454        didCallFunctionImpl(cookie);
455#endif
456}
457
458inline InspectorInstrumentationCookie InspectorInstrumentation::willChangeXHRReadyState(ScriptExecutionContext* context, XMLHttpRequest* request)
459{
460#if ENABLE(INSPECTOR)
461    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
462        return willChangeXHRReadyStateImpl(inspectorAgent, request);
463#endif
464    return InspectorInstrumentationCookie();
465}
466
467inline void InspectorInstrumentation::didChangeXHRReadyState(const InspectorInstrumentationCookie& cookie)
468{
469#if ENABLE(INSPECTOR)
470    if (hasFrontends() && cookie.first)
471        didChangeXHRReadyStateImpl(cookie);
472#endif
473}
474
475inline InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEvent(Document* document, const Event& event, DOMWindow* window, Node* node, const Vector<EventContext>& ancestors)
476{
477#if ENABLE(INSPECTOR)
478    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
479        return willDispatchEventImpl(inspectorAgent, event, window, node, ancestors);
480#endif
481    return InspectorInstrumentationCookie();
482}
483
484inline void InspectorInstrumentation::didDispatchEvent(const InspectorInstrumentationCookie& cookie)
485{
486#if ENABLE(INSPECTOR)
487    if (hasFrontends() && cookie.first)
488        didDispatchEventImpl(cookie);
489#endif
490}
491
492inline InspectorInstrumentationCookie InspectorInstrumentation::willDispatchEventOnWindow(Frame* frame, const Event& event, DOMWindow* window)
493{
494#if ENABLE(INSPECTOR)
495    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
496        return willDispatchEventOnWindowImpl(inspectorAgent, event, window);
497#endif
498    return InspectorInstrumentationCookie();
499}
500
501inline void InspectorInstrumentation::didDispatchEventOnWindow(const InspectorInstrumentationCookie& cookie)
502{
503#if ENABLE(INSPECTOR)
504    if (hasFrontends() && cookie.first)
505        didDispatchEventOnWindowImpl(cookie);
506#endif
507}
508
509inline InspectorInstrumentationCookie InspectorInstrumentation::willEvaluateScript(Frame* frame, const String& url, int lineNumber)
510{
511#if ENABLE(INSPECTOR)
512    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
513        return willEvaluateScriptImpl(inspectorAgent, url, lineNumber);
514#endif
515    return InspectorInstrumentationCookie();
516}
517
518inline void InspectorInstrumentation::didEvaluateScript(const InspectorInstrumentationCookie& cookie)
519{
520#if ENABLE(INSPECTOR)
521    if (hasFrontends() && cookie.first)
522        didEvaluateScriptImpl(cookie);
523#endif
524}
525
526inline InspectorInstrumentationCookie InspectorInstrumentation::willFireTimer(ScriptExecutionContext* context, int timerId)
527{
528#if ENABLE(INSPECTOR)
529    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
530        return willFireTimerImpl(inspectorAgent, timerId);
531#endif
532    return InspectorInstrumentationCookie();
533}
534
535inline void InspectorInstrumentation::didFireTimer(const InspectorInstrumentationCookie& cookie)
536{
537#if ENABLE(INSPECTOR)
538    if (hasFrontends() && cookie.first)
539        didFireTimerImpl(cookie);
540#endif
541}
542
543inline InspectorInstrumentationCookie InspectorInstrumentation::willLayout(Frame* frame)
544{
545#if ENABLE(INSPECTOR)
546    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
547        return willLayoutImpl(inspectorAgent);
548#endif
549    return InspectorInstrumentationCookie();
550}
551
552inline void InspectorInstrumentation::didLayout(const InspectorInstrumentationCookie& cookie)
553{
554#if ENABLE(INSPECTOR)
555    if (hasFrontends() && cookie.first)
556        didLayoutImpl(cookie);
557#endif
558}
559
560inline InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHR(ScriptExecutionContext* context, XMLHttpRequest* request)
561{
562#if ENABLE(INSPECTOR)
563    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
564        return willLoadXHRImpl(inspectorAgent, request);
565#endif
566    return InspectorInstrumentationCookie();
567}
568
569inline void InspectorInstrumentation::didLoadXHR(const InspectorInstrumentationCookie& cookie)
570{
571#if ENABLE(INSPECTOR)
572    if (hasFrontends() && cookie.first)
573        didLoadXHRImpl(cookie);
574#endif
575}
576
577inline InspectorInstrumentationCookie InspectorInstrumentation::willPaint(Frame* frame, const IntRect& rect)
578{
579#if ENABLE(INSPECTOR)
580    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
581        return willPaintImpl(inspectorAgent, rect);
582#endif
583    return InspectorInstrumentationCookie();
584}
585
586inline void InspectorInstrumentation::didPaint(const InspectorInstrumentationCookie& cookie)
587{
588#if ENABLE(INSPECTOR)
589    if (hasFrontends() && cookie.first)
590        didPaintImpl(cookie);
591#endif
592}
593
594inline InspectorInstrumentationCookie InspectorInstrumentation::willRecalculateStyle(Document* document)
595{
596#if ENABLE(INSPECTOR)
597    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
598        return willRecalculateStyleImpl(inspectorAgent);
599#endif
600    return InspectorInstrumentationCookie();
601}
602
603inline void InspectorInstrumentation::didRecalculateStyle(const InspectorInstrumentationCookie& cookie)
604{
605#if ENABLE(INSPECTOR)
606    if (hasFrontends() && cookie.first)
607        didRecalculateStyleImpl(cookie);
608#endif
609}
610
611inline void InspectorInstrumentation::applyUserAgentOverride(Frame* frame, String* userAgent)
612{
613#if ENABLE(INSPECTOR)
614    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
615        applyUserAgentOverrideImpl(inspectorAgent, userAgent);
616#endif
617}
618
619inline void InspectorInstrumentation::willSendRequest(Frame* frame, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
620{
621#if ENABLE(INSPECTOR)
622    if (InspectorAgent* ic = inspectorAgentForFrame(frame))
623        willSendRequestImpl(ic, identifier, loader, request, redirectResponse);
624#endif
625}
626
627inline void InspectorInstrumentation::markResourceAsCached(Page* page, unsigned long identifier)
628{
629#if ENABLE(INSPECTOR)
630    markResourceAsCachedImpl(inspectorAgentForPage(page), identifier);
631#endif
632}
633
634inline void InspectorInstrumentation::didLoadResourceFromMemoryCache(Page* page, DocumentLoader* loader, const CachedResource* resource)
635{
636#if ENABLE(INSPECTOR)
637    didLoadResourceFromMemoryCacheImpl(inspectorAgentForPage(page), loader, resource);
638#endif
639}
640
641inline InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceData(Frame* frame, unsigned long identifier)
642{
643#if ENABLE(INSPECTOR)
644    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
645        return willReceiveResourceDataImpl(inspectorAgent, identifier);
646#endif
647    return InspectorInstrumentationCookie();
648}
649
650inline void InspectorInstrumentation::didReceiveResourceData(const InspectorInstrumentationCookie& cookie)
651{
652#if ENABLE(INSPECTOR)
653    if (hasFrontends() && cookie.first)
654        didReceiveResourceDataImpl(cookie);
655#endif
656}
657
658inline InspectorInstrumentationCookie InspectorInstrumentation::willReceiveResourceResponse(Frame* frame, unsigned long identifier, const ResourceResponse& response)
659{
660#if ENABLE(INSPECTOR)
661    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
662        return willReceiveResourceResponseImpl(inspectorAgent, identifier, response);
663#endif
664    return InspectorInstrumentationCookie();
665}
666
667inline void InspectorInstrumentation::didReceiveResourceResponse(const InspectorInstrumentationCookie& cookie, unsigned long identifier, DocumentLoader* loader, const ResourceResponse& response)
668{
669#if ENABLE(INSPECTOR)
670    // Call this unconditionally so that we're able to log to console with no front-end attached.
671    didReceiveResourceResponseImpl(cookie, identifier, loader, response);
672#endif
673}
674
675inline void InspectorInstrumentation::continueAfterXFrameOptionsDenied(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
676{
677#if ENABLE(INSPECTOR)
678    if (inspectorAgentWithFrontendForFrame(frame))
679        InspectorInstrumentation::continueAfterXFrameOptionsDeniedImpl(frame, loader, identifier, r);
680#endif
681}
682
683inline void InspectorInstrumentation::continueWithPolicyDownload(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
684{
685#if ENABLE(INSPECTOR)
686    if (inspectorAgentWithFrontendForFrame(frame))
687        InspectorInstrumentation::continueWithPolicyDownloadImpl(frame, loader, identifier, r);
688#endif
689}
690
691inline void InspectorInstrumentation::continueWithPolicyIgnore(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
692{
693#if ENABLE(INSPECTOR)
694    if (inspectorAgentWithFrontendForFrame(frame))
695        InspectorInstrumentation::continueWithPolicyIgnoreImpl(frame, loader, identifier, r);
696#endif
697}
698
699inline void InspectorInstrumentation::didReceiveContentLength(Frame* frame, unsigned long identifier, int dataLength, int encodedDataLength)
700{
701#if ENABLE(INSPECTOR)
702    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
703        didReceiveContentLengthImpl(inspectorAgent, identifier, dataLength, encodedDataLength);
704#endif
705}
706
707inline void InspectorInstrumentation::didFinishLoading(Frame* frame, unsigned long identifier, double finishTime)
708{
709#if ENABLE(INSPECTOR)
710    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
711        didFinishLoadingImpl(inspectorAgent, identifier, finishTime);
712#endif
713}
714
715inline void InspectorInstrumentation::didFailLoading(Frame* frame, unsigned long identifier, const ResourceError& error)
716{
717#if ENABLE(INSPECTOR)
718    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
719        didFailLoadingImpl(inspectorAgent, identifier, error);
720#endif
721}
722
723inline void InspectorInstrumentation::resourceRetrievedByXMLHttpRequest(ScriptExecutionContext* context, unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber)
724{
725#if ENABLE(INSPECTOR)
726    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
727        resourceRetrievedByXMLHttpRequestImpl(inspectorAgent, identifier, sourceString, url, sendURL, sendLineNumber);
728#endif
729}
730
731inline void InspectorInstrumentation::scriptImported(ScriptExecutionContext* context, unsigned long identifier, const String& sourceString)
732{
733#if ENABLE(INSPECTOR)
734    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
735        scriptImportedImpl(inspectorAgent, identifier, sourceString);
736#endif
737}
738
739inline void InspectorInstrumentation::domContentLoadedEventFired(Frame* frame, const KURL& url)
740{
741#if ENABLE(INSPECTOR)
742    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
743        domContentLoadedEventFiredImpl(inspectorAgent, frame, url);
744#endif
745}
746
747inline void InspectorInstrumentation::loadEventFired(Frame* frame, const KURL& url)
748{
749#if ENABLE(INSPECTOR)
750    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
751        loadEventFiredImpl(inspectorAgent, frame, url);
752#endif
753}
754
755inline void InspectorInstrumentation::frameDetachedFromParent(Frame* frame)
756{
757#if ENABLE(INSPECTOR)
758    if (InspectorAgent* inspectorAgent = inspectorAgentForFrame(frame))
759        frameDetachedFromParentImpl(inspectorAgent, frame);
760#endif
761}
762
763inline void InspectorInstrumentation::didCommitLoad(Frame* frame, DocumentLoader* loader)
764{
765#if ENABLE(INSPECTOR)
766    if (!frame)
767        return;
768    Page* page = frame->page();
769    if (!page)
770        return;
771    if (InspectorAgent* inspectorAgent = inspectorAgentForPage(page))
772        didCommitLoadImpl(page, inspectorAgent, loader);
773#endif
774}
775
776inline InspectorInstrumentationCookie InspectorInstrumentation::willWriteHTML(Document* document, unsigned int length, unsigned int startLine)
777{
778#if ENABLE(INSPECTOR)
779    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForDocument(document))
780        return willWriteHTMLImpl(inspectorAgent, length, startLine);
781#endif
782    return InspectorInstrumentationCookie();
783}
784
785inline void InspectorInstrumentation::didWriteHTML(const InspectorInstrumentationCookie& cookie, unsigned int endLine)
786{
787#if ENABLE(INSPECTOR)
788    if (hasFrontends() && cookie.first)
789        didWriteHTMLImpl(cookie, endLine);
790#endif
791}
792
793#if ENABLE(DOM_STORAGE)
794inline void InspectorInstrumentation::didUseDOMStorage(Page* page, StorageArea* storageArea, bool isLocalStorage, Frame* frame)
795{
796#if ENABLE(INSPECTOR)
797    if (InspectorAgent* inspectorAgent = inspectorAgentForPage(page))
798        didUseDOMStorageImpl(inspectorAgent, storageArea, isLocalStorage, frame);
799#endif
800}
801#endif
802
803#if ENABLE(WORKERS)
804inline void InspectorInstrumentation::didCreateWorker(ScriptExecutionContext* context, intptr_t id, const String& url, bool isSharedWorker)
805{
806#if ENABLE(INSPECTOR)
807    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
808        didCreateWorkerImpl(inspectorAgent, id, url, isSharedWorker);
809#endif
810}
811
812inline void InspectorInstrumentation::didDestroyWorker(ScriptExecutionContext* context, intptr_t id)
813{
814#if ENABLE(INSPECTOR)
815    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForContext(context))
816        didDestroyWorkerImpl(inspectorAgent, id);
817#endif
818}
819#endif
820
821
822#if ENABLE(WEB_SOCKETS)
823inline void InspectorInstrumentation::didCreateWebSocket(ScriptExecutionContext* context, unsigned long identifier, const KURL& requestURL, const KURL& documentURL)
824{
825#if ENABLE(INSPECTOR)
826    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
827        didCreateWebSocketImpl(inspectorAgent, identifier, requestURL, documentURL);
828#endif
829}
830
831inline void InspectorInstrumentation::willSendWebSocketHandshakeRequest(ScriptExecutionContext* context, unsigned long identifier, const WebSocketHandshakeRequest& request)
832{
833#if ENABLE(INSPECTOR)
834    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
835        willSendWebSocketHandshakeRequestImpl(inspectorAgent, identifier, request);
836#endif
837}
838
839inline void InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(ScriptExecutionContext* context, unsigned long identifier, const WebSocketHandshakeResponse& response)
840{
841#if ENABLE(INSPECTOR)
842    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
843        didReceiveWebSocketHandshakeResponseImpl(inspectorAgent, identifier, response);
844#endif
845}
846
847inline void InspectorInstrumentation::didCloseWebSocket(ScriptExecutionContext* context, unsigned long identifier)
848{
849#if ENABLE(INSPECTOR)
850    if (InspectorAgent* inspectorAgent = inspectorAgentForContext(context))
851        didCloseWebSocketImpl(inspectorAgent, identifier);
852#endif
853}
854#endif
855
856inline void InspectorInstrumentation::networkStateChanged(Page* page)
857{
858#if ENABLE(INSPECTOR) && ENABLE(OFFLINE_WEB_APPLICATIONS)
859    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForPage(page))
860        networkStateChangedImpl(inspectorAgent);
861#endif
862}
863
864#if ENABLE(OFFLINE_WEB_APPLICATIONS)
865inline void InspectorInstrumentation::updateApplicationCacheStatus(Frame* frame)
866{
867#if ENABLE(INSPECTOR)
868    if (InspectorAgent* inspectorAgent = inspectorAgentWithFrontendForFrame(frame))
869        updateApplicationCacheStatusImpl(inspectorAgent, frame);
870#endif
871}
872#endif
873
874#if ENABLE(INSPECTOR)
875inline bool InspectorInstrumentation::hasFrontend(Page* page)
876{
877    return inspectorAgentWithFrontendForPage(page);
878}
879#endif
880
881
882#if ENABLE(INSPECTOR)
883inline InspectorAgent* InspectorInstrumentation::inspectorAgentForContext(ScriptExecutionContext* context)
884{
885    if (context && context->isDocument())
886        return inspectorAgentForPage(static_cast<Document*>(context)->page());
887    return 0;
888}
889
890inline InspectorAgent* InspectorInstrumentation::inspectorAgentForFrame(Frame* frame)
891{
892    if (frame)
893        return inspectorAgentForPage(frame->page());
894    return 0;
895}
896
897inline InspectorAgent* InspectorInstrumentation::inspectorAgentForPage(Page* page)
898{
899    if (!page)
900        return 0;
901    return inspectorAgents().get(page);
902}
903
904inline InspectorAgent* InspectorInstrumentation::inspectorAgentWithFrontendForContext(ScriptExecutionContext* context)
905{
906    if (hasFrontends() && context && context->isDocument())
907        return inspectorAgentWithFrontendForPage(static_cast<Document*>(context)->page());
908    return 0;
909}
910
911inline InspectorAgent* InspectorInstrumentation::inspectorAgentWithFrontendForDocument(Document* document)
912{
913    if (hasFrontends() && document)
914        return inspectorAgentWithFrontendForPage(document->page());
915    return 0;
916}
917
918inline InspectorAgent* InspectorInstrumentation::inspectorAgentWithFrontendForFrame(Frame* frame)
919{
920    if (hasFrontends() && frame)
921        return inspectorAgentWithFrontendForPage(frame->page());
922    return 0;
923}
924
925inline InspectorAgent* InspectorInstrumentation::inspectorAgentWithFrontendForPage(Page* page)
926{
927    if (page) {
928        if (InspectorAgent* inspectorAgent = inspectorAgentForPage(page)) {
929            if (hasFrontend(inspectorAgent))
930                return inspectorAgent;
931        }
932    }
933    return 0;
934}
935
936#endif
937
938} // namespace WebCore
939
940#endif // !defined(InspectorInstrumentation_h)
941