2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "InspectorWebAgentBase.h"
35 #include "LayoutRect.h"
36 #include <inspector/InspectorBackendDispatchers.h>
37 #include <inspector/InspectorFrontendDispatchers.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/text/WTFString.h>
42 class InspectorObject;
48 class DOMWrapperWorld;
51 class InspectorClient;
52 class InspectorOverlay;
59 typedef String ErrorString;
61 class InspectorPageAgent final : public InspectorAgentBase, public Inspector::PageBackendDispatcherHandler {
62 WTF_MAKE_NONCOPYABLE(InspectorPageAgent);
63 WTF_MAKE_FAST_ALLOCATED;
65 InspectorPageAgent(PageAgentContext&, InspectorClient*, InspectorOverlay*);
78 static bool cachedResourceContent(CachedResource*, String* result, bool* base64Encoded);
79 static bool sharedBufferContent(RefPtr<SharedBuffer>&&, const String& textEncodingName, bool withBase64Encode, String* result);
80 static void resourceContent(ErrorString&, Frame*, const URL&, String* result, bool* base64Encoded);
81 static String sourceMapURLForResource(CachedResource*);
83 static CachedResource* cachedResource(Frame*, const URL&);
84 static Inspector::Protocol::Page::ResourceType resourceTypeJson(ResourceType);
85 static ResourceType cachedResourceType(const CachedResource&);
86 static Inspector::Protocol::Page::ResourceType cachedResourceTypeJson(const CachedResource&);
88 // Page API for InspectorFrontend
89 void enable(ErrorString&) override;
90 void disable(ErrorString&) override;
91 void addScriptToEvaluateOnLoad(ErrorString&, const String& source, String* result) override;
92 void removeScriptToEvaluateOnLoad(ErrorString&, const String& identifier) override;
93 void reload(ErrorString&, const bool* optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad) override;
94 void navigate(ErrorString&, const String& url) override;
95 void getCookies(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>>& cookies) override;
96 void deleteCookie(ErrorString&, const String& cookieName, const String& url) override;
97 void getResourceTree(ErrorString&, RefPtr<Inspector::Protocol::Page::FrameResourceTree>&) override;
98 void getResourceContent(ErrorString&, const String& frameId, const String& url, String* content, bool* base64Encoded) override;
99 void searchInResource(ErrorString&, const String& frameId, const String& url, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, const String* optionalRequestId, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>>&) override;
100 void searchInResources(ErrorString&, const String&, const bool* caseSensitive, const bool* isRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>>&) override;
101 void setDocumentContent(ErrorString&, const String& frameId, const String& html) override;
102 void setShowPaintRects(ErrorString&, bool show) override;
103 void getScriptExecutionStatus(ErrorString&, Inspector::PageBackendDispatcherHandler::Result*) override;
104 void setScriptExecutionDisabled(ErrorString&, bool) override;
105 void setEmulatedMedia(ErrorString&, const String&) override;
106 void getCompositingBordersVisible(ErrorString&, bool* out_param) override;
107 void setCompositingBordersVisible(ErrorString&, bool) override;
108 void snapshotNode(ErrorString&, int nodeId, String* outDataURL) override;
109 void snapshotRect(ErrorString&, int x, int y, int width, int height, const String& coordinateSystem, String* outDataURL) override;
110 void handleJavaScriptDialog(ErrorString&, bool accept, const String* promptText) override;
111 void archive(ErrorString&, String* data) override;
113 // InspectorInstrumentation
114 void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld&);
115 void domContentEventFired();
116 void loadEventFired();
117 void frameNavigated(Frame&);
118 void frameDetached(Frame&);
119 void loaderDetachedFromFrame(DocumentLoader&);
120 void frameStartedLoading(Frame&);
121 void frameStoppedLoading(Frame&);
122 void frameScheduledNavigation(Frame&, double delay);
123 void frameClearedScheduledNavigation(Frame&);
124 void willRunJavaScriptDialog(const String& message);
125 void didRunJavaScriptDialog();
126 void applyEmulatedMedia(String&);
127 void didPaint(RenderObject&, const LayoutRect&);
130 void didRecalculateStyle();
131 void scriptsEnabled(bool isEnabled);
133 // Inspector Controller API
134 void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
135 void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
138 Page& page() { return m_page; }
139 MainFrame& mainFrame();
140 String createIdentifier();
141 Frame* frameForId(const String& frameId);
142 WEBCORE_EXPORT String frameId(Frame*);
143 bool hasIdForFrame(Frame*) const;
144 String loaderId(DocumentLoader*);
145 Frame* findFrameWithSecurityOrigin(const String& originRawString);
146 Frame* assertFrame(ErrorString&, const String& frameId);
147 static DocumentLoader* assertDocumentLoader(ErrorString&, Frame*);
152 static bool mainResourceContent(Frame*, bool withBase64Encode, String* result);
153 static bool dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result);
155 Ref<Inspector::Protocol::Page::Frame> buildObjectForFrame(Frame*);
156 Ref<Inspector::Protocol::Page::FrameResourceTree> buildObjectForFrameTree(Frame*);
158 std::unique_ptr<Inspector::PageFrontendDispatcher> m_frontendDispatcher;
159 RefPtr<Inspector::PageBackendDispatcher> m_backendDispatcher;
162 InspectorClient* m_client { nullptr };
163 InspectorOverlay* m_overlay { nullptr };
165 long m_lastScriptIdentifier { 0 };
166 String m_pendingScriptToEvaluateOnLoadOnce;
167 String m_scriptToEvaluateOnLoadOnce;
168 HashMap<Frame*, String> m_frameToIdentifier;
169 HashMap<String, Frame*> m_identifierToFrame;
170 HashMap<DocumentLoader*, String> m_loaderToIdentifier;
171 bool m_enabled { false };
172 bool m_isFirstLayoutAfterOnLoad { false };
173 bool m_originalScriptExecutionDisabled { false };
174 bool m_ignoreScriptsEnabledNotification { false };
175 bool m_showPaintRects { false };
176 String m_emulatedMedia;
177 RefPtr<Inspector::InspectorObject> m_scriptsToEvaluateOnLoad;
180 } // namespace WebCore