blob: bef8f425c3ef08d749508c6523719e9b3b5cf762 [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"
Sasha Bermeisterf97ff39a2018-01-29 04:50:5616#include "content/browser/quota_dispatcher_host.h"
Sam McNally6f337bc2017-11-01 02:40:2117#include "content/browser/renderer_host/render_process_host_impl.h"
18#include "content/browser/storage_partition_impl.h"
19#include "content/browser/websockets/websocket_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3120#include "content/public/browser/browser_context.h"
21#include "content/public/browser/browser_thread.h"
22#include "content/public/browser/content_browser_client.h"
Sam McNally8b4f74d2017-11-10 00:07:5623#include "content/public/browser/render_frame_host.h"
Sam McNallyfaf9a402017-10-31 03:06:3124#include "content/public/browser/render_process_host.h"
Victor Costan3e7fa0c2017-12-15 23:23:3025#include "content/public/common/content_switches.h"
Ke He31d0bb02018-02-24 07:16:2426#include "services/device/public/mojom/constants.mojom.h"
27#include "services/device/public/mojom/vibration_manager.mojom.h"
John Abd-El-Malek3bbbdf92018-01-30 03:27:3528#include "services/network/restricted_cookie_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3129#include "services/service_manager/public/cpp/binder_registry.h"
30#include "services/service_manager/public/cpp/connector.h"
Ken Rockotd7e999b2018-02-11 15:48:2131#include "services/shape_detection/public/mojom/barcodedetection.mojom.h"
32#include "services/shape_detection/public/mojom/constants.mojom.h"
33#include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
34#include "services/shape_detection/public/mojom/textdetection.mojom.h"
Luciano Pacheco626c99e82018-03-22 01:06:5635#include "third_party/WebKit/public/platform/modules/cache_storage/cache_storage.mojom.h"
Peter Beverlood6e38b42017-11-28 20:37:4336#include "third_party/WebKit/public/platform/modules/notifications/notification_service.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3137#include "url/origin.h"
38
39namespace content {
40namespace {
41
42// A holder for a parameterized BinderRegistry for content-layer interfaces
43// exposed to web workers.
Sam McNally8b4f74d2017-11-10 00:07:5644class RendererInterfaceBinders {
Sam McNallyfaf9a402017-10-31 03:06:3145 public:
Sam McNally8b4f74d2017-11-10 00:07:5646 RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); }
Sam McNallyfaf9a402017-10-31 03:06:3147
48 // Bind an interface request |interface_pipe| for |interface_name| received
49 // from a web worker with origin |origin| hosted in the renderer |host|.
50 void BindInterface(const std::string& interface_name,
51 mojo::ScopedMessagePipeHandle interface_pipe,
52 RenderProcessHost* host,
53 const url::Origin& origin) {
54 if (parameterized_binder_registry_.TryBindInterface(
55 interface_name, &interface_pipe, host, origin)) {
56 return;
57 }
58
59 GetContentClient()->browser()->BindInterfaceRequestFromWorker(
60 host, origin, interface_name, std::move(interface_pipe));
61 }
62
Sam McNally8b4f74d2017-11-10 00:07:5663 // Try binding an interface request |interface_pipe| for |interface_name|
64 // received from |frame|.
65 bool TryBindInterface(const std::string& interface_name,
66 mojo::ScopedMessagePipeHandle* interface_pipe,
67 RenderFrameHost* frame) {
68 return parameterized_binder_registry_.TryBindInterface(
69 interface_name, interface_pipe, frame->GetProcess(),
70 frame->GetLastCommittedOrigin());
71 }
72
Sam McNallyfaf9a402017-10-31 03:06:3173 private:
74 void InitializeParameterizedBinderRegistry();
75
76 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
77 const url::Origin&>
78 parameterized_binder_registry_;
79};
80
81// Forwards service requests to Service Manager since the renderer cannot launch
82// out-of-process services on is own.
83template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3484void ForwardServiceRequest(const char* service_name,
85 mojo::InterfaceRequest<Interface> request,
86 RenderProcessHost* host,
87 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3188 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
89 connector->BindInterface(service_name, std::move(request));
90}
91
Daniel Bratell60854de2018-01-03 17:43:0692void GetRestrictedCookieManagerForWorker(
Victor Costan3e7fa0c2017-12-15 23:23:3093 network::mojom::RestrictedCookieManagerRequest request,
94 RenderProcessHost* render_process_host,
95 const url::Origin& origin) {
96 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
97 switches::kEnableExperimentalWebPlatformFeatures)) {
98 return;
99 }
100
101 StoragePartition* storage_partition =
102 render_process_host->GetStoragePartition();
John Abd-El-Malek53670dd2018-01-18 22:07:21103 network::mojom::NetworkContext* network_context =
Victor Costan3e7fa0c2017-12-15 23:23:30104 storage_partition->GetNetworkContext();
105 uint32_t render_process_id = render_process_host->GetID();
106 network_context->GetRestrictedCookieManager(
107 std::move(request), render_process_id, MSG_ROUTING_NONE);
108}
109
Sam McNally8b4f74d2017-11-10 00:07:56110// Register renderer-exposed interfaces. Each registered interface binder is
111// exposed to all renderer-hosted execution context types (document/frame,
112// dedicated worker, shared worker and service worker) where the appropriate
113// capability spec in the content_browser manifest includes the interface. For
114// interface requests from frames, binders registered on the frame itself
115// override binders registered here.
116void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:34117 parameterized_binder_registry_.AddInterface(base::Bind(
118 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetection>,
119 shape_detection::mojom::kServiceName));
120 parameterized_binder_registry_.AddInterface(base::Bind(
121 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
122 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31123 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34124 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31125 shape_detection::mojom::kServiceName));
126 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34127 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56128 device::mojom::kServiceName));
Anita Woodruff31afadd22018-03-16 16:20:30129 parameterized_binder_registry_.AddInterface(
130 base::Bind([](blink::mojom::WebSocketRequest request,
131 RenderProcessHost* host, const url::Origin& origin) {
Noel Gordon064dbbf2018-02-16 03:35:15132 WebSocketManager::CreateWebSocketWithOrigin(
133 host->GetID(), origin, std::move(request), MSG_ROUTING_NONE);
Sam McNally6f337bc2017-11-01 02:40:21134 }));
135 parameterized_binder_registry_.AddInterface(
136 base::Bind([](payments::mojom::PaymentManagerRequest request,
137 RenderProcessHost* host, const url::Origin& origin) {
138 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
139 ->GetPaymentAppContext()
140 ->CreatePaymentManager(std::move(request));
141 }));
Luciano Pacheco626c99e82018-03-22 01:06:56142 parameterized_binder_registry_.AddInterface(base::BindRepeating(
143 [](blink::mojom::CacheStorageRequest request, RenderProcessHost* host,
144 const url::Origin& origin) {
145 static_cast<RenderProcessHostImpl*>(host)->BindCacheStorage(
146 std::move(request), origin);
147 }));
Sam McNally6f337bc2017-11-01 02:40:21148 parameterized_binder_registry_.AddInterface(
149 base::Bind([](blink::mojom::PermissionServiceRequest request,
150 RenderProcessHost* host, const url::Origin& origin) {
151 static_cast<RenderProcessHostImpl*>(host)
152 ->permission_service_context()
Sam McNally2e5c71f2017-12-11 03:24:27153 .CreateServiceForWorker(std::move(request), origin);
Sam McNally6f337bc2017-11-01 02:40:21154 }));
Joshua Bellfdfe23e2017-12-07 19:54:34155 parameterized_binder_registry_.AddInterface(base::BindRepeating(
156 [](blink::mojom::LockManagerRequest request, RenderProcessHost* host,
157 const url::Origin& origin) {
158 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
159 ->GetLockManager()
Sam McNally37e39a82017-12-20 03:35:50160 ->CreateService(std::move(request), origin);
Joshua Bellfdfe23e2017-12-07 19:54:34161 }));
Sam McNally8b4f74d2017-11-10 00:07:56162 parameterized_binder_registry_.AddInterface(
163 base::Bind(&CreateDedicatedWorkerHostFactory));
Peter Beverlood6e38b42017-11-28 20:37:43164 parameterized_binder_registry_.AddInterface(
165 base::Bind([](blink::mojom::NotificationServiceRequest request,
166 RenderProcessHost* host, const url::Origin& origin) {
167 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
168 ->GetPlatformNotificationContext()
169 ->CreateService(host->GetID(), origin, std::move(request));
170 }));
Sam McNally54bc0282017-12-13 02:42:29171 parameterized_binder_registry_.AddInterface(
172 base::BindRepeating(&BackgroundFetchServiceImpl::Create));
Victor Costan3e7fa0c2017-12-15 23:23:30173 parameterized_binder_registry_.AddInterface(
Daniel Bratell60854de2018-01-03 17:43:06174 base::BindRepeating(GetRestrictedCookieManagerForWorker));
Sasha Bermeisterf97ff39a2018-01-29 04:50:56175 parameterized_binder_registry_.AddInterface(
176 base::BindRepeating(&QuotaDispatcherHost::CreateForWorker));
Sam McNally8b4f74d2017-11-10 00:07:56177}
178
179RendererInterfaceBinders& GetRendererInterfaceBinders() {
180 CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ());
181 return binders;
Sam McNallyfaf9a402017-10-31 03:06:31182}
183
184} // namespace
185
186void BindWorkerInterface(const std::string& interface_name,
187 mojo::ScopedMessagePipeHandle interface_pipe,
188 RenderProcessHost* host,
189 const url::Origin& origin) {
190 DCHECK_CURRENTLY_ON(BrowserThread::UI);
191
Sam McNally8b4f74d2017-11-10 00:07:56192 GetRendererInterfaceBinders().BindInterface(
193 interface_name, std::move(interface_pipe), host, origin);
194}
195
196bool TryBindFrameInterface(const std::string& interface_name,
197 mojo::ScopedMessagePipeHandle* interface_pipe,
198 RenderFrameHost* frame) {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200
201 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
202 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31203}
204
205} // namespace content