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