atom_browser_client.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 ATOM_BROWSER_ATOM_BROWSER_CLIENT_H_
  5. #define ATOM_BROWSER_ATOM_BROWSER_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/synchronization/lock.h"
  12. #include "content/public/browser/content_browser_client.h"
  13. #include "content/public/browser/render_process_host_observer.h"
  14. #include "net/ssl/client_cert_identity.h"
  15. namespace content {
  16. class QuotaPermissionContext;
  17. class ClientCertificateDelegate;
  18. } // namespace content
  19. namespace net {
  20. class SSLCertRequestInfo;
  21. }
  22. namespace atom {
  23. class AtomResourceDispatcherHostDelegate;
  24. class NotificationPresenter;
  25. class PlatformNotificationService;
  26. class AtomBrowserClient : public content::ContentBrowserClient,
  27. public content::RenderProcessHostObserver {
  28. public:
  29. static AtomBrowserClient* Get();
  30. static void SetApplicationLocale(const std::string& locale);
  31. AtomBrowserClient();
  32. ~AtomBrowserClient() override;
  33. using Delegate = content::ContentBrowserClient;
  34. void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  35. // Returns the WebContents for pending render processes.
  36. content::WebContents* GetWebContentsFromProcessID(int process_id);
  37. // Don't force renderer process to restart for once.
  38. static void SuppressRendererProcessRestartForOnce();
  39. NotificationPresenter* GetNotificationPresenter();
  40. void WebNotificationAllowed(int render_process_id,
  41. const base::Callback<void(bool, bool)>& callback);
  42. // content::NavigatorDelegate
  43. std::vector<std::unique_ptr<content::NavigationThrottle>>
  44. CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
  45. // content::ContentBrowserClient:
  46. std::string GetApplicationLocale() override;
  47. // content::ContentBrowserClient:
  48. bool ShouldEnableStrictSiteIsolation() override;
  49. protected:
  50. void RenderProcessWillLaunch(
  51. content::RenderProcessHost* host,
  52. service_manager::mojom::ServiceRequest* service_request) override;
  53. content::SpeechRecognitionManagerDelegate*
  54. CreateSpeechRecognitionManagerDelegate() override;
  55. content::TtsControllerDelegate* GetTtsControllerDelegate() override;
  56. void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
  57. content::WebPreferences* prefs) override;
  58. SiteInstanceForNavigationType ShouldOverrideSiteInstanceForNavigation(
  59. content::RenderFrameHost* current_rfh,
  60. content::RenderFrameHost* speculative_rfh,
  61. content::BrowserContext* browser_context,
  62. const GURL& url,
  63. bool has_navigation_started,
  64. bool has_request_started,
  65. content::SiteInstance** affinity_site_instance) const override;
  66. void RegisterPendingSiteInstance(
  67. content::RenderFrameHost* render_frame_host,
  68. content::SiteInstance* pending_site_instance) override;
  69. void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
  70. int child_process_id) override;
  71. void AdjustUtilityServiceProcessCommandLine(
  72. const service_manager::Identity& identity,
  73. base::CommandLine* command_line) override;
  74. void DidCreatePpapiPlugin(content::BrowserPpapiHost* browser_host) override;
  75. std::string GetGeolocationApiKey() override;
  76. content::QuotaPermissionContext* CreateQuotaPermissionContext() override;
  77. content::GeneratedCodeCacheSettings GetGeneratedCodeCacheSettings(
  78. content::BrowserContext* context) override;
  79. void AllowCertificateError(
  80. content::WebContents* web_contents,
  81. int cert_error,
  82. const net::SSLInfo& ssl_info,
  83. const GURL& request_url,
  84. content::ResourceType resource_type,
  85. bool strict_enforcement,
  86. bool expired_previous_decision,
  87. const base::Callback<void(content::CertificateRequestResultType)>&
  88. callback) override;
  89. void SelectClientCertificate(
  90. content::WebContents* web_contents,
  91. net::SSLCertRequestInfo* cert_request_info,
  92. net::ClientCertIdentityList client_certs,
  93. std::unique_ptr<content::ClientCertificateDelegate> delegate) override;
  94. void ResourceDispatcherHostCreated() override;
  95. bool CanCreateWindow(content::RenderFrameHost* opener,
  96. const GURL& opener_url,
  97. const GURL& opener_top_level_frame_url,
  98. const url::Origin& source_origin,
  99. content::mojom::WindowContainerType container_type,
  100. const GURL& target_url,
  101. const content::Referrer& referrer,
  102. const std::string& frame_name,
  103. WindowOpenDisposition disposition,
  104. const blink::mojom::WindowFeatures& features,
  105. const std::vector<std::string>& additional_features,
  106. const scoped_refptr<network::ResourceRequestBody>& body,
  107. bool user_gesture,
  108. bool opener_suppressed,
  109. bool* no_javascript_access) override;
  110. void GetAdditionalAllowedSchemesForFileSystem(
  111. std::vector<std::string>* additional_schemes) override;
  112. void GetAdditionalWebUISchemes(
  113. std::vector<std::string>* additional_schemes) override;
  114. void SiteInstanceDeleting(content::SiteInstance* site_instance) override;
  115. std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
  116. content::ResourceContext* resource_context) override;
  117. std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider()
  118. override;
  119. network::mojom::NetworkContextPtr CreateNetworkContext(
  120. content::BrowserContext* browser_context,
  121. bool in_memory,
  122. const base::FilePath& relative_partition_path) override;
  123. void RegisterOutOfProcessServices(OutOfProcessServiceMap* services) override;
  124. base::Optional<service_manager::Manifest> GetServiceManifestOverlay(
  125. base::StringPiece name) override;
  126. net::NetLog* GetNetLog() override;
  127. content::MediaObserver* GetMediaObserver() override;
  128. content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
  129. content::PlatformNotificationService* GetPlatformNotificationService()
  130. override;
  131. content::BrowserMainParts* CreateBrowserMainParts(
  132. const content::MainFunctionParams&) override;
  133. base::FilePath GetDefaultDownloadDirectory() override;
  134. scoped_refptr<network::SharedURLLoaderFactory>
  135. GetSystemSharedURLLoaderFactory() override;
  136. void OnNetworkServiceCreated(
  137. network::mojom::NetworkService* network_service) override;
  138. bool ShouldBypassCORB(int render_process_id) const override;
  139. std::string GetProduct() const override;
  140. std::string GetUserAgent() const override;
  141. // content::RenderProcessHostObserver:
  142. void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
  143. void RenderProcessReady(content::RenderProcessHost* host) override;
  144. void RenderProcessExited(
  145. content::RenderProcessHost* host,
  146. const content::ChildProcessTerminationInfo& info) override;
  147. bool HandleExternalProtocol(
  148. const GURL& url,
  149. content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
  150. int child_id,
  151. content::NavigationUIData* navigation_data,
  152. bool is_main_frame,
  153. ui::PageTransition page_transition,
  154. bool has_user_gesture,
  155. const std::string& method,
  156. const net::HttpRequestHeaders& headers) override;
  157. private:
  158. struct ProcessPreferences {
  159. bool sandbox = false;
  160. bool native_window_open = false;
  161. bool disable_popups = false;
  162. bool web_security = true;
  163. };
  164. bool ShouldForceNewSiteInstance(content::RenderFrameHost* current_rfh,
  165. content::RenderFrameHost* speculative_rfh,
  166. content::BrowserContext* browser_context,
  167. const GURL& dest_url,
  168. bool has_request_started) const;
  169. bool NavigationWasRedirectedCrossSite(
  170. content::BrowserContext* browser_context,
  171. content::SiteInstance* current_instance,
  172. content::SiteInstance* speculative_instance,
  173. const GURL& dest_url,
  174. bool has_request_started) const;
  175. void AddProcessPreferences(int process_id, ProcessPreferences prefs);
  176. void RemoveProcessPreferences(int process_id);
  177. bool IsProcessObserved(int process_id) const;
  178. bool IsRendererSandboxed(int process_id) const;
  179. bool RendererUsesNativeWindowOpen(int process_id) const;
  180. bool RendererDisablesPopups(int process_id) const;
  181. std::string GetAffinityPreference(content::RenderFrameHost* rfh) const;
  182. content::SiteInstance* GetSiteInstanceFromAffinity(
  183. content::BrowserContext* browser_context,
  184. const GURL& url,
  185. content::RenderFrameHost* rfh) const;
  186. void ConsiderSiteInstanceForAffinity(content::RenderFrameHost* rfh,
  187. content::SiteInstance* site_instance);
  188. // pending_render_process => web contents.
  189. std::map<int, content::WebContents*> pending_processes_;
  190. std::map<int, base::ProcessId> render_process_host_pids_;
  191. // list of site per affinity. weak_ptr to prevent instance locking
  192. std::map<std::string, content::SiteInstance*> site_per_affinities_;
  193. std::unique_ptr<AtomResourceDispatcherHostDelegate>
  194. resource_dispatcher_host_delegate_;
  195. std::unique_ptr<PlatformNotificationService> notification_service_;
  196. std::unique_ptr<NotificationPresenter> notification_presenter_;
  197. Delegate* delegate_ = nullptr;
  198. mutable base::Lock process_preferences_lock_;
  199. std::map<int, ProcessPreferences> process_preferences_;
  200. DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
  201. };
  202. } // namespace atom
  203. #endif // ATOM_BROWSER_ATOM_BROWSER_CLIENT_H_