1/*
2 * Copyright (C) 2011 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 InspectorPageAgent_h
32#define InspectorPageAgent_h
33
34#if ENABLE(INSPECTOR)
35
36#include "DeviceOrientationData.h"
37#include "Frame.h"
38#include "GeolocationPosition.h"
39#include "InspectorBaseAgent.h"
40#include "InspectorFrontend.h"
41#include <wtf/HashMap.h>
42#include <wtf/RefCounted.h>
43#include <wtf/Vector.h>
44#include <wtf/text/WTFString.h>
45
46namespace WebCore {
47
48class CachedResource;
49class DOMWrapperWorld;
50class DocumentLoader;
51class Frame;
52class Frontend;
53class GraphicsContext;
54class InjectedScriptManager;
55class InspectorAgent;
56class InspectorArray;
57class InspectorClient;
58class InspectorObject;
59class InspectorOverlay;
60class InspectorState;
61class InstrumentingAgents;
62class KURL;
63class Page;
64class RegularExpression;
65class SharedBuffer;
66class TextResourceDecoder;
67
68typedef String ErrorString;
69
70class InspectorPageAgent : public InspectorBaseAgent<InspectorPageAgent>, public InspectorBackendDispatcher::PageCommandHandler {
71    WTF_MAKE_NONCOPYABLE(InspectorPageAgent);
72public:
73    enum ResourceType {
74        DocumentResource,
75        StylesheetResource,
76        ImageResource,
77        FontResource,
78        ScriptResource,
79        XHRResource,
80        WebSocketResource,
81        OtherResource
82    };
83
84    static PassOwnPtr<InspectorPageAgent> create(InstrumentingAgents*, Page*, InspectorAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorClient*, InspectorOverlay*);
85
86    static bool cachedResourceContent(CachedResource*, String* result, bool* base64Encoded);
87    static bool sharedBufferContent(PassRefPtr<SharedBuffer>, const String& textEncodingName, bool withBase64Encode, String* result);
88    static void resourceContent(ErrorString*, Frame*, const KURL&, String* result, bool* base64Encoded);
89    static String sourceMapURLForResource(CachedResource*);
90
91    static PassRefPtr<SharedBuffer> resourceData(Frame*, const KURL&, String* textEncodingName);
92    static CachedResource* cachedResource(Frame*, const KURL&);
93    static TypeBuilder::Page::ResourceType::Enum resourceTypeJson(ResourceType);
94    static ResourceType cachedResourceType(const CachedResource&);
95    static TypeBuilder::Page::ResourceType::Enum cachedResourceTypeJson(const CachedResource&);
96
97    // Page API for InspectorFrontend
98    virtual void enable(ErrorString*);
99    virtual void disable(ErrorString*);
100    virtual void addScriptToEvaluateOnLoad(ErrorString*, const String& source, String* result);
101    virtual void removeScriptToEvaluateOnLoad(ErrorString*, const String& identifier);
102    virtual void reload(ErrorString*, const bool* optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad, const String* optionalScriptPreprocessor);
103    virtual void navigate(ErrorString*, const String& url);
104    virtual void getCookies(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::Page::Cookie> >& cookies, WTF::String* cookiesString);
105    virtual void deleteCookie(ErrorString*, const String& cookieName, const String& url);
106    virtual void getResourceTree(ErrorString*, RefPtr<TypeBuilder::Page::FrameResourceTree>&);
107    virtual void getResourceContent(ErrorString*, const String& frameId, const String& url, String* content, bool* base64Encoded);
108    virtual void searchInResource(ErrorString*, const String& frameId, const String& url, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> >&);
109    virtual void searchInResources(ErrorString*, const String&, const bool* caseSensitive, const bool* isRegex, RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchResult> >&);
110    virtual void setDocumentContent(ErrorString*, const String& frameId, const String& html);
111    virtual void canOverrideDeviceMetrics(ErrorString*, bool*);
112    virtual void setDeviceMetricsOverride(ErrorString*, int width, int height, double fontScaleFactor, bool fitWindow);
113    virtual void setShowPaintRects(ErrorString*, bool show);
114    virtual void canShowDebugBorders(ErrorString*, bool*);
115    virtual void setShowDebugBorders(ErrorString*, bool show);
116    virtual void canShowFPSCounter(ErrorString*, bool*);
117    virtual void setShowFPSCounter(ErrorString*, bool show);
118    virtual void canContinuouslyPaint(ErrorString*, bool*);
119    virtual void setContinuousPaintingEnabled(ErrorString*, bool enabled);
120    virtual void getScriptExecutionStatus(ErrorString*, PageCommandHandler::Result::Enum*);
121    virtual void setScriptExecutionDisabled(ErrorString*, bool);
122    virtual void setGeolocationOverride(ErrorString*, const double*, const double*, const double*);
123    virtual void clearGeolocationOverride(ErrorString*);
124    virtual void canOverrideGeolocation(ErrorString*, bool* out_param);
125    virtual void setDeviceOrientationOverride(ErrorString*, double, double, double);
126    virtual void clearDeviceOrientationOverride(ErrorString*);
127    virtual void canOverrideDeviceOrientation(ErrorString*, bool*);
128    virtual void setTouchEmulationEnabled(ErrorString*, bool);
129    virtual void setEmulatedMedia(ErrorString*, const String&);
130    virtual void getCompositingBordersVisible(ErrorString*, bool* out_param);
131    virtual void setCompositingBordersVisible(ErrorString*, bool);
132    virtual void captureScreenshot(ErrorString*, String* data);
133    virtual void handleJavaScriptDialog(ErrorString*, bool accept, const String* promptText);
134
135    // Geolocation override helpers.
136    GeolocationPosition* overrideGeolocationPosition(GeolocationPosition*);
137
138    // DeviceOrientation helper
139    DeviceOrientationData* overrideDeviceOrientation(DeviceOrientationData*);
140
141    // InspectorInstrumentation API
142    void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld*);
143    void domContentEventFired();
144    void loadEventFired();
145    void frameNavigated(DocumentLoader*);
146    void frameDetached(Frame*);
147    void loaderDetachedFromFrame(DocumentLoader*);
148    void frameStartedLoading(Frame*);
149    void frameStoppedLoading(Frame*);
150    void frameScheduledNavigation(Frame*, double delay);
151    void frameClearedScheduledNavigation(Frame*);
152    void willRunJavaScriptDialog(const String& message);
153    void didRunJavaScriptDialog();
154    void applyScreenWidthOverride(long*);
155    void applyScreenHeightOverride(long*);
156    void applyEmulatedMedia(String*);
157    void didPaint(GraphicsContext*, const LayoutRect&);
158    void didLayout();
159    void didScroll();
160    void didRecalculateStyle();
161    void scriptsEnabled(bool isEnabled);
162
163    // Inspector Controller API
164    virtual void setFrontend(InspectorFrontend*);
165    virtual void clearFrontend();
166    virtual void restore();
167
168    void webViewResized(const IntSize&);
169
170    // Cross-agents API
171    Page* page() { return m_page; }
172    Frame* mainFrame();
173    String createIdentifier();
174    Frame* frameForId(const String& frameId);
175    String frameId(Frame*);
176    bool hasIdForFrame(Frame*) const;
177    String loaderId(DocumentLoader*);
178    Frame* findFrameWithSecurityOrigin(const String& originRawString);
179    Frame* assertFrame(ErrorString*, const String& frameId);
180    String scriptPreprocessor() { return m_scriptPreprocessor; }
181    static DocumentLoader* assertDocumentLoader(ErrorString*, Frame*);
182
183private:
184    InspectorPageAgent(InstrumentingAgents*, Page*, InspectorAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorClient*, InspectorOverlay*);
185    bool deviceMetricsChanged(int width, int height, double fontScaleFactor, bool fitWindow);
186    void updateViewMetrics(int, int, double, bool);
187#if ENABLE(TOUCH_EVENTS)
188    void updateTouchEventEmulationInPage(bool);
189#endif
190
191    static bool mainResourceContent(Frame*, bool withBase64Encode, String* result);
192    static bool dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result);
193
194    PassRefPtr<TypeBuilder::Page::Frame> buildObjectForFrame(Frame*);
195    PassRefPtr<TypeBuilder::Page::FrameResourceTree> buildObjectForFrameTree(Frame*);
196    Page* m_page;
197    InspectorAgent* m_inspectorAgent;
198    InjectedScriptManager* m_injectedScriptManager;
199    InspectorClient* m_client;
200    InspectorFrontend::Page* m_frontend;
201    InspectorOverlay* m_overlay;
202    long m_lastScriptIdentifier;
203    String m_pendingScriptToEvaluateOnLoadOnce;
204    String m_scriptToEvaluateOnLoadOnce;
205    String m_pendingScriptPreprocessor;
206    String m_scriptPreprocessor;
207    HashMap<Frame*, String> m_frameToIdentifier;
208    HashMap<String, Frame*> m_identifierToFrame;
209    HashMap<DocumentLoader*, String> m_loaderToIdentifier;
210    bool m_enabled;
211    bool m_isFirstLayoutAfterOnLoad;
212    bool m_originalScriptExecutionDisabled;
213    bool m_geolocationOverridden;
214    bool m_ignoreScriptsEnabledNotification;
215    RefPtr<GeolocationPosition> m_geolocationPosition;
216    RefPtr<GeolocationPosition> m_platformGeolocationPosition;
217    RefPtr<DeviceOrientationData> m_deviceOrientation;
218};
219
220
221} // namespace WebCore
222
223#endif // ENABLE(INSPECTOR)
224
225#endif // !defined(InspectorPagerAgent_h)
226