cast_content_browser_client.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROMECAST_BROWSER_CAST_CONTENT_BROWSER_CLIENT_H_
  5. #define CHROMECAST_BROWSER_CAST_CONTENT_BROWSER_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/threading/thread.h"
  12. #include "build/build_config.h"
  13. #include "build/buildflag.h"
  14. #include "chromecast/chromecast_buildflags.h"
  15. #include "chromecast/metrics/cast_metrics_service_client.h"
  16. #include "content/public/browser/certificate_request_result_type.h"
  17. #include "content/public/browser/content_browser_client.h"
  18. #include "media/mojo/buildflags.h"
  19. #include "media/mojo/mojom/media_service.mojom.h"
  20. #include "media/mojo/mojom/renderer.mojom.h"
  21. #include "mojo/public/cpp/bindings/pending_receiver.h"
  22. #include "services/metrics/public/cpp/ukm_source_id.h"
  23. #include "services/service_manager/public/cpp/binder_registry.h"
  24. #include "services/service_manager/public/cpp/identity.h"
  25. #include "services/service_manager/public/cpp/manifest.h"
  26. #include "services/service_manager/public/mojom/interface_provider.mojom-forward.h"
  27. #include "services/service_manager/public/mojom/service.mojom-forward.h"
  28. #include "storage/browser/quota/quota_settings.h"
  29. class PrefService;
  30. namespace base {
  31. struct OnTaskRunnerDeleter;
  32. }
  33. namespace breakpad {
  34. class CrashHandlerHostLinux;
  35. }
  36. namespace device {
  37. class BluetoothAdapterCast;
  38. }
  39. namespace media {
  40. class CdmFactory;
  41. }
  42. namespace metrics {
  43. class MetricsService;
  44. }
  45. namespace net {
  46. class SSLPrivateKey;
  47. class X509Certificate;
  48. }
  49. namespace chromecast {
  50. class CastService;
  51. class CastSystemMemoryPressureEvaluatorAdjuster;
  52. class CastWebService;
  53. class CastWindowManager;
  54. class CastFeatureListCreator;
  55. class DisplaySettingsManager;
  56. class GeneralAudienceBrowsingService;
  57. class MemoryPressureControllerImpl;
  58. class ServiceConnector;
  59. namespace media {
  60. class MediaCapsImpl;
  61. class CmaBackendFactory;
  62. class MediaPipelineBackendManager;
  63. class MediaResourceTracker;
  64. class VideoGeometrySetterService;
  65. class VideoPlaneController;
  66. class VideoModeSwitcher;
  67. class VideoResolutionPolicy;
  68. }
  69. namespace shell {
  70. class CastBrowserMainParts;
  71. class CastNetworkContexts;
  72. class CastContentBrowserClient
  73. : public content::ContentBrowserClient,
  74. public chromecast::metrics::CastMetricsServiceDelegate {
  75. public:
  76. // Creates an implementation of CastContentBrowserClient. Platform should
  77. // link in an implementation as needed.
  78. static std::unique_ptr<CastContentBrowserClient> Create(
  79. CastFeatureListCreator* cast_feature_list_creator);
  80. // Returns a list of headers that will be exempt from CORS preflight checks.
  81. // This is needed since currently servers don't have the correct response to
  82. // preflight checks.
  83. static std::vector<std::string> GetCorsExemptHeadersList();
  84. CastContentBrowserClient(const CastContentBrowserClient&) = delete;
  85. CastContentBrowserClient& operator=(const CastContentBrowserClient&) = delete;
  86. ~CastContentBrowserClient() override;
  87. // Creates a ServiceConnector for routing Cast-related service interface
  88. // binding requests.
  89. virtual std::unique_ptr<chromecast::ServiceConnector>
  90. CreateServiceConnector();
  91. // Creates and returns the CastService instance for the current process.
  92. virtual std::unique_ptr<CastService> CreateCastService(
  93. content::BrowserContext* browser_context,
  94. CastSystemMemoryPressureEvaluatorAdjuster*
  95. cast_system_memory_pressure_evaluator_adjuster,
  96. PrefService* pref_service,
  97. media::VideoPlaneController* video_plane_controller,
  98. CastWindowManager* window_manager,
  99. CastWebService* web_service,
  100. DisplaySettingsManager* display_settings_manager);
  101. virtual media::VideoModeSwitcher* GetVideoModeSwitcher();
  102. virtual void InitializeURLLoaderThrottleDelegate();
  103. virtual void SetPersistentCookieAccessSettings(PrefService* pref_service);
  104. // Returns the task runner that must be used for media IO.
  105. scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner();
  106. // Gets object for enforcing video resolution policy restrictions.
  107. virtual media::VideoResolutionPolicy* GetVideoResolutionPolicy();
  108. // Creates a CmaBackendFactory.
  109. virtual media::CmaBackendFactory* GetCmaBackendFactory();
  110. media::MediaResourceTracker* media_resource_tracker();
  111. void ResetMediaResourceTracker();
  112. media::MediaPipelineBackendManager* media_pipeline_backend_manager();
  113. std::unique_ptr<::media::AudioManager> CreateAudioManager(
  114. ::media::AudioLogFactory* audio_log_factory) override;
  115. bool OverridesAudioManager() override;
  116. media::MediaCapsImpl* media_caps();
  117. #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
  118. // Create a BluetoothAdapter for WebBluetooth support.
  119. // TODO(slan): This further couples the browser to the Cast service. Remove
  120. // this once the dedicated Bluetooth service has been implemented.
  121. // (b/76155468)
  122. virtual scoped_refptr<device::BluetoothAdapterCast> CreateBluetoothAdapter();
  123. #endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
  124. // chromecast::metrics::CastMetricsServiceDelegate implementation:
  125. void SetMetricsClientId(const std::string& client_id) override;
  126. void RegisterMetricsProviders(
  127. ::metrics::MetricsService* metrics_service) override;
  128. // Returns whether or not the remote debugging service should be started
  129. // on browser startup.
  130. virtual bool EnableRemoteDebuggingImmediately();
  131. // Note: These were originally part of ContentBrowserClient, but have been
  132. // lifted into this class as they're now only used by Chromecast. This is a
  133. // transitional step to avoid breakage in the internal downstream repository.
  134. virtual void RunServiceInstance(
  135. const service_manager::Identity& identity,
  136. mojo::PendingReceiver<service_manager::mojom::Service>* receiver);
  137. virtual absl::optional<service_manager::Manifest> GetServiceManifestOverlay(
  138. base::StringPiece service_name);
  139. std::vector<service_manager::Manifest> GetExtraServiceManifests();
  140. std::vector<std::string> GetStartupServices();
  141. // content::ContentBrowserClient implementation:
  142. std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
  143. bool is_integration_test) override;
  144. void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
  145. bool IsHandledURL(const GURL& url) override;
  146. void SiteInstanceGotProcess(content::SiteInstance* site_instance) override;
  147. void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
  148. int child_process_id) override;
  149. std::string GetAcceptLangs(content::BrowserContext* context) override;
  150. network::mojom::NetworkContext* GetSystemNetworkContext() override;
  151. void OverrideWebkitPrefs(content::WebContents* web_contents,
  152. blink::web_pref::WebPreferences* prefs) override;
  153. std::string GetApplicationLocale() override;
  154. scoped_refptr<content::QuotaPermissionContext> CreateQuotaPermissionContext()
  155. override;
  156. void AllowCertificateError(
  157. content::WebContents* web_contents,
  158. int cert_error,
  159. const net::SSLInfo& ssl_info,
  160. const GURL& request_url,
  161. bool is_primary_main_frame_request,
  162. bool strict_enforcement,
  163. base::OnceCallback<void(content::CertificateRequestResultType)> callback)
  164. override;
  165. base::OnceClosure SelectClientCertificate(
  166. content::WebContents* web_contents,
  167. net::SSLCertRequestInfo* cert_request_info,
  168. net::ClientCertIdentityList client_certs,
  169. std::unique_ptr<content::ClientCertificateDelegate> delegate) override;
  170. bool CanCreateWindow(content::RenderFrameHost* opener,
  171. const GURL& opener_url,
  172. const GURL& opener_top_level_frame_url,
  173. const url::Origin& source_origin,
  174. content::mojom::WindowContainerType container_type,
  175. const GURL& target_url,
  176. const content::Referrer& referrer,
  177. const std::string& frame_name,
  178. WindowOpenDisposition disposition,
  179. const blink::mojom::WindowFeatures& features,
  180. bool user_gesture,
  181. bool opener_suppressed,
  182. bool* no_javascript_access) override;
  183. // New Mojo bindings should be added to
  184. // cast_content_browser_client_receiver_bindings.cc, so that they go through
  185. // security review.
  186. void ExposeInterfacesToRenderer(
  187. service_manager::BinderRegistry* registry,
  188. blink::AssociatedInterfaceRegistry* associated_registry,
  189. content::RenderProcessHost* render_process_host) override;
  190. void BindMediaServiceReceiver(content::RenderFrameHost* render_frame_host,
  191. mojo::GenericPendingReceiver receiver) override;
  192. void RegisterBrowserInterfaceBindersForFrame(
  193. content::RenderFrameHost* render_frame_host,
  194. mojo::BinderMapWithContext<content::RenderFrameHost*>* map) override;
  195. mojo::Remote<::media::mojom::MediaService> RunSecondaryMediaService()
  196. override;
  197. void GetAdditionalMappedFilesForChildProcess(
  198. const base::CommandLine& command_line,
  199. int child_process_id,
  200. content::PosixFileDescriptorInfo* mappings) override;
  201. void GetAdditionalWebUISchemes(
  202. std::vector<std::string>* additional_schemes) override;
  203. std::unique_ptr<content::DevToolsManagerDelegate>
  204. CreateDevToolsManagerDelegate() override;
  205. std::unique_ptr<content::NavigationUIData> GetNavigationUIData(
  206. content::NavigationHandle* navigation_handle) override;
  207. bool ShouldEnableStrictSiteIsolation() override;
  208. std::vector<std::unique_ptr<content::NavigationThrottle>>
  209. CreateThrottlesForNavigation(content::NavigationHandle* handle) override;
  210. void RegisterNonNetworkNavigationURLLoaderFactories(
  211. int frame_tree_node_id,
  212. ukm::SourceIdObj ukm_source_id,
  213. NonNetworkURLLoaderFactoryMap* factories) override;
  214. void RegisterNonNetworkSubresourceURLLoaderFactories(
  215. int render_process_id,
  216. int render_frame_id,
  217. const absl::optional<url::Origin>& request_initiator_origin,
  218. NonNetworkURLLoaderFactoryMap* factories) override;
  219. void OnNetworkServiceCreated(
  220. network::mojom::NetworkService* network_service) override;
  221. void ConfigureNetworkContextParams(
  222. content::BrowserContext* context,
  223. bool in_memory,
  224. const base::FilePath& relative_partition_path,
  225. network::mojom::NetworkContextParams* network_context_params,
  226. cert_verifier::mojom::CertVerifierCreationParams*
  227. cert_verifier_creation_params) override;
  228. std::string GetUserAgent() override;
  229. bool DoesSiteRequireDedicatedProcess(content::BrowserContext* browser_context,
  230. const GURL& effective_site_url) override;
  231. bool IsWebUIAllowedToMakeNetworkRequests(const url::Origin& origin) override;
  232. bool ShouldAllowInsecurePrivateNetworkRequests(
  233. content::BrowserContext* browser_context,
  234. const url::Origin& origin) override;
  235. std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
  236. CreateURLLoaderThrottles(
  237. const network::ResourceRequest& request,
  238. content::BrowserContext* browser_context,
  239. const base::RepeatingCallback<content::WebContents*()>& wc_getter,
  240. content::NavigationUIData* navigation_ui_data,
  241. int frame_tree_node_id) override;
  242. CastFeatureListCreator* GetCastFeatureListCreator() {
  243. return cast_feature_list_creator_;
  244. }
  245. void CreateGeneralAudienceBrowsingService();
  246. virtual std::unique_ptr<::media::CdmFactory> CreateCdmFactory(
  247. ::media::mojom::FrameInterfaceFactory* frame_interfaces);
  248. #if BUILDFLAG(ENABLE_CAST_RENDERER)
  249. void BindGpuHostReceiver(mojo::GenericPendingReceiver receiver) override;
  250. #endif // BUILDFLAG(ENABLE_CAST_RENDERER)
  251. CastNetworkContexts* cast_network_contexts() {
  252. return cast_network_contexts_.get();
  253. }
  254. protected:
  255. explicit CastContentBrowserClient(
  256. CastFeatureListCreator* cast_feature_list_creator);
  257. CastBrowserMainParts* browser_main_parts() {
  258. return cast_browser_main_parts_;
  259. }
  260. void BindMediaRenderer(
  261. mojo::PendingReceiver<::media::mojom::Renderer> receiver);
  262. void GetApplicationMediaInfo(std::string* application_session_id,
  263. bool* mixer_audio_enabled,
  264. content::RenderFrameHost* render_frame_host);
  265. // Returns whether buffering should be used for the CMA Pipeline created for
  266. // this runtime instance. May be called from any thread.
  267. virtual bool IsBufferingEnabled();
  268. private:
  269. // Create device cert/key
  270. virtual scoped_refptr<net::X509Certificate> DeviceCert();
  271. virtual scoped_refptr<net::SSLPrivateKey> DeviceKey();
  272. virtual bool IsWhitelisted(const GURL& gurl,
  273. const std::string& session_id,
  274. int render_process_id,
  275. int render_frame_id,
  276. bool for_device_auth);
  277. void SelectClientCertificateOnIOThread(
  278. GURL requesting_url,
  279. const std::string& session_id,
  280. int render_process_id,
  281. int render_frame_id,
  282. scoped_refptr<base::SequencedTaskRunner> original_runner,
  283. base::OnceCallback<void(scoped_refptr<net::X509Certificate>,
  284. scoped_refptr<net::SSLPrivateKey>)>
  285. continue_callback);
  286. #if !BUILDFLAG(IS_FUCHSIA)
  287. // Returns the crash signal FD corresponding to the current process type.
  288. int GetCrashSignalFD(const base::CommandLine& command_line);
  289. #if !BUILDFLAG(IS_ANDROID)
  290. // Creates a CrashHandlerHost instance for the given process type.
  291. breakpad::CrashHandlerHostLinux* CreateCrashHandlerHost(
  292. const std::string& process_type);
  293. // A static cache to hold crash_handlers for each process_type
  294. std::map<std::string, breakpad::CrashHandlerHostLinux*> crash_handlers_;
  295. // Notify renderers of memory pressure (Android renderers register directly
  296. // with OS for this).
  297. std::unique_ptr<MemoryPressureControllerImpl> memory_pressure_controller_;
  298. #endif // !BUILDFLAG(IS_ANDROID)
  299. #endif // !BUILDFLAG(IS_FUCHSIA)
  300. // CMA thread used by AudioManager, MojoRenderer, and MediaPipelineBackend.
  301. std::unique_ptr<base::Thread> media_thread_;
  302. // Tracks usage of media resource by e.g. CMA pipeline, CDM.
  303. media::MediaResourceTracker* media_resource_tracker_ = nullptr;
  304. #if BUILDFLAG(ENABLE_CAST_RENDERER)
  305. void CreateMediaService(
  306. mojo::PendingReceiver<::media::mojom::MediaService> receiver);
  307. // VideoGeometrySetterService must be constructed On a sequence, and later
  308. // runs and destructs on this sequence.
  309. void CreateVideoGeometrySetterServiceOnMediaThread();
  310. void BindVideoGeometrySetterServiceOnMediaThread(
  311. mojo::GenericPendingReceiver receiver);
  312. // video_geometry_setter_service_ lives on media thread.
  313. std::unique_ptr<media::VideoGeometrySetterService, base::OnTaskRunnerDeleter>
  314. video_geometry_setter_service_;
  315. #endif
  316. // Created by CastContentBrowserClient but owned by BrowserMainLoop.
  317. CastBrowserMainParts* cast_browser_main_parts_;
  318. std::unique_ptr<CastNetworkContexts> cast_network_contexts_;
  319. std::unique_ptr<media::CmaBackendFactory> cma_backend_factory_;
  320. std::unique_ptr<GeneralAudienceBrowsingService>
  321. general_audience_browsing_service_;
  322. CastFeatureListCreator* cast_feature_list_creator_;
  323. };
  324. } // namespace shell
  325. } // namespace chromecast
  326. #endif // CHROMECAST_BROWSER_CAST_CONTENT_BROWSER_CLIENT_H_