eaaa1e503ef93e081a4f4f4ae1c360a199179064
[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     void reloadPage();
129
130     void restoreInspectorStateFromCookie(const String& inspectorCookie);
131
132     void inspect(Node*);
133     void highlight(Node*);
134     void hideHighlight();
135     void highlightDOMNode(long nodeId);
136     void hideDOMNodeHighlight() { hideHighlight(); }
137
138     void highlightFrame(unsigned long frameId);
139     void hideFrameHighlight() { hideHighlight(); }
140
141     void show();
142     void showPanel(const String&);
143     void close();
144
145     void connectFrontend();
146     void reuseFrontend();
147     void disconnectFrontend();
148     InspectorFrontend* frontend() const { return m_frontend.get(); }
149
150     InspectorResourceAgent* resourceAgent();
151
152     InspectorController* inspectorAgent() { return this; }
153     InspectorConsoleAgent* consoleAgent() { return m_consoleAgent.get(); }
154     InspectorCSSAgent* cssAgent() { return m_cssAgent.get(); }
155     InspectorDOMAgent* domAgent() { return m_domAgent.get(); }
156     InjectedScriptHost* injectedScriptAgent() { return m_injectedScriptHost.get(); }
157     InspectorRuntimeAgent* runtimeAgent() { return m_runtimeAgent.get(); }
158     InspectorTimelineAgent* timelineAgent() { return m_timelineAgent.get(); }
159 #if ENABLE(DATABASE)
160     InspectorDatabaseAgent* databaseAgent() { return m_databaseAgent.get(); }
161 #endif
162 #if ENABLE(DOM_STORAGE)
163     InspectorDOMStorageAgent* domStorageAgent() { return m_domStorageAgent.get(); }
164 #endif
165 #if ENABLE(FILE_SYSTEM)
166     InspectorFileSystemAgent* fileSystemAgent() { return m_fileSystemAgent.get(); }
167 #endif
168 #if ENABLE(JAVASCRIPT_DEBUGGER)
169     InspectorBrowserDebuggerAgent* browserDebuggerAgent() const { return m_browserDebuggerAgent.get(); }
170     InspectorDebuggerAgent* debuggerAgent() const { return m_debuggerAgent.get(); }
171     InspectorProfilerAgent* profilerAgent() const { return m_profilerAgent.get(); }
172 #endif
173 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
174     InspectorApplicationCacheAgent* applicationCacheAgent() { return m_applicationCacheAgent.get(); }
175 #endif
176
177
178     bool searchingForNodeInPage() const;
179     void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
180     bool handleMousePress();
181
182     void setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> client);
183     bool hasInspectorFrontendClient() const { return m_inspectorFrontendClient; }
184
185     void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld*);
186
187     void didCommitLoad(DocumentLoader*);
188
189     void setExtraHeaders(PassRefPtr<InspectorObject>);
190
191     void startTimelineProfiler();
192     void stopTimelineProfiler();
193
194     void getCookies(RefPtr<InspectorArray>* cookies, WTF::String* cookiesString);
195     void deleteCookie(const String& cookieName, const String& domain);
196
197     void mainResourceFiredLoadEvent(DocumentLoader*, const KURL&);
198     void mainResourceFiredDOMContentEvent(DocumentLoader*, const KURL&);
199
200 #if ENABLE(WORKERS)
201     enum WorkerAction { WorkerCreated, WorkerDestroyed };
202
203     void postWorkerNotificationToFrontend(const InspectorWorkerResource&, WorkerAction);
204     void didCreateWorker(intptr_t, const String& url, bool isSharedWorker);
205     void didDestroyWorker(intptr_t);
206 #endif
207
208 #if ENABLE(DATABASE)
209     void didOpenDatabase(PassRefPtr<Database>, const String& domain, const String& name, const String& version);
210 #endif
211
212 #if ENABLE(DOM_STORAGE)
213     void didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame);
214 #endif
215
216 #if ENABLE(WEB_SOCKETS)
217     void didCreateWebSocket(unsigned long identifier, const KURL& requestURL, const KURL& documentURL);
218     void willSendWebSocketHandshakeRequest(unsigned long identifier, const WebSocketHandshakeRequest&);
219     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const WebSocketHandshakeResponse&);
220     void didCloseWebSocket(unsigned long identifier);
221 #endif
222
223     bool hasFrontend() const { return m_frontend; }
224
225     void drawNodeHighlight(GraphicsContext&) const;
226     void openInInspectedWindow(const String& url);
227     void drawElementTitle(GraphicsContext&, const IntRect& boundingBox, const FloatRect& overlayRect, WebCore::Settings*) const;
228
229 #if ENABLE(JAVASCRIPT_DEBUGGER)
230     bool isRecordingUserInitiatedProfile() const;
231     void startProfiling() { startUserInitiatedProfiling(); }
232     void startUserInitiatedProfiling();
233     void stopProfiling() { stopUserInitiatedProfiling(); }
234     void stopUserInitiatedProfiling();
235     void enableProfiler(bool always = false, bool skipRecompile = false);
236     void disableProfiler(bool always = false);
237     bool profilerEnabled() const;
238
239     void showAndEnableDebugger();
240     void enableDebugger(bool always);
241     void disableDebugger(bool always = false);
242     bool debuggerEnabled() const { return m_debuggerAgent; }
243     void resume();
244
245     void setStickyBreakpoints(PassRefPtr<InspectorObject> breakpoints);
246 #endif
247
248     // Generic code called from custom implementations.
249     void evaluateForTestInFrontend(long testCallId, const String& script);
250
251     void addScriptToEvaluateOnLoad(const String& source);
252     void removeAllScriptsToEvaluateOnLoad();
253     void setInspectorExtensionAPI(const String& source);
254
255     bool inspectorStartsAttached();
256     void setInspectorStartsAttached(bool);
257     void setInspectorAttachedHeight(long height);
258     long inspectorAttachedHeight() const;
259
260     InspectorState* state() { return m_state.get(); }
261     InspectorSettings* settings() { return m_settings.get(); }
262
263     // InspectorAgent API
264     void getInspectorState(RefPtr<InspectorObject>* state);
265     void setMonitoringXHREnabled(bool enabled, bool* newState);
266     void populateScriptObjects();
267     // Following are used from InspectorBackend and internally.
268     void setSearchingForNode(bool enabled, bool* newState);
269     void didEvaluateForTestInFrontend(long callId, const String& jsonResult);
270
271     // InspectorInstrumentation API
272     void ensureSettingsLoaded();
273     void willSendRequest(ResourceRequest&);
274
275 private:
276     void pushDataCollectedOffline();
277     void restoreDebugger();
278     enum ProfilerRestoreAction {
279         ProfilerRestoreNoAction = 0,
280         ProfilerRestoreResetAgent = 1
281     };
282     void restoreProfiler(ProfilerRestoreAction action);
283     void unbindAllResources();
284     void setSearchingForNode(bool enabled);
285
286     void releaseFrontendLifetimeAgents();
287
288 #if ENABLE(JAVASCRIPT_DEBUGGER)
289     void toggleRecordButton(bool);
290     void restoreStickyBreakpoints();
291     void restoreStickyBreakpoint(PassRefPtr<InspectorObject> breakpoint);
292 #endif
293
294     PassRefPtr<InspectorObject> buildObjectForCookie(const Cookie&);
295     PassRefPtr<InspectorArray> buildArrayForCookies(ListHashSet<Cookie>&);
296
297     void focusNode();
298     bool isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl);
299
300     Page* m_inspectedPage;
301     InspectorClient* m_client;
302     OwnPtr<InspectorFrontendClient> m_inspectorFrontendClient;
303     bool m_openingFrontend;
304     OwnPtr<InspectorFrontend> m_frontend;
305     OwnPtr<InspectorCSSAgent> m_cssAgent;
306     RefPtr<InspectorDOMAgent> m_domAgent;
307
308 #if ENABLE(DATABASE)
309     RefPtr<InspectorDatabaseAgent> m_databaseAgent;
310 #endif
311
312 #if ENABLE(DOM_STORAGE)
313     RefPtr<InspectorDOMStorageAgent> m_domStorageAgent;
314 #endif
315
316     OwnPtr<InspectorTimelineAgent> m_timelineAgent;
317     OwnPtr<InspectorState> m_state;
318     OwnPtr<InspectorSettings> m_settings;
319
320 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
321     OwnPtr<InspectorApplicationCacheAgent> m_applicationCacheAgent;
322 #endif
323     
324 #if ENABLE(FILE_SYSTEM)
325     RefPtr<InspectorFileSystemAgent> m_fileSystemAgent;
326 #endif 
327
328     RefPtr<Node> m_nodeToFocus;
329     RefPtr<InspectorResourceAgent> m_resourceAgent;
330     OwnPtr<InspectorRuntimeAgent> m_runtimeAgent;
331
332 #if ENABLE(DATABASE)
333     typedef HashMap<int, RefPtr<InspectorDatabaseResource> > DatabaseResourcesMap;
334     DatabaseResourcesMap m_databaseResources;
335 #endif
336 #if ENABLE(DOM_STORAGE)
337     typedef HashMap<int, RefPtr<InspectorDOMStorageResource> > DOMStorageResourcesMap;
338     DOMStorageResourcesMap m_domStorageResources;
339 #endif
340
341     String m_showAfterVisible;
342     RefPtr<Node> m_highlightedNode;
343     OwnPtr<InspectorBackendDispatcher> m_inspectorBackendDispatcher;
344     RefPtr<InjectedScriptHost> m_injectedScriptHost;
345     OwnPtr<InspectorConsoleAgent> m_consoleAgent;
346
347     Vector<pair<long, String> > m_pendingEvaluateTestCommands;
348     Vector<String> m_scriptsToEvaluateOnLoad;
349     String m_inspectorExtensionAPI;
350 #if ENABLE(JAVASCRIPT_DEBUGGER)
351     bool m_attachDebuggerWhenShown;
352     OwnPtr<InspectorDebuggerAgent> m_debuggerAgent;
353     OwnPtr<InspectorBrowserDebuggerAgent> m_browserDebuggerAgent;
354     OwnPtr<InspectorProfilerAgent> m_profilerAgent;
355 #endif
356     OwnPtr<HTTPHeaderMap> m_extraHeaders;
357 #if ENABLE(WORKERS)
358     typedef HashMap<intptr_t, RefPtr<InspectorWorkerResource> > WorkersMap;
359
360     WorkersMap m_workers;
361 #endif
362 };
363
364 } // namespace WebCore
365
366 #endif // !defined(InspectorController_h)