blob: eecfa5e32ee412b9a8305ec7bcbf29837b24e160 [file] [log] [blame]
Sam McNallyfaf9a402017-10-31 03:06:311// Copyright 2017 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
Sam McNally8b4f74d2017-11-10 00:07:565#include "content/browser/renderer_interface_binders.h"
Sam McNallyfaf9a402017-10-31 03:06:316
7#include <utility>
8
Sam McNally6f337bc2017-11-01 02:40:219#include "base/bind.h"
Sam McNally8b4f74d2017-11-10 00:07:5610#include "content/browser/background_fetch/background_fetch_service_impl.h"
11#include "content/browser/dedicated_worker/dedicated_worker_host.h"
Joshua Bellfdfe23e2017-12-07 19:54:3412#include "content/browser/locks/lock_manager.h"
Peter Beverlood6e38b42017-11-28 20:37:4313#include "content/browser/notifications/platform_notification_context_impl.h"
Sam McNally6f337bc2017-11-01 02:40:2114#include "content/browser/payments/payment_manager.h"
15#include "content/browser/permissions/permission_service_context.h"
16#include "content/browser/renderer_host/render_process_host_impl.h"
17#include "content/browser/storage_partition_impl.h"
18#include "content/browser/websockets/websocket_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3119#include "content/public/browser/browser_context.h"
20#include "content/public/browser/browser_thread.h"
21#include "content/public/browser/content_browser_client.h"
Sam McNally8b4f74d2017-11-10 00:07:5622#include "content/public/browser/render_frame_host.h"
Sam McNallyfaf9a402017-10-31 03:06:3123#include "content/public/browser/render_process_host.h"
Sam McNally8b4f74d2017-11-10 00:07:5624#include "services/device/public/interfaces/constants.mojom.h"
25#include "services/device/public/interfaces/vibration_manager.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3126#include "services/service_manager/public/cpp/binder_registry.h"
27#include "services/service_manager/public/cpp/connector.h"
28#include "services/shape_detection/public/interfaces/barcodedetection.mojom.h"
29#include "services/shape_detection/public/interfaces/constants.mojom.h"
30#include "services/shape_detection/public/interfaces/facedetection_provider.mojom.h"
31#include "services/shape_detection/public/interfaces/textdetection.mojom.h"
Peter Beverlood6e38b42017-11-28 20:37:4332#include "third_party/WebKit/public/platform/modules/notifications/notification_service.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3133#include "url/origin.h"
34
35namespace content {
36namespace {
37
38// A holder for a parameterized BinderRegistry for content-layer interfaces
39// exposed to web workers.
Sam McNally8b4f74d2017-11-10 00:07:5640class RendererInterfaceBinders {
Sam McNallyfaf9a402017-10-31 03:06:3141 public:
Sam McNally8b4f74d2017-11-10 00:07:5642 RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); }
Sam McNallyfaf9a402017-10-31 03:06:3143
44 // Bind an interface request |interface_pipe| for |interface_name| received
45 // from a web worker with origin |origin| hosted in the renderer |host|.
46 void BindInterface(const std::string& interface_name,
47 mojo::ScopedMessagePipeHandle interface_pipe,
48 RenderProcessHost* host,
49 const url::Origin& origin) {
50 if (parameterized_binder_registry_.TryBindInterface(
51 interface_name, &interface_pipe, host, origin)) {
52 return;
53 }
54
55 GetContentClient()->browser()->BindInterfaceRequestFromWorker(
56 host, origin, interface_name, std::move(interface_pipe));
57 }
58
Sam McNally8b4f74d2017-11-10 00:07:5659 // Try binding an interface request |interface_pipe| for |interface_name|
60 // received from |frame|.
61 bool TryBindInterface(const std::string& interface_name,
62 mojo::ScopedMessagePipeHandle* interface_pipe,
63 RenderFrameHost* frame) {
64 return parameterized_binder_registry_.TryBindInterface(
65 interface_name, interface_pipe, frame->GetProcess(),
66 frame->GetLastCommittedOrigin());
67 }
68
Sam McNallyfaf9a402017-10-31 03:06:3169 private:
70 void InitializeParameterizedBinderRegistry();
71
72 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
73 const url::Origin&>
74 parameterized_binder_registry_;
75};
76
77// Forwards service requests to Service Manager since the renderer cannot launch
78// out-of-process services on is own.
79template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3480void ForwardServiceRequest(const char* service_name,
81 mojo::InterfaceRequest<Interface> request,
82 RenderProcessHost* host,
83 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3184 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
85 connector->BindInterface(service_name, std::move(request));
86}
87
Sam McNally8b4f74d2017-11-10 00:07:5688// Register renderer-exposed interfaces. Each registered interface binder is
89// exposed to all renderer-hosted execution context types (document/frame,
90// dedicated worker, shared worker and service worker) where the appropriate
91// capability spec in the content_browser manifest includes the interface. For
92// interface requests from frames, binders registered on the frame itself
93// override binders registered here.
94void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:3495 parameterized_binder_registry_.AddInterface(base::Bind(
96 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetection>,
97 shape_detection::mojom::kServiceName));
98 parameterized_binder_registry_.AddInterface(base::Bind(
99 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
100 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31101 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34102 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31103 shape_detection::mojom::kServiceName));
104 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34105 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56106 device::mojom::kServiceName));
107 parameterized_binder_registry_.AddInterface(
Sam McNally6f337bc2017-11-01 02:40:21108 base::Bind([](blink::mojom::WebSocketRequest request,
109 RenderProcessHost* host, const url::Origin& origin) {
110 WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE,
111 std::move(request));
112 }));
113 parameterized_binder_registry_.AddInterface(
114 base::Bind([](payments::mojom::PaymentManagerRequest request,
115 RenderProcessHost* host, const url::Origin& origin) {
116 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
117 ->GetPaymentAppContext()
118 ->CreatePaymentManager(std::move(request));
119 }));
120 parameterized_binder_registry_.AddInterface(
121 base::Bind([](blink::mojom::PermissionServiceRequest request,
122 RenderProcessHost* host, const url::Origin& origin) {
123 static_cast<RenderProcessHostImpl*>(host)
124 ->permission_service_context()
Sam McNally2e5c71f2017-12-11 03:24:27125 .CreateServiceForWorker(std::move(request), origin);
Sam McNally6f337bc2017-11-01 02:40:21126 }));
Joshua Bellfdfe23e2017-12-07 19:54:34127 parameterized_binder_registry_.AddInterface(base::BindRepeating(
128 [](blink::mojom::LockManagerRequest request, RenderProcessHost* host,
129 const url::Origin& origin) {
130 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
131 ->GetLockManager()
132 ->CreateService(std::move(request));
133 }));
Sam McNally8b4f74d2017-11-10 00:07:56134 parameterized_binder_registry_.AddInterface(
135 base::Bind(&CreateDedicatedWorkerHostFactory));
Peter Beverlood6e38b42017-11-28 20:37:43136 parameterized_binder_registry_.AddInterface(
137 base::Bind([](blink::mojom::NotificationServiceRequest request,
138 RenderProcessHost* host, const url::Origin& origin) {
139 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
140 ->GetPlatformNotificationContext()
141 ->CreateService(host->GetID(), origin, std::move(request));
142 }));
Sam McNally8b4f74d2017-11-10 00:07:56143}
144
145RendererInterfaceBinders& GetRendererInterfaceBinders() {
146 CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ());
147 return binders;
Sam McNallyfaf9a402017-10-31 03:06:31148}
149
150} // namespace
151
152void BindWorkerInterface(const std::string& interface_name,
153 mojo::ScopedMessagePipeHandle interface_pipe,
154 RenderProcessHost* host,
155 const url::Origin& origin) {
156 DCHECK_CURRENTLY_ON(BrowserThread::UI);
157
Sam McNally8b4f74d2017-11-10 00:07:56158 GetRendererInterfaceBinders().BindInterface(
159 interface_name, std::move(interface_pipe), host, origin);
160}
161
162bool TryBindFrameInterface(const std::string& interface_name,
163 mojo::ScopedMessagePipeHandle* interface_pipe,
164 RenderFrameHost* frame) {
165 DCHECK_CURRENTLY_ON(BrowserThread::UI);
166
167 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
168 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31169}
170
171} // namespace content