blob: 81e1e824d0f28c322280c3983d4db3f61433617f [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"
Blink Reformata30d4232018-04-07 15:31:0635#include "third_party/blink/public/platform/modules/cache_storage/cache_storage.mojom.h"
36#include "third_party/blink/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
Yutaka Hirano24632bb2018-03-23 08:55:1276 static void CreateWebSocket(network::mojom::WebSocketRequest request,
77 RenderProcessHost* host,
78 const url::Origin& origin);
79
Sam McNallyfaf9a402017-10-31 03:06:3180 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
81 const url::Origin&>
82 parameterized_binder_registry_;
83};
84
85// Forwards service requests to Service Manager since the renderer cannot launch
86// out-of-process services on is own.
87template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3488void ForwardServiceRequest(const char* service_name,
89 mojo::InterfaceRequest<Interface> request,
90 RenderProcessHost* host,
91 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3192 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
93 connector->BindInterface(service_name, std::move(request));
94}
95
Daniel Bratell60854de2018-01-03 17:43:0696void GetRestrictedCookieManagerForWorker(
Victor Costan3e7fa0c2017-12-15 23:23:3097 network::mojom::RestrictedCookieManagerRequest request,
98 RenderProcessHost* render_process_host,
99 const url::Origin& origin) {
100 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kEnableExperimentalWebPlatformFeatures)) {
102 return;
103 }
104
105 StoragePartition* storage_partition =
106 render_process_host->GetStoragePartition();
John Abd-El-Malek53670dd2018-01-18 22:07:21107 network::mojom::NetworkContext* network_context =
Victor Costan3e7fa0c2017-12-15 23:23:30108 storage_partition->GetNetworkContext();
109 uint32_t render_process_id = render_process_host->GetID();
110 network_context->GetRestrictedCookieManager(
111 std::move(request), render_process_id, MSG_ROUTING_NONE);
112}
113
Sam McNally8b4f74d2017-11-10 00:07:56114// Register renderer-exposed interfaces. Each registered interface binder is
115// exposed to all renderer-hosted execution context types (document/frame,
116// dedicated worker, shared worker and service worker) where the appropriate
117// capability spec in the content_browser manifest includes the interface. For
118// interface requests from frames, binders registered on the frame itself
119// override binders registered here.
120void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:34121 parameterized_binder_registry_.AddInterface(base::Bind(
122 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetection>,
123 shape_detection::mojom::kServiceName));
124 parameterized_binder_registry_.AddInterface(base::Bind(
125 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
126 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31127 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34128 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31129 shape_detection::mojom::kServiceName));
130 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34131 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56132 device::mojom::kServiceName));
Yutaka Hirano24632bb2018-03-23 08:55:12133 parameterized_binder_registry_.AddInterface(
134 base::BindRepeating(CreateWebSocket));
Sam McNally6f337bc2017-11-01 02:40:21135 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
Yutaka Hirano24632bb2018-03-23 08:55:12184void RendererInterfaceBinders::CreateWebSocket(
185 network::mojom::WebSocketRequest request,
186 RenderProcessHost* host,
187 const url::Origin& origin) {
Hiroki Nakagawaca3a0bee2018-04-25 08:30:02188 WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE, origin,
189 std::move(request));
Yutaka Hirano24632bb2018-03-23 08:55:12190}
191
Sam McNallyfaf9a402017-10-31 03:06:31192} // namespace
193
194void BindWorkerInterface(const std::string& interface_name,
195 mojo::ScopedMessagePipeHandle interface_pipe,
196 RenderProcessHost* host,
197 const url::Origin& origin) {
198 DCHECK_CURRENTLY_ON(BrowserThread::UI);
199
Sam McNally8b4f74d2017-11-10 00:07:56200 GetRendererInterfaceBinders().BindInterface(
201 interface_name, std::move(interface_pipe), host, origin);
202}
203
204bool TryBindFrameInterface(const std::string& interface_name,
205 mojo::ScopedMessagePipeHandle* interface_pipe,
206 RenderFrameHost* frame) {
207 DCHECK_CURRENTLY_ON(BrowserThread::UI);
208
209 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
210 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31211}
212
213} // namespace content