Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 1 | // 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 McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 5 | #include "content/browser/renderer_interface_binders.h" |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
Sam McNally | 6f337bc | 2017-11-01 02:40:21 | [diff] [blame] | 9 | #include "base/bind.h" |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 10 | #include "content/browser/background_fetch/background_fetch_service_impl.h" |
| 11 | #include "content/browser/dedicated_worker/dedicated_worker_host.h" |
Joshua Bell | fdfe23e | 2017-12-07 19:54:34 | [diff] [blame] | 12 | #include "content/browser/locks/lock_manager.h" |
Peter Beverloo | d6e38b4 | 2017-11-28 20:37:43 | [diff] [blame] | 13 | #include "content/browser/notifications/platform_notification_context_impl.h" |
Sam McNally | 6f337bc | 2017-11-01 02:40:21 | [diff] [blame] | 14 | #include "content/browser/payments/payment_manager.h" |
| 15 | #include "content/browser/permissions/permission_service_context.h" |
| 16 | #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 McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 19 | #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 McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 22 | #include "content/public/browser/render_frame_host.h" |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 23 | #include "content/public/browser/render_process_host.h" |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 24 | #include "services/device/public/interfaces/constants.mojom.h" |
| 25 | #include "services/device/public/interfaces/vibration_manager.mojom.h" |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 26 | #include "services/service_manager/public/cpp/binder_registry.h" |
| 27 | #include "services/service_manager/public/cpp/connector.h" |
| 28 | #include "services/shape_detection/public/interfaces/barcodedetection.mojom.h" |
| 29 | #include "services/shape_detection/public/interfaces/constants.mojom.h" |
| 30 | #include "services/shape_detection/public/interfaces/facedetection_provider.mojom.h" |
| 31 | #include "services/shape_detection/public/interfaces/textdetection.mojom.h" |
Peter Beverloo | d6e38b4 | 2017-11-28 20:37:43 | [diff] [blame] | 32 | #include "third_party/WebKit/public/platform/modules/notifications/notification_service.mojom.h" |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 33 | #include "url/origin.h" |
| 34 | |
| 35 | namespace content { |
| 36 | namespace { |
| 37 | |
| 38 | // A holder for a parameterized BinderRegistry for content-layer interfaces |
| 39 | // exposed to web workers. |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 40 | class RendererInterfaceBinders { |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 41 | public: |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 42 | RendererInterfaceBinders() { InitializeParameterizedBinderRegistry(); } |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 43 | |
| 44 | // Bind an interface request |interface_pipe| for |interface_name| received |
| 45 | // from a web worker with origin |origin| hosted in the renderer |host|. |
| 46 | void BindInterface(const std::string& interface_name, |
| 47 | mojo::ScopedMessagePipeHandle interface_pipe, |
| 48 | RenderProcessHost* host, |
| 49 | const url::Origin& origin) { |
| 50 | if (parameterized_binder_registry_.TryBindInterface( |
| 51 | interface_name, &interface_pipe, host, origin)) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | GetContentClient()->browser()->BindInterfaceRequestFromWorker( |
| 56 | host, origin, interface_name, std::move(interface_pipe)); |
| 57 | } |
| 58 | |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 59 | // Try binding an interface request |interface_pipe| for |interface_name| |
| 60 | // received from |frame|. |
| 61 | bool TryBindInterface(const std::string& interface_name, |
| 62 | mojo::ScopedMessagePipeHandle* interface_pipe, |
| 63 | RenderFrameHost* frame) { |
| 64 | return parameterized_binder_registry_.TryBindInterface( |
| 65 | interface_name, interface_pipe, frame->GetProcess(), |
| 66 | frame->GetLastCommittedOrigin()); |
| 67 | } |
| 68 | |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 69 | private: |
| 70 | void InitializeParameterizedBinderRegistry(); |
| 71 | |
| 72 | service_manager::BinderRegistryWithArgs<RenderProcessHost*, |
| 73 | const url::Origin&> |
| 74 | parameterized_binder_registry_; |
| 75 | }; |
| 76 | |
| 77 | // Forwards service requests to Service Manager since the renderer cannot launch |
| 78 | // out-of-process services on is own. |
| 79 | template <typename Interface> |
Mostyn Bramley-Moore | d80630e0 | 2017-11-13 09:03:34 | [diff] [blame] | 80 | void ForwardServiceRequest(const char* service_name, |
| 81 | mojo::InterfaceRequest<Interface> request, |
| 82 | RenderProcessHost* host, |
| 83 | const url::Origin& origin) { |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 84 | auto* connector = BrowserContext::GetConnectorFor(host->GetBrowserContext()); |
| 85 | connector->BindInterface(service_name, std::move(request)); |
| 86 | } |
| 87 | |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 88 | // Register renderer-exposed interfaces. Each registered interface binder is |
| 89 | // exposed to all renderer-hosted execution context types (document/frame, |
| 90 | // dedicated worker, shared worker and service worker) where the appropriate |
| 91 | // capability spec in the content_browser manifest includes the interface. For |
| 92 | // interface requests from frames, binders registered on the frame itself |
| 93 | // override binders registered here. |
| 94 | void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() { |
Mostyn Bramley-Moore | d80630e0 | 2017-11-13 09:03:34 | [diff] [blame] | 95 | parameterized_binder_registry_.AddInterface(base::Bind( |
| 96 | &ForwardServiceRequest<shape_detection::mojom::BarcodeDetection>, |
| 97 | shape_detection::mojom::kServiceName)); |
| 98 | parameterized_binder_registry_.AddInterface(base::Bind( |
| 99 | &ForwardServiceRequest<shape_detection::mojom::FaceDetectionProvider>, |
| 100 | shape_detection::mojom::kServiceName)); |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 101 | parameterized_binder_registry_.AddInterface( |
Mostyn Bramley-Moore | d80630e0 | 2017-11-13 09:03:34 | [diff] [blame] | 102 | base::Bind(&ForwardServiceRequest<shape_detection::mojom::TextDetection>, |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 103 | shape_detection::mojom::kServiceName)); |
| 104 | parameterized_binder_registry_.AddInterface( |
Mostyn Bramley-Moore | d80630e0 | 2017-11-13 09:03:34 | [diff] [blame] | 105 | base::Bind(&ForwardServiceRequest<device::mojom::VibrationManager>, |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 106 | device::mojom::kServiceName)); |
| 107 | parameterized_binder_registry_.AddInterface( |
Sam McNally | 6f337bc | 2017-11-01 02:40:21 | [diff] [blame] | 108 | base::Bind([](blink::mojom::WebSocketRequest request, |
| 109 | RenderProcessHost* host, const url::Origin& origin) { |
| 110 | WebSocketManager::CreateWebSocket(host->GetID(), MSG_ROUTING_NONE, |
| 111 | std::move(request)); |
| 112 | })); |
| 113 | parameterized_binder_registry_.AddInterface( |
| 114 | base::Bind([](payments::mojom::PaymentManagerRequest request, |
| 115 | RenderProcessHost* host, const url::Origin& origin) { |
| 116 | static_cast<StoragePartitionImpl*>(host->GetStoragePartition()) |
| 117 | ->GetPaymentAppContext() |
| 118 | ->CreatePaymentManager(std::move(request)); |
| 119 | })); |
| 120 | parameterized_binder_registry_.AddInterface( |
| 121 | base::Bind([](blink::mojom::PermissionServiceRequest request, |
| 122 | RenderProcessHost* host, const url::Origin& origin) { |
| 123 | static_cast<RenderProcessHostImpl*>(host) |
| 124 | ->permission_service_context() |
Sam McNally | 2e5c71f | 2017-12-11 03:24:27 | [diff] [blame^] | 125 | .CreateServiceForWorker(std::move(request), origin); |
Sam McNally | 6f337bc | 2017-11-01 02:40:21 | [diff] [blame] | 126 | })); |
Joshua Bell | fdfe23e | 2017-12-07 19:54:34 | [diff] [blame] | 127 | parameterized_binder_registry_.AddInterface(base::BindRepeating( |
| 128 | [](blink::mojom::LockManagerRequest request, RenderProcessHost* host, |
| 129 | const url::Origin& origin) { |
| 130 | static_cast<StoragePartitionImpl*>(host->GetStoragePartition()) |
| 131 | ->GetLockManager() |
| 132 | ->CreateService(std::move(request)); |
| 133 | })); |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 134 | parameterized_binder_registry_.AddInterface( |
| 135 | base::Bind(&CreateDedicatedWorkerHostFactory)); |
Peter Beverloo | d6e38b4 | 2017-11-28 20:37:43 | [diff] [blame] | 136 | parameterized_binder_registry_.AddInterface( |
| 137 | base::Bind([](blink::mojom::NotificationServiceRequest request, |
| 138 | RenderProcessHost* host, const url::Origin& origin) { |
| 139 | static_cast<StoragePartitionImpl*>(host->GetStoragePartition()) |
| 140 | ->GetPlatformNotificationContext() |
| 141 | ->CreateService(host->GetID(), origin, std::move(request)); |
| 142 | })); |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | RendererInterfaceBinders& GetRendererInterfaceBinders() { |
| 146 | CR_DEFINE_STATIC_LOCAL(RendererInterfaceBinders, binders, ()); |
| 147 | return binders; |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | } // namespace |
| 151 | |
| 152 | void BindWorkerInterface(const std::string& interface_name, |
| 153 | mojo::ScopedMessagePipeHandle interface_pipe, |
| 154 | RenderProcessHost* host, |
| 155 | const url::Origin& origin) { |
| 156 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 157 | |
Sam McNally | 8b4f74d | 2017-11-10 00:07:56 | [diff] [blame] | 158 | GetRendererInterfaceBinders().BindInterface( |
| 159 | interface_name, std::move(interface_pipe), host, origin); |
| 160 | } |
| 161 | |
| 162 | bool TryBindFrameInterface(const std::string& interface_name, |
| 163 | mojo::ScopedMessagePipeHandle* interface_pipe, |
| 164 | RenderFrameHost* frame) { |
| 165 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 166 | |
| 167 | return GetRendererInterfaceBinders().TryBindInterface(interface_name, |
| 168 | interface_pipe, frame); |
Sam McNally | faf9a40 | 2017-10-31 03:06:31 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace content |