atom_browser_client.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef SHELL_BROWSER_ATOM_BROWSER_CLIENT_H_
  5. #define SHELL_BROWSER_ATOM_BROWSER_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/files/file_path.h"
  12. #include "base/synchronization/lock.h"
  13. #include "content/public/browser/content_browser_client.h"
  14. #include "content/public/browser/render_process_host_observer.h"
  15. #include "electron/buildflags/buildflags.h"
  16. #include "net/ssl/client_cert_identity.h"
  17. namespace content {
  18. class QuotaPermissionContext;
  19. class ClientCertificateDelegate;
  20. } // namespace content
  21. namespace net {
  22. class SSLCertRequestInfo;
  23. }
  24. namespace electron {
  25. class NotificationPresenter;
  26. class PlatformNotificationService;
  27. class AtomBrowserClient : public content::ContentBrowserClient,
  28. public content::RenderProcessHostObserver {
  29. public:
  30. static AtomBrowserClient* Get();
  31. static void SetApplicationLocale(const std::string& locale);
  32. AtomBrowserClient();
  33. ~AtomBrowserClient() override;
  34. using Delegate = content::ContentBrowserClient;
  35. void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  36. // Returns the WebContents for pending render processes.
  37. content::WebContents* GetWebContentsFromProcessID(int process_id);
  38. // Don't force renderer process to restart for once.
  39. static void SuppressRendererProcessRestartForOnce();
  40. NotificationPresenter* GetNotificationPresenter();
  41. void WebNotificationAllowed(int render_process_id,
  42. base::OnceCallback<void(bool, bool)> callback);
  43. // content::NavigatorDelegate
  44. std::vector<std::unique_ptr<content::NavigationThrottle>>
  45. CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
  46. // content::ContentBrowserClient:
  47. std::string GetApplicationLocale() override;
  48. base::FilePath GetFontLookupTableCacheDir() override;
  49. // content::ContentBrowserClient:
  50. bool ShouldEnableStrictSiteIsolation() override;
  51. std::string GetUserAgent() override;
  52. void SetUserAgent(const std::string& user_agent);
  53. void SetCanUseCustomSiteInstance(bool should_disable);
  54. bool CanUseCustomSiteInstance() override;
  55. protected:
  56. void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
  57. content::SpeechRecognitionManagerDelegate*
  58. CreateSpeechRecognitionManagerDelegate() override;
  59. content::TtsControllerDelegate* GetTtsControllerDelegate() override;
  60. void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
  61. content::WebPreferences* prefs) override;
  62. SiteInstanceForNavigationType ShouldOverrideSiteInstanceForNavigation(
  63. content::RenderFrameHost* current_rfh,
  64. content::RenderFrameHost* speculative_rfh,
  65. content::BrowserContext* browser_context,
  66. const GURL& url,
  67. bool has_navigation_started,
  68. bool has_request_started,
  69. content::SiteInstance** affinity_site_instance) const override;
  70. void RegisterPendingSiteInstance(
  71. content::RenderFrameHost* render_frame_host,
  72. content::SiteInstance* pending_site_instance) override;
  73. void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
  74. int child_process_id) override;
  75. void AdjustUtilityServiceProcessCommandLine(
  76. const service_manager::Identity& identity,
  77. base::CommandLine* command_line) override;
  78. void DidCreatePpapiPlugin(content::BrowserPpapiHost* browser_host) override;
  79. std::string GetGeolocationApiKey() override;
  80. scoped_refptr<content::QuotaPermissionContext> CreateQuotaPermissionContext()
  81. override;
  82. content::GeneratedCodeCacheSettings GetGeneratedCodeCacheSettings(
  83. content::BrowserContext* context) override;
  84. void AllowCertificateError(
  85. content::WebContents* web_contents,
  86. int cert_error,
  87. const net::SSLInfo& ssl_info,
  88. const GURL& request_url,
  89. bool is_main_frame_request,
  90. bool strict_enforcement,
  91. const base::Callback<void(content::CertificateRequestResultType)>&
  92. callback) override;
  93. base::OnceClosure SelectClientCertificate(
  94. content::WebContents* web_contents,
  95. net::SSLCertRequestInfo* cert_request_info,
  96. net::ClientCertIdentityList client_certs,
  97. std::unique_ptr<content::ClientCertificateDelegate> delegate) override;
  98. bool CanCreateWindow(content::RenderFrameHost* opener,
  99. const GURL& opener_url,
  100. const GURL& opener_top_level_frame_url,
  101. const url::Origin& source_origin,
  102. content::mojom::WindowContainerType container_type,
  103. const GURL& target_url,
  104. const content::Referrer& referrer,
  105. const std::string& frame_name,
  106. WindowOpenDisposition disposition,
  107. const blink::mojom::WindowFeatures& features,
  108. const std::vector<std::string>& additional_features,
  109. const scoped_refptr<network::ResourceRequestBody>& body,
  110. bool user_gesture,
  111. bool opener_suppressed,
  112. bool* no_javascript_access) override;
  113. #if BUILDFLAG(ENABLE_PICTURE_IN_PICTURE)
  114. std::unique_ptr<content::OverlayWindow> CreateWindowForPictureInPicture(
  115. content::PictureInPictureWindowController* controller) override;
  116. #endif
  117. void GetAdditionalAllowedSchemesForFileSystem(
  118. std::vector<std::string>* additional_schemes) override;
  119. void GetAdditionalWebUISchemes(
  120. std::vector<std::string>* additional_schemes) override;
  121. void SiteInstanceDeleting(content::SiteInstance* site_instance) override;
  122. std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
  123. content::ResourceContext* resource_context) override;
  124. std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider()
  125. override;
  126. network::mojom::NetworkContextPtr CreateNetworkContext(
  127. content::BrowserContext* browser_context,
  128. bool in_memory,
  129. const base::FilePath& relative_partition_path) override;
  130. network::mojom::NetworkContext* GetSystemNetworkContext() override;
  131. base::Optional<service_manager::Manifest> GetServiceManifestOverlay(
  132. base::StringPiece name) override;
  133. content::MediaObserver* GetMediaObserver() override;
  134. content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
  135. content::PlatformNotificationService* GetPlatformNotificationService(
  136. content::BrowserContext* browser_context) override;
  137. std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
  138. const content::MainFunctionParams&) override;
  139. base::FilePath GetDefaultDownloadDirectory() override;
  140. scoped_refptr<network::SharedURLLoaderFactory>
  141. GetSystemSharedURLLoaderFactory() override;
  142. void OnNetworkServiceCreated(
  143. network::mojom::NetworkService* network_service) override;
  144. std::vector<base::FilePath> GetNetworkContextsParentDirectory() override;
  145. std::string GetProduct() override;
  146. void RegisterNonNetworkNavigationURLLoaderFactories(
  147. int frame_tree_node_id,
  148. NonNetworkURLLoaderFactoryMap* factories) override;
  149. void RegisterNonNetworkSubresourceURLLoaderFactories(
  150. int render_process_id,
  151. int render_frame_id,
  152. NonNetworkURLLoaderFactoryMap* factories) override;
  153. void CreateWebSocket(
  154. content::RenderFrameHost* frame,
  155. WebSocketFactory factory,
  156. const GURL& url,
  157. const GURL& site_for_cookies,
  158. const base::Optional<std::string>& user_agent,
  159. mojo::PendingRemote<network::mojom::WebSocketHandshakeClient>
  160. handshake_client) override;
  161. bool WillInterceptWebSocket(content::RenderFrameHost*) override;
  162. bool WillCreateURLLoaderFactory(
  163. content::BrowserContext* browser_context,
  164. content::RenderFrameHost* frame,
  165. int render_process_id,
  166. URLLoaderFactoryType type,
  167. const url::Origin& request_initiator,
  168. mojo::PendingReceiver<network::mojom::URLLoaderFactory>* factory_receiver,
  169. mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
  170. header_client,
  171. bool* bypass_redirect_checks) override;
  172. network::mojom::URLLoaderFactoryPtrInfo
  173. CreateURLLoaderFactoryForNetworkRequests(
  174. content::RenderProcessHost* process,
  175. network::mojom::NetworkContext* network_context,
  176. mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
  177. header_client,
  178. const url::Origin& request_initiator) override;
  179. #if defined(OS_WIN)
  180. bool PreSpawnRenderer(sandbox::TargetPolicy* policy,
  181. RendererSpawnFlags flags) override;
  182. #endif
  183. bool HandleExternalProtocol(
  184. const GURL& url,
  185. content::WebContents::Getter web_contents_getter,
  186. int child_id,
  187. content::NavigationUIData* navigation_data,
  188. bool is_main_frame,
  189. ui::PageTransition page_transition,
  190. bool has_user_gesture,
  191. network::mojom::URLLoaderFactoryPtr* out_factory) override;
  192. std::unique_ptr<content::LoginDelegate> CreateLoginDelegate(
  193. const net::AuthChallengeInfo& auth_info,
  194. content::WebContents* web_contents,
  195. const content::GlobalRequestID& request_id,
  196. bool is_main_frame,
  197. const GURL& url,
  198. scoped_refptr<net::HttpResponseHeaders> response_headers,
  199. bool first_auth_attempt,
  200. LoginAuthRequiredCallback auth_required_callback) override;
  201. // content::RenderProcessHostObserver:
  202. void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
  203. void RenderProcessReady(content::RenderProcessHost* host) override;
  204. void RenderProcessExited(
  205. content::RenderProcessHost* host,
  206. const content::ChildProcessTerminationInfo& info) override;
  207. private:
  208. struct ProcessPreferences {
  209. bool sandbox = false;
  210. bool native_window_open = false;
  211. bool disable_popups = false;
  212. bool web_security = true;
  213. };
  214. bool ShouldForceNewSiteInstance(content::RenderFrameHost* current_rfh,
  215. content::RenderFrameHost* speculative_rfh,
  216. content::BrowserContext* browser_context,
  217. const GURL& dest_url,
  218. bool has_request_started) const;
  219. bool NavigationWasRedirectedCrossSite(
  220. content::BrowserContext* browser_context,
  221. content::SiteInstance* current_instance,
  222. content::SiteInstance* speculative_instance,
  223. const GURL& dest_url,
  224. bool has_request_started) const;
  225. void AddProcessPreferences(int process_id, ProcessPreferences prefs);
  226. void RemoveProcessPreferences(int process_id);
  227. bool IsProcessObserved(int process_id) const;
  228. bool IsRendererSandboxed(int process_id) const;
  229. bool RendererUsesNativeWindowOpen(int process_id) const;
  230. bool RendererDisablesPopups(int process_id) const;
  231. std::string GetAffinityPreference(content::RenderFrameHost* rfh) const;
  232. content::SiteInstance* GetSiteInstanceFromAffinity(
  233. content::BrowserContext* browser_context,
  234. const GURL& url,
  235. content::RenderFrameHost* rfh) const;
  236. void ConsiderSiteInstanceForAffinity(content::RenderFrameHost* rfh,
  237. content::SiteInstance* site_instance);
  238. bool IsRendererSubFrame(int process_id) const;
  239. // pending_render_process => web contents.
  240. std::map<int, content::WebContents*> pending_processes_;
  241. std::map<int, base::ProcessId> render_process_host_pids_;
  242. std::set<int> renderer_is_subframe_;
  243. // list of site per affinity. weak_ptr to prevent instance locking
  244. std::map<std::string, content::SiteInstance*> site_per_affinities_;
  245. std::unique_ptr<PlatformNotificationService> notification_service_;
  246. std::unique_ptr<NotificationPresenter> notification_presenter_;
  247. Delegate* delegate_ = nullptr;
  248. std::map<int, ProcessPreferences> process_preferences_;
  249. std::string user_agent_override_ = "";
  250. bool disable_process_restart_tricks_ = false;
  251. // Simple shared ID generator, used by ProxyingURLLoaderFactory and
  252. // ProxyingWebSocket classes.
  253. uint64_t next_id_ = 0;
  254. DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
  255. };
  256. } // namespace electron
  257. #endif // SHELL_BROWSER_ATOM_BROWSER_CLIENT_H_