inspectable_web_contents_impl.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE-CHROMIUM file.
  5. #ifndef BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_IMPL_H_
  6. #define BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_IMPL_H_
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/weak_ptr.h"
  11. #include "brightray/browser/devtools_contents_resizing_strategy.h"
  12. #include "brightray/browser/devtools_embedder_message_dispatcher.h"
  13. #include "brightray/browser/inspectable_web_contents.h"
  14. #include "content/public/browser/devtools_agent_host.h"
  15. #include "content/public/browser/devtools_frontend_host.h"
  16. #include "content/public/browser/web_contents_delegate.h"
  17. #include "content/public/browser/web_contents_observer.h"
  18. #include "net/url_request/url_fetcher_delegate.h"
  19. #include "ui/gfx/geometry/rect.h"
  20. class PrefService;
  21. class PrefRegistrySimple;
  22. namespace brightray {
  23. class InspectableWebContentsDelegate;
  24. class InspectableWebContentsView;
  25. class InspectableWebContentsImpl
  26. : public InspectableWebContents,
  27. public content::DevToolsAgentHostClient,
  28. public content::WebContentsObserver,
  29. public content::WebContentsDelegate,
  30. public DevToolsEmbedderMessageDispatcher::Delegate,
  31. public net::URLFetcherDelegate {
  32. public:
  33. static void RegisterPrefs(PrefRegistrySimple* pref_registry);
  34. explicit InspectableWebContentsImpl(content::WebContents*);
  35. ~InspectableWebContentsImpl() override;
  36. InspectableWebContentsView* GetView() const override;
  37. content::WebContents* GetWebContents() const override;
  38. content::WebContents* GetDevToolsWebContents() const override;
  39. void SetDelegate(InspectableWebContentsDelegate* delegate) override;
  40. InspectableWebContentsDelegate* GetDelegate() const override;
  41. void SetDevToolsWebContents(content::WebContents* devtools) override;
  42. void SetDockState(const std::string& state) override;
  43. void ShowDevTools() override;
  44. void CloseDevTools() override;
  45. bool IsDevToolsViewShowing() override;
  46. void AttachTo(scoped_refptr<content::DevToolsAgentHost>) override;
  47. void Detach() override;
  48. void CallClientFunction(const std::string& function_name,
  49. const base::Value* arg1,
  50. const base::Value* arg2,
  51. const base::Value* arg3) override;
  52. void InspectElement(int x, int y) override;
  53. // Return the last position and size of devtools window.
  54. gfx::Rect GetDevToolsBounds() const;
  55. void SaveDevToolsBounds(const gfx::Rect& bounds);
  56. // Return the last set zoom level of devtools window.
  57. double GetDevToolsZoomLevel() const;
  58. void UpdateDevToolsZoomLevel(double level);
  59. private:
  60. // DevToolsEmbedderMessageDispacher::Delegate
  61. void ActivateWindow() override;
  62. void CloseWindow() override;
  63. void LoadCompleted() override;
  64. void SetInspectedPageBounds(const gfx::Rect& rect) override;
  65. void InspectElementCompleted() override;
  66. void InspectedURLChanged(const std::string& url) override;
  67. void LoadNetworkResource(const DispatchCallback& callback,
  68. const std::string& url,
  69. const std::string& headers,
  70. int stream_id) override;
  71. void SetIsDocked(const DispatchCallback& callback, bool is_docked) override;
  72. void OpenInNewTab(const std::string& url) override;
  73. void SaveToFile(const std::string& url,
  74. const std::string& content,
  75. bool save_as) override;
  76. void AppendToFile(const std::string& url,
  77. const std::string& content) override;
  78. void RequestFileSystems() override;
  79. void AddFileSystem(const std::string& file_system_path) override;
  80. void RemoveFileSystem(const std::string& file_system_path) override;
  81. void UpgradeDraggedFileSystemPermissions(
  82. const std::string& file_system_url) override;
  83. void IndexPath(int index_request_id,
  84. const std::string& file_system_path) override;
  85. void StopIndexing(int index_request_id) override;
  86. void SearchInPath(int search_request_id,
  87. const std::string& file_system_path,
  88. const std::string& query) override;
  89. void SetWhitelistedShortcuts(const std::string& message) override;
  90. void ZoomIn() override;
  91. void ZoomOut() override;
  92. void ResetZoom() override;
  93. void SetDevicesUpdatesEnabled(bool enabled) override;
  94. void DispatchProtocolMessageFromDevToolsFrontend(
  95. const std::string& message) override;
  96. void SendJsonRequest(const DispatchCallback& callback,
  97. const std::string& browser_id,
  98. const std::string& url) override;
  99. void GetPreferences(const DispatchCallback& callback) override;
  100. void SetPreference(const std::string& name,
  101. const std::string& value) override;
  102. void RemovePreference(const std::string& name) override;
  103. void ClearPreferences() override;
  104. void RegisterExtensionsAPI(const std::string& origin,
  105. const std::string& script) override;
  106. // content::DevToolsFrontendHostDelegate:
  107. void HandleMessageFromDevToolsFrontend(const std::string& message);
  108. // content::DevToolsAgentHostClient:
  109. void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host,
  110. const std::string& message) override;
  111. void AgentHostClosed(content::DevToolsAgentHost* agent_host,
  112. bool replaced) override;
  113. // content::WebContentsObserver:
  114. void RenderFrameHostChanged(content::RenderFrameHost* old_host,
  115. content::RenderFrameHost* new_host) override;
  116. void WebContentsDestroyed() override;
  117. void OnWebContentsFocused(
  118. content::RenderWidgetHost* render_widget_host) override;
  119. void ReadyToCommitNavigation(
  120. content::NavigationHandle* navigation_handle) override;
  121. void DidFinishNavigation(
  122. content::NavigationHandle* navigation_handle) override;
  123. // content::WebContentsDelegate:
  124. bool DidAddMessageToConsole(content::WebContents* source,
  125. int32_t level,
  126. const base::string16& message,
  127. int32_t line_no,
  128. const base::string16& source_id) override;
  129. bool ShouldCreateWebContents(
  130. content::WebContents* web_contents,
  131. content::RenderFrameHost* opener,
  132. content::SiteInstance* source_site_instance,
  133. int32_t route_id,
  134. int32_t main_frame_route_id,
  135. int32_t main_frame_widget_route_id,
  136. content::mojom::WindowContainerType window_container_type,
  137. const GURL& opener_url,
  138. const std::string& frame_name,
  139. const GURL& target_url,
  140. const std::string& partition_id,
  141. content::SessionStorageNamespace* session_storage_namespace) override;
  142. void HandleKeyboardEvent(content::WebContents*,
  143. const content::NativeWebKeyboardEvent&) override;
  144. void CloseContents(content::WebContents* source) override;
  145. content::ColorChooser* OpenColorChooser(
  146. content::WebContents* source,
  147. SkColor color,
  148. const std::vector<content::ColorSuggestion>& suggestions) override;
  149. void RunFileChooser(content::RenderFrameHost* render_frame_host,
  150. const content::FileChooserParams& params) override;
  151. void EnumerateDirectory(content::WebContents* source,
  152. int request_id,
  153. const base::FilePath& path) override;
  154. // net::URLFetcherDelegate:
  155. void OnURLFetchComplete(const net::URLFetcher* source) override;
  156. void SendMessageAck(int request_id, const base::Value* arg1);
  157. bool frontend_loaded_;
  158. scoped_refptr<content::DevToolsAgentHost> agent_host_;
  159. std::unique_ptr<content::DevToolsFrontendHost> frontend_host_;
  160. std::unique_ptr<DevToolsEmbedderMessageDispatcher>
  161. embedder_message_dispatcher_;
  162. DevToolsContentsResizingStrategy contents_resizing_strategy_;
  163. gfx::Rect devtools_bounds_;
  164. bool can_dock_;
  165. std::string dock_state_;
  166. using PendingRequestsMap = std::map<const net::URLFetcher*, DispatchCallback>;
  167. PendingRequestsMap pending_requests_;
  168. InspectableWebContentsDelegate* delegate_; // weak references.
  169. PrefService* pref_service_; // weak reference.
  170. std::unique_ptr<content::WebContents> web_contents_;
  171. // The default devtools created by this class when we don't have an external
  172. // one assigned by SetDevToolsWebContents.
  173. std::unique_ptr<content::WebContents> managed_devtools_web_contents_;
  174. // The external devtools assigned by SetDevToolsWebContents.
  175. content::WebContents* external_devtools_web_contents_ = nullptr;
  176. std::unique_ptr<InspectableWebContentsView> view_;
  177. using ExtensionsAPIs = std::map<std::string, std::string>;
  178. ExtensionsAPIs extensions_api_;
  179. base::WeakPtrFactory<InspectableWebContentsImpl> weak_factory_;
  180. DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl);
  181. };
  182. } // namespace brightray
  183. #endif // BRIGHTRAY_BROWSER_INSPECTABLE_WEB_CONTENTS_IMPL_H_