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 THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_INSPECTOR_PAGE_AGENT_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_INSPECTOR_PAGE_AGENT_H_
33 
34 #include "base/macros.h"
35 #include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h"
36 #include "third_party/blink/renderer/core/core_export.h"
37 #include "third_party/blink/renderer/core/inspector/inspector_base_agent.h"
38 #include "third_party/blink/renderer/core/inspector/protocol/Page.h"
39 #include "third_party/blink/renderer/core/loader/frame_loader_types.h"
40 #include "third_party/blink/renderer/core/page/chrome_client.h"
41 #include "third_party/blink/renderer/platform/wtf/hash_map.h"
42 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
43 #include "v8/include/v8-inspector.h"
44 
45 namespace blink {
46 
47 namespace probe {
48 class RecalculateStyle;
49 class UpdateLayout;
50 }  // namespace probe
51 
52 class Resource;
53 class Document;
54 class DocumentLoader;
55 class InspectedFrames;
56 class InspectorResourceContentLoader;
57 class LocalFrame;
58 class ScriptSourceCode;
59 enum class ResourceType : uint8_t;
60 
61 using blink::protocol::Maybe;
62 
63 class CORE_EXPORT InspectorPageAgent final
64     : public InspectorBaseAgent<protocol::Page::Metainfo> {
65  public:
66   class Client {
67    public:
68     virtual ~Client() = default;
PageLayoutInvalidated(bool resized)69     virtual void PageLayoutInvalidated(bool resized) {}
WaitForDebugger()70     virtual void WaitForDebugger() {}
71   };
72 
73   enum ResourceType {
74     kDocumentResource,
75     kStylesheetResource,
76     kImageResource,
77     kFontResource,
78     kMediaResource,
79     kScriptResource,
80     kTextTrackResource,
81     kXHRResource,
82     kFetchResource,
83     kEventSourceResource,
84     kWebSocketResource,
85     kManifestResource,
86     kSignedExchangeResource,
87     kOtherResource
88   };
89 
90   static HeapVector<Member<Document>> ImportsForFrame(LocalFrame*);
91   static bool CachedResourceContent(const Resource*,
92                                     String* result,
93                                     bool* base64_encoded);
94   static bool SharedBufferContent(scoped_refptr<const SharedBuffer>,
95                                   const String& mime_type,
96                                   const String& text_encoding_name,
97                                   String* result,
98                                   bool* base64_encoded);
99 
100   static String ResourceTypeJson(ResourceType);
101   static ResourceType ToResourceType(const blink::ResourceType);
102   static String CachedResourceTypeJson(const Resource&);
103 
104   InspectorPageAgent(InspectedFrames*,
105                      Client*,
106                      InspectorResourceContentLoader*,
107                      v8_inspector::V8InspectorSession*);
108 
109   // Page API for frontend
110   protocol::Response enable() override;
111   protocol::Response disable() override;
112   protocol::Response addScriptToEvaluateOnLoad(const String& script_source,
113                                                String* identifier) override;
114   protocol::Response removeScriptToEvaluateOnLoad(
115       const String& identifier) override;
116   protocol::Response addScriptToEvaluateOnNewDocument(
117       const String& source,
118       Maybe<String> world_name,
119       String* identifier) override;
120   protocol::Response removeScriptToEvaluateOnNewDocument(
121       const String& identifier) override;
122   protocol::Response setLifecycleEventsEnabled(bool) override;
123   protocol::Response reload(Maybe<bool> bypass_cache,
124                             Maybe<String> script_to_evaluate_on_load) override;
125   protocol::Response stopLoading() override;
126   protocol::Response setAdBlockingEnabled(bool) override;
127   protocol::Response getResourceTree(
128       std::unique_ptr<protocol::Page::FrameResourceTree>* frame_tree) override;
129   protocol::Response getFrameTree(
130       std::unique_ptr<protocol::Page::FrameTree>*) override;
131   void getResourceContent(const String& frame_id,
132                           const String& url,
133                           std::unique_ptr<GetResourceContentCallback>) override;
134   void searchInResource(const String& frame_id,
135                         const String& url,
136                         const String& query,
137                         Maybe<bool> case_sensitive,
138                         Maybe<bool> is_regex,
139                         std::unique_ptr<SearchInResourceCallback>) override;
140   protocol::Response setDocumentContent(const String& frame_id,
141                                         const String& html) override;
142   protocol::Response setBypassCSP(bool enabled) override;
143 
144   protocol::Response startScreencast(Maybe<String> format,
145                                      Maybe<int> quality,
146                                      Maybe<int> max_width,
147                                      Maybe<int> max_height,
148                                      Maybe<int> every_nth_frame) override;
149   protocol::Response stopScreencast() override;
150   protocol::Response getLayoutMetrics(
151       std::unique_ptr<protocol::Page::LayoutViewport>*,
152       std::unique_ptr<protocol::Page::VisualViewport>*,
153       std::unique_ptr<protocol::DOM::Rect>*) override;
154   protocol::Response createIsolatedWorld(const String& frame_id,
155                                          Maybe<String> world_name,
156                                          Maybe<bool> grant_universal_access,
157                                          int* execution_context_id) override;
158   protocol::Response setFontFamilies(
159       std::unique_ptr<protocol::Page::FontFamilies>) override;
160   protocol::Response setFontSizes(
161       std::unique_ptr<protocol::Page::FontSizes>) override;
162   protocol::Response generateTestReport(const String& message,
163                                         Maybe<String> group) override;
164 
165   protocol::Response setProduceCompilationCache(bool enabled) override;
166   protocol::Response addCompilationCache(const String& url,
167                                          const protocol::Binary& data) override;
168   protocol::Response clearCompilationCache() override;
169   protocol::Response waitForDebugger() override;
170   protocol::Response setInterceptFileChooserDialog(bool enabled) override;
171 
172   // InspectorInstrumentation API
173   void DidClearDocumentOfWindowObject(LocalFrame*);
174   void DidNavigateWithinDocument(LocalFrame*);
175   void DomContentLoadedEventFired(LocalFrame*);
176   void LoadEventFired(LocalFrame*);
177   void WillCommitLoad(LocalFrame*, DocumentLoader*);
178   void FrameAttachedToParent(LocalFrame*);
179   void FrameDetachedFromParent(LocalFrame*, FrameDetachType);
180   void FrameStartedLoading(LocalFrame*);
181   void FrameStoppedLoading(LocalFrame*);
182   void FrameRequestedNavigation(Frame* target_frame,
183                                 const KURL&,
184                                 ClientNavigationReason,
185                                 NavigationPolicy);
186   void FrameScheduledNavigation(LocalFrame*,
187                                 const KURL&,
188                                 base::TimeDelta delay,
189                                 ClientNavigationReason);
190   void FrameClearedScheduledNavigation(LocalFrame*);
191   void WillRunJavaScriptDialog();
192   void DidRunJavaScriptDialog();
193   void DidResizeMainFrame();
194   void DidChangeViewport();
195   void LifecycleEvent(LocalFrame*,
196                       DocumentLoader*,
197                       const char* name,
198                       double timestamp);
199   void PaintTiming(Document*, const char* name, double timestamp);
200   void Will(const probe::UpdateLayout&);
201   void Did(const probe::UpdateLayout&);
202   void Will(const probe::RecalculateStyle&);
203   void Did(const probe::RecalculateStyle&);
204   void WindowOpen(const KURL&,
205                   const AtomicString&,
206                   const WebWindowFeatures&,
207                   bool);
208   void ConsumeCompilationCache(const ScriptSourceCode& source,
209                                v8::ScriptCompiler::CachedData**);
210   void ProduceCompilationCache(const ScriptSourceCode& source,
211                                v8::Local<v8::Script> script);
212   void FileChooserOpened(LocalFrame* frame,
213                          HTMLInputElement* element,
214                          bool* intercepted);
215 
216   // Inspector Controller API
217   void Restore() override;
218   bool ScreencastEnabled();
219 
220   void Trace(Visitor*) const override;
221 
222  private:
223   void GetResourceContentAfterResourcesContentLoaded(
224       const String& frame_id,
225       const String& url,
226       std::unique_ptr<GetResourceContentCallback>);
227   void SearchContentAfterResourcesContentLoaded(
228       const String& frame_id,
229       const String& url,
230       const String& query,
231       bool case_sensitive,
232       bool is_regex,
233       std::unique_ptr<SearchInResourceCallback>);
234   scoped_refptr<DOMWrapperWorld> EnsureDOMWrapperWorld(
235       LocalFrame* frame,
236       const String& world_name,
237       bool grant_universal_access);
238 
239   static KURL UrlWithoutFragment(const KURL&);
240 
241   void PageLayoutInvalidated(bool resized);
242 
243   std::unique_ptr<protocol::Page::Frame> BuildObjectForFrame(LocalFrame*);
244   std::unique_ptr<protocol::Page::FrameTree> BuildObjectForFrameTree(
245       LocalFrame*);
246   std::unique_ptr<protocol::Page::FrameResourceTree> BuildObjectForResourceTree(
247       LocalFrame*);
248   Member<InspectedFrames> inspected_frames_;
249   HashMap<String, protocol::Binary> compilation_cache_;
250   using FrameIsolatedWorlds = HashMap<String, scoped_refptr<DOMWrapperWorld>>;
251   HeapHashMap<WeakMember<LocalFrame>, FrameIsolatedWorlds> isolated_worlds_;
252   v8_inspector::V8InspectorSession* v8_session_;
253   Client* client_;
254   String pending_script_to_evaluate_on_load_once_;
255   String script_to_evaluate_on_load_once_;
256   Member<InspectorResourceContentLoader> inspector_resource_content_loader_;
257   bool intercept_file_chooser_ = false;
258   int resource_content_loader_client_id_;
259   InspectorAgentState::Boolean enabled_;
260   InspectorAgentState::Boolean screencast_enabled_;
261   InspectorAgentState::Boolean lifecycle_events_enabled_;
262   InspectorAgentState::Boolean bypass_csp_enabled_;
263   InspectorAgentState::StringMap scripts_to_evaluate_on_load_;
264   InspectorAgentState::StringMap worlds_to_evaluate_on_load_;
265   InspectorAgentState::String standard_font_family_;
266   InspectorAgentState::String fixed_font_family_;
267   InspectorAgentState::String serif_font_family_;
268   InspectorAgentState::String sans_serif_font_family_;
269   InspectorAgentState::String cursive_font_family_;
270   InspectorAgentState::String fantasy_font_family_;
271   InspectorAgentState::String pictograph_font_family_;
272   InspectorAgentState::Integer standard_font_size_;
273   InspectorAgentState::Integer fixed_font_size_;
274   InspectorAgentState::Boolean produce_compilation_cache_;
275   DISALLOW_COPY_AND_ASSIGN(InspectorPageAgent);
276 };
277 
278 }  // namespace blink
279 
280 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_INSPECTOR_PAGE_AGENT_H_
281