2011-01-21 Pavel Podivilov <podivilov@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / InspectorController.h
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef InspectorController_h
30 #define InspectorController_h
31
32 #include "CharacterData.h"
33 #include "Console.h"
34 #include "Cookie.h"
35 #include "Page.h"
36 #include "PlatformString.h"
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/ListHashSet.h>
40 #include <wtf/RefCounted.h>
41 #include <wtf/Vector.h>
42 #include <wtf/text/StringHash.h>
43
44 namespace WebCore {
45
46 class CachedResource;
47 class CharacterData;
48 class Database;
49 class DOMWrapperWorld;
50 class Document;
51 class DocumentLoader;
52 class FloatRect;
53 class GraphicsContext;
54 class HTTPHeaderMap;
55 class HitTestResult;
56 class InjectedScript;
57 class InjectedScriptHost;
58 class InspectorArray;
59 class InspectorBackendDispatcher;
60 class InspectorBrowserDebuggerAgent;
61 class InspectorClient;
62 class InspectorConsoleAgent;
63 class InspectorCSSAgent;
64 class InspectorDOMAgent;
65 class InspectorDOMStorageAgent;
66 class InspectorDOMStorageResource;
67 class InspectorDatabaseAgent;
68 class InspectorDatabaseResource;
69 class InspectorDebuggerAgent;
70 class InspectorFrontend;
71 class InspectorFrontendClient;
72 class InspectorObject;
73 class InspectorProfilerAgent;
74 class InspectorResourceAgent;
75 class InspectorRuntimeAgent;
76 class InspectorSettings;
77 class InspectorState;
78 class InspectorStorageAgent;
79 class InspectorTimelineAgent;
80 class InspectorValue;
81 class InspectorWorkerResource;
82 class IntRect;
83 class KURL;
84 class Node;
85 class Page;
86 class ResourceRequest;
87 class ResourceResponse;
88 class ResourceError;
89 class ScriptArguments;
90 class ScriptCallStack;
91 class ScriptProfile;
92 class SharedBuffer;
93 class StorageArea;
94
95 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
96 class InspectorApplicationCacheAgent;
97 #endif
98
99 #if ENABLE(FILE_SYSTEM)
100 class InspectorFileSystemAgent;
101 #endif
102
103 #if ENABLE(WEB_SOCKETS)
104 class WebSocketHandshakeRequest;
105 class WebSocketHandshakeResponse;
106 #endif
107
108 class InspectorController {
109     WTF_MAKE_NONCOPYABLE(InspectorController); WTF_MAKE_FAST_ALLOCATED;
110 public:
111     static const char* const ConsolePanel;
112     static const char* const ElementsPanel;
113     static const char* const ProfilesPanel;
114     static const char* const ScriptsPanel;
115
116     InspectorController(Page*, InspectorClient*);
117     ~InspectorController();
118
119     InspectorBackendDispatcher* inspectorBackendDispatcher() { return m_inspectorBackendDispatcher.get(); }
120     InspectorClient* inspectorClient() { return m_client; }
121     InjectedScriptHost* injectedScriptHost() { return m_injectedScriptHost.get(); }
122
123     void inspectedPageDestroyed();
124
125     bool enabled() const;
126
127     Page* inspectedPage() const { return m_inspectedPage; }
128     KURL inspectedURL() const;
129     void reloadPage();
130
131     void restoreInspectorStateFromCookie(const String& inspectorCookie);
132
133     void inspect(Node*);
134     void highlight(Node*);
135     void hideHighlight();
136     void highlightDOMNode(long nodeId);
137     void hideDOMNodeHighlight() { hideHighlight(); }
138
139     void highlightFrame(unsigned long frameId);
140     void hideFrameHighlight() { hideHighlight(); }
141
142     void show();
143     void showPanel(const String&);
144     void close();
145
146     void connectFrontend();
147     void reuseFrontend();
148     void disconnectFrontend();
149     InspectorFrontend* frontend() const { return m_frontend.get(); }
150
151     InspectorResourceAgent* resourceAgent();
152
153     InspectorController* inspectorAgent() { return this; }
154     InspectorConsoleAgent* consoleAgent() { return m_consoleAgent.get(); }
155     InspectorCSSAgent* cssAgent() { return m_cssAgent.get(); }
156     InspectorDOMAgent* domAgent() { return m_domAgent.get(); }
157     InjectedScriptHost* injectedScriptAgent() { return m_injectedScriptHost.get(); }
158     InspectorRuntimeAgent* runtimeAgent() { return m_runtimeAgent.get(); }
159     InspectorTimelineAgent* timelineAgent() { return m_timelineAgent.get(); }
160 #if ENABLE(DATABASE)
161     InspectorDatabaseAgent* databaseAgent() { return m_databaseAgent.get(); }
162 #endif
163 #if ENABLE(DOM_STORAGE)
164     InspectorDOMStorageAgent* domStorageAgent() { return m_domStorageAgent.get(); }
165 #endif
166 #if ENABLE(FILE_SYSTEM)
167     InspectorFileSystemAgent* fileSystemAgent() { return m_fileSystemAgent.get(); }
168 #endif
169 #if ENABLE(JAVASCRIPT_DEBUGGER)
170     InspectorBrowserDebuggerAgent* browserDebuggerAgent() const { return m_browserDebuggerAgent.get(); }
171     InspectorDebuggerAgent* debuggerAgent() const { return m_debuggerAgent.get(); }
172     InspectorProfilerAgent* profilerAgent() const { return m_profilerAgent.get(); }
173 #endif
174 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
175     InspectorApplicationCacheAgent* applicationCacheAgent() { return m_applicationCacheAgent.get(); }
176 #endif
177
178
179     bool searchingForNodeInPage() const;
180     void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
181     bool handleMousePress();
182
183     void setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> client);
184     bool hasInspectorFrontendClient() const { return m_inspectorFrontendClient; }
185
186     void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld*);
187
188     void didCommitLoad(DocumentLoader*);
189
190     void setExtraHeaders(PassRefPtr<InspectorObject>);
191
192     void startTimelineProfiler();
193     void stopTimelineProfiler();
194
195     void getCookies(RefPtr<InspectorArray>* cookies, WTF::String* cookiesString);
196     void deleteCookie(const String& cookieName, const String& domain);
197
198     void mainResourceFiredLoadEvent(DocumentLoader*, const KURL&);
199     void mainResourceFiredDOMContentEvent(DocumentLoader*, const KURL&);
200
201 #if ENABLE(WORKERS)
202     enum WorkerAction { WorkerCreated, WorkerDestroyed };
203
204     void postWorkerNotificationToFrontend(const InspectorWorkerResource&, WorkerAction);
205     void didCreateWorker(intptr_t, const String& url, bool isSharedWorker);
206     void didDestroyWorker(intptr_t);
207 #endif
208
209 #if ENABLE(DATABASE)
210     void didOpenDatabase(PassRefPtr<Database>, const String& domain, const String& name, const String& version);
211 #endif
212
213 #if ENABLE(DOM_STORAGE)
214     void didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame);
215 #endif
216
217 #if ENABLE(WEB_SOCKETS)
218     void didCreateWebSocket(unsigned long identifier, const KURL& requestURL, const KURL& documentURL);
219     void willSendWebSocketHandshakeRequest(unsigned long identifier, const WebSocketHandshakeRequest&);
220     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const WebSocketHandshakeResponse&);
221     void didCloseWebSocket(unsigned long identifier);
222 #endif
223
224     bool hasFrontend() const { return m_frontend; }
225
226     void drawNodeHighlight(GraphicsContext&) const;
227     void openInInspectedWindow(const String& url);
228     void drawElementTitle(GraphicsContext&, const IntRect& boundingBox, const FloatRect& overlayRect, WebCore::Settings*) const;
229
230 #if ENABLE(JAVASCRIPT_DEBUGGER)
231     bool isRecordingUserInitiatedProfile() const;
232     void startProfiling() { startUserInitiatedProfiling(); }
233     void startUserInitiatedProfiling();
234     void stopProfiling() { stopUserInitiatedProfiling(); }
235     void stopUserInitiatedProfiling();
236     void enableProfiler(bool always = false, bool skipRecompile = false);
237     void disableProfiler(bool always = false);
238     bool profilerEnabled() const;
239
240     void showAndEnableDebugger();
241     void enableDebugger(bool always);
242     void disableDebugger(bool always = false);
243     bool debuggerEnabled() const { return m_debuggerAgent; }
244     void resume();
245
246     void setAllBrowserBreakpoints(PassRefPtr<InspectorObject>);
247 #endif
248
249     // Generic code called from custom implementations.
250     void evaluateForTestInFrontend(long testCallId, const String& script);
251
252     void addScriptToEvaluateOnLoad(const String& source);
253     void removeAllScriptsToEvaluateOnLoad();
254     void setInspectorExtensionAPI(const String& source);
255
256     bool inspectorStartsAttached();
257     void setInspectorStartsAttached(bool);
258     void setInspectorAttachedHeight(long height);
259     long inspectorAttachedHeight() const;
260
261     InspectorState* state() { return m_state.get(); }
262     InspectorSettings* settings() { return m_settings.get(); }
263
264     // InspectorAgent API
265     void getInspectorState(RefPtr<InspectorObject>* state);
266     void setMonitoringXHREnabled(bool enabled, bool* newState);
267     void populateScriptObjects();
268     // Following are used from InspectorBackend and internally.
269     void setSearchingForNode(bool enabled, bool* newState);
270     void didEvaluateForTestInFrontend(long callId, const String& jsonResult);
271
272     // InspectorInstrumentation API
273     void ensureSettingsLoaded();
274     void willSendRequest(ResourceRequest&);
275
276 private:
277     void pushDataCollectedOffline();
278     void restoreDebugger();
279     enum ProfilerRestoreAction {
280         ProfilerRestoreNoAction = 0,
281         ProfilerRestoreResetAgent = 1
282     };
283     void restoreProfiler(ProfilerRestoreAction action);
284     void unbindAllResources();
285     void setSearchingForNode(bool enabled);
286
287     void releaseFrontendLifetimeAgents();
288
289 #if ENABLE(JAVASCRIPT_DEBUGGER)
290     void toggleRecordButton(bool);
291 #endif
292
293     PassRefPtr<InspectorObject> buildObjectForCookie(const Cookie&);
294     PassRefPtr<InspectorArray> buildArrayForCookies(ListHashSet<Cookie>&);
295
296     void focusNode();
297     bool isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl);
298
299     Page* m_inspectedPage;
300     InspectorClient* m_client;
301     OwnPtr<InspectorFrontendClient> m_inspectorFrontendClient;
302     bool m_openingFrontend;
303     OwnPtr<InspectorFrontend> m_frontend;
304     OwnPtr<InspectorCSSAgent> m_cssAgent;
305     RefPtr<InspectorDOMAgent> m_domAgent;
306
307 #if ENABLE(DATABASE)
308     RefPtr<InspectorDatabaseAgent> m_databaseAgent;
309 #endif
310
311 #if ENABLE(DOM_STORAGE)
312     RefPtr<InspectorDOMStorageAgent> m_domStorageAgent;
313 #endif
314
315     OwnPtr<InspectorTimelineAgent> m_timelineAgent;
316     OwnPtr<InspectorState> m_state;
317     OwnPtr<InspectorSettings> m_settings;
318
319 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
320     OwnPtr<InspectorApplicationCacheAgent> m_applicationCacheAgent;
321 #endif
322     
323 #if ENABLE(FILE_SYSTEM)
324     RefPtr<InspectorFileSystemAgent> m_fileSystemAgent;
325 #endif 
326
327     RefPtr<Node> m_nodeToFocus;
328     RefPtr<InspectorResourceAgent> m_resourceAgent;
329     OwnPtr<InspectorRuntimeAgent> m_runtimeAgent;
330
331 #if ENABLE(DATABASE)
332     typedef HashMap<int, RefPtr<InspectorDatabaseResource> > DatabaseResourcesMap;
333     DatabaseResourcesMap m_databaseResources;
334 #endif
335 #if ENABLE(DOM_STORAGE)
336     typedef HashMap<int, RefPtr<InspectorDOMStorageResource> > DOMStorageResourcesMap;
337     DOMStorageResourcesMap m_domStorageResources;
338 #endif
339
340     String m_showAfterVisible;
341     RefPtr<Node> m_highlightedNode;
342     OwnPtr<InspectorBackendDispatcher> m_inspectorBackendDispatcher;
343     RefPtr<InjectedScriptHost> m_injectedScriptHost;
344     OwnPtr<InspectorConsoleAgent> m_consoleAgent;
345
346     Vector<pair<long, String> > m_pendingEvaluateTestCommands;
347     Vector<String> m_scriptsToEvaluateOnLoad;
348     String m_inspectorExtensionAPI;
349 #if ENABLE(JAVASCRIPT_DEBUGGER)
350     bool m_attachDebuggerWhenShown;
351     OwnPtr<InspectorDebuggerAgent> m_debuggerAgent;
352     OwnPtr<InspectorBrowserDebuggerAgent> m_browserDebuggerAgent;
353     OwnPtr<InspectorProfilerAgent> m_profilerAgent;
354 #endif
355     OwnPtr<HTTPHeaderMap> m_extraHeaders;
356 #if ENABLE(WORKERS)
357     typedef HashMap<intptr_t, RefPtr<InspectorWorkerResource> > WorkersMap;
358
359     WorkersMap m_workers;
360 #endif
361 };
362
363 } // namespace WebCore
364
365 #endif // !defined(InspectorController_h)