1 // Copyright 2015 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.
5 #include "device/devices_app/devices_app.h"
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "device/core/device_client.h"
15 #include "device/devices_app/usb/device_manager_impl.h"
16 #include "device/devices_app/usb/public/cpp/device_manager_delegate.h"
17 #include "device/usb/usb_service.h"
18 #include "mojo/application/public/cpp/application_connection.h"
19 #include "mojo/application/public/cpp/application_impl.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
27 // The number of seconds to wait without any bound DeviceManagers before
29 const int64 kIdleTimeoutInSeconds
= 10;
31 // A usb::DeviceManagerDelegate implementation which provides origin-based
32 // device access control.
33 class USBDeviceManagerDelegate
: public usb::DeviceManagerDelegate
{
35 explicit USBDeviceManagerDelegate(const GURL
& remote_url
)
36 : remote_url_(remote_url
) {}
37 ~USBDeviceManagerDelegate() override
{}
40 // usb::DeviceManagerDelegate:
41 bool IsDeviceAllowed(const usb::DeviceInfo
& device
) override
{
42 // Limited set of conditions to allow localhost connection for testing. This
43 // does not presume to catch all common local host strings.
44 if (remote_url_
.host() == "127.0.0.1" || remote_url_
.host() == "localhost")
47 // Also let browser apps and mojo apptests talk to all devices.
48 if (remote_url_
.SchemeIs("system") ||
49 remote_url_
== GURL("mojo://devices_apptests/"))
52 // TODO(rockot/reillyg): Implement origin-based device access control.
58 DISALLOW_COPY_AND_ASSIGN(USBDeviceManagerDelegate
);
61 // A DeviceClient implementation to be constructed iff the app is not running
62 // in an embedder that provides a DeviceClient (i.e. running as a standalone
63 // Mojo app, not in Chrome).
64 class AppDeviceClient
: public DeviceClient
{
66 explicit AppDeviceClient(
67 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
)
68 : usb_service_(UsbService::GetInstance(blocking_task_runner
)) {}
69 ~AppDeviceClient() override
{}
73 UsbService
* GetUsbService() override
{ return usb_service_
; }
75 UsbService
* usb_service_
;
80 // This class insures that a UsbService has been initialized and is accessible
81 // via the DeviceClient interface.
82 class DevicesApp::USBServiceInitializer
{
84 USBServiceInitializer()
85 : blocking_thread_("USB service blocking I/O thread") {
86 blocking_thread_
.Start();
87 app_device_client_
.reset(
88 new AppDeviceClient(blocking_thread_
.task_runner()));
91 ~USBServiceInitializer() {}
94 scoped_ptr
<AppDeviceClient
> app_device_client_
;
95 base::Thread blocking_thread_
;
97 DISALLOW_COPY_AND_ASSIGN(USBServiceInitializer
);
100 DevicesApp::DevicesApp(
101 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner
)
102 : app_impl_(nullptr),
103 service_task_runner_(service_task_runner
),
104 active_device_manager_count_(0) {
107 DevicesApp::~DevicesApp() {
110 void DevicesApp::Initialize(mojo::ApplicationImpl
* app
) {
112 if (!service_task_runner_
) {
113 service_initializer_
.reset(new USBServiceInitializer
);
114 service_task_runner_
= base::ThreadTaskRunnerHandle::Get();
119 bool DevicesApp::ConfigureIncomingConnection(
120 mojo::ApplicationConnection
* connection
) {
121 connection
->AddService
<usb::DeviceManager
>(this);
125 void DevicesApp::Quit() {
126 service_initializer_
.reset();
130 void DevicesApp::Create(mojo::ApplicationConnection
* connection
,
131 mojo::InterfaceRequest
<usb::DeviceManager
> request
) {
132 scoped_ptr
<usb::DeviceManagerDelegate
> delegate(new USBDeviceManagerDelegate(
133 GURL(connection
->GetRemoteApplicationURL())));
135 // Owned by its message pipe.
136 usb::DeviceManagerImpl
* device_manager
= new usb::DeviceManagerImpl(
137 request
.Pass(), delegate
.Pass(), service_task_runner_
);
138 device_manager
->set_connection_error_handler(
139 base::Bind(&DevicesApp::OnConnectionError
, base::Unretained(this)));
141 active_device_manager_count_
++;
142 idle_timeout_callback_
.Cancel();
145 void DevicesApp::OnConnectionError() {
146 DCHECK_GE(active_device_manager_count_
, 0u);
147 active_device_manager_count_
--;
148 if (active_device_manager_count_
== 0) {
149 // If the last DeviceManager connection has been dropped, kick off an idle
150 // timeout to shut ourselves down.
155 void DevicesApp::StartIdleTimer() {
156 // Passing unretained |app_impl_| is safe here because |app_impl_| is
157 // guaranteed to outlive |this|, and the callback is canceled if |this| is
159 idle_timeout_callback_
.Reset(base::Bind(&mojo::ApplicationImpl::Terminate
,
160 base::Unretained(app_impl_
)));
161 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
162 FROM_HERE
, idle_timeout_callback_
.callback(),
163 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds
));
166 } // namespace device