1 // Copyright 2019 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.
5 // This file exposes services from the browser to child processes.
7 #include "chrome/browser/chrome_content_browser_client.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/badging/badge_manager.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/cache_stats_recorder.h"
14 #include "chrome/browser/chrome_browser_interface_binders.h"
15 #include "chrome/browser/chrome_content_browser_client_parts.h"
16 #include "chrome/browser/content_settings/content_settings_manager_delegate.h"
17 #include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings.h"
18 #include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings_factory.h"
19 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
20 #include "chrome/browser/net_benchmarking.h"
21 #include "chrome/browser/predictors/loading_predictor.h"
22 #include "chrome/browser/predictors/loading_predictor_factory.h"
23 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
24 #include "components/content_capture/browser/content_capture_receiver_manager.h"
25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
26 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
27 #include "components/metrics/call_stack_profile_collector.h"
28 #include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
29 #include "components/safe_browsing/buildflags.h"
30 #include "components/safe_browsing/content/browser/mojo_safe_browsing_impl.h"
31 #include "components/spellcheck/spellcheck_buildflags.h"
32 #include "content/public/browser/browser_task_traits.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/render_process_host.h"
35 #include "content/public/browser/resource_context.h"
36 #include "media/mojo/buildflags.h"
37 #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
38 #include "third_party/widevine/cdm/buildflags.h"
40 #if defined(OS_ANDROID)
41 #include "chrome/browser/download/android/available_offline_content_provider.h"
43 #include "chrome/browser/win/conflicts/module_database.h"
44 #include "chrome/browser/win/conflicts/module_event_sink_impl.h"
45 #elif defined(OS_CHROMEOS)
46 #include "chromeos/components/cdm_factory_daemon/cdm_factory_daemon_proxy.h"
49 #if BUILDFLAG(ENABLE_EXTENSIONS)
50 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
51 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
52 #include "extensions/browser/extensions_browser_client.h"
55 #if BUILDFLAG(ENABLE_LIBRARY_CDMS)
56 #include "chrome/browser/media/output_protection_impl.h"
57 #include "chrome/browser/media/platform_verification_impl.h"
60 #if BUILDFLAG(ENABLE_MOJO_CDM) && defined(OS_ANDROID)
61 #include "chrome/browser/media/android/cdm/media_drm_storage_factory.h"
64 #if BUILDFLAG(ENABLE_SPELLCHECK)
65 #include "chrome/browser/spellchecker/spell_check_host_chrome_impl.h"
66 #include "components/spellcheck/common/spellcheck.mojom.h"
67 #if BUILDFLAG(HAS_SPELLCHECK_PANEL)
68 #include "chrome/browser/spellchecker/spell_check_panel_host_impl.h"
74 // Helper method for ExposeInterfacesToRenderer() that checks the latest
75 // SafeBrowsing pref value on the UI thread before hopping over to the IO
77 void MaybeCreateSafeBrowsingForRenderer(
79 content::ResourceContext* resource_context,
80 base::RepeatingCallback<scoped_refptr<safe_browsing::UrlCheckerDelegate>(
81 bool safe_browsing_enabled,
82 bool should_check_on_sb_disabled)> get_checker_delegate,
83 mojo::PendingReceiver<safe_browsing::mojom::SafeBrowsing> receiver) {
84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
86 content::RenderProcessHost* render_process_host =
87 content::RenderProcessHost::FromID(process_id);
88 if (!render_process_host)
91 bool safe_browsing_enabled = safe_browsing::IsSafeBrowsingEnabled(
92 *Profile::FromBrowserContext(render_process_host->GetBrowserContext())
94 content::GetIOThreadTaskRunner({})->PostTask(
97 &safe_browsing::MojoSafeBrowsingImpl::MaybeCreate, process_id,
99 base::BindRepeating(get_checker_delegate, safe_browsing_enabled,
100 // Navigation initiated from renderer should never
101 // check when safe browsing is disabled, because
102 // enterprise check only supports mainframe URL.
103 /*should_check_on_sb_disabled=*/false),
104 std::move(receiver)));
109 void ChromeContentBrowserClient::ExposeInterfacesToRenderer(
110 service_manager::BinderRegistry* registry,
111 blink::AssociatedInterfaceRegistry* associated_registry,
112 content::RenderProcessHost* render_process_host) {
113 // The CacheStatsRecorder is an associated binding, instead of a
114 // non-associated one, because the sender (in the renderer process) posts the
115 // message after a time delay, in order to rate limit. The association
116 // protects against the render process host ID being recycled in that time
117 // gap between the preparation and the execution of that IPC.
118 associated_registry->AddInterface(
119 base::Bind(&CacheStatsRecorder::Create, render_process_host->GetID()));
121 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner =
122 content::GetUIThreadTaskRunner({});
123 registry->AddInterface(
124 base::BindRepeating(&metrics::CallStackProfileCollector::Create));
126 if (NetBenchmarking::CheckBenchmarkingEnabled()) {
128 Profile::FromBrowserContext(render_process_host->GetBrowserContext());
129 auto* loading_predictor =
130 predictors::LoadingPredictorFactory::GetForProfile(profile);
131 registry->AddInterface(
133 &NetBenchmarking::Create,
134 loading_predictor ? loading_predictor->GetWeakPtr() : nullptr,
135 render_process_host->GetID()),
139 #if BUILDFLAG(SAFE_BROWSING_DB_LOCAL) || BUILDFLAG(SAFE_BROWSING_DB_REMOTE)
140 if (safe_browsing_service_) {
141 content::ResourceContext* resource_context =
142 render_process_host->GetBrowserContext()->GetResourceContext();
143 registry->AddInterface(
145 &MaybeCreateSafeBrowsingForRenderer, render_process_host->GetID(),
148 &ChromeContentBrowserClient::GetSafeBrowsingUrlCheckerDelegate,
149 base::Unretained(this))),
155 // Add the ModuleEventSink interface. This is the interface used by renderer
156 // processes to notify the browser of modules in their address space. The
157 // process handle is not yet available at this point so pass in a callback
158 // to allow to retrieve a duplicate at the time the interface is actually
160 auto get_process = base::BindRepeating(
161 [](int id) -> base::Process {
162 auto* host = content::RenderProcessHost::FromID(id);
164 return host->GetProcess().Duplicate();
165 return base::Process();
167 render_process_host->GetID());
168 registry->AddInterface(
170 &ModuleEventSinkImpl::Create, std::move(get_process),
171 content::PROCESS_TYPE_RENDERER,
172 base::BindRepeating(&ModuleDatabase::HandleModuleLoadEvent)),
175 #if defined(OS_ANDROID)
177 Profile::FromBrowserContext(render_process_host->GetBrowserContext());
178 registry->AddInterface(
179 base::BindRepeating(&android::AvailableOfflineContentProvider::Create,
181 content::GetUIThreadTaskRunner({}));
184 for (auto* ep : extra_parts_) {
185 ep->ExposeInterfacesToRenderer(registry, associated_registry,
186 render_process_host);
190 void ChromeContentBrowserClient::BindMediaServiceReceiver(
191 content::RenderFrameHost* render_frame_host,
192 mojo::GenericPendingReceiver receiver) {
193 #if BUILDFLAG(ENABLE_LIBRARY_CDMS)
194 if (auto r = receiver.As<media::mojom::OutputProtection>()) {
195 OutputProtectionImpl::Create(render_frame_host, std::move(r));
199 if (auto r = receiver.As<media::mojom::PlatformVerification>()) {
200 PlatformVerificationImpl::Create(render_frame_host, std::move(r));
203 #endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
205 #if BUILDFLAG(ENABLE_MOJO_CDM) && defined(OS_ANDROID)
206 if (auto r = receiver.As<media::mojom::MediaDrmStorage>()) {
207 CreateMediaDrmStorage(render_frame_host, std::move(r));
213 void ChromeContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
214 content::RenderFrameHost* render_frame_host,
215 mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
216 chrome::internal::PopulateChromeFrameBinders(map);
217 chrome::internal::PopulateChromeWebUIFrameBinders(map);
219 #if BUILDFLAG(ENABLE_EXTENSIONS)
220 const GURL& site = render_frame_host->GetSiteInstance()->GetSiteURL();
221 if (!site.SchemeIs(extensions::kExtensionScheme))
224 content::BrowserContext* browser_context =
225 render_frame_host->GetProcess()->GetBrowserContext();
226 auto* extension = extensions::ExtensionRegistry::Get(browser_context)
227 ->enabled_extensions()
228 .GetByID(site.host());
231 extensions::ExtensionsBrowserClient::Get()
232 ->RegisterBrowserInterfaceBindersForFrame(map, render_frame_host,
237 bool ChromeContentBrowserClient::BindAssociatedReceiverFromFrame(
238 content::RenderFrameHost* render_frame_host,
239 const std::string& interface_name,
240 mojo::ScopedInterfaceEndpointHandle* handle) {
241 if (interface_name == autofill::mojom::AutofillDriver::Name_) {
242 autofill::ContentAutofillDriverFactory::BindAutofillDriver(
243 mojo::PendingAssociatedReceiver<autofill::mojom::AutofillDriver>(
248 if (interface_name == autofill::mojom::PasswordManagerDriver::Name_) {
249 password_manager::ContentPasswordManagerDriverFactory::
250 BindPasswordManagerDriver(
251 mojo::PendingAssociatedReceiver<
252 autofill::mojom::PasswordManagerDriver>(std::move(*handle)),
256 if (interface_name == content_capture::mojom::ContentCaptureReceiver::Name_) {
257 content_capture::ContentCaptureReceiverManager::BindContentCaptureReceiver(
258 mojo::PendingAssociatedReceiver<
259 content_capture::mojom::ContentCaptureReceiver>(std::move(*handle)),
267 void ChromeContentBrowserClient::BindBadgeServiceReceiverFromServiceWorker(
268 content::RenderProcessHost* service_worker_process_host,
269 const GURL& service_worker_scope,
270 mojo::PendingReceiver<blink::mojom::BadgeService> receiver) {
271 #if !defined(OS_ANDROID)
272 badging::BadgeManager::BindServiceWorkerReceiver(
273 service_worker_process_host, service_worker_scope, std::move(receiver));
277 void ChromeContentBrowserClient::BindGpuHostReceiver(
278 mojo::GenericPendingReceiver receiver) {
279 if (auto r = receiver.As<metrics::mojom::CallStackProfileCollector>()) {
280 metrics::CallStackProfileCollector::Create(std::move(r));
284 #if defined(OS_CHROMEOS)
285 if (auto r = receiver.As<chromeos::cdm::mojom::CdmFactoryDaemon>())
286 chromeos::CdmFactoryDaemonProxy::Create(std::move(r));
287 #endif // OS_CHROMEOS
290 void ChromeContentBrowserClient::BindUtilityHostReceiver(
291 mojo::GenericPendingReceiver receiver) {
292 if (auto r = receiver.As<metrics::mojom::CallStackProfileCollector>())
293 metrics::CallStackProfileCollector::Create(std::move(r));
296 void ChromeContentBrowserClient::BindHostReceiverForRenderer(
297 content::RenderProcessHost* render_process_host,
298 mojo::GenericPendingReceiver receiver) {
299 if (auto host_receiver =
300 receiver.As<content_settings::mojom::ContentSettingsManager>()) {
301 content_settings::ContentSettingsManagerImpl::Create(
302 render_process_host, std::move(host_receiver),
303 std::make_unique<chrome::ContentSettingsManagerDelegate>());
307 #if BUILDFLAG(ENABLE_SPELLCHECK)
308 if (auto host_receiver = receiver.As<spellcheck::mojom::SpellCheckHost>()) {
309 SpellCheckHostChromeImpl::Create(render_process_host->GetID(),
310 std::move(host_receiver));
314 #if BUILDFLAG(HAS_SPELLCHECK_PANEL)
315 if (auto host_receiver =
316 receiver.As<spellcheck::mojom::SpellCheckPanelHost>()) {
317 SpellCheckPanelHostImpl::Create(render_process_host->GetID(),
318 std::move(host_receiver));
321 #endif // BUILDFLAG(HAS_SPELLCHECK_PANEL)
322 #endif // BUILDFLAG(ENABLE_SPELLCHECK)
324 #if BUILDFLAG(ENABLE_PLUGINS)
325 if (auto host_receiver = receiver.As<chrome::mojom::MetricsService>()) {
326 ChromeMetricsServiceAccessor::BindMetricsServiceReceiver(
327 std::move(host_receiver));
329 #endif // BUILDFLAG(ENABLE_PLUGINS)