blob: 3a22004e3618374fcc689fa0aad653167c7bad31 [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 Costanfe70d142018-06-04 22:31:3811#include "content/browser/child_process_security_policy_impl.h"
Victor Costan7184272a2018-05-15 13:17:4312#include "content/browser/cookie_store/cookie_store_context.h"
Joshua Bellfdfe23e2017-12-07 19:54:3413#include "content/browser/locks/lock_manager.h"
Peter Beverlood6e38b42017-11-28 20:37:4314#include "content/browser/notifications/platform_notification_context_impl.h"
Sam McNally6f337bc2017-11-01 02:40:2115#include "content/browser/payments/payment_manager.h"
16#include "content/browser/permissions/permission_service_context.h"
Sasha Bermeisterf97ff39a2018-01-29 04:50:5617#include "content/browser/quota_dispatcher_host.h"
Sam McNally6f337bc2017-11-01 02:40:2118#include "content/browser/renderer_host/render_process_host_impl.h"
19#include "content/browser/storage_partition_impl.h"
20#include "content/browser/websockets/websocket_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3121#include "content/public/browser/browser_context.h"
22#include "content/public/browser/browser_thread.h"
23#include "content/public/browser/content_browser_client.h"
Sam McNally8b4f74d2017-11-10 00:07:5624#include "content/public/browser/render_frame_host.h"
Sam McNallyfaf9a402017-10-31 03:06:3125#include "content/public/browser/render_process_host.h"
Victor Costan3e7fa0c2017-12-15 23:23:3026#include "content/public/common/content_switches.h"
Ke He31d0bb02018-02-24 07:16:2427#include "services/device/public/mojom/constants.mojom.h"
28#include "services/device/public/mojom/vibration_manager.mojom.h"
John Abd-El-Malek3bbbdf92018-01-30 03:27:3529#include "services/network/restricted_cookie_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3130#include "services/service_manager/public/cpp/binder_registry.h"
31#include "services/service_manager/public/cpp/connector.h"
junweifu3605ba12018-06-04 08:21:0532#include "services/shape_detection/public/mojom/barcodedetection_provider.mojom.h"
Ken Rockotd7e999b2018-02-11 15:48:2133#include "services/shape_detection/public/mojom/constants.mojom.h"
34#include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
35#include "services/shape_detection/public/mojom/textdetection.mojom.h"
Victor Costan7184272a2018-05-15 13:17:4336#include "third_party/blink/public/mojom/cookie_store/cookie_store.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0637#include "third_party/blink/public/platform/modules/cache_storage/cache_storage.mojom.h"
38#include "third_party/blink/public/platform/modules/notifications/notification_service.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3139#include "url/origin.h"
40
41namespace content {
42namespace {
43
44// A holder for a parameterized BinderRegistry for content-layer interfaces
45// exposed to web workers.
Sam McNally8b4f74d2017-11-10 00:07:5646class RendererInterfaceBinders {
Sam McNallyfaf9a402017-10-31 03:06:3147 public:
Sam McNally8b4f74d2017-11-10 00:07:5648 RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); }
Sam McNallyfaf9a402017-10-31 03:06:3149
50 // Bind an interface request |interface_pipe| for |interface_name| received
51 // from a web worker with origin |origin| hosted in the renderer |host|.
52 void BindInterface(const std::string& interface_name,
53 mojo::ScopedMessagePipeHandle interface_pipe,
54 RenderProcessHost* host,
55 const url::Origin& origin) {
56 if (parameterized_binder_registry_.TryBindInterface(
57 interface_name, &interface_pipe, host, origin)) {
58 return;
59 }
60
61 GetContentClient()->browser()->BindInterfaceRequestFromWorker(
62 host, origin, interface_name, std::move(interface_pipe));
63 }
64
Sam McNally8b4f74d2017-11-10 00:07:5665 // Try binding an interface request |interface_pipe| for |interface_name|
66 // received from |frame|.
67 bool TryBindInterface(const std::string& interface_name,
68 mojo::ScopedMessagePipeHandle* interface_pipe,
69 RenderFrameHost* frame) {
70 return parameterized_binder_registry_.TryBindInterface(
71 interface_name, interface_pipe, frame->GetProcess(),
72 frame->GetLastCommittedOrigin());
73 }
74
Sam McNallyfaf9a402017-10-31 03:06:3175 private:
76 void InitializeParameterizedBinderRegistry();
77
Yutaka Hirano24632bb2018-03-23 08:55:1278 static void CreateWebSocket(network::mojom::WebSocketRequest request,
79 RenderProcessHost* host,
80 const url::Origin& origin);
81
Sam McNallyfaf9a402017-10-31 03:06:3182 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
83 const url::Origin&>
84 parameterized_binder_registry_;
85};
86
87// Forwards service requests to Service Manager since the renderer cannot launch
88// out-of-process services on is own.
89template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3490void ForwardServiceRequest(const char* service_name,
91 mojo::InterfaceRequest<Interface> request,
92 RenderProcessHost* host,
93 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3194 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
95 connector->BindInterface(service_name, std::move(request));
96}
97
Victor Costanfe70d142018-06-04 22:31:3898void GetRestrictedCookieManager(
Victor Costan3e7fa0c2017-12-15 23:23:3099 network::mojom::RestrictedCookieManagerRequest request,
100 RenderProcessHost* render_process_host,
101 const url::Origin& origin) {
Victor Costan3e7fa0c2017-12-15 23:23:30102 StoragePartition* storage_partition =
103 render_process_host->GetStoragePartition();
John Abd-El-Malek53670dd2018-01-18 22:07:21104 network::mojom::NetworkContext* network_context =
Victor Costan3e7fa0c2017-12-15 23:23:30105 storage_partition->GetNetworkContext();
Victor Costanfe70d142018-06-04 22:31:38106 network_context->GetRestrictedCookieManager(std::move(request), origin);
Victor Costan3e7fa0c2017-12-15 23:23:30107}
108
Sam McNally8b4f74d2017-11-10 00:07:56109// Register renderer-exposed interfaces. Each registered interface binder is
110// exposed to all renderer-hosted execution context types (document/frame,
111// dedicated worker, shared worker and service worker) where the appropriate
112// capability spec in the content_browser manifest includes the interface. For
113// interface requests from frames, binders registered on the frame itself
114// override binders registered here.
115void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:34116 parameterized_binder_registry_.AddInterface(base::Bind(
junweifu3605ba12018-06-04 08:21:05117 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetectionProvider>,
Mostyn Bramley-Moored80630e02017-11-13 09:03:34118 shape_detection::mojom::kServiceName));
119 parameterized_binder_registry_.AddInterface(base::Bind(
120 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
121 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31122 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34123 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31124 shape_detection::mojom::kServiceName));
125 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34126 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56127 device::mojom::kServiceName));
Hiroki Nakagawa80b16712018-04-27 03:03:56128
129 // Used for shared workers and service workers to create a websocket.
130 // In other cases, RenderFrameHostImpl for documents or DedicatedWorkerHost
131 // for dedicated workers handles interface requests in order to associate
132 // websockets with a frame. Shared workers and service workers don't have to
133 // do it because they don't have a frame.
134 // TODO(nhiroki): Consider moving this into SharedWorkerHost and
135 // ServiceWorkerProviderHost.
Yutaka Hirano24632bb2018-03-23 08:55:12136 parameterized_binder_registry_.AddInterface(
137 base::BindRepeating(CreateWebSocket));
Hiroki Nakagawa80b16712018-04-27 03:03:56138
Sam McNally6f337bc2017-11-01 02:40:21139 parameterized_binder_registry_.AddInterface(
140 base::Bind([](payments::mojom::PaymentManagerRequest request,
141 RenderProcessHost* host, const url::Origin& origin) {
142 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
143 ->GetPaymentAppContext()
144 ->CreatePaymentManager(std::move(request));
145 }));
Luciano Pacheco626c99e82018-03-22 01:06:56146 parameterized_binder_registry_.AddInterface(base::BindRepeating(
147 [](blink::mojom::CacheStorageRequest request, RenderProcessHost* host,
148 const url::Origin& origin) {
149 static_cast<RenderProcessHostImpl*>(host)->BindCacheStorage(
150 std::move(request), origin);
151 }));
Sam McNally6f337bc2017-11-01 02:40:21152 parameterized_binder_registry_.AddInterface(
153 base::Bind([](blink::mojom::PermissionServiceRequest request,
154 RenderProcessHost* host, const url::Origin& origin) {
155 static_cast<RenderProcessHostImpl*>(host)
156 ->permission_service_context()
Sam McNally2e5c71f2017-12-11 03:24:27157 .CreateServiceForWorker(std::move(request), origin);
Sam McNally6f337bc2017-11-01 02:40:21158 }));
Joshua Bellfdfe23e2017-12-07 19:54:34159 parameterized_binder_registry_.AddInterface(base::BindRepeating(
160 [](blink::mojom::LockManagerRequest request, RenderProcessHost* host,
161 const url::Origin& origin) {
162 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
163 ->GetLockManager()
Sam McNally37e39a82017-12-20 03:35:50164 ->CreateService(std::move(request), origin);
Joshua Bellfdfe23e2017-12-07 19:54:34165 }));
Sam McNally8b4f74d2017-11-10 00:07:56166 parameterized_binder_registry_.AddInterface(
Peter Beverlood6e38b42017-11-28 20:37:43167 base::Bind([](blink::mojom::NotificationServiceRequest request,
168 RenderProcessHost* host, const url::Origin& origin) {
169 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
170 ->GetPlatformNotificationContext()
171 ->CreateService(host->GetID(), origin, std::move(request));
172 }));
Sam McNally54bc0282017-12-13 02:42:29173 parameterized_binder_registry_.AddInterface(
174 base::BindRepeating(&BackgroundFetchServiceImpl::Create));
Victor Costan3e7fa0c2017-12-15 23:23:30175 parameterized_binder_registry_.AddInterface(
Victor Costanfe70d142018-06-04 22:31:38176 base::BindRepeating(GetRestrictedCookieManager));
Sasha Bermeisterf97ff39a2018-01-29 04:50:56177 parameterized_binder_registry_.AddInterface(
178 base::BindRepeating(&QuotaDispatcherHost::CreateForWorker));
Victor Costan7184272a2018-05-15 13:17:43179 parameterized_binder_registry_.AddInterface(base::BindRepeating(
180 [](blink::mojom::CookieStoreRequest request, RenderProcessHost* host,
181 const url::Origin& origin) {
182 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
183 ->GetCookieStoreContext()
184 ->CreateService(std::move(request), origin);
185 }));
Sam McNally8b4f74d2017-11-10 00:07:56186}
187
188RendererInterfaceBinders& GetRendererInterfaceBinders() {
189 CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ());
190 return binders;
Sam McNallyfaf9a402017-10-31 03:06:31191}
192
Yutaka Hirano24632bb2018-03-23 08:55:12193void RendererInterfaceBinders::CreateWebSocket(
194 network::mojom::WebSocketRequest request,
195 RenderProcessHost* host,
196 const url::Origin& origin) {
Hiroki Nakagawaca3a0bee2018-04-25 08:30:02197 WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE, origin,
198 std::move(request));
Yutaka Hirano24632bb2018-03-23 08:55:12199}
200
Sam McNallyfaf9a402017-10-31 03:06:31201} // namespace
202
203void BindWorkerInterface(const std::string& interface_name,
204 mojo::ScopedMessagePipeHandle interface_pipe,
205 RenderProcessHost* host,
206 const url::Origin& origin) {
207 DCHECK_CURRENTLY_ON(BrowserThread::UI);
208
Sam McNally8b4f74d2017-11-10 00:07:56209 GetRendererInterfaceBinders().BindInterface(
210 interface_name, std::move(interface_pipe), host, origin);
211}
212
213bool TryBindFrameInterface(const std::string& interface_name,
214 mojo::ScopedMessagePipeHandle* interface_pipe,
215 RenderFrameHost* frame) {
216 DCHECK_CURRENTLY_ON(BrowserThread::UI);
217
218 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
219 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31220}
221
222} // namespace content