blob: 7ebbd387e53be650f6c818cfc7eac4d4cc72c84b [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"
Victor Costan7184272a2018-05-15 13:17:4311#include "content/browser/cookie_store/cookie_store_context.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"
junweifu3605ba12018-06-04 08:21:0531#include "services/shape_detection/public/mojom/barcodedetection_provider.mojom.h"
Ken Rockotd7e999b2018-02-11 15:48:2132#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"
Victor Costan7184272a2018-05-15 13:17:4335#include "third_party/blink/public/mojom/cookie_store/cookie_store.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0636#include "third_party/blink/public/platform/modules/cache_storage/cache_storage.mojom.h"
37#include "third_party/blink/public/platform/modules/notifications/notification_service.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3138#include "url/origin.h"
39
40namespace content {
41namespace {
42
43// A holder for a parameterized BinderRegistry for content-layer interfaces
44// exposed to web workers.
Sam McNally8b4f74d2017-11-10 00:07:5645class RendererInterfaceBinders {
Sam McNallyfaf9a402017-10-31 03:06:3146 public:
Sam McNally8b4f74d2017-11-10 00:07:5647 RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); }
Sam McNallyfaf9a402017-10-31 03:06:3148
49 // Bind an interface request |interface_pipe| for |interface_name| received
50 // from a web worker with origin |origin| hosted in the renderer |host|.
51 void BindInterface(const std::string& interface_name,
52 mojo::ScopedMessagePipeHandle interface_pipe,
53 RenderProcessHost* host,
54 const url::Origin& origin) {
55 if (parameterized_binder_registry_.TryBindInterface(
56 interface_name, &interface_pipe, host, origin)) {
57 return;
58 }
59
60 GetContentClient()->browser()->BindInterfaceRequestFromWorker(
61 host, origin, interface_name, std::move(interface_pipe));
62 }
63
Sam McNally8b4f74d2017-11-10 00:07:5664 // Try binding an interface request |interface_pipe| for |interface_name|
65 // received from |frame|.
66 bool TryBindInterface(const std::string& interface_name,
67 mojo::ScopedMessagePipeHandle* interface_pipe,
68 RenderFrameHost* frame) {
69 return parameterized_binder_registry_.TryBindInterface(
70 interface_name, interface_pipe, frame->GetProcess(),
71 frame->GetLastCommittedOrigin());
72 }
73
Sam McNallyfaf9a402017-10-31 03:06:3174 private:
75 void InitializeParameterizedBinderRegistry();
76
Yutaka Hirano24632bb2018-03-23 08:55:1277 static void CreateWebSocket(network::mojom::WebSocketRequest request,
78 RenderProcessHost* host,
79 const url::Origin& origin);
80
Sam McNallyfaf9a402017-10-31 03:06:3181 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
82 const url::Origin&>
83 parameterized_binder_registry_;
84};
85
86// Forwards service requests to Service Manager since the renderer cannot launch
87// out-of-process services on is own.
88template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3489void ForwardServiceRequest(const char* service_name,
90 mojo::InterfaceRequest<Interface> request,
91 RenderProcessHost* host,
92 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3193 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
94 connector->BindInterface(service_name, std::move(request));
95}
96
Daniel Bratell60854de2018-01-03 17:43:0697void GetRestrictedCookieManagerForWorker(
Victor Costan3e7fa0c2017-12-15 23:23:3098 network::mojom::RestrictedCookieManagerRequest request,
99 RenderProcessHost* render_process_host,
100 const url::Origin& origin) {
101 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
102 switches::kEnableExperimentalWebPlatformFeatures)) {
103 return;
104 }
105
106 StoragePartition* storage_partition =
107 render_process_host->GetStoragePartition();
John Abd-El-Malek53670dd2018-01-18 22:07:21108 network::mojom::NetworkContext* network_context =
Victor Costan3e7fa0c2017-12-15 23:23:30109 storage_partition->GetNetworkContext();
110 uint32_t render_process_id = render_process_host->GetID();
111 network_context->GetRestrictedCookieManager(
112 std::move(request), render_process_id, MSG_ROUTING_NONE);
113}
114
Sam McNally8b4f74d2017-11-10 00:07:56115// Register renderer-exposed interfaces. Each registered interface binder is
116// exposed to all renderer-hosted execution context types (document/frame,
117// dedicated worker, shared worker and service worker) where the appropriate
118// capability spec in the content_browser manifest includes the interface. For
119// interface requests from frames, binders registered on the frame itself
120// override binders registered here.
121void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:34122 parameterized_binder_registry_.AddInterface(base::Bind(
junweifu3605ba12018-06-04 08:21:05123 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetectionProvider>,
Mostyn Bramley-Moored80630e02017-11-13 09:03:34124 shape_detection::mojom::kServiceName));
125 parameterized_binder_registry_.AddInterface(base::Bind(
126 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
127 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31128 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34129 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31130 shape_detection::mojom::kServiceName));
131 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34132 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56133 device::mojom::kServiceName));
Hiroki Nakagawa80b16712018-04-27 03:03:56134
135 // Used for shared workers and service workers to create a websocket.
136 // In other cases, RenderFrameHostImpl for documents or DedicatedWorkerHost
137 // for dedicated workers handles interface requests in order to associate
138 // websockets with a frame. Shared workers and service workers don't have to
139 // do it because they don't have a frame.
140 // TODO(nhiroki): Consider moving this into SharedWorkerHost and
141 // ServiceWorkerProviderHost.
Yutaka Hirano24632bb2018-03-23 08:55:12142 parameterized_binder_registry_.AddInterface(
143 base::BindRepeating(CreateWebSocket));
Hiroki Nakagawa80b16712018-04-27 03:03:56144
Sam McNally6f337bc2017-11-01 02:40:21145 parameterized_binder_registry_.AddInterface(
146 base::Bind([](payments::mojom::PaymentManagerRequest request,
147 RenderProcessHost* host, const url::Origin& origin) {
148 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
149 ->GetPaymentAppContext()
150 ->CreatePaymentManager(std::move(request));
151 }));
Luciano Pacheco626c99e82018-03-22 01:06:56152 parameterized_binder_registry_.AddInterface(base::BindRepeating(
153 [](blink::mojom::CacheStorageRequest request, RenderProcessHost* host,
154 const url::Origin& origin) {
155 static_cast<RenderProcessHostImpl*>(host)->BindCacheStorage(
156 std::move(request), origin);
157 }));
Sam McNally6f337bc2017-11-01 02:40:21158 parameterized_binder_registry_.AddInterface(
159 base::Bind([](blink::mojom::PermissionServiceRequest request,
160 RenderProcessHost* host, const url::Origin& origin) {
161 static_cast<RenderProcessHostImpl*>(host)
162 ->permission_service_context()
Sam McNally2e5c71f2017-12-11 03:24:27163 .CreateServiceForWorker(std::move(request), origin);
Sam McNally6f337bc2017-11-01 02:40:21164 }));
Joshua Bellfdfe23e2017-12-07 19:54:34165 parameterized_binder_registry_.AddInterface(base::BindRepeating(
166 [](blink::mojom::LockManagerRequest request, RenderProcessHost* host,
167 const url::Origin& origin) {
168 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
169 ->GetLockManager()
Sam McNally37e39a82017-12-20 03:35:50170 ->CreateService(std::move(request), origin);
Joshua Bellfdfe23e2017-12-07 19:54:34171 }));
Sam McNally8b4f74d2017-11-10 00:07:56172 parameterized_binder_registry_.AddInterface(
Peter Beverlood6e38b42017-11-28 20:37:43173 base::Bind([](blink::mojom::NotificationServiceRequest request,
174 RenderProcessHost* host, const url::Origin& origin) {
175 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
176 ->GetPlatformNotificationContext()
177 ->CreateService(host->GetID(), origin, std::move(request));
178 }));
Sam McNally54bc0282017-12-13 02:42:29179 parameterized_binder_registry_.AddInterface(
180 base::BindRepeating(&BackgroundFetchServiceImpl::Create));
Victor Costan3e7fa0c2017-12-15 23:23:30181 parameterized_binder_registry_.AddInterface(
Daniel Bratell60854de2018-01-03 17:43:06182 base::BindRepeating(GetRestrictedCookieManagerForWorker));
Sasha Bermeisterf97ff39a2018-01-29 04:50:56183 parameterized_binder_registry_.AddInterface(
184 base::BindRepeating(&QuotaDispatcherHost::CreateForWorker));
Victor Costan7184272a2018-05-15 13:17:43185 parameterized_binder_registry_.AddInterface(base::BindRepeating(
186 [](blink::mojom::CookieStoreRequest request, RenderProcessHost* host,
187 const url::Origin& origin) {
188 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
189 ->GetCookieStoreContext()
190 ->CreateService(std::move(request), origin);
191 }));
Sam McNally8b4f74d2017-11-10 00:07:56192}
193
194RendererInterfaceBinders& GetRendererInterfaceBinders() {
195 CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ());
196 return binders;
Sam McNallyfaf9a402017-10-31 03:06:31197}
198
Yutaka Hirano24632bb2018-03-23 08:55:12199void RendererInterfaceBinders::CreateWebSocket(
200 network::mojom::WebSocketRequest request,
201 RenderProcessHost* host,
202 const url::Origin& origin) {
Hiroki Nakagawaca3a0bee2018-04-25 08:30:02203 WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE, origin,
204 std::move(request));
Yutaka Hirano24632bb2018-03-23 08:55:12205}
206
Sam McNallyfaf9a402017-10-31 03:06:31207} // namespace
208
209void BindWorkerInterface(const std::string& interface_name,
210 mojo::ScopedMessagePipeHandle interface_pipe,
211 RenderProcessHost* host,
212 const url::Origin& origin) {
213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
214
Sam McNally8b4f74d2017-11-10 00:07:56215 GetRendererInterfaceBinders().BindInterface(
216 interface_name, std::move(interface_pipe), host, origin);
217}
218
219bool TryBindFrameInterface(const std::string& interface_name,
220 mojo::ScopedMessagePipeHandle* interface_pipe,
221 RenderFrameHost* frame) {
222 DCHECK_CURRENTLY_ON(BrowserThread::UI);
223
224 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
225 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31226}
227
228} // namespace content