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/usb/device_manager_impl.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "device/core/device_client.h"
13 #include "device/devices_app/usb/device_impl.h"
14 #include "device/devices_app/usb/public/cpp/device_manager_delegate.h"
15 #include "device/devices_app/usb/public/interfaces/device.mojom.h"
16 #include "device/devices_app/usb/type_converters.h"
17 #include "device/usb/usb_device.h"
18 #include "device/usb/usb_device_filter.h"
19 #include "device/usb/usb_service.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
21 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
28 void OnGetDevicesOnServiceThread(
29 const UsbService::GetDevicesCallback
& callback
,
30 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
31 const std::vector
<scoped_refptr
<UsbDevice
>>& devices
) {
32 callback_task_runner
->PostTask(FROM_HERE
, base::Bind(callback
, devices
));
35 void GetDevicesOnServiceThread(
36 const UsbService::GetDevicesCallback
& callback
,
37 scoped_refptr
<base::TaskRunner
> callback_task_runner
) {
38 DCHECK(DeviceClient::Get());
39 UsbService
* usb_service
= DeviceClient::Get()->GetUsbService();
41 std::vector
<scoped_refptr
<UsbDevice
>> no_devices
;
42 callback_task_runner
->PostTask(FROM_HERE
, base::Bind(callback
, no_devices
));
45 usb_service
->GetDevices(
46 base::Bind(&OnGetDevicesOnServiceThread
, callback
, callback_task_runner
));
49 void RunOpenDeviceCallback(const DeviceManager::OpenDeviceCallback
& callback
,
50 OpenDeviceError error
) {
54 void OnOpenDeviceOnServiceThread(
55 mojo::InterfaceRequest
<Device
> device_request
,
56 const DeviceManager::OpenDeviceCallback
& callback
,
57 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
58 scoped_refptr
<UsbDeviceHandle
> device_handle
) {
60 callback_task_runner
->PostTask(FROM_HERE
,
61 base::Bind(&RunOpenDeviceCallback
, callback
,
62 OPEN_DEVICE_ERROR_ACCESS_DENIED
));
66 // Owned by its MessagePipe.
67 new DeviceImpl(device_handle
, device_request
.Pass());
69 callback_task_runner
->PostTask(
71 base::Bind(&RunOpenDeviceCallback
, callback
, OPEN_DEVICE_ERROR_OK
));
74 void OpenDeviceOnServiceThread(
75 const std::string
& guid
,
76 mojo::InterfaceRequest
<Device
> device_request
,
77 const DeviceManager::OpenDeviceCallback
& callback
,
78 scoped_refptr
<base::TaskRunner
> callback_task_runner
) {
79 DCHECK(DeviceClient::Get());
80 UsbService
* usb_service
= DeviceClient::Get()->GetUsbService();
82 callback_task_runner
->PostTask(FROM_HERE
,
83 base::Bind(&RunOpenDeviceCallback
, callback
,
84 OPEN_DEVICE_ERROR_NOT_FOUND
));
87 scoped_refptr
<UsbDevice
> device
= usb_service
->GetDevice(guid
);
89 callback_task_runner
->PostTask(FROM_HERE
,
90 base::Bind(&RunOpenDeviceCallback
, callback
,
91 OPEN_DEVICE_ERROR_NOT_FOUND
));
94 device
->Open(base::Bind(&OnOpenDeviceOnServiceThread
,
95 base::Passed(&device_request
), callback
,
96 callback_task_runner
));
101 DeviceManagerImpl::DeviceManagerImpl(
102 mojo::InterfaceRequest
<DeviceManager
> request
,
103 scoped_ptr
<DeviceManagerDelegate
> delegate
,
104 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner
)
105 : binding_(this, request
.Pass()),
106 delegate_(delegate
.Pass()),
107 service_task_runner_(service_task_runner
),
108 weak_factory_(this) {
111 DeviceManagerImpl::~DeviceManagerImpl() {
114 void DeviceManagerImpl::set_connection_error_handler(
115 const mojo::Closure
& error_handler
) {
116 binding_
.set_connection_error_handler(error_handler
);
119 void DeviceManagerImpl::GetDevices(EnumerationOptionsPtr options
,
120 const GetDevicesCallback
& callback
) {
121 auto get_devices_callback
=
122 base::Bind(&DeviceManagerImpl::OnGetDevices
, weak_factory_
.GetWeakPtr(),
123 base::Passed(&options
), callback
);
124 service_task_runner_
->PostTask(
125 FROM_HERE
, base::Bind(&GetDevicesOnServiceThread
, get_devices_callback
,
126 base::ThreadTaskRunnerHandle::Get()));
129 void DeviceManagerImpl::OpenDevice(
130 const mojo::String
& guid
,
131 mojo::InterfaceRequest
<Device
> device_request
,
132 const OpenDeviceCallback
& callback
) {
133 service_task_runner_
->PostTask(
134 FROM_HERE
, base::Bind(&OpenDeviceOnServiceThread
, guid
,
135 base::Passed(&device_request
), callback
,
136 base::ThreadTaskRunnerHandle::Get()));
139 void DeviceManagerImpl::OnGetDevices(
140 EnumerationOptionsPtr options
,
141 const GetDevicesCallback
& callback
,
142 const std::vector
<scoped_refptr
<UsbDevice
>>& devices
) {
143 auto filters
= options
->filters
.To
<std::vector
<UsbDeviceFilter
>>();
144 mojo::Array
<DeviceInfoPtr
> device_infos(0);
145 for (size_t i
= 0; i
< devices
.size(); ++i
) {
146 DeviceInfoPtr device_info
= DeviceInfo::From(*devices
[i
]);
147 if (UsbDeviceFilter::MatchesAny(devices
[i
], filters
) &&
148 delegate_
->IsDeviceAllowed(*device_info
)) {
149 const UsbConfigDescriptor
* config
= devices
[i
]->GetConfiguration();
150 device_info
->configurations
= mojo::Array
<ConfigurationInfoPtr
>::New(0);
152 device_info
->configurations
.push_back(ConfigurationInfo::From(*config
));
153 device_infos
.push_back(device_info
.Pass());
156 callback
.Run(device_infos
.Pass());
160 } // namespace device