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/usb/usb_service.h"
17 #include "mojo/application/public/cpp/application_connection.h"
18 #include "mojo/application/public/cpp/application_impl.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
26 // The number of seconds to wait without any bound DeviceManagers before
28 const int64 kIdleTimeoutInSeconds
= 10;
30 // A DeviceClient implementation to be constructed iff the app is not running
31 // in an embedder that provides a DeviceClient (i.e. running as a standalone
32 // Mojo app, not in Chrome).
33 class AppDeviceClient
: public DeviceClient
{
35 explicit AppDeviceClient(
36 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
)
37 : blocking_task_runner_(blocking_task_runner
) {}
38 ~AppDeviceClient() override
{}
42 UsbService
* GetUsbService() override
{
44 usb_service_
= UsbService::Create(blocking_task_runner_
);
46 return usb_service_
.get();
49 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
50 scoped_ptr
<UsbService
> usb_service_
;
55 // This class insures that a UsbService has been initialized and is accessible
56 // via the DeviceClient interface.
57 class DevicesApp::USBServiceInitializer
{
59 USBServiceInitializer()
60 : blocking_thread_("USB service blocking I/O thread") {
61 blocking_thread_
.Start();
62 app_device_client_
.reset(
63 new AppDeviceClient(blocking_thread_
.task_runner()));
66 ~USBServiceInitializer() {}
69 scoped_ptr
<AppDeviceClient
> app_device_client_
;
70 base::Thread blocking_thread_
;
72 DISALLOW_COPY_AND_ASSIGN(USBServiceInitializer
);
75 DevicesApp::DevicesApp(
76 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner
)
78 service_task_runner_(service_task_runner
),
79 active_device_manager_count_(0) {
82 DevicesApp::~DevicesApp() {
85 void DevicesApp::Initialize(mojo::ApplicationImpl
* app
) {
87 if (!service_task_runner_
) {
88 service_initializer_
.reset(new USBServiceInitializer
);
89 service_task_runner_
= base::ThreadTaskRunnerHandle::Get();
94 bool DevicesApp::ConfigureIncomingConnection(
95 mojo::ApplicationConnection
* connection
) {
96 connection
->AddService
<usb::DeviceManager
>(this);
100 void DevicesApp::Quit() {
101 service_initializer_
.reset();
105 void DevicesApp::Create(mojo::ApplicationConnection
* connection
,
106 mojo::InterfaceRequest
<usb::DeviceManager
> request
) {
107 // Bind the new device manager to the connecting application's permission
109 usb::PermissionProviderPtr permission_provider
;
110 connection
->ConnectToService(&permission_provider
);
112 // Owned by its message pipe.
113 usb::DeviceManagerImpl
* device_manager
= new usb::DeviceManagerImpl(
114 request
.Pass(), permission_provider
.Pass(), service_task_runner_
);
115 device_manager
->set_connection_error_handler(
116 base::Bind(&DevicesApp::OnConnectionError
, base::Unretained(this)));
118 active_device_manager_count_
++;
119 idle_timeout_callback_
.Cancel();
122 void DevicesApp::OnConnectionError() {
123 DCHECK_GE(active_device_manager_count_
, 0u);
124 active_device_manager_count_
--;
125 if (active_device_manager_count_
== 0) {
126 // If the last DeviceManager connection has been dropped, kick off an idle
127 // timeout to shut ourselves down.
132 void DevicesApp::StartIdleTimer() {
133 // Passing unretained |app_impl_| is safe here because |app_impl_| is
134 // guaranteed to outlive |this|, and the callback is canceled if |this| is
136 idle_timeout_callback_
.Reset(base::Bind(&mojo::ApplicationImpl::Quit
,
137 base::Unretained(app_impl_
)));
138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
139 FROM_HERE
, idle_timeout_callback_
.callback(),
140 base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds
));
143 } // namespace device