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
35#include "core/InspectorFrontend.h"
36#include "core/inspector/InspectorBaseAgent.h"
37#include "core/inspector/InspectorResourceContentLoader.h"
38#include "wtf/HashMap.h"
39#include "wtf/text/WTFString.h"
40
41namespace blink {
42
43class Resource;
44class DOMWrapperWorld;
45class Document;
46class DocumentLoader;
47class LocalFrame;
48class GraphicsContext;
49class GraphicsLayer;
50class InjectedScriptManager;
51class InspectorClient;
52class InspectorOverlay;
53class InspectorResourceContentLoader;
54class InstrumentingAgents;
55class IntSize;
56class KURL;
57class LayoutRect;
58class Page;
59class RenderObject;
60class SharedBuffer;
61class StyleResolver;
62
63typedef String ErrorString;
64
65class InspectorPageAgent FINAL : public InspectorBaseAgent<InspectorPageAgent>, public InspectorBackendDispatcher::PageCommandHandler {
66    WTF_MAKE_NONCOPYABLE(InspectorPageAgent);
67public:
68    enum ResourceType {
69        DocumentResource,
70        StylesheetResource,
71        ImageResource,
72        FontResource,
73        MediaResource,
74        ScriptResource,
75        TextTrackResource,
76        XHRResource,
77        WebSocketResource,
78        OtherResource
79    };
80
81    static PassOwnPtrWillBeRawPtr<InspectorPageAgent> create(Page*, InjectedScriptManager*, InspectorClient*, InspectorOverlay*);
82
83    // Settings overrides.
84    void setTextAutosizingEnabled(bool);
85    void setDeviceScaleAdjustment(float);
86    void setPreferCompositingToLCDTextEnabled(bool);
87
88    static Vector<Document*> importsForFrame(LocalFrame*);
89    static bool cachedResourceContent(Resource*, String* result, bool* base64Encoded);
90    static bool sharedBufferContent(PassRefPtr<SharedBuffer>, const String& textEncodingName, bool withBase64Encode, String* result);
91
92    static PassRefPtr<SharedBuffer> resourceData(LocalFrame*, const KURL&, String* textEncodingName);
93    static Resource* cachedResource(LocalFrame*, const KURL&);
94    static TypeBuilder::Page::ResourceType::Enum resourceTypeJson(ResourceType);
95    static ResourceType cachedResourceType(const Resource&);
96    static TypeBuilder::Page::ResourceType::Enum cachedResourceTypeJson(const Resource&);
97
98    // Page API for InspectorFrontend
99    virtual void enable(ErrorString*) OVERRIDE;
100    virtual void disable(ErrorString*) OVERRIDE;
101    virtual void addScriptToEvaluateOnLoad(ErrorString*, const String& source, String* result) OVERRIDE;
102    virtual void removeScriptToEvaluateOnLoad(ErrorString*, const String& identifier) OVERRIDE;
103    virtual void reload(ErrorString*, const bool* optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad, const String* optionalScriptPreprocessor) OVERRIDE;
104    virtual void navigate(ErrorString*, const String& url, String* frameId) OVERRIDE;
105    virtual void getCookies(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::Page::Cookie> >& cookies) OVERRIDE;
106    virtual void deleteCookie(ErrorString*, const String& cookieName, const String& url) OVERRIDE;
107    virtual void getResourceTree(ErrorString*, RefPtr<TypeBuilder::Page::FrameResourceTree>&) OVERRIDE;
108    virtual void getResourceContent(ErrorString*, const String& frameId, const String& url, PassRefPtrWillBeRawPtr<GetResourceContentCallback>) OVERRIDE;
109    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> >&) OVERRIDE;
110    virtual void setDocumentContent(ErrorString*, const String& frameId, const String& html) OVERRIDE;
111    virtual void setDeviceMetricsOverride(ErrorString*, int width, int height, double deviceScaleFactor, bool mobile, bool fitWindow, const double* optionalScale, const double* optionalOffsetX, const double* optionalOffsetY) OVERRIDE;
112    virtual void clearDeviceMetricsOverride(ErrorString*) OVERRIDE;
113    virtual void resetScrollAndPageScaleFactor(ErrorString*) OVERRIDE;
114    virtual void setPageScaleFactor(ErrorString*, double pageScaleFactor) OVERRIDE;
115    virtual void setShowPaintRects(ErrorString*, bool show) OVERRIDE;
116    virtual void setShowDebugBorders(ErrorString*, bool show) OVERRIDE;
117    virtual void setShowFPSCounter(ErrorString*, bool show) OVERRIDE;
118    virtual void setContinuousPaintingEnabled(ErrorString*, bool enabled) OVERRIDE;
119    virtual void setShowScrollBottleneckRects(ErrorString*, bool show) OVERRIDE;
120    virtual void getScriptExecutionStatus(ErrorString*, PageCommandHandler::Result::Enum*) OVERRIDE;
121    virtual void setScriptExecutionDisabled(ErrorString*, bool) OVERRIDE;
122    virtual void setTouchEmulationEnabled(ErrorString*, bool) OVERRIDE;
123    virtual void setEmulatedMedia(ErrorString*, const String&) OVERRIDE;
124    virtual void startScreencast(ErrorString*, const String* format, const int* quality, const int* maxWidth, const int* maxHeight) OVERRIDE;
125    virtual void stopScreencast(ErrorString*) OVERRIDE;
126    virtual void setShowViewportSizeOnResize(ErrorString*, bool show, const bool* showGrid) OVERRIDE;
127
128    // InspectorInstrumentation API
129    void didClearDocumentOfWindowObject(LocalFrame*);
130    void domContentLoadedEventFired(LocalFrame*);
131    void loadEventFired(LocalFrame*);
132    void didCommitLoad(LocalFrame*, DocumentLoader*);
133    void frameAttachedToParent(LocalFrame*);
134    void frameDetachedFromParent(LocalFrame*);
135    void loaderDetachedFromFrame(DocumentLoader*);
136    void frameStartedLoading(LocalFrame*);
137    void frameStoppedLoading(LocalFrame*);
138    void frameScheduledNavigation(LocalFrame*, double delay);
139    void frameClearedScheduledNavigation(LocalFrame*);
140    void willRunJavaScriptDialog(const String& message);
141    void didRunJavaScriptDialog();
142    bool applyViewportStyleOverride(StyleResolver*);
143    void applyEmulatedMedia(String*);
144    void didPaint(RenderObject*, const GraphicsLayer*, GraphicsContext*, const LayoutRect&);
145    void didLayout(RenderObject*);
146    void didScroll();
147    void didResizeMainFrame();
148    void didRecalculateStyle(int);
149    void scriptsEnabled(bool isEnabled);
150
151    // Inspector Controller API
152    virtual void setFrontend(InspectorFrontend*) OVERRIDE;
153    virtual void clearFrontend() OVERRIDE;
154    virtual void restore() OVERRIDE;
155    virtual void discardAgent() OVERRIDE;
156
157    // Cross-agents API
158    Page* page() { return m_page; }
159    LocalFrame* mainFrame();
160    String createIdentifier();
161    LocalFrame* frameForId(const String& frameId);
162    String frameId(LocalFrame*);
163    bool hasIdForFrame(LocalFrame*) const;
164    String loaderId(DocumentLoader*);
165    LocalFrame* findFrameWithSecurityOrigin(const String& originRawString);
166    LocalFrame* assertFrame(ErrorString*, const String& frameId);
167    String scriptPreprocessorSource() { return m_scriptPreprocessorSource; }
168    const AtomicString& resourceSourceMapURL(const String& url);
169    bool deviceMetricsOverrideEnabled();
170    void deviceOrPageScaleFactorChanged();
171    bool screencastEnabled();
172    static DocumentLoader* assertDocumentLoader(ErrorString*, LocalFrame*);
173    InspectorResourceContentLoader* resourceContentLoader() { return m_inspectorResourceContentLoader.get(); }
174    void clearEditedResourcesContent();
175    void addEditedResourceContent(const String& url, const String& content);
176    bool getEditedResourceContent(const String& url, String* content);
177
178    virtual void trace(Visitor*) OVERRIDE;
179
180private:
181    class GetResourceContentLoadListener;
182
183    InspectorPageAgent(Page*, InjectedScriptManager*, InspectorClient*, InspectorOverlay*);
184    bool deviceMetricsChanged(bool enabled, int width, int height, double deviceScaleFactor, bool mobile, bool fitWindow, double scale, double offsetX, double offsetY);
185    void updateViewMetricsFromState();
186    void updateViewMetrics(bool enabled, int width, int height, double deviceScaleFactor, bool mobile, bool fitWindow, double scale, double offsetX, double offsetY);
187    void updateTouchEventEmulationInPage(bool);
188    bool compositingEnabled(ErrorString*);
189
190    void getResourceContentAfterResourcesContentLoaded(const String& frameId, const String& url, PassRefPtrWillBeRawPtr<GetResourceContentCallback>);
191
192    static bool dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result);
193
194    void viewportChanged();
195
196    PassRefPtr<TypeBuilder::Page::Frame> buildObjectForFrame(LocalFrame*);
197    PassRefPtr<TypeBuilder::Page::FrameResourceTree> buildObjectForFrameTree(LocalFrame*);
198    RawPtrWillBeMember<Page> m_page;
199    RawPtrWillBeMember<InjectedScriptManager> m_injectedScriptManager;
200    InspectorClient* m_client;
201    InspectorFrontend::Page* m_frontend;
202    InspectorOverlay* m_overlay;
203    long m_lastScriptIdentifier;
204    String m_pendingScriptToEvaluateOnLoadOnce;
205    String m_scriptToEvaluateOnLoadOnce;
206    String m_pendingScriptPreprocessor;
207    String m_scriptPreprocessorSource;
208    HashMap<LocalFrame*, String> m_frameToIdentifier;
209    HashMap<String, LocalFrame*> m_identifierToFrame;
210    HashMap<DocumentLoader*, String> m_loaderToIdentifier;
211    bool m_enabled;
212    bool m_ignoreScriptsEnabledNotification;
213    bool m_deviceMetricsOverridden;
214    bool m_emulateMobileEnabled;
215
216    bool m_touchEmulationEnabled;
217    bool m_originalTouchEnabled;
218    bool m_originalDeviceSupportsMouse;
219    bool m_originalDeviceSupportsTouch;
220    int m_originalMaxTouchPoints;
221
222    bool m_embedderTextAutosizingEnabled;
223    double m_embedderFontScaleFactor;
224    bool m_embedderPreferCompositingToLCDTextEnabled;
225
226    OwnPtrWillBeMember<InspectorResourceContentLoader> m_inspectorResourceContentLoader;
227    HashMap<String, String> m_editedResourceContent;
228};
229
230
231} // namespace blink
232
233
234#endif // !defined(InspectorPagerAgent_h)
235