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.
4 
5 #include "content/browser/browser_interface_binders.h"
6 
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "base/feature_list.h"
10 #include "build/build_config.h"
11 #include "cc/base/switches.h"
12 #include "content/browser/background_fetch/background_fetch_service_impl.h"
13 #include "content/browser/bad_message.h"
14 #include "content/browser/browser_main_loop.h"
15 #include "content/browser/content_index/content_index_service_impl.h"
16 #include "content/browser/conversions/conversion_internals.mojom.h"
17 #include "content/browser/conversions/conversion_internals_ui.h"
18 #include "content/browser/cookie_store/cookie_store_context.h"
19 #include "content/browser/eye_dropper_chooser_impl.h"
20 #include "content/browser/feature_observer.h"
21 #include "content/browser/federated_learning/floc_service_impl.h"
22 #include "content/browser/gpu/gpu_process_host.h"
23 #include "content/browser/image_capture/image_capture_impl.h"
24 #include "content/browser/keyboard_lock/keyboard_lock_service_impl.h"
25 #include "content/browser/loader/content_security_notifier.h"
26 #include "content/browser/media/midi_host.h"
27 #include "content/browser/media/session/media_session_service_impl.h"
28 #include "content/browser/picture_in_picture/picture_in_picture_service_impl.h"
29 #include "content/browser/process_internals/process_internals.mojom.h"
30 #include "content/browser/process_internals/process_internals_ui.h"
31 #include "content/browser/renderer_host/clipboard_host_impl.h"
32 #include "content/browser/renderer_host/media/media_devices_dispatcher_host.h"
33 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
34 #include "content/browser/renderer_host/raw_clipboard_host_impl.h"
35 #include "content/browser/renderer_host/render_frame_host_impl.h"
36 #include "content/browser/renderer_host/render_process_host_impl.h"
37 #include "content/browser/screen_enumeration/screen_enumeration_impl.h"
38 #include "content/browser/service_worker/service_worker_host.h"
39 #include "content/browser/speech/speech_recognition_dispatcher_host.h"
40 #include "content/browser/wake_lock/wake_lock_service_impl.h"
41 #include "content/browser/web_contents/file_chooser_impl.h"
42 #include "content/browser/web_contents/web_contents_impl.h"
43 #include "content/browser/worker_host/dedicated_worker_host.h"
44 #include "content/browser/worker_host/shared_worker_connector_impl.h"
45 #include "content/browser/worker_host/shared_worker_host.h"
46 #include "content/browser/xr/service/vr_service_impl.h"
47 #include "content/common/input/input_injector.mojom.h"
48 #include "content/public/browser/browser_context.h"
49 #include "content/public/browser/browser_thread.h"
50 #include "content/public/browser/device_service.h"
51 #include "content/public/browser/service_worker_context.h"
52 #include "content/public/browser/shared_worker_instance.h"
53 #include "content/public/browser/storage_partition.h"
54 #include "content/public/common/content_client.h"
55 #include "content/public/common/content_features.h"
56 #include "content/public/common/url_constants.h"
57 #include "device/gamepad/gamepad_monitor.h"
58 #include "device/gamepad/public/mojom/gamepad.mojom.h"
59 #include "device/vr/buildflags/buildflags.h"
60 #include "device/vr/public/mojom/vr_service.mojom.h"
61 #include "media/capture/mojom/image_capture.mojom.h"
62 #include "media/mojo/mojom/interface_factory.mojom-forward.h"
63 #include "media/mojo/mojom/media_metrics_provider.mojom.h"
64 #include "media/mojo/mojom/remoting.mojom.h"
65 #include "media/mojo/mojom/video_decode_perf_history.mojom.h"
66 #include "media/mojo/services/video_decode_perf_history.h"
67 #include "services/device/public/mojom/sensor_provider.mojom.h"
68 #include "services/device/public/mojom/vibration_manager.mojom.h"
69 #include "services/metrics/public/mojom/ukm_interface.mojom.h"
70 #include "services/metrics/ukm_recorder_interface.h"
71 #include "services/network/public/cpp/cross_origin_embedder_policy.h"
72 #include "services/network/public/mojom/restricted_cookie_manager.mojom.h"
73 #include "services/shape_detection/public/mojom/barcodedetection_provider.mojom.h"
74 #include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
75 #include "services/shape_detection/public/mojom/shape_detection_service.mojom.h"
76 #include "services/shape_detection/public/mojom/textdetection.mojom.h"
77 #include "third_party/blink/public/common/features.h"
78 #include "third_party/blink/public/mojom/appcache/appcache.mojom.h"
79 #include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
80 #include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
81 #include "third_party/blink/public/mojom/badging/badging.mojom.h"
82 #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom.h"
83 #include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom.h"
84 #include "third_party/blink/public/mojom/choosers/color_chooser.mojom.h"
85 #include "third_party/blink/public/mojom/content_index/content_index.mojom.h"
86 #include "third_party/blink/public/mojom/cookie_store/cookie_store.mojom.h"
87 #include "third_party/blink/public/mojom/credentialmanager/credential_manager.mojom.h"
88 #include "third_party/blink/public/mojom/feature_observer/feature_observer.mojom.h"
89 #include "third_party/blink/public/mojom/file_system_access/native_file_system_manager.mojom.h"
90 #include "third_party/blink/public/mojom/filesystem/file_system.mojom.h"
91 #include "third_party/blink/public/mojom/geolocation/geolocation_service.mojom.h"
92 #include "third_party/blink/public/mojom/idle/idle_manager.mojom.h"
93 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom.h"
94 #include "third_party/blink/public/mojom/input/input_host.mojom.h"
95 #include "third_party/blink/public/mojom/insecure_input/insecure_input_service.mojom.h"
96 #include "third_party/blink/public/mojom/keyboard_lock/keyboard_lock.mojom.h"
97 #include "third_party/blink/public/mojom/loader/content_security_notifier.mojom.h"
98 #include "third_party/blink/public/mojom/loader/navigation_predictor.mojom.h"
99 #include "third_party/blink/public/mojom/locks/lock_manager.mojom.h"
100 #include "third_party/blink/public/mojom/media/renderer_audio_input_stream_factory.mojom.h"
101 #include "third_party/blink/public/mojom/media/renderer_audio_output_stream_factory.mojom.h"
102 #include "third_party/blink/public/mojom/mediasession/media_session.mojom.h"
103 #include "third_party/blink/public/mojom/mediastream/media_devices.mojom.h"
104 #include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
105 #include "third_party/blink/public/mojom/native_io/native_io.mojom.h"
106 #include "third_party/blink/public/mojom/notifications/notification_service.mojom.h"
107 #include "third_party/blink/public/mojom/payments/payment_app.mojom.h"
108 #include "third_party/blink/public/mojom/payments/payment_credential.mojom.h"
109 #include "third_party/blink/public/mojom/permissions/permission.mojom.h"
110 #include "third_party/blink/public/mojom/picture_in_picture/picture_in_picture.mojom.h"
111 #include "third_party/blink/public/mojom/prerender/prerender.mojom.h"
112 #include "third_party/blink/public/mojom/presentation/presentation.mojom.h"
113 #include "third_party/blink/public/mojom/quota/quota_manager_host.mojom.h"
114 #include "third_party/blink/public/mojom/sms/webotp_service.mojom.h"
115 #include "third_party/blink/public/mojom/speech/speech_recognizer.mojom.h"
116 #include "third_party/blink/public/mojom/speech/speech_synthesis.mojom.h"
117 #include "third_party/blink/public/mojom/usb/web_usb_service.mojom.h"
118 #include "third_party/blink/public/mojom/wake_lock/wake_lock.mojom.h"
119 #include "third_party/blink/public/mojom/webaudio/audio_context_manager.mojom.h"
120 #include "third_party/blink/public/mojom/webauthn/authenticator.mojom.h"
121 #include "third_party/blink/public/mojom/webauthn/virtual_authenticator.mojom.h"
122 #include "third_party/blink/public/mojom/webid/federated_auth_request.mojom.h"
123 #include "third_party/blink/public/mojom/websockets/websocket_connector.mojom.h"
124 #include "third_party/blink/public/mojom/webtransport/quic_transport_connector.mojom.h"
125 #include "third_party/blink/public/mojom/worker/dedicated_worker_host_factory.mojom.h"
126 #include "third_party/blink/public/mojom/worker/shared_worker_connector.mojom.h"
127 #include "third_party/blink/public/public_buildflags.h"
128 
129 #if !defined(OS_ANDROID)
130 #include "content/browser/direct_sockets/direct_sockets_service_impl.h"
131 #include "content/browser/installedapp/installed_app_provider_impl.h"
132 #include "content/public/common/content_switches.h"
133 #include "media/mojo/mojom/speech_recognition_service.mojom.h"
134 #include "third_party/blink/public/mojom/hid/hid.mojom.h"
135 #include "third_party/blink/public/mojom/installedapp/installed_app_provider.mojom.h"
136 #include "third_party/blink/public/mojom/serial/serial.mojom.h"
137 #endif
138 
139 #if defined(OS_ANDROID)
140 #include "content/browser/android/date_time_chooser_android.h"
141 #include "content/browser/android/text_suggestion_host_android.h"
142 #include "content/browser/renderer_host/render_widget_host_view_android.h"
143 #include "services/device/public/mojom/nfc.mojom.h"
144 #include "third_party/blink/public/mojom/hid/hid.mojom.h"
145 #include "third_party/blink/public/mojom/unhandled_tap_notifier/unhandled_tap_notifier.mojom.h"
146 #endif
147 
148 #if BUILDFLAG(ENABLE_MEDIA_REMOTING)
149 #include "media/mojo/mojom/remoting.mojom-forward.h"
150 #endif
151 
152 #if defined(OS_MAC)
153 #include "content/browser/renderer_host/text_input_host_impl.h"
154 #include "third_party/blink/public/mojom/input/text_input_host.mojom.h"
155 #endif
156 
157 #if defined(OS_MAC) && defined(ARCH_CPU_ARM_FAMILY)
158 #include "media/mojo/mojom/cdm_infobar_service.mojom.h"
159 #endif
160 
161 namespace content {
162 namespace internal {
163 
164 namespace {
165 
BindShapeDetectionServiceOnIOThread(mojo::PendingReceiver<shape_detection::mojom::ShapeDetectionService> receiver)166 void BindShapeDetectionServiceOnIOThread(
167     mojo::PendingReceiver<shape_detection::mojom::ShapeDetectionService>
168         receiver) {
169   auto* gpu = GpuProcessHost::Get();
170   if (gpu)
171     gpu->RunService(std::move(receiver));
172 }
173 
GetShapeDetectionService()174 shape_detection::mojom::ShapeDetectionService* GetShapeDetectionService() {
175   static base::NoDestructor<
176       mojo::Remote<shape_detection::mojom::ShapeDetectionService>>
177       remote;
178   if (!*remote) {
179     GetIOThreadTaskRunner({})->PostTask(
180         FROM_HERE, base::BindOnce(&BindShapeDetectionServiceOnIOThread,
181                                   remote->BindNewPipeAndPassReceiver()));
182     remote->reset_on_disconnect();
183   }
184 
185   return remote->get();
186 }
187 
BindBarcodeDetectionProvider(mojo::PendingReceiver<shape_detection::mojom::BarcodeDetectionProvider> receiver)188 void BindBarcodeDetectionProvider(
189     mojo::PendingReceiver<shape_detection::mojom::BarcodeDetectionProvider>
190         receiver) {
191   GetShapeDetectionService()->BindBarcodeDetectionProvider(std::move(receiver));
192 }
193 
BindFaceDetectionProvider(mojo::PendingReceiver<shape_detection::mojom::FaceDetectionProvider> receiver)194 void BindFaceDetectionProvider(
195     mojo::PendingReceiver<shape_detection::mojom::FaceDetectionProvider>
196         receiver) {
197   GetShapeDetectionService()->BindFaceDetectionProvider(std::move(receiver));
198 }
199 
BindTextDetection(mojo::PendingReceiver<shape_detection::mojom::TextDetection> receiver)200 void BindTextDetection(
201     mojo::PendingReceiver<shape_detection::mojom::TextDetection> receiver) {
202   GetShapeDetectionService()->BindTextDetection(std::move(receiver));
203 }
204 
205 #if defined(OS_MAC)
BindTextInputHost(mojo::PendingReceiver<blink::mojom::TextInputHost> receiver)206 void BindTextInputHost(
207     mojo::PendingReceiver<blink::mojom::TextInputHost> receiver) {
208   GetIOThreadTaskRunner({})->PostTask(
209       FROM_HERE,
210       base::BindOnce(&TextInputHostImpl::Create, std::move(receiver)));
211 }
212 #endif
213 
BindUkmRecorderInterface(mojo::PendingReceiver<ukm::mojom::UkmRecorderInterface> receiver)214 void BindUkmRecorderInterface(
215     mojo::PendingReceiver<ukm::mojom::UkmRecorderInterface> receiver) {
216   metrics::UkmRecorderInterface::Create(ukm::UkmRecorder::Get(),
217                                         std::move(receiver));
218 }
219 
BindBadgeServiceForServiceWorkerOnUI(int service_worker_process_id,const GURL & service_worker_scope,mojo::PendingReceiver<blink::mojom::BadgeService> receiver)220 void BindBadgeServiceForServiceWorkerOnUI(
221     int service_worker_process_id,
222     const GURL& service_worker_scope,
223     mojo::PendingReceiver<blink::mojom::BadgeService> receiver) {
224   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
225 
226   content::RenderProcessHost* render_process_host =
227       content::RenderProcessHost::FromID(service_worker_process_id);
228   if (!render_process_host)
229     return;
230 
231   GetContentClient()->browser()->BindBadgeServiceReceiverFromServiceWorker(
232       render_process_host, service_worker_scope, std::move(receiver));
233 }
234 
BindBadgeServiceForServiceWorker(ServiceWorkerHost * service_worker_host,mojo::PendingReceiver<blink::mojom::BadgeService> receiver)235 void BindBadgeServiceForServiceWorker(
236     ServiceWorkerHost* service_worker_host,
237     mojo::PendingReceiver<blink::mojom::BadgeService> receiver) {
238   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
239   content::RunOrPostTaskOnThread(
240       FROM_HERE, content::BrowserThread::UI,
241       base::BindOnce(&BindBadgeServiceForServiceWorkerOnUI,
242                      service_worker_host->worker_process_id(),
243                      service_worker_host->version()->scope(),
244                      std::move(receiver)));
245 }
246 
BindColorChooserFactoryForFrame(RenderFrameHost * host,mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver)247 void BindColorChooserFactoryForFrame(
248     RenderFrameHost* host,
249     mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
250   auto* web_contents =
251       static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host));
252   web_contents->OnColorChooserFactoryReceiver(std::move(receiver));
253 }
254 
BindConversionInternalsHandler(content::RenderFrameHost * host,mojo::PendingReceiver<::mojom::ConversionInternalsHandler> receiver)255 void BindConversionInternalsHandler(
256     content::RenderFrameHost* host,
257     mojo::PendingReceiver<::mojom::ConversionInternalsHandler> receiver) {
258   auto* contents = WebContents::FromRenderFrameHost(host);
259   DCHECK_EQ(contents->GetLastCommittedURL().host_piece(),
260             kChromeUIConversionInternalsHost);
261   DCHECK(contents->GetLastCommittedURL().SchemeIs(kChromeUIScheme));
262 
263   content::WebUI* web_ui = contents->GetWebUI();
264 
265   // Performs a safe downcast to the concrete ConversionInternalsUI subclass.
266   ConversionInternalsUI* conversion_internals_ui =
267       web_ui ? web_ui->GetController()->GetAs<ConversionInternalsUI>()
268              : nullptr;
269 
270   // This is expected to be called only for main frames and for the right WebUI
271   // pages matching the same WebUI associated to the RenderFrameHost.
272   if (host->GetParent() || !conversion_internals_ui) {
273     ReceivedBadMessage(
274         host->GetProcess(),
275         bad_message::BadMessageReason::RFH_INVALID_WEB_UI_CONTROLLER);
276     return;
277   }
278 
279   conversion_internals_ui->BindInterface(std::move(receiver));
280 }
281 
BindProcessInternalsHandler(content::RenderFrameHost * host,mojo::PendingReceiver<::mojom::ProcessInternalsHandler> receiver)282 void BindProcessInternalsHandler(
283     content::RenderFrameHost* host,
284     mojo::PendingReceiver<::mojom::ProcessInternalsHandler> receiver) {
285   auto* contents = WebContents::FromRenderFrameHost(host);
286   DCHECK_EQ(contents->GetLastCommittedURL().host_piece(),
287             kChromeUIProcessInternalsHost);
288 
289   content::WebUI* web_ui = contents->GetWebUI();
290 
291   // Performs a safe downcast to the concrete ProcessInternalsUI subclass.
292   ProcessInternalsUI* process_internals_ui =
293       web_ui ? web_ui->GetController()->GetAs<ProcessInternalsUI>() : nullptr;
294 
295   // This is expected to be called only for main frames and for the right WebUI
296   // pages matching the same WebUI associated to the RenderFrameHost.
297   if (host->GetParent() || !process_internals_ui) {
298     ReceivedBadMessage(
299         host->GetProcess(),
300         bad_message::BadMessageReason::RFH_INVALID_WEB_UI_CONTROLLER);
301     return;
302   }
303 
304   process_internals_ui->BindProcessInternalsHandler(std::move(receiver), host);
305 }
306 
BindQuotaManagerHost(content::RenderFrameHost * host,mojo::PendingReceiver<blink::mojom::QuotaManagerHost> receiver)307 void BindQuotaManagerHost(
308     content::RenderFrameHost* host,
309     mojo::PendingReceiver<blink::mojom::QuotaManagerHost> receiver) {
310   host->GetProcess()->BindQuotaManagerHost(host->GetRoutingID(),
311                                            host->GetLastCommittedOrigin(),
312                                            std::move(receiver));
313 }
314 
BindNativeIOHost(content::RenderFrameHost * host,mojo::PendingReceiver<blink::mojom::NativeIOHost> receiver)315 void BindNativeIOHost(
316     content::RenderFrameHost* host,
317     mojo::PendingReceiver<blink::mojom::NativeIOHost> receiver) {
318   static_cast<RenderProcessHostImpl*>(host->GetProcess())
319       ->BindNativeIOHost(host->GetLastCommittedOrigin(), std::move(receiver));
320 }
321 
BindSharedWorkerConnector(RenderFrameHostImpl * host,mojo::PendingReceiver<blink::mojom::SharedWorkerConnector> receiver)322 void BindSharedWorkerConnector(
323     RenderFrameHostImpl* host,
324     mojo::PendingReceiver<blink::mojom::SharedWorkerConnector> receiver) {
325   SharedWorkerConnectorImpl::Create(host->GetGlobalFrameRoutingId(),
326                                     std::move(receiver));
327 }
328 
329 #if defined(OS_ANDROID)
BindDateTimeChooserForFrame(content::RenderFrameHost * host,mojo::PendingReceiver<blink::mojom::DateTimeChooser> receiver)330 void BindDateTimeChooserForFrame(
331     content::RenderFrameHost* host,
332     mojo::PendingReceiver<blink::mojom::DateTimeChooser> receiver) {
333   auto* date_time_chooser = DateTimeChooserAndroid::FromWebContents(
334       WebContents::FromRenderFrameHost(host));
335   date_time_chooser->OnDateTimeChooserReceiver(std::move(receiver));
336 }
337 
BindTextSuggestionHostForFrame(content::RenderFrameHost * host,mojo::PendingReceiver<blink::mojom::TextSuggestionHost> receiver)338 void BindTextSuggestionHostForFrame(
339     content::RenderFrameHost* host,
340     mojo::PendingReceiver<blink::mojom::TextSuggestionHost> receiver) {
341   auto* view = static_cast<RenderWidgetHostViewAndroid*>(host->GetView());
342   if (!view || !view->text_suggestion_host())
343     return;
344 
345   view->text_suggestion_host()->BindTextSuggestionHost(std::move(receiver));
346 }
347 #endif
348 
349 template <typename WorkerHost, typename Interface>
350 base::RepeatingCallback<void(mojo::PendingReceiver<Interface>)>
BindWorkerReceiver(void (RenderProcessHostImpl::* method)(mojo::PendingReceiver<Interface>),WorkerHost * host)351 BindWorkerReceiver(
352     void (RenderProcessHostImpl::*method)(mojo::PendingReceiver<Interface>),
353     WorkerHost* host) {
354   return base::BindRepeating(
355       [](WorkerHost* host,
356          void (RenderProcessHostImpl::*method)(
357              mojo::PendingReceiver<Interface>),
358          mojo::PendingReceiver<Interface> receiver) {
359         auto* process_host =
360             static_cast<RenderProcessHostImpl*>(host->GetProcessHost());
361         if (process_host)
362           (process_host->*method)(std::move(receiver));
363       },
364       base::Unretained(host), method);
365 }
366 
367 template <typename WorkerHost, typename Interface>
368 base::RepeatingCallback<void(const url::Origin&,
369                              mojo::PendingReceiver<Interface>)>
BindWorkerReceiverForOrigin(void (RenderProcessHostImpl::* method)(const url::Origin &,mojo::PendingReceiver<Interface>),WorkerHost * host)370 BindWorkerReceiverForOrigin(
371     void (RenderProcessHostImpl::*method)(const url::Origin&,
372                                           mojo::PendingReceiver<Interface>),
373     WorkerHost* host) {
374   return base::BindRepeating(
375       [](WorkerHost* host,
376          void (RenderProcessHostImpl::*method)(
377              const url::Origin&, mojo::PendingReceiver<Interface>),
378          const url::Origin& origin, mojo::PendingReceiver<Interface> receiver) {
379         auto* process_host =
380             static_cast<RenderProcessHostImpl*>(host->GetProcessHost());
381         if (process_host)
382           (process_host->*method)(origin, std::move(receiver));
383       },
384       base::Unretained(host), method);
385 }
386 
387 template <typename WorkerHost, typename Interface>
388 base::RepeatingCallback<void(const url::Origin&,
389                              mojo::PendingReceiver<Interface>)>
BindWorkerReceiverForOriginAndFrameId(void (RenderProcessHostImpl::* method)(int,const url::Origin &,mojo::PendingReceiver<Interface>),WorkerHost * host)390 BindWorkerReceiverForOriginAndFrameId(
391     void (RenderProcessHostImpl::*method)(int,
392                                           const url::Origin&,
393                                           mojo::PendingReceiver<Interface>),
394     WorkerHost* host) {
395   return base::BindRepeating(
396       [](WorkerHost* host,
397          void (RenderProcessHostImpl::*method)(
398              int, const url::Origin&, mojo::PendingReceiver<Interface>),
399          const url::Origin& origin, mojo::PendingReceiver<Interface> receiver) {
400         auto* process_host =
401             static_cast<RenderProcessHostImpl*>(host->GetProcessHost());
402         if (process_host)
403           (process_host->*method)(MSG_ROUTING_NONE, origin,
404                                   std::move(receiver));
405       },
406       base::Unretained(host), method);
407 }
408 
409 template <typename... Args>
RunOrPostTaskToBindServiceWorkerReceiver(ServiceWorkerHost * host,void (RenderProcessHostImpl::* method)(Args...),Args...args)410 void RunOrPostTaskToBindServiceWorkerReceiver(
411     ServiceWorkerHost* host,
412     void (RenderProcessHostImpl::*method)(Args...),
413     Args... args) {
414   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
415   content::RunOrPostTaskOnThread(
416       FROM_HERE, BrowserThread::UI,
417       base::BindOnce(
418           [](int worker_process_id,
419              void (RenderProcessHostImpl::*method)(Args...), Args... args) {
420             auto* process_host = static_cast<RenderProcessHostImpl*>(
421                 RenderProcessHost::FromID(worker_process_id));
422             if (process_host)
423               (process_host->*method)(std::forward<Args>(args)...);
424           },
425           host->worker_process_id(), method, std::forward<Args>(args)...));
426 }
427 
428 template <typename Interface>
429 base::RepeatingCallback<void(mojo::PendingReceiver<Interface>)>
BindServiceWorkerReceiver(void (RenderProcessHostImpl::* method)(mojo::PendingReceiver<Interface>),ServiceWorkerHost * host)430 BindServiceWorkerReceiver(
431     void (RenderProcessHostImpl::*method)(mojo::PendingReceiver<Interface>),
432     ServiceWorkerHost* host) {
433   return base::BindRepeating(
434       [](ServiceWorkerHost* host,
435          void (RenderProcessHostImpl::*method)(
436              mojo::PendingReceiver<Interface>),
437          mojo::PendingReceiver<Interface> receiver) {
438         RunOrPostTaskToBindServiceWorkerReceiver(host, method,
439                                                  std::move(receiver));
440       },
441       base::Unretained(host), method);
442 }
443 
444 template <typename Interface>
445 base::RepeatingCallback<void(const ServiceWorkerVersionInfo&,
446                              mojo::PendingReceiver<Interface>)>
BindServiceWorkerReceiverForOrigin(void (RenderProcessHostImpl::* method)(const url::Origin &,mojo::PendingReceiver<Interface>),ServiceWorkerHost * host)447 BindServiceWorkerReceiverForOrigin(
448     void (RenderProcessHostImpl::*method)(const url::Origin&,
449                                           mojo::PendingReceiver<Interface>),
450     ServiceWorkerHost* host) {
451   return base::BindRepeating(
452       [](ServiceWorkerHost* host,
453          void (RenderProcessHostImpl::*method)(
454              const url::Origin&, mojo::PendingReceiver<Interface>),
455          const ServiceWorkerVersionInfo& info,
456          mojo::PendingReceiver<Interface> receiver) {
457         auto origin = info.origin;
458         RunOrPostTaskToBindServiceWorkerReceiver<
459             const url::Origin&, mojo::PendingReceiver<Interface>>(
460             host, method, origin, std::move(receiver));
461       },
462       base::Unretained(host), method);
463 }
464 
465 template <typename Interface>
466 base::RepeatingCallback<void(const ServiceWorkerVersionInfo&,
467                              mojo::PendingReceiver<Interface>)>
BindServiceWorkerReceiverForOriginAndFrameId(void (RenderProcessHostImpl::* method)(int,const url::Origin &,mojo::PendingReceiver<Interface>),ServiceWorkerHost * host)468 BindServiceWorkerReceiverForOriginAndFrameId(
469     void (RenderProcessHostImpl::*method)(int,
470                                           const url::Origin&,
471                                           mojo::PendingReceiver<Interface>),
472     ServiceWorkerHost* host) {
473   return base::BindRepeating(
474       [](ServiceWorkerHost* host,
475          void (RenderProcessHostImpl::*method)(
476              int, const url::Origin&, mojo::PendingReceiver<Interface>),
477          const ServiceWorkerVersionInfo& info,
478          mojo::PendingReceiver<Interface> receiver) {
479         auto origin = info.origin;
480         RunOrPostTaskToBindServiceWorkerReceiver<
481             int, const url::Origin&, mojo::PendingReceiver<Interface>>(
482             host, method, MSG_ROUTING_NONE, origin, std::move(receiver));
483       },
484       base::Unretained(host), method);
485 }
486 template <typename Interface>
EmptyBinderForFrame(RenderFrameHost * host,mojo::PendingReceiver<Interface> receiver)487 void EmptyBinderForFrame(RenderFrameHost* host,
488                          mojo::PendingReceiver<Interface> receiver) {
489   DLOG(ERROR) << "Empty binder for interface " << Interface::Name_
490               << " for the frame/document scope";
491 }
492 
GetVibrationManagerBinderOverride()493 VibrationManagerBinder& GetVibrationManagerBinderOverride() {
494   static base::NoDestructor<VibrationManagerBinder> binder;
495   return *binder;
496 }
497 
BindVibrationManager(mojo::PendingReceiver<device::mojom::VibrationManager> receiver)498 void BindVibrationManager(
499     mojo::PendingReceiver<device::mojom::VibrationManager> receiver) {
500   const auto& binder = GetVibrationManagerBinderOverride();
501   if (binder)
502     binder.Run(std::move(receiver));
503   else
504     GetDeviceService().BindVibrationManager(std::move(receiver));
505 }
506 
507 }  // namespace
508 
509 // Documents/frames
PopulateFrameBinders(RenderFrameHostImpl * host,mojo::BinderMap * map)510 void PopulateFrameBinders(RenderFrameHostImpl* host, mojo::BinderMap* map) {
511   if (StoragePartition::IsAppCacheEnabled()) {
512     map->Add<blink::mojom::AppCacheBackend>(base::BindRepeating(
513         &RenderFrameHostImpl::CreateAppCacheBackend, base::Unretained(host)));
514   }
515 
516   map->Add<blink::mojom::AudioContextManager>(base::BindRepeating(
517       &RenderFrameHostImpl::GetAudioContextManager, base::Unretained(host)));
518 
519   map->Add<blink::mojom::CacheStorage>(base::BindRepeating(
520       &RenderFrameHostImpl::BindCacheStorage, base::Unretained(host)));
521 
522   map->Add<blink::mojom::ContactsManager>(base::BindRepeating(
523       &RenderFrameHostImpl::GetContactsManager, base::Unretained(host)));
524 
525   map->Add<blink::mojom::ContentSecurityNotifier>(base::BindRepeating(
526       [](RenderFrameHostImpl* host,
527          mojo::PendingReceiver<blink::mojom::ContentSecurityNotifier>
528              receiver) {
529         mojo::MakeSelfOwnedReceiver(std::make_unique<ContentSecurityNotifier>(
530                                         host->GetGlobalFrameRoutingId()),
531                                     std::move(receiver));
532       },
533       base::Unretained(host)));
534 
535   map->Add<blink::mojom::DedicatedWorkerHostFactory>(base::BindRepeating(
536       &RenderFrameHostImpl::CreateDedicatedWorkerHostFactory,
537       base::Unretained(host)));
538 
539   map->Add<blink::mojom::FeatureObserver>(base::BindRepeating(
540       &RenderFrameHostImpl::GetFeatureObserver, base::Unretained(host)));
541 
542   map->Add<blink::mojom::FontAccessManager>(base::BindRepeating(
543       &RenderFrameHostImpl::GetFontAccessManager, base::Unretained(host)));
544 
545   map->Add<blink::mojom::FileSystemManager>(base::BindRepeating(
546       &RenderFrameHostImpl::GetFileSystemManager, base::Unretained(host)));
547 
548   map->Add<blink::mojom::GeolocationService>(base::BindRepeating(
549       &RenderFrameHostImpl::GetGeolocationService, base::Unretained(host)));
550 
551   map->Add<blink::mojom::IdleManager>(base::BindRepeating(
552       &RenderFrameHostImpl::BindIdleManager, base::Unretained(host)));
553 
554   map->Add<blink::mojom::NativeFileSystemManager>(
555       base::BindRepeating(&RenderFrameHostImpl::GetNativeFileSystemManager,
556                           base::Unretained(host)));
557 
558   // BrowserMainLoop::GetInstance() may be null on unit tests.
559   if (BrowserMainLoop::GetInstance()) {
560     map->Add<midi::mojom::MidiSessionProvider>(
561         base::BindRepeating(&MidiHost::BindReceiver,
562                             host->GetProcess()->GetID(),
563                             BrowserMainLoop::GetInstance()->midi_service()),
564         GetIOThreadTaskRunner({}));
565   }
566 
567   map->Add<blink::mojom::NotificationService>(base::BindRepeating(
568       &RenderFrameHostImpl::CreateNotificationService, base::Unretained(host)));
569 
570   map->Add<blink::mojom::PermissionService>(base::BindRepeating(
571       &RenderFrameHostImpl::CreatePermissionService, base::Unretained(host)));
572 
573   map->Add<blink::mojom::PresentationService>(base::BindRepeating(
574       &RenderFrameHostImpl::GetPresentationService, base::Unretained(host)));
575 
576   map->Add<blink::mojom::QuotaManagerHost>(
577       base::BindRepeating(&BindQuotaManagerHost, base::Unretained(host)));
578 
579   map->Add<blink::mojom::SharedWorkerConnector>(
580       base::BindRepeating(&BindSharedWorkerConnector, base::Unretained(host)));
581 
582   map->Add<blink::mojom::SpeechRecognizer>(
583       base::BindRepeating(&SpeechRecognitionDispatcherHost::Create,
584                           host->GetProcess()->GetID(), host->GetRoutingID()),
585       GetIOThreadTaskRunner({}));
586 
587   map->Add<blink::mojom::SpeechSynthesis>(base::BindRepeating(
588       &RenderFrameHostImpl::GetSpeechSynthesis, base::Unretained(host)));
589 
590   map->Add<blink::mojom::ScreenEnumeration>(
591       base::BindRepeating(&RenderFrameHostImpl::BindScreenEnumerationReceiver,
592                           base::Unretained(host)));
593 
594   if (base::FeatureList::IsEnabled(features::kWebOTP)) {
595     map->Add<blink::mojom::WebOTPService>(
596         base::BindRepeating(&RenderFrameHostImpl::BindWebOTPServiceReceiver,
597                             base::Unretained(host)));
598   }
599 
600   if (base::FeatureList::IsEnabled(features::kWebID)) {
601     map->Add<blink::mojom::FederatedAuthRequest>(base::BindRepeating(
602         &RenderFrameHostImpl::BindFederatedAuthRequestReceiver,
603         base::Unretained(host)));
604   }
605 
606   map->Add<blink::mojom::WebUsbService>(base::BindRepeating(
607       &RenderFrameHostImpl::CreateWebUsbService, base::Unretained(host)));
608 
609   map->Add<blink::mojom::WebSocketConnector>(base::BindRepeating(
610       &RenderFrameHostImpl::CreateWebSocketConnector, base::Unretained(host)));
611 
612   map->Add<blink::mojom::LockManager>(base::BindRepeating(
613       &RenderFrameHostImpl::CreateLockManager, base::Unretained(host)));
614 
615   map->Add<blink::mojom::NativeIOHost>(
616       base::BindRepeating(&BindNativeIOHost, base::Unretained(host)));
617 
618   map->Add<blink::mojom::IDBFactory>(base::BindRepeating(
619       &RenderFrameHostImpl::CreateIDBFactory, base::Unretained(host)));
620 
621   map->Add<blink::mojom::FileChooser>(
622       base::BindRepeating(&FileChooserImpl::Create, base::Unretained(host)));
623 
624   map->Add<device::mojom::GamepadMonitor>(
625       base::BindRepeating(&device::GamepadMonitor::Create));
626 
627   map->Add<device::mojom::SensorProvider>(base::BindRepeating(
628       &RenderFrameHostImpl::GetSensorProvider, base::Unretained(host)));
629 
630   map->Add<device::mojom::VibrationManager>(
631       base::BindRepeating(&BindVibrationManager));
632 
633   map->Add<payments::mojom::PaymentManager>(base::BindRepeating(
634       &RenderFrameHostImpl::CreatePaymentManager, base::Unretained(host)));
635 
636   map->Add<blink::mojom::WebBluetoothService>(base::BindRepeating(
637       &RenderFrameHostImpl::CreateWebBluetoothService, base::Unretained(host)));
638 
639   map->Add<blink::mojom::PushMessaging>(base::BindRepeating(
640       &RenderFrameHostImpl::GetPushMessaging, base::Unretained(host)));
641 
642   map->Add<blink::mojom::Authenticator>(base::BindRepeating(
643       &RenderFrameHostImpl::GetAuthenticator, base::Unretained(host)));
644 
645   map->Add<blink::mojom::QuicTransportConnector>(
646       base::BindRepeating(&RenderFrameHostImpl::CreateQuicTransportConnector,
647                           base::Unretained(host)));
648 
649   map->Add<blink::test::mojom::VirtualAuthenticatorManager>(
650       base::BindRepeating(&RenderFrameHostImpl::GetVirtualAuthenticatorManager,
651                           base::Unretained(host)));
652 
653   // BrowserMainLoop::GetInstance() may be null on unit tests.
654   if (BrowserMainLoop::GetInstance()) {
655     // BrowserMainLoop, which owns MediaStreamManager, is alive for the lifetime
656     // of Mojo communication (see BrowserMainLoop::ShutdownThreadsAndCleanUp(),
657     // which shuts down Mojo). Hence, passing that MediaStreamManager instance
658     // as a raw pointer here is safe.
659     MediaStreamManager* media_stream_manager =
660         BrowserMainLoop::GetInstance()->media_stream_manager();
661 
662     map->Add<blink::mojom::MediaDevicesDispatcherHost>(
663         base::BindRepeating(&MediaDevicesDispatcherHost::Create,
664                             host->GetProcess()->GetID(), host->GetRoutingID(),
665                             base::Unretained(media_stream_manager)),
666         GetIOThreadTaskRunner({}));
667 
668     map->Add<blink::mojom::MediaStreamDispatcherHost>(
669         base::BindRepeating(&MediaStreamDispatcherHost::Create,
670                             host->GetProcess()->GetID(), host->GetRoutingID(),
671                             base::Unretained(media_stream_manager)),
672         GetIOThreadTaskRunner({}));
673   }
674 
675   map->Add<blink::mojom::RendererAudioInputStreamFactory>(
676       base::BindRepeating(&RenderFrameHostImpl::CreateAudioInputStreamFactory,
677                           base::Unretained(host)));
678 
679   map->Add<blink::mojom::RendererAudioOutputStreamFactory>(
680       base::BindRepeating(&RenderFrameHostImpl::CreateAudioOutputStreamFactory,
681                           base::Unretained(host)));
682 
683   map->Add<media::mojom::ImageCapture>(
684       base::BindRepeating(&ImageCaptureImpl::Create, base::Unretained(host)));
685 
686   map->Add<media::mojom::InterfaceFactory>(base::BindRepeating(
687       &RenderFrameHostImpl::BindMediaInterfaceFactoryReceiver,
688       base::Unretained(host)));
689 
690   map->Add<media::mojom::MediaMetricsProvider>(base::BindRepeating(
691       &RenderFrameHostImpl::BindMediaMetricsProviderReceiver,
692       base::Unretained(host)));
693 
694 #if BUILDFLAG(ENABLE_MEDIA_REMOTING)
695   map->Add<media::mojom::RemoterFactory>(
696       base::BindRepeating(&RenderFrameHostImpl::BindMediaRemoterFactoryReceiver,
697                           base::Unretained(host)));
698 #endif
699 
700   map->Add<blink::mojom::OneShotBackgroundSyncService>(
701       base::BindRepeating(&RenderProcessHost::CreateOneShotSyncService,
702                           base::Unretained(host->GetProcess())));
703 
704   map->Add<blink::mojom::PeriodicBackgroundSyncService>(
705       base::BindRepeating(&RenderProcessHost::CreatePeriodicSyncService,
706                           base::Unretained(host->GetProcess())));
707 
708   map->Add<media::mojom::VideoDecodePerfHistory>(
709       base::BindRepeating(&RenderProcessHost::BindVideoDecodePerfHistory,
710                           base::Unretained(host->GetProcess())));
711 
712   map->Add<network::mojom::RestrictedCookieManager>(
713       base::BindRepeating(&RenderFrameHostImpl::BindRestrictedCookieManager,
714                           base::Unretained(host)));
715 
716   map->Add<network::mojom::HasTrustTokensAnswerer>(
717       base::BindRepeating(&RenderFrameHostImpl::BindHasTrustTokensAnswerer,
718                           base::Unretained(host)));
719 
720   map->Add<shape_detection::mojom::BarcodeDetectionProvider>(
721       base::BindRepeating(&BindBarcodeDetectionProvider));
722 
723   map->Add<shape_detection::mojom::FaceDetectionProvider>(
724       base::BindRepeating(&BindFaceDetectionProvider));
725 
726   map->Add<shape_detection::mojom::TextDetection>(
727       base::BindRepeating(&BindTextDetection));
728 
729   auto* command_line = base::CommandLine::ForCurrentProcess();
730   if (command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)) {
731     map->Add<mojom::InputInjector>(
732         base::BindRepeating(&RenderFrameHostImpl::BindInputInjectorReceiver,
733                             base::Unretained(host)));
734   }
735 
736 #if defined(OS_ANDROID)
737   if (base::FeatureList::IsEnabled(features::kWebNfc)) {
738     map->Add<device::mojom::NFC>(base::BindRepeating(
739         &RenderFrameHostImpl::BindNFCReceiver, base::Unretained(host)));
740   }
741 #else
742   map->Add<blink::mojom::HidService>(base::BindRepeating(
743       &RenderFrameHostImpl::GetHidService, base::Unretained(host)));
744 
745   map->Add<blink::mojom::InstalledAppProvider>(
746       base::BindRepeating(&RenderFrameHostImpl::CreateInstalledAppProvider,
747                           base::Unretained(host)));
748 
749   map->Add<blink::mojom::SerialService>(base::BindRepeating(
750       &RenderFrameHostImpl::BindSerialService, base::Unretained(host)));
751 #endif  // !defined(OS_ANDROID)
752 
753 #if defined(OS_MAC)
754   map->Add<blink::mojom::TextInputHost>(
755       base::BindRepeating(&BindTextInputHost));
756 #endif
757 }
758 
PopulateBinderMapWithContext(RenderFrameHostImpl * host,mojo::BinderMapWithContext<RenderFrameHost * > * map)759 void PopulateBinderMapWithContext(
760     RenderFrameHostImpl* host,
761     mojo::BinderMapWithContext<RenderFrameHost*>* map) {
762   // Register empty binders for interfaces not bound by content but requested
763   // by blink.
764   // This avoids renderer kills when no binder is found in the absence of the
765   // production embedder (such as in tests).
766   map->Add<blink::mojom::InsecureInputService>(base::BindRepeating(
767       &EmptyBinderForFrame<blink::mojom::InsecureInputService>));
768   if (base::FeatureList::IsEnabled(blink::features::kPrerender2)) {
769     map->Add<blink::mojom::PrerenderProcessor>(base::BindRepeating(
770         &RenderFrameHostImpl::BindPrerenderProcessor, base::Unretained(host)));
771   } else {
772     map->Add<blink::mojom::PrerenderProcessor>(base::BindRepeating(
773         &EmptyBinderForFrame<blink::mojom::PrerenderProcessor>));
774   }
775   map->Add<payments::mojom::PaymentCredential>(base::BindRepeating(
776       &EmptyBinderForFrame<payments::mojom::PaymentCredential>));
777   map->Add<payments::mojom::PaymentRequest>(base::BindRepeating(
778       &EmptyBinderForFrame<payments::mojom::PaymentRequest>));
779   map->Add<blink::mojom::AnchorElementMetricsHost>(base::BindRepeating(
780       &EmptyBinderForFrame<blink::mojom::AnchorElementMetricsHost>));
781   map->Add<blink::mojom::CredentialManager>(base::BindRepeating(
782       &EmptyBinderForFrame<blink::mojom::CredentialManager>));
783 #if !defined(OS_ANDROID)
784   if (base::FeatureList::IsEnabled(features::kDirectSockets)) {
785     map->Add<blink::mojom::DirectSocketsService>(
786         base::BindRepeating(&DirectSocketsServiceImpl::CreateForFrame));
787   }
788   map->Add<media::mojom::SpeechRecognitionContext>(base::BindRepeating(
789       &EmptyBinderForFrame<media::mojom::SpeechRecognitionContext>));
790   map->Add<media::mojom::SpeechRecognitionClientBrowserInterface>(
791       base::BindRepeating(
792           &EmptyBinderForFrame<
793               media::mojom::SpeechRecognitionClientBrowserInterface>));
794 #endif
795 #if BUILDFLAG(ENABLE_UNHANDLED_TAP)
796   map->Add<blink::mojom::UnhandledTapNotifier>(base::BindRepeating(
797       &EmptyBinderForFrame<blink::mojom::UnhandledTapNotifier>));
798 #endif
799 
800   map->Add<blink::mojom::BackgroundFetchService>(
801       base::BindRepeating(&BackgroundFetchServiceImpl::CreateForFrame));
802   map->Add<blink::mojom::ColorChooserFactory>(
803       base::BindRepeating(&BindColorChooserFactoryForFrame));
804   map->Add<blink::mojom::EyeDropperChooser>(
805       base::BindRepeating(&EyeDropperChooserImpl::Create));
806   map->Add<blink::mojom::CookieStore>(
807       base::BindRepeating(&CookieStoreContext::CreateServiceForFrame));
808   map->Add<blink::mojom::ContentIndexService>(
809       base::BindRepeating(&ContentIndexServiceImpl::CreateForFrame));
810   map->Add<blink::mojom::KeyboardLockService>(
811       base::BindRepeating(&KeyboardLockServiceImpl::CreateMojoService));
812   map->Add<blink::mojom::FlocService>(
813       base::BindRepeating(&FlocServiceImpl::CreateMojoService));
814   map->Add<blink::mojom::MediaSessionService>(
815       base::BindRepeating(&MediaSessionServiceImpl::Create));
816   map->Add<blink::mojom::PictureInPictureService>(
817       base::BindRepeating(&PictureInPictureServiceImpl::Create));
818   map->Add<blink::mojom::WakeLockService>(
819       base::BindRepeating(&WakeLockServiceImpl::Create));
820 #if BUILDFLAG(ENABLE_VR)
821   map->Add<device::mojom::VRService>(
822       base::BindRepeating(&VRServiceImpl::Create));
823 #else
824   map->Add<device::mojom::VRService>(
825       base::BindRepeating(&EmptyBinderForFrame<device::mojom::VRService>));
826 #endif
827   map->Add<::mojom::ConversionInternalsHandler>(
828       base::BindRepeating(&BindConversionInternalsHandler));
829   map->Add<::mojom::ProcessInternalsHandler>(
830       base::BindRepeating(&BindProcessInternalsHandler));
831 #if defined(OS_ANDROID)
832   map->Add<blink::mojom::DateTimeChooser>(
833       base::BindRepeating(&BindDateTimeChooserForFrame));
834   map->Add<blink::mojom::TextSuggestionHost>(
835       base::BindRepeating(&BindTextSuggestionHostForFrame));
836 #else
837   // TODO(crbug.com/1060004): add conditions on the renderer side instead.
838   map->Add<blink::mojom::TextSuggestionHost>(base::BindRepeating(
839       &EmptyBinderForFrame<blink::mojom::TextSuggestionHost>));
840 #endif  // defined(OS_ANDROID)
841 
842   map->Add<blink::mojom::ClipboardHost>(
843       base::BindRepeating(&ClipboardHostImpl::Create));
844   map->Add<blink::mojom::RawClipboardHost>(
845       base::BindRepeating(&RawClipboardHostImpl::Create));
846 
847 #if defined(OS_MAC) && defined(ARCH_CPU_ARM_FAMILY)
848   map->Add<media::mojom::CdmInfobarService>(base::BindRepeating(
849       &EmptyBinderForFrame<media::mojom::CdmInfobarService>));
850 #endif
851 
852   GetContentClient()->browser()->RegisterBrowserInterfaceBindersForFrame(host,
853                                                                          map);
854 }
855 
PopulateBinderMap(RenderFrameHostImpl * host,mojo::BinderMap * map)856 void PopulateBinderMap(RenderFrameHostImpl* host, mojo::BinderMap* map) {
857   PopulateFrameBinders(host, map);
858 }
859 
GetContextForHost(RenderFrameHostImpl * host)860 RenderFrameHost* GetContextForHost(RenderFrameHostImpl* host) {
861   return host;
862 }
863 
864 // Dedicated workers
GetContextForHost(DedicatedWorkerHost * host)865 const url::Origin& GetContextForHost(DedicatedWorkerHost* host) {
866   return host->GetWorkerOrigin();
867 }
868 
PopulateDedicatedWorkerBinders(DedicatedWorkerHost * host,mojo::BinderMap * map)869 void PopulateDedicatedWorkerBinders(DedicatedWorkerHost* host,
870                                     mojo::BinderMap* map) {
871   // Do nothing for interfaces that the renderer might request, but doesn't
872   // always expect to be bound.
873   map->Add<blink::mojom::FeatureObserver>(base::DoNothing());
874 
875   // static binders
876   map->Add<shape_detection::mojom::BarcodeDetectionProvider>(
877       base::BindRepeating(&BindBarcodeDetectionProvider));
878   map->Add<shape_detection::mojom::FaceDetectionProvider>(
879       base::BindRepeating(&BindFaceDetectionProvider));
880   map->Add<shape_detection::mojom::TextDetection>(
881       base::BindRepeating(&BindTextDetection));
882   map->Add<ukm::mojom::UkmRecorderInterface>(
883       base::BindRepeating(&BindUkmRecorderInterface));
884 
885   // worker host binders
886   // base::Unretained(host) is safe because the map is owned by
887   // |DedicatedWorkerHost::broker_|.
888   map->Add<blink::mojom::IdleManager>(base::BindRepeating(
889       &DedicatedWorkerHost::CreateIdleManager, base::Unretained(host)));
890   map->Add<blink::mojom::DedicatedWorkerHostFactory>(
891       base::BindRepeating(&DedicatedWorkerHost::CreateNestedDedicatedWorker,
892                           base::Unretained(host)));
893   if (base::FeatureList::IsEnabled(features::kWebOTP)) {
894     map->Add<blink::mojom::WebOTPService>(
895         base::BindRepeating(&DedicatedWorkerHost::BindWebOTPServiceReceiver,
896                             base::Unretained(host)));
897   }
898   map->Add<blink::mojom::WebUsbService>(base::BindRepeating(
899       &DedicatedWorkerHost::CreateWebUsbService, base::Unretained(host)));
900   map->Add<blink::mojom::WebSocketConnector>(base::BindRepeating(
901       &DedicatedWorkerHost::CreateWebSocketConnector, base::Unretained(host)));
902   map->Add<blink::mojom::QuicTransportConnector>(
903       base::BindRepeating(&DedicatedWorkerHost::CreateQuicTransportConnector,
904                           base::Unretained(host)));
905   map->Add<blink::mojom::WakeLockService>(base::BindRepeating(
906       &DedicatedWorkerHost::CreateWakeLockService, base::Unretained(host)));
907   map->Add<blink::mojom::ContentSecurityNotifier>(
908       base::BindRepeating(&DedicatedWorkerHost::CreateContentSecurityNotifier,
909                           base::Unretained(host)));
910   map->Add<blink::mojom::CacheStorage>(base::BindRepeating(
911       &DedicatedWorkerHost::BindCacheStorage, base::Unretained(host)));
912 #if !defined(OS_ANDROID)
913   map->Add<blink::mojom::SerialService>(base::BindRepeating(
914       &DedicatedWorkerHost::BindSerialService, base::Unretained(host)));
915 #endif  // !defined(OS_ANDROID)
916 
917   // render process host binders
918   map->Add<media::mojom::VideoDecodePerfHistory>(BindWorkerReceiver(
919       &RenderProcessHostImpl::BindVideoDecodePerfHistory, host));
920 }
921 
PopulateBinderMapWithContext(DedicatedWorkerHost * host,mojo::BinderMapWithContext<const url::Origin &> * map)922 void PopulateBinderMapWithContext(
923     DedicatedWorkerHost* host,
924     mojo::BinderMapWithContext<const url::Origin&>* map) {
925   // render process host binders taking an origin
926   map->Add<payments::mojom::PaymentManager>(BindWorkerReceiverForOrigin(
927       &RenderProcessHostImpl::CreatePaymentManagerForOrigin, host));
928   map->Add<blink::mojom::PermissionService>(BindWorkerReceiverForOrigin(
929       &RenderProcessHostImpl::CreatePermissionService, host));
930   map->Add<blink::mojom::FileSystemManager>(BindWorkerReceiverForOrigin(
931       &RenderProcessHostImpl::BindFileSystemManager, host));
932   map->Add<blink::mojom::NativeFileSystemManager>(BindWorkerReceiverForOrigin(
933       &RenderProcessHostImpl::BindNativeFileSystemManager, host));
934   map->Add<blink::mojom::NativeIOHost>(BindWorkerReceiverForOrigin(
935       &RenderProcessHostImpl::BindNativeIOHost, host));
936   map->Add<blink::mojom::NotificationService>(BindWorkerReceiverForOrigin(
937       &RenderProcessHostImpl::CreateNotificationService, host));
938   map->Add<blink::mojom::IDBFactory>(
939       BindWorkerReceiverForOrigin(&RenderProcessHostImpl::BindIndexedDB, host));
940 
941   // render process host binders taking a frame id and an origin
942   map->Add<blink::mojom::LockManager>(BindWorkerReceiverForOriginAndFrameId(
943       &RenderProcessHostImpl::CreateLockManager, host));
944   map->Add<blink::mojom::QuotaManagerHost>(
945       BindWorkerReceiverForOriginAndFrameId(
946           &RenderProcessHostImpl::BindQuotaManagerHost, host));
947 }
948 
PopulateBinderMap(DedicatedWorkerHost * host,mojo::BinderMap * map)949 void PopulateBinderMap(DedicatedWorkerHost* host, mojo::BinderMap* map) {
950   PopulateDedicatedWorkerBinders(host, map);
951 }
952 
953 // Shared workers
GetContextForHost(SharedWorkerHost * host)954 url::Origin GetContextForHost(SharedWorkerHost* host) {
955   return url::Origin::Create(host->instance().url());
956 }
957 
PopulateSharedWorkerBinders(SharedWorkerHost * host,mojo::BinderMap * map)958 void PopulateSharedWorkerBinders(SharedWorkerHost* host, mojo::BinderMap* map) {
959   // Do nothing for interfaces that the renderer might request, but doesn't
960   // always expect to be bound.
961   map->Add<blink::mojom::FeatureObserver>(base::DoNothing());
962   // Ignore the pending receiver because it's not clear how to handle
963   // notifications about content security (e.g., mixed contents and certificate
964   // errors) on shared workers. Generally these notifications are routed to the
965   // ancestor frame's WebContents like dedicated workers, but shared workers
966   // don't have the ancestor frame.
967   map->Add<blink::mojom::ContentSecurityNotifier>(base::DoNothing());
968 
969   // static binders
970   map->Add<shape_detection::mojom::BarcodeDetectionProvider>(
971       base::BindRepeating(&BindBarcodeDetectionProvider));
972   map->Add<shape_detection::mojom::FaceDetectionProvider>(
973       base::BindRepeating(&BindFaceDetectionProvider));
974   map->Add<shape_detection::mojom::TextDetection>(
975       base::BindRepeating(&BindTextDetection));
976   map->Add<ukm::mojom::UkmRecorderInterface>(
977       base::BindRepeating(&BindUkmRecorderInterface));
978 
979   // worker host binders
980   // base::Unretained(host) is safe because the map is owned by
981   // |SharedWorkerHost::broker_|.
982   if (StoragePartition::IsAppCacheEnabled()) {
983     map->Add<blink::mojom::AppCacheBackend>(base::BindRepeating(
984         &SharedWorkerHost::CreateAppCacheBackend, base::Unretained(host)));
985   }
986   map->Add<blink::mojom::QuicTransportConnector>(base::BindRepeating(
987       &SharedWorkerHost::CreateQuicTransportConnector, base::Unretained(host)));
988   map->Add<blink::mojom::CacheStorage>(base::BindRepeating(
989       &SharedWorkerHost::BindCacheStorage, base::Unretained(host)));
990 
991   // render process host binders
992   map->Add<media::mojom::VideoDecodePerfHistory>(BindWorkerReceiver(
993       &RenderProcessHostImpl::BindVideoDecodePerfHistory, host));
994 }
995 
PopulateBinderMapWithContext(SharedWorkerHost * host,mojo::BinderMapWithContext<const url::Origin &> * map)996 void PopulateBinderMapWithContext(
997     SharedWorkerHost* host,
998     mojo::BinderMapWithContext<const url::Origin&>* map) {
999   // render process host binders taking an origin
1000   map->Add<blink::mojom::FileSystemManager>(BindWorkerReceiverForOrigin(
1001       &RenderProcessHostImpl::BindFileSystemManager, host));
1002   map->Add<payments::mojom::PaymentManager>(BindWorkerReceiverForOrigin(
1003       &RenderProcessHostImpl::CreatePaymentManagerForOrigin, host));
1004   map->Add<blink::mojom::PermissionService>(BindWorkerReceiverForOrigin(
1005       &RenderProcessHostImpl::CreatePermissionService, host));
1006   map->Add<blink::mojom::NativeFileSystemManager>(BindWorkerReceiverForOrigin(
1007       &RenderProcessHostImpl::BindNativeFileSystemManager, host));
1008   map->Add<blink::mojom::NativeIOHost>(BindWorkerReceiverForOrigin(
1009       &RenderProcessHostImpl::BindNativeIOHost, host));
1010   map->Add<blink::mojom::NotificationService>(BindWorkerReceiverForOrigin(
1011       &RenderProcessHostImpl::CreateNotificationService, host));
1012   map->Add<blink::mojom::WebSocketConnector>(BindWorkerReceiverForOrigin(
1013       &RenderProcessHostImpl::CreateWebSocketConnector, host));
1014   map->Add<blink::mojom::IDBFactory>(
1015       BindWorkerReceiverForOrigin(&RenderProcessHostImpl::BindIndexedDB, host));
1016 
1017   // render process host binders taking a frame id and an origin
1018   map->Add<blink::mojom::LockManager>(BindWorkerReceiverForOriginAndFrameId(
1019       &RenderProcessHostImpl::CreateLockManager, host));
1020   map->Add<blink::mojom::QuotaManagerHost>(
1021       BindWorkerReceiverForOriginAndFrameId(
1022           &RenderProcessHostImpl::BindQuotaManagerHost, host));
1023 }
1024 
PopulateBinderMap(SharedWorkerHost * host,mojo::BinderMap * map)1025 void PopulateBinderMap(SharedWorkerHost* host, mojo::BinderMap* map) {
1026   PopulateSharedWorkerBinders(host, map);
1027 }
1028 
1029 // Service workers
GetContextForHost(ServiceWorkerHost * host)1030 ServiceWorkerVersionInfo GetContextForHost(ServiceWorkerHost* host) {
1031   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
1032   return host->version()->GetInfo();
1033 }
1034 
PopulateServiceWorkerBinders(ServiceWorkerHost * host,mojo::BinderMap * map)1035 void PopulateServiceWorkerBinders(ServiceWorkerHost* host,
1036                                   mojo::BinderMap* map) {
1037   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
1038 
1039   // Do nothing for interfaces that the renderer might request, but doesn't
1040   // always expect to be bound.
1041   map->Add<blink::mojom::FeatureObserver>(base::DoNothing());
1042   // Ignore the pending receiver because it's not clear how to handle
1043   // notifications about content security (e.g., mixed contents and certificate
1044   // errors) on service workers. Generally these notifications are routed to the
1045   // ancestor frame's WebContents like dedicated workers, but service workers
1046   // don't have the ancestor frame.
1047   map->Add<blink::mojom::ContentSecurityNotifier>(base::DoNothing());
1048 
1049   // static binders
1050   map->Add<shape_detection::mojom::BarcodeDetectionProvider>(
1051       base::BindRepeating(&BindBarcodeDetectionProvider));
1052   map->Add<shape_detection::mojom::FaceDetectionProvider>(
1053       base::BindRepeating(&BindFaceDetectionProvider));
1054   map->Add<shape_detection::mojom::TextDetection>(
1055       base::BindRepeating(&BindTextDetection));
1056   map->Add<ukm::mojom::UkmRecorderInterface>(
1057       base::BindRepeating(&BindUkmRecorderInterface));
1058 
1059   // worker host binders
1060   map->Add<blink::mojom::QuicTransportConnector>(
1061       base::BindRepeating(&ServiceWorkerHost::CreateQuicTransportConnector,
1062                           base::Unretained(host)));
1063   map->Add<blink::mojom::CacheStorage>(base::BindRepeating(
1064       &ServiceWorkerHost::BindCacheStorage, base::Unretained(host)));
1065   map->Add<blink::mojom::BadgeService>(
1066       base::BindRepeating(&BindBadgeServiceForServiceWorker, host));
1067 
1068   // render process host binders
1069   map->Add<media::mojom::VideoDecodePerfHistory>(BindServiceWorkerReceiver(
1070       &RenderProcessHostImpl::BindVideoDecodePerfHistory, host));
1071   map->Add<blink::mojom::OneShotBackgroundSyncService>(
1072       BindServiceWorkerReceiver(
1073           &RenderProcessHostImpl::CreateOneShotSyncService, host));
1074   map->Add<blink::mojom::PeriodicBackgroundSyncService>(
1075       BindServiceWorkerReceiver(
1076           &RenderProcessHostImpl::CreatePeriodicSyncService, host));
1077 }
1078 
PopulateBinderMapWithContext(ServiceWorkerHost * host,mojo::BinderMapWithContext<const ServiceWorkerVersionInfo &> * map)1079 void PopulateBinderMapWithContext(
1080     ServiceWorkerHost* host,
1081     mojo::BinderMapWithContext<const ServiceWorkerVersionInfo&>* map) {
1082   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
1083 
1084   // static binders
1085   // Use a task runner if ServiceWorkerHost lives on the IO thread, as
1086   // CreateForWorker() needs to be called on the UI thread.
1087   if (ServiceWorkerContext::IsServiceWorkerOnUIEnabled()) {
1088     map->Add<blink::mojom::BackgroundFetchService>(
1089         base::BindRepeating(&BackgroundFetchServiceImpl::CreateForWorker));
1090     map->Add<blink::mojom::ContentIndexService>(
1091         base::BindRepeating(&ContentIndexServiceImpl::CreateForWorker));
1092     map->Add<blink::mojom::CookieStore>(
1093         base::BindRepeating(&CookieStoreContext::CreateServiceForWorker));
1094   } else {
1095     map->Add<blink::mojom::BackgroundFetchService>(
1096         base::BindRepeating(&BackgroundFetchServiceImpl::CreateForWorker),
1097         GetUIThreadTaskRunner({}));
1098     map->Add<blink::mojom::ContentIndexService>(
1099         base::BindRepeating(&ContentIndexServiceImpl::CreateForWorker),
1100         GetUIThreadTaskRunner({}));
1101     map->Add<blink::mojom::CookieStore>(
1102         base::BindRepeating(&CookieStoreContext::CreateServiceForWorker),
1103         GetUIThreadTaskRunner({}));
1104   }
1105 
1106   // render process host binders taking an origin
1107   map->Add<payments::mojom::PaymentManager>(BindServiceWorkerReceiverForOrigin(
1108       &RenderProcessHostImpl::CreatePaymentManagerForOrigin, host));
1109   map->Add<blink::mojom::PermissionService>(BindServiceWorkerReceiverForOrigin(
1110       &RenderProcessHostImpl::CreatePermissionService, host));
1111   map->Add<blink::mojom::NativeFileSystemManager>(
1112       BindServiceWorkerReceiverForOrigin(
1113           &RenderProcessHostImpl::BindNativeFileSystemManager, host));
1114   map->Add<blink::mojom::NativeIOHost>(BindServiceWorkerReceiverForOrigin(
1115       &RenderProcessHostImpl::BindNativeIOHost, host));
1116   map->Add<blink::mojom::NotificationService>(
1117       BindServiceWorkerReceiverForOrigin(
1118           &RenderProcessHostImpl::CreateNotificationService, host));
1119   map->Add<blink::mojom::WebSocketConnector>(BindServiceWorkerReceiverForOrigin(
1120       &RenderProcessHostImpl::CreateWebSocketConnector, host));
1121   map->Add<network::mojom::RestrictedCookieManager>(
1122       BindServiceWorkerReceiverForOrigin(
1123           &RenderProcessHostImpl::BindRestrictedCookieManagerForServiceWorker,
1124           host));
1125   map->Add<blink::mojom::IDBFactory>(BindServiceWorkerReceiverForOrigin(
1126       &RenderProcessHostImpl::BindIndexedDB, host));
1127 
1128   // render process host binders taking a frame id and an origin
1129   map->Add<blink::mojom::LockManager>(
1130       BindServiceWorkerReceiverForOriginAndFrameId(
1131           &RenderProcessHostImpl::CreateLockManager, host));
1132   map->Add<blink::mojom::QuotaManagerHost>(
1133       BindServiceWorkerReceiverForOriginAndFrameId(
1134           &RenderProcessHostImpl::BindQuotaManagerHost, host));
1135 }
1136 
PopulateBinderMap(ServiceWorkerHost * host,mojo::BinderMap * map)1137 void PopulateBinderMap(ServiceWorkerHost* host, mojo::BinderMap* map) {
1138   DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
1139   PopulateServiceWorkerBinders(host, map);
1140 }
1141 
1142 }  // namespace internal
1143 
OverrideVibrationManagerBinderForTesting(VibrationManagerBinder binder)1144 void OverrideVibrationManagerBinderForTesting(VibrationManagerBinder binder) {
1145   internal::GetVibrationManagerBinderOverride() = std::move(binder);
1146 }
1147 
1148 }  // namespace content
1149