OSDN Git Service

80d094f1923cc47bb04326af25c091003f0db164
[android-x86/external-webkit.git] / 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 ConsoleMessage;
49 class Database;
50 class Document;
51 class DocumentLoader;
52 class FloatRect;
53 class GraphicsContext;
54 class HitTestResult;
55 class InjectedScript;
56 class InjectedScriptHost;
57 class InspectorArray;
58 class InspectorBackend;
59 class InspectorBackendDispatcher;
60 class InspectorClient;
61 class InspectorCSSStore;
62 class InspectorDOMAgent;
63 class InspectorDOMStorageResource;
64 class InspectorDatabaseResource;
65 class InspectorDebuggerAgent;
66 class InspectorFrontend;
67 class InspectorFrontendClient;
68 class InspectorObject;
69 class InspectorProfilerAgent;
70 class InspectorResourceAgent;
71 class InspectorState;
72 class InspectorStorageAgent;
73 class InspectorTimelineAgent;
74 class InspectorValue;
75 class InspectorWorkerResource;
76 class IntRect;
77 class KURL;
78 class Node;
79 class Page;
80 class ResourceRequest;
81 class ResourceResponse;
82 class ResourceError;
83 class ScriptCallStack;
84 class ScriptProfile;
85 class SharedBuffer;
86 class Storage;
87 class StorageArea;
88
89 #define LEGACY_RESOURCE_TRACKING_ENABLED 1
90
91 #if LEGACY_RESOURCE_TRACKING_ENABLED
92 class InspectorResource;
93 #endif
94
95 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
96 class InspectorApplicationCacheAgent;
97 #endif
98
99 #if ENABLE(WEB_SOCKETS)
100 class WebSocketHandshakeRequest;
101 class WebSocketHandshakeResponse;
102 #endif
103
104 class InspectorController : public Noncopyable {
105 public:
106 #if LEGACY_RESOURCE_TRACKING_ENABLED
107     typedef HashMap<unsigned long, RefPtr<InspectorResource> > ResourcesMap;
108     typedef HashMap<RefPtr<Frame>, ResourcesMap*> FrameResourcesMap;
109 #endif
110     typedef HashMap<int, RefPtr<InspectorDatabaseResource> > DatabaseResourcesMap;
111     typedef HashMap<int, RefPtr<InspectorDOMStorageResource> > DOMStorageResourcesMap;
112
113     static const char* const ConsolePanel;
114     static const char* const ElementsPanel;
115     static const char* const ProfilesPanel;
116     static const char* const ScriptsPanel;
117
118     InspectorController(Page*, InspectorClient*);
119     ~InspectorController();
120
121     InspectorBackend* inspectorBackend() { return m_inspectorBackend.get(); }
122     InspectorBackendDispatcher* inspectorBackendDispatcher() { return m_inspectorBackendDispatcher.get(); }
123     InspectorClient* inspectorClient() { return m_client; }
124     InjectedScriptHost* injectedScriptHost() { return m_injectedScriptHost.get(); }
125
126     void inspectedPageDestroyed();
127
128     bool enabled() const;
129
130     Page* inspectedPage() const { return m_inspectedPage; }
131     void reloadPage();
132
133     void restoreInspectorStateFromCookie(const String& inspectorCookie);
134
135     void inspect(Node*);
136     void highlight(Node*);
137     void hideHighlight();
138     void highlightDOMNode(long nodeId);
139     void hideDOMNodeHighlight() { hideHighlight(); }
140
141     void show();
142     void showPanel(const String&);
143     void close();
144
145     void connectFrontend();
146     void reuseFrontend();
147     void disconnectFrontend();
148
149     void setConsoleMessagesEnabled(bool enabled, bool* newState);
150     void addMessageToConsole(MessageSource, MessageType, MessageLevel, ScriptCallStack*, const String& message);
151     void addMessageToConsole(MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceID);
152     void clearConsoleMessages();
153     const Vector<OwnPtr<ConsoleMessage> >& consoleMessages() const { return m_consoleMessages; }
154
155     bool searchingForNodeInPage() const;
156     void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
157     void handleMousePress();
158
159     void setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> client);
160     bool hasInspectorFrontendClient() const { return m_inspectorFrontendClient; }
161
162     void inspectedWindowScriptObjectCleared(Frame*);
163
164     void didCommitLoad(DocumentLoader*);
165     void frameDetachedFromParent(Frame*);
166     void didLoadResourceFromMemoryCache(DocumentLoader*, const CachedResource*);
167
168     void identifierForInitialRequest(unsigned long identifier, DocumentLoader*, const ResourceRequest&);
169     void willSendRequest(unsigned long identifier, ResourceRequest&, const ResourceResponse& redirectResponse);
170     void markResourceAsCached(unsigned long identifier);
171     void didReceiveResponse(unsigned long identifier, DocumentLoader*, const ResourceResponse&);
172     void didReceiveContentLength(unsigned long identifier, int lengthReceived);
173     void didFinishLoading(unsigned long identifier, double finishTime);
174     void didFailLoading(unsigned long identifier, const ResourceError&);
175     void resourceRetrievedByXMLHttpRequest(unsigned long identifier, const String& sourceString, const String& url, const String& sendURL, unsigned sendLineNumber);
176     void scriptImported(unsigned long identifier, const String& sourceString);
177
178     void setResourceTrackingEnabled(bool enabled, bool always, bool* newState);
179 #if LEGACY_RESOURCE_TRACKING_ENABLED
180     void setResourceTrackingEnabled(bool enabled);
181     bool resourceTrackingEnabled() const;
182 #endif
183
184     void ensureSettingsLoaded();
185
186     void startTimelineProfiler();
187     void stopTimelineProfiler();
188     InspectorTimelineAgent* timelineAgent() { return m_timelineAgent.get(); }
189
190     void getCookies(RefPtr<InspectorArray>* cookies, WTF::String* cookiesString);
191     void deleteCookie(const String& cookieName, const String& domain);
192
193 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
194     InspectorApplicationCacheAgent* applicationCacheAgent() { return m_applicationCacheAgent.get(); }
195 #endif
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 #if ENABLE(DOM_STORAGE)
212     void didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame);
213     void selectDOMStorage(Storage* storage);
214     void getDOMStorageEntries(long storageId, RefPtr<InspectorArray>* entries);
215     void setDOMStorageItem(long storageId, const String& key, const String& value, bool* success);
216     void removeDOMStorageItem(long storageId, const String& key, bool* success);
217 #endif
218 #if ENABLE(WEB_SOCKETS)
219     void didCreateWebSocket(unsigned long identifier, const KURL& requestURL, const KURL& documentURL);
220     void willSendWebSocketHandshakeRequest(unsigned long identifier, const WebSocketHandshakeRequest&);
221     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, const WebSocketHandshakeResponse&);
222     void didCloseWebSocket(unsigned long identifier);
223 #endif
224
225 #if LEGACY_RESOURCE_TRACKING_ENABLED
226     const ResourcesMap& resources() const { return m_resources; }
227 #endif
228
229     bool hasFrontend() const { return m_frontend; }
230
231     void drawNodeHighlight(GraphicsContext&) const;
232     void openInInspectedWindow(const String& url);
233     void drawElementTitle(GraphicsContext&, const IntRect& boundingBox, const FloatRect& overlayRect, WebCore::Settings*) const;
234
235     void count(const String& title, unsigned lineNumber, const String& sourceID);
236
237     void startTiming(const String& title);
238     bool stopTiming(const String& title, double& elapsed);
239
240     void startGroup(MessageSource source, ScriptCallStack* callFrame, bool collapsed = false);
241     void endGroup(MessageSource source, unsigned lineNumber, const String& sourceURL);
242
243     void markTimeline(const String& message);
244
245 #if ENABLE(JAVASCRIPT_DEBUGGER)
246     void addProfile(PassRefPtr<ScriptProfile>, unsigned lineNumber, const String& sourceURL);
247     void addProfileFinishedMessageToConsole(PassRefPtr<ScriptProfile>, unsigned lineNumber, const String& sourceURL);
248     void addStartProfilingMessageToConsole(const String& title, unsigned lineNumber, const String& sourceURL);
249     bool isRecordingUserInitiatedProfile() const;
250     String getCurrentUserInitiatedProfileName(bool incrementProfileNumber = false);
251     void startUserInitiatedProfiling();
252     void stopUserInitiatedProfiling();
253     void enableProfiler(bool always = false, bool skipRecompile = false);
254     void disableProfiler(bool always = false);
255     bool profilerEnabled() const;
256     InspectorProfilerAgent* profilerAgent() const { return m_profilerAgent.get(); }
257
258     void enableDebugger();
259     void disableDebugger(bool always = false);
260     bool debuggerEnabled() const { return m_debuggerAgent; }
261     InspectorDebuggerAgent* debuggerAgent() const { return m_debuggerAgent.get(); }
262     void resume();
263
264     void setNativeBreakpoint(PassRefPtr<InspectorObject> breakpoint, String* breakpointId);
265     void removeNativeBreakpoint(const String& breakpointId);
266 #endif
267
268     void evaluateForTestInFrontend(long testCallId, const String& script);
269
270     InjectedScript injectedScriptForNodeId(long id);
271
272     void addScriptToEvaluateOnLoad(const String& source);
273     void removeAllScriptsToEvaluateOnLoad();
274     void setInspectorExtensionAPI(const String& source);
275
276     bool inspectorStartsAttached();
277     void setInspectorStartsAttached(bool);
278     void setInspectorAttachedHeight(long height);
279     int inspectorAttachedHeight() const;
280
281     static const unsigned defaultAttachedHeight;
282
283 private:
284     void getInspectorState(RefPtr<InspectorObject>* state);
285     void setConsoleMessagesEnabled(bool enabled);
286
287     friend class InspectorBackend;
288     friend class InspectorBackendDispatcher;
289     friend class InspectorInstrumentation;
290     friend class InjectedScriptHost;
291
292     void populateScriptObjects();
293     void restoreDebugger();
294     void restoreProfiler();
295     void unbindAllResources();
296     void setSearchingForNode(bool enabled);
297
298     // Following are used from InspectorBackend and internally.
299     void setSearchingForNode(bool enabled, bool* newState);
300
301     void setMonitoringXHREnabled(bool enabled, bool* newState);
302     InspectorDOMAgent* domAgent() { return m_domAgent.get(); }
303     void releaseFrontendLifetimeAgents();
304
305 #if ENABLE(JAVASCRIPT_DEBUGGER)
306     void toggleRecordButton(bool);
307     void enableDebuggerFromFrontend(bool always);
308
309     String findEventListenerBreakpoint(const String& eventName);
310     String findXHRBreakpoint(const String& url);
311 #endif
312 #if ENABLE(DATABASE)
313     void selectDatabase(Database* database);
314     Database* databaseForId(long databaseId);
315 #endif
316 #if ENABLE(DOM_STORAGE)
317     InspectorDOMStorageResource* getDOMStorageResourceForId(long storageId);
318 #endif
319
320     PassRefPtr<InspectorObject> buildObjectForCookie(const Cookie&);
321     PassRefPtr<InspectorArray> buildArrayForCookies(ListHashSet<Cookie>&);
322
323     void focusNode();
324
325     void addConsoleMessage(PassOwnPtr<ConsoleMessage>);
326
327 #if LEGACY_RESOURCE_TRACKING_ENABLED
328     void addResource(InspectorResource*);
329     void removeResource(InspectorResource*);
330     InspectorResource* getTrackedResource(unsigned long identifier);
331     void pruneResources(ResourcesMap*, DocumentLoader* loaderToKeep = 0);
332     void removeAllResources(ResourcesMap* map) { pruneResources(map); }
333 #endif
334     void getResourceContent(unsigned long identifier, bool encode, String* content);
335
336     bool isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl);
337
338     void didEvaluateForTestInFrontend(long callId, const String& jsonResult);
339
340 #if ENABLE(JAVASCRIPT_DEBUGGER)
341     friend class InspectorDebuggerAgent;
342     String breakpointsSettingKey();
343     PassRefPtr<InspectorValue> loadBreakpoints();
344     void saveBreakpoints(PassRefPtr<InspectorObject> breakpoints);
345 #endif
346
347     Page* m_inspectedPage;
348     InspectorClient* m_client;
349     OwnPtr<InspectorFrontendClient> m_inspectorFrontendClient;
350     bool m_openingFrontend;
351     OwnPtr<InspectorFrontend> m_frontend;
352     RefPtr<InspectorDOMAgent> m_domAgent;
353     RefPtr<InspectorStorageAgent> m_storageAgent;
354     OwnPtr<InspectorCSSStore> m_cssStore;
355     OwnPtr<InspectorTimelineAgent> m_timelineAgent;
356     OwnPtr<InspectorState> m_state;
357
358 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
359     OwnPtr<InspectorApplicationCacheAgent> m_applicationCacheAgent;
360 #endif
361     RefPtr<Node> m_nodeToFocus;
362 #if LEGACY_RESOURCE_TRACKING_ENABLED
363     RefPtr<InspectorResource> m_mainResource;
364     ResourcesMap m_resources;
365     HashSet<String> m_knownResources;
366     FrameResourcesMap m_frameResources;
367 #endif
368     RefPtr<InspectorResourceAgent> m_resourceAgent;
369     unsigned long m_mainResourceIdentifier;
370     double m_loadEventTime;
371     double m_domContentEventTime;
372     Vector<OwnPtr<ConsoleMessage> > m_consoleMessages;
373     unsigned m_expiredConsoleMessageCount;
374     HashMap<String, double> m_times;
375     HashMap<String, unsigned> m_counts;
376 #if ENABLE(DATABASE)
377     DatabaseResourcesMap m_databaseResources;
378 #endif
379 #if ENABLE(DOM_STORAGE)
380     DOMStorageResourcesMap m_domStorageResources;
381 #endif
382     String m_showAfterVisible;
383     RefPtr<Node> m_highlightedNode;
384     unsigned m_groupLevel;
385     ConsoleMessage* m_previousMessage;
386     bool m_settingsLoaded;
387     RefPtr<InspectorBackend> m_inspectorBackend;
388     OwnPtr<InspectorBackendDispatcher> m_inspectorBackendDispatcher;
389     RefPtr<InjectedScriptHost> m_injectedScriptHost;
390
391     typedef HashMap<String, String> Settings;
392     mutable Settings m_settings;
393
394     Vector<pair<long, String> > m_pendingEvaluateTestCommands;
395     Vector<String> m_scriptsToEvaluateOnLoad;
396     String m_inspectorExtensionAPI;
397 #if ENABLE(JAVASCRIPT_DEBUGGER)
398     bool m_attachDebuggerWhenShown;
399     OwnPtr<InspectorDebuggerAgent> m_debuggerAgent;
400
401     HashMap<String, String> m_nativeBreakpoints;
402     HashSet<String> m_eventListenerBreakpoints;
403     HashMap<String, String> m_XHRBreakpoints;
404
405     unsigned int m_lastBreakpointId;
406
407     OwnPtr<InspectorProfilerAgent> m_profilerAgent;
408 #endif
409 #if ENABLE(WORKERS)
410     typedef HashMap<intptr_t, RefPtr<InspectorWorkerResource> > WorkersMap;
411
412     WorkersMap m_workers;
413 #endif
414 };
415
416 } // namespace WebCore
417
418 #endif // !defined(InspectorController_h)