blob: 74a060efdc5c40d876d24abcdc4042ac071666b9 [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"
Joshua Bellfdfe23e2017-12-07 19:54:3411#include "content/browser/locks/lock_manager.h"
Peter Beverlood6e38b42017-11-28 20:37:4312#include "content/browser/notifications/platform_notification_context_impl.h"
Sam McNally6f337bc2017-11-01 02:40:2113#include "content/browser/payments/payment_manager.h"
14#include "content/browser/permissions/permission_service_context.h"
Sasha Bermeisterf97ff39a2018-01-29 04:50:5615#include "content/browser/quota_dispatcher_host.h"
Sam McNally6f337bc2017-11-01 02:40:2116#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"
Victor Costan3e7fa0c2017-12-15 23:23:3024#include "content/public/common/content_switches.h"
Ke He31d0bb02018-02-24 07:16:2425#include "services/device/public/mojom/constants.mojom.h"
26#include "services/device/public/mojom/vibration_manager.mojom.h"
John Abd-El-Malek3bbbdf92018-01-30 03:27:3527#include "services/network/restricted_cookie_manager.h"
Sam McNallyfaf9a402017-10-31 03:06:3128#include "services/service_manager/public/cpp/binder_registry.h"
29#include "services/service_manager/public/cpp/connector.h"
Ken Rockotd7e999b2018-02-11 15:48:2130#include "services/shape_detection/public/mojom/barcodedetection.mojom.h"
31#include "services/shape_detection/public/mojom/constants.mojom.h"
32#include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
33#include "services/shape_detection/public/mojom/textdetection.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0634#include "third_party/blink/public/platform/modules/cache_storage/cache_storage.mojom.h"
35#include "third_party/blink/public/platform/modules/notifications/notification_service.mojom.h"
Sam McNallyfaf9a402017-10-31 03:06:3136#include "url/origin.h"
37
38namespace content {
39namespace {
40
41// A holder for a parameterized BinderRegistry for content-layer interfaces
42// exposed to web workers.
Sam McNally8b4f74d2017-11-10 00:07:5643class RendererInterfaceBinders {
Sam McNallyfaf9a402017-10-31 03:06:3144 public:
Sam McNally8b4f74d2017-11-10 00:07:5645 RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); }
Sam McNallyfaf9a402017-10-31 03:06:3146
47 // Bind an interface request |interface_pipe| for |interface_name| received
48 // from a web worker with origin |origin| hosted in the renderer |host|.
49 void BindInterface(const std::string& interface_name,
50 mojo::ScopedMessagePipeHandle interface_pipe,
51 RenderProcessHost* host,
52 const url::Origin& origin) {
53 if (parameterized_binder_registry_.TryBindInterface(
54 interface_name, &interface_pipe, host, origin)) {
55 return;
56 }
57
58 GetContentClient()->browser()->BindInterfaceRequestFromWorker(
59 host, origin, interface_name, std::move(interface_pipe));
60 }
61
Sam McNally8b4f74d2017-11-10 00:07:5662 // Try binding an interface request |interface_pipe| for |interface_name|
63 // received from |frame|.
64 bool TryBindInterface(const std::string& interface_name,
65 mojo::ScopedMessagePipeHandle* interface_pipe,
66 RenderFrameHost* frame) {
67 return parameterized_binder_registry_.TryBindInterface(
68 interface_name, interface_pipe, frame->GetProcess(),
69 frame->GetLastCommittedOrigin());
70 }
71
Sam McNallyfaf9a402017-10-31 03:06:3172 private:
73 void InitializeParameterizedBinderRegistry();
74
Yutaka Hirano24632bb2018-03-23 08:55:1275 static void CreateWebSocket(network::mojom::WebSocketRequest request,
76 RenderProcessHost* host,
77 const url::Origin& origin);
78
Sam McNallyfaf9a402017-10-31 03:06:3179 service_manager::BinderRegistryWithArgs<RenderProcessHost*,
80 const url::Origin&>
81 parameterized_binder_registry_;
82};
83
84// Forwards service requests to Service Manager since the renderer cannot launch
85// out-of-process services on is own.
86template <typename Interface>
Mostyn Bramley-Moored80630e02017-11-13 09:03:3487void ForwardServiceRequest(const char* service_name,
88 mojo::InterfaceRequest<Interface> request,
89 RenderProcessHost* host,
90 const url::Origin& origin) {
Sam McNallyfaf9a402017-10-31 03:06:3191 auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext());
92 connector->BindInterface(service_name, std::move(request));
93}
94
Daniel Bratell60854de2018-01-03 17:43:0695void GetRestrictedCookieManagerForWorker(
Victor Costan3e7fa0c2017-12-15 23:23:3096 network::mojom::RestrictedCookieManagerRequest request,
97 RenderProcessHost* render_process_host,
98 const url::Origin& origin) {
99 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
100 switches::kEnableExperimentalWebPlatformFeatures)) {
101 return;
102 }
103
104 StoragePartition* storage_partition =
105 render_process_host->GetStoragePartition();
John Abd-El-Malek53670dd2018-01-18 22:07:21106 network::mojom::NetworkContext* network_context =
Victor Costan3e7fa0c2017-12-15 23:23:30107 storage_partition->GetNetworkContext();
108 uint32_t render_process_id = render_process_host->GetID();
109 network_context->GetRestrictedCookieManager(
110 std::move(request), render_process_id, MSG_ROUTING_NONE);
111}
112
Sam McNally8b4f74d2017-11-10 00:07:56113// Register renderer-exposed interfaces. Each registered interface binder is
114// exposed to all renderer-hosted execution context types (document/frame,
115// dedicated worker, shared worker and service worker) where the appropriate
116// capability spec in the content_browser manifest includes the interface. For
117// interface requests from frames, binders registered on the frame itself
118// override binders registered here.
119void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
Mostyn Bramley-Moored80630e02017-11-13 09:03:34120 parameterized_binder_registry_.AddInterface(base::Bind(
121 &ForwardServiceRequest<shape_detection::mojom::BarcodeDetection>,
122 shape_detection::mojom::kServiceName));
123 parameterized_binder_registry_.AddInterface(base::Bind(
124 &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>,
125 shape_detection::mojom::kServiceName));
Sam McNallyfaf9a402017-10-31 03:06:31126 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34127 base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>,
Sam McNallyfaf9a402017-10-31 03:06:31128 shape_detection::mojom::kServiceName));
129 parameterized_binder_registry_.AddInterface(
Mostyn Bramley-Moored80630e02017-11-13 09:03:34130 base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>,
Sam McNally8b4f74d2017-11-10 00:07:56131 device::mojom::kServiceName));
Hiroki Nakagawa80b16712018-04-27 03:03:56132
133 // Used for shared workers and service workers to create a websocket.
134 // In other cases, RenderFrameHostImpl for documents or DedicatedWorkerHost
135 // for dedicated workers handles interface requests in order to associate
136 // websockets with a frame. Shared workers and service workers don't have to
137 // do it because they don't have a frame.
138 // TODO(nhiroki): Consider moving this into SharedWorkerHost and
139 // ServiceWorkerProviderHost.
Yutaka Hirano24632bb2018-03-23 08:55:12140 parameterized_binder_registry_.AddInterface(
141 base::BindRepeating(CreateWebSocket));
Hiroki Nakagawa80b16712018-04-27 03:03:56142
Sam McNally6f337bc2017-11-01 02:40:21143 parameterized_binder_registry_.AddInterface(
144 base::Bind([](payments::mojom::PaymentManagerRequest request,
145 RenderProcessHost* host, const url::Origin& origin) {
146 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
147 ->GetPaymentAppContext()
148 ->CreatePaymentManager(std::move(request));
149 }));
Luciano Pacheco626c99e82018-03-22 01:06:56150 parameterized_binder_registry_.AddInterface(base::BindRepeating(
151 [](blink::mojom::CacheStorageRequest request, RenderProcessHost* host,
152 const url::Origin& origin) {
153 static_cast<RenderProcessHostImpl*>(host)->BindCacheStorage(
154 std::move(request), origin);
155 }));
Sam McNally6f337bc2017-11-01 02:40:21156 parameterized_binder_registry_.AddInterface(
157 base::Bind([](blink::mojom::PermissionServiceRequest request,
158 RenderProcessHost* host, const url::Origin& origin) {
159 static_cast<RenderProcessHostImpl*>(host)
160 ->permission_service_context()
Sam McNally2e5c71f2017-12-11 03:24:27161 .CreateServiceForWorker(std::move(request), origin);
Sam McNally6f337bc2017-11-01 02:40:21162 }));
Joshua Bellfdfe23e2017-12-07 19:54:34163 parameterized_binder_registry_.AddInterface(base::BindRepeating(
164 [](blink::mojom::LockManagerRequest request, RenderProcessHost* host,
165 const url::Origin& origin) {
166 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
167 ->GetLockManager()
Sam McNally37e39a82017-12-20 03:35:50168 ->CreateService(std::move(request), origin);
Joshua Bellfdfe23e2017-12-07 19:54:34169 }));
Sam McNally8b4f74d2017-11-10 00:07:56170 parameterized_binder_registry_.AddInterface(
Peter Beverlood6e38b42017-11-28 20:37:43171 base::Bind([](blink::mojom::NotificationServiceRequest request,
172 RenderProcessHost* host, const url::Origin& origin) {
173 static_cast<StoragePartitionImpl*>(host->GetStoragePartition())
174 ->GetPlatformNotificationContext()
175 ->CreateService(host->GetID(), origin, std::move(request));
176 }));
Sam McNally54bc0282017-12-13 02:42:29177 parameterized_binder_registry_.AddInterface(
178 base::BindRepeating(&BackgroundFetchServiceImpl::Create));
Victor Costan3e7fa0c2017-12-15 23:23:30179 parameterized_binder_registry_.AddInterface(
Daniel Bratell60854de2018-01-03 17:43:06180 base::BindRepeating(GetRestrictedCookieManagerForWorker));
Sasha Bermeisterf97ff39a2018-01-29 04:50:56181 parameterized_binder_registry_.AddInterface(
182 base::BindRepeating(&QuotaDispatcherHost::CreateForWorker));
Sam McNally8b4f74d2017-11-10 00:07:56183}
184
185RendererInterfaceBinders& GetRendererInterfaceBinders() {
186 CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ());
187 return binders;
Sam McNallyfaf9a402017-10-31 03:06:31188}
189
Yutaka Hirano24632bb2018-03-23 08:55:12190void RendererInterfaceBinders::CreateWebSocket(
191 network::mojom::WebSocketRequest request,
192 RenderProcessHost* host,
193 const url::Origin& origin) {
Hiroki Nakagawaca3a0bee2018-04-25 08:30:02194 WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE, origin,
195 std::move(request));
Yutaka Hirano24632bb2018-03-23 08:55:12196}
197
Sam McNallyfaf9a402017-10-31 03:06:31198} // namespace
199
200void BindWorkerInterface(const std::string& interface_name,
201 mojo::ScopedMessagePipeHandle interface_pipe,
202 RenderProcessHost* host,
203 const url::Origin& origin) {
204 DCHECK_CURRENTLY_ON(BrowserThread::UI);
205
Sam McNally8b4f74d2017-11-10 00:07:56206 GetRendererInterfaceBinders().BindInterface(
207 interface_name, std::move(interface_pipe), host, origin);
208}
209
210bool TryBindFrameInterface(const std::string& interface_name,
211 mojo::ScopedMessagePipeHandle* interface_pipe,
212 RenderFrameHost* frame) {
213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
214
215 return GetRendererInterfaceBinders().TryBindInterface(interface_name,
216 interface_pipe, frame);
Sam McNallyfaf9a402017-10-31 03:06:31217}
218
219} // namespace content