1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#pragma once
33
34#include "CachedResource.h"
35#include "InspectorWebAgentBase.h"
36#include "LayoutRect.h"
37#include <JavaScriptCore/InspectorBackendDispatchers.h>
38#include <JavaScriptCore/InspectorFrontendDispatchers.h>
39#include <wtf/HashMap.h>
40#include <wtf/Seconds.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class DocumentLoader;
46class Frame;
47class InspectorClient;
48class InspectorOverlay;
49class Page;
50class RenderObject;
51class SharedBuffer;
52
53typedef String ErrorString;
54
55class InspectorPageAgent final : public InspectorAgentBase, public Inspector::PageBackendDispatcherHandler {
56 WTF_MAKE_NONCOPYABLE(InspectorPageAgent);
57 WTF_MAKE_FAST_ALLOCATED;
58public:
59 InspectorPageAgent(PageAgentContext&, InspectorClient*, InspectorOverlay*);
60
61 enum ResourceType {
62 DocumentResource,
63 StylesheetResource,
64 ImageResource,
65 FontResource,
66 ScriptResource,
67 XHRResource,
68 FetchResource,
69 PingResource,
70 BeaconResource,
71 WebSocketResource,
72#if ENABLE(APPLICATION_MANIFEST)
73 ApplicationManifestResource,
74#endif
75 OtherResource,
76 };
77
78 static bool sharedBufferContent(RefPtr<SharedBuffer>&&, const String& textEncodingName, bool withBase64Encode, String* result);
79 static Vector<CachedResource*> cachedResourcesForFrame(Frame*);
80 static void resourceContent(ErrorString&, Frame*, const URL&, String* result, bool* base64Encoded);
81 static String sourceMapURLForResource(CachedResource*);
82 static CachedResource* cachedResource(Frame*, const URL&);
83 static Inspector::Protocol::Page::ResourceType resourceTypeJSON(ResourceType);
84 static ResourceType inspectorResourceType(CachedResource::Type);
85 static ResourceType inspectorResourceType(const CachedResource&);
86 static Inspector::Protocol::Page::ResourceType cachedResourceTypeJSON(const CachedResource&);
87 static Frame* findFrameWithSecurityOrigin(Page&, const String& originRawString);
88 static DocumentLoader* assertDocumentLoader(ErrorString&, Frame*);
89
90 // Page API for InspectorFrontend
91 void enable(ErrorString&) final;
92 void disable(ErrorString&) final;
93 void reload(ErrorString&, const bool* optionalReloadFromOrigin, const bool* optionalRevalidateAllResources) final;
94 void navigate(ErrorString&, const String& url) final;
95 void overrideUserAgent(ErrorString&, const String* value) final;
96 void overrideSetting(ErrorString&, const String& setting, const bool* value) final;
97 void getCookies(ErrorString&, RefPtr<JSON::ArrayOf<Inspector::Protocol::Page::Cookie>>& cookies) final;
98 void deleteCookie(ErrorString&, const String& cookieName, const String& url) final;
99 void getResourceTree(ErrorString&, RefPtr<Inspector::Protocol::Page::FrameResourceTree>&) final;
100 void getResourceContent(ErrorString&, const String& frameId, const String& url, String* content, bool* base64Encoded) final;
101 void searchInResource(ErrorString&, const String& frameId, const String& url, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, const String* optionalRequestId, RefPtr<JSON::ArrayOf<Inspector::Protocol::GenericTypes::SearchMatch>>&) final;
102 void searchInResources(ErrorString&, const String&, const bool* caseSensitive, const bool* isRegex, RefPtr<JSON::ArrayOf<Inspector::Protocol::Page::SearchResult>>&) final;
103 void setShowRulers(ErrorString&, bool) final;
104 void setShowPaintRects(ErrorString&, bool show) final;
105 void setEmulatedMedia(ErrorString&, const String&) final;
106 void setForcedAppearance(ErrorString&, const String&) final;
107 void getCompositingBordersVisible(ErrorString&, bool* out_param) final;
108 void setCompositingBordersVisible(ErrorString&, bool) final;
109 void snapshotNode(ErrorString&, int nodeId, String* outDataURL) final;
110 void snapshotRect(ErrorString&, int x, int y, int width, int height, const String& coordinateSystem, String* outDataURL) final;
111 void archive(ErrorString&, String* data) final;
112
113 // InspectorInstrumentation
114 void domContentEventFired();
115 void loadEventFired();
116 void frameNavigated(Frame&);
117 void frameDetached(Frame&);
118 void loaderDetachedFromFrame(DocumentLoader&);
119 void frameStartedLoading(Frame&);
120 void frameStoppedLoading(Frame&);
121 void frameScheduledNavigation(Frame&, Seconds delay);
122 void frameClearedScheduledNavigation(Frame&);
123 void defaultAppearanceDidChange(bool useDarkAppearance);
124 void applyUserAgentOverride(String&);
125 void applyEmulatedMedia(String&);
126 void didPaint(RenderObject&, const LayoutRect&);
127 void didLayout();
128 void didScroll();
129 void didRecalculateStyle();
130
131 // Inspector Controller API
132 void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) final;
133 void willDestroyFrontendAndBackend(Inspector::DisconnectReason) final;
134
135 // Cross-agents API
136 Frame* frameForId(const String& frameId);
137 WEBCORE_EXPORT String frameId(Frame*);
138 String loaderId(DocumentLoader*);
139 Frame* assertFrame(ErrorString&, const String& frameId);
140
141private:
142 double timestamp();
143
144 static bool mainResourceContent(Frame*, bool withBase64Encode, String* result);
145 static bool dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result);
146
147 Ref<Inspector::Protocol::Page::Frame> buildObjectForFrame(Frame*);
148 Ref<Inspector::Protocol::Page::FrameResourceTree> buildObjectForFrameTree(Frame*);
149
150 std::unique_ptr<Inspector::PageFrontendDispatcher> m_frontendDispatcher;
151 RefPtr<Inspector::PageBackendDispatcher> m_backendDispatcher;
152
153 Page& m_inspectedPage;
154 InspectorClient* m_client { nullptr };
155 InspectorOverlay* m_overlay { nullptr };
156
157 HashMap<Frame*, String> m_frameToIdentifier;
158 HashMap<String, Frame*> m_identifierToFrame;
159 HashMap<DocumentLoader*, String> m_loaderToIdentifier;
160 String m_userAgentOverride;
161 String m_emulatedMedia;
162 String m_forcedAppearance;
163 bool m_isFirstLayoutAfterOnLoad { false };
164 bool m_showPaintRects { false };
165};
166
167} // namespace WebCore
168