Process Alt-Svc headers.
[chromium-blink-merge.git] / device / devices_app / usb / device_manager_impl.cc
blobda3681b18d5834c3cf668e56f4fce570b461c8a3
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"
7 #include "base/bind.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"
23 namespace device {
24 namespace usb {
26 namespace {
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();
40 if (!usb_service) {
41 std::vector<scoped_refptr<UsbDevice>> no_devices;
42 callback_task_runner->PostTask(FROM_HERE, base::Bind(callback, no_devices));
43 return;
45 usb_service->GetDevices(
46 base::Bind(&OnGetDevicesOnServiceThread, callback, callback_task_runner));
49 void RunOpenDeviceCallback(const DeviceManager::OpenDeviceCallback& callback,
50 OpenDeviceError error) {
51 callback.Run(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) {
59 if (!device_handle) {
60 callback_task_runner->PostTask(FROM_HERE,
61 base::Bind(&RunOpenDeviceCallback, callback,
62 OPEN_DEVICE_ERROR_ACCESS_DENIED));
63 return;
66 // Owned by its MessagePipe.
67 new DeviceImpl(device_handle, device_request.Pass());
69 callback_task_runner->PostTask(
70 FROM_HERE,
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();
81 if (!usb_service) {
82 callback_task_runner->PostTask(FROM_HERE,
83 base::Bind(&RunOpenDeviceCallback, callback,
84 OPEN_DEVICE_ERROR_NOT_FOUND));
85 return;
87 scoped_refptr<UsbDevice> device = usb_service->GetDevice(guid);
88 if (!device) {
89 callback_task_runner->PostTask(FROM_HERE,
90 base::Bind(&RunOpenDeviceCallback, callback,
91 OPEN_DEVICE_ERROR_NOT_FOUND));
92 return;
94 device->Open(base::Bind(&OnOpenDeviceOnServiceThread,
95 base::Passed(&device_request), callback,
96 callback_task_runner));
99 } // namespace
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);
151 if (config)
152 device_info->configurations.push_back(ConfigurationInfo::From(*config));
153 device_infos.push_back(device_info.Pass());
156 callback.Run(device_infos.Pass());
159 } // namespace usb
160 } // namespace device