2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef InspectorController_h
30 #define InspectorController_h
32 #include "CharacterData.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>
53 class GraphicsContext;
56 class InjectedScriptHost;
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;
72 class InspectorStorageAgent;
73 class InspectorTimelineAgent;
75 class InspectorWorkerResource;
80 class ResourceRequest;
81 class ResourceResponse;
83 class ScriptCallStack;
89 #define LEGACY_RESOURCE_TRACKING_ENABLED 1
91 #if LEGACY_RESOURCE_TRACKING_ENABLED
92 class InspectorResource;
95 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
96 class InspectorApplicationCacheAgent;
99 #if ENABLE(WEB_SOCKETS)
100 class WebSocketHandshakeRequest;
101 class WebSocketHandshakeResponse;
104 class InspectorController : public Noncopyable {
106 #if LEGACY_RESOURCE_TRACKING_ENABLED
107 typedef HashMap<unsigned long, RefPtr<InspectorResource> > ResourcesMap;
108 typedef HashMap<RefPtr<Frame>, ResourcesMap*> FrameResourcesMap;
110 typedef HashMap<int, RefPtr<InspectorDatabaseResource> > DatabaseResourcesMap;
111 typedef HashMap<int, RefPtr<InspectorDOMStorageResource> > DOMStorageResourcesMap;
113 static const char* const ConsolePanel;
114 static const char* const ElementsPanel;
115 static const char* const ProfilesPanel;
116 static const char* const ScriptsPanel;
118 InspectorController(Page*, InspectorClient*);
119 ~InspectorController();
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(); }
126 void inspectedPageDestroyed();
128 bool enabled() const;
130 Page* inspectedPage() const { return m_inspectedPage; }
133 void restoreInspectorStateFromCookie(const String& inspectorCookie);
136 void highlight(Node*);
137 void hideHighlight();
138 void highlightDOMNode(long nodeId);
139 void hideDOMNodeHighlight() { hideHighlight(); }
142 void showPanel(const String&);
145 void connectFrontend();
146 void reuseFrontend();
147 void disconnectFrontend();
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; }
155 bool searchingForNodeInPage() const;
156 void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
157 void handleMousePress();
159 void setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> client);
160 bool hasInspectorFrontendClient() const { return m_inspectorFrontendClient; }
162 void inspectedWindowScriptObjectCleared(Frame*);
164 void didCommitLoad(DocumentLoader*);
165 void frameDetachedFromParent(Frame*);
166 void didLoadResourceFromMemoryCache(DocumentLoader*, const CachedResource*);
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);
178 void setResourceTrackingEnabled(bool enabled, bool always, bool* newState);
179 #if LEGACY_RESOURCE_TRACKING_ENABLED
180 void setResourceTrackingEnabled(bool enabled);
181 bool resourceTrackingEnabled() const;
184 void ensureSettingsLoaded();
186 void startTimelineProfiler();
187 void stopTimelineProfiler();
188 InspectorTimelineAgent* timelineAgent() { return m_timelineAgent.get(); }
190 void getCookies(RefPtr<InspectorArray>* cookies, WTF::String* cookiesString);
191 void deleteCookie(const String& cookieName, const String& domain);
193 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
194 InspectorApplicationCacheAgent* applicationCacheAgent() { return m_applicationCacheAgent.get(); }
197 void mainResourceFiredLoadEvent(DocumentLoader*, const KURL&);
198 void mainResourceFiredDOMContentEvent(DocumentLoader*, const KURL&);
201 enum WorkerAction { WorkerCreated, WorkerDestroyed };
203 void postWorkerNotificationToFrontend(const InspectorWorkerResource&, WorkerAction);
204 void didCreateWorker(intptr_t, const String& url, bool isSharedWorker);
205 void didDestroyWorker(intptr_t);
209 void didOpenDatabase(PassRefPtr<Database>, const String& domain, const String& name, const String& version);
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);
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);
225 #if LEGACY_RESOURCE_TRACKING_ENABLED
226 const ResourcesMap& resources() const { return m_resources; }
229 bool hasFrontend() const { return m_frontend; }
231 void drawNodeHighlight(GraphicsContext&) const;
232 void openInInspectedWindow(const String& url);
233 void drawElementTitle(GraphicsContext&, const IntRect& boundingBox, const FloatRect& overlayRect, WebCore::Settings*) const;
235 void count(const String& title, unsigned lineNumber, const String& sourceID);
237 void startTiming(const String& title);
238 bool stopTiming(const String& title, double& elapsed);
240 void startGroup(MessageSource source, ScriptCallStack* callFrame, bool collapsed = false);
241 void endGroup(MessageSource source, unsigned lineNumber, const String& sourceURL);
243 void markTimeline(const String& message);
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(); }
258 void enableDebugger();
259 void disableDebugger(bool always = false);
260 bool debuggerEnabled() const { return m_debuggerAgent; }
261 InspectorDebuggerAgent* debuggerAgent() const { return m_debuggerAgent.get(); }
264 void setNativeBreakpoint(PassRefPtr<InspectorObject> breakpoint, String* breakpointId);
265 void removeNativeBreakpoint(const String& breakpointId);
268 void evaluateForTestInFrontend(long testCallId, const String& script);
270 InjectedScript injectedScriptForNodeId(long id);
272 void addScriptToEvaluateOnLoad(const String& source);
273 void removeAllScriptsToEvaluateOnLoad();
274 void setInspectorExtensionAPI(const String& source);
276 bool inspectorStartsAttached();
277 void setInspectorStartsAttached(bool);
278 void setInspectorAttachedHeight(long height);
279 int inspectorAttachedHeight() const;
281 static const unsigned defaultAttachedHeight;
284 void getInspectorState(RefPtr<InspectorObject>* state);
285 void setConsoleMessagesEnabled(bool enabled);
287 friend class InspectorBackend;
288 friend class InspectorBackendDispatcher;
289 friend class InspectorInstrumentation;
290 friend class InjectedScriptHost;
292 void populateScriptObjects();
293 void restoreDebugger();
294 void restoreProfiler();
295 void unbindAllResources();
296 void setSearchingForNode(bool enabled);
298 // Following are used from InspectorBackend and internally.
299 void setSearchingForNode(bool enabled, bool* newState);
301 void setMonitoringXHREnabled(bool enabled, bool* newState);
302 InspectorDOMAgent* domAgent() { return m_domAgent.get(); }
303 void releaseFrontendLifetimeAgents();
305 #if ENABLE(JAVASCRIPT_DEBUGGER)
306 void toggleRecordButton(bool);
307 void enableDebuggerFromFrontend(bool always);
309 String findEventListenerBreakpoint(const String& eventName);
310 String findXHRBreakpoint(const String& url);
313 void selectDatabase(Database* database);
314 Database* databaseForId(long databaseId);
316 #if ENABLE(DOM_STORAGE)
317 InspectorDOMStorageResource* getDOMStorageResourceForId(long storageId);
320 PassRefPtr<InspectorObject> buildObjectForCookie(const Cookie&);
321 PassRefPtr<InspectorArray> buildArrayForCookies(ListHashSet<Cookie>&);
325 void addConsoleMessage(PassOwnPtr<ConsoleMessage>);
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); }
334 void getResourceContent(unsigned long identifier, bool encode, String* content);
336 bool isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl);
338 void didEvaluateForTestInFrontend(long callId, const String& jsonResult);
340 #if ENABLE(JAVASCRIPT_DEBUGGER)
341 friend class InspectorDebuggerAgent;
342 String breakpointsSettingKey();
343 PassRefPtr<InspectorValue> loadBreakpoints();
344 void saveBreakpoints(PassRefPtr<InspectorObject> breakpoints);
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;
358 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
359 OwnPtr<InspectorApplicationCacheAgent> m_applicationCacheAgent;
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;
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;
377 DatabaseResourcesMap m_databaseResources;
379 #if ENABLE(DOM_STORAGE)
380 DOMStorageResourcesMap m_domStorageResources;
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;
391 typedef HashMap<String, String> Settings;
392 mutable Settings m_settings;
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;
401 HashMap<String, String> m_nativeBreakpoints;
402 HashSet<String> m_eventListenerBreakpoints;
403 HashMap<String, String> m_XHRBreakpoints;
405 unsigned int m_lastBreakpointId;
407 OwnPtr<InspectorProfilerAgent> m_profilerAgent;
410 typedef HashMap<intptr_t, RefPtr<InspectorWorkerResource> > WorkersMap;
412 WorkersMap m_workers;
416 } // namespace WebCore
418 #endif // !defined(InspectorController_h)