OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "device/usb/device_manager_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "device/core/device_client.h" | |
10 #include "device/usb/device_impl.h" | |
11 #include "device/usb/public/cpp/device_manager_delegate.h" | |
12 #include "device/usb/public/cpp/device_manager_factory.h" | |
13 #include "device/usb/public/interfaces/device.mojom.h" | |
14 #include "device/usb/type_converters.h" | |
15 #include "device/usb/usb_device.h" | |
16 #include "device/usb/usb_device_filter.h" | |
17 #include "device/usb/usb_service.h" | |
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | |
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
20 | |
21 namespace device { | |
22 namespace usb { | |
23 | |
24 // static | |
25 void DeviceManagerFactory::Build(mojo::InterfaceRequest<DeviceManager> request, | |
26 scoped_ptr<DeviceManagerDelegate> delegate) { | |
27 // Owned by its MessagePipe. | |
28 new DeviceManagerImpl(request.Pass(), delegate.Pass()); | |
29 } | |
30 | |
31 DeviceManagerImpl::DeviceManagerImpl( | |
32 mojo::InterfaceRequest<DeviceManager> request, | |
33 scoped_ptr<DeviceManagerDelegate> delegate) | |
34 : binding_(this, request.Pass()), | |
35 delegate_(delegate.Pass()), | |
36 weak_factory_(this) { | |
37 } | |
38 | |
39 DeviceManagerImpl::~DeviceManagerImpl() { | |
40 } | |
41 | |
42 void DeviceManagerImpl::GetDevices(EnumerationOptionsPtr options, | |
43 const GetDevicesCallback& callback) { | |
44 DCHECK(DeviceClient::Get()); | |
45 UsbService* usb_service = DeviceClient::Get()->GetUsbService(); | |
46 if (!usb_service) { | |
47 mojo::Array<DeviceInfoPtr> results(0); | |
48 callback.Run(results.Pass()); | |
49 return; | |
50 } | |
51 std::vector<UsbDeviceFilter> filters = | |
52 options->filters.To<std::vector<UsbDeviceFilter>>(); | |
53 usb_service->GetDevices(base::Bind(&DeviceManagerImpl::OnGetDevices, | |
54 weak_factory_.GetWeakPtr(), callback, | |
55 filters)); | |
56 } | |
57 | |
58 void DeviceManagerImpl::OpenDevice( | |
59 const mojo::String& guid, | |
60 mojo::InterfaceRequest<Device> device_request, | |
61 const OpenDeviceCallback& callback) { | |
62 DCHECK(DeviceClient::Get()); | |
63 UsbService* usb_service = DeviceClient::Get()->GetUsbService(); | |
64 if (!usb_service) { | |
65 callback.Run(OPEN_DEVICE_ERROR_NOT_FOUND); | |
66 return; | |
67 } | |
68 scoped_refptr<UsbDevice> device = usb_service->GetDevice(guid); | |
69 if (!device) { | |
70 callback.Run(OPEN_DEVICE_ERROR_NOT_FOUND); | |
71 return; | |
72 } | |
73 device->Open(base::Bind(&DeviceManagerImpl::OnOpenDevice, | |
74 weak_factory_.GetWeakPtr(), callback, | |
75 base::Passed(&device_request))); | |
76 } | |
77 | |
78 void DeviceManagerImpl::OnGetDevices( | |
79 const GetDevicesCallback& callback, | |
80 const std::vector<UsbDeviceFilter>& filters, | |
81 const std::vector<scoped_refptr<UsbDevice>>& devices) { | |
82 mojo::Array<DeviceInfoPtr> device_infos(0); | |
83 for (size_t i = 0; i < devices.size(); ++i) { | |
84 DeviceInfoPtr device_info = DeviceInfo::From(*devices[i]); | |
85 if (UsbDeviceFilter::MatchesAny(devices[i], filters) && | |
86 delegate_->IsDeviceAllowed(*device_info)) { | |
87 const UsbConfigDescriptor* config = devices[i]->GetConfiguration(); | |
88 device_info->configurations = mojo::Array<ConfigurationInfoPtr>::New(0); | |
89 if (config) | |
90 device_info->configurations.push_back(ConfigurationInfo::From(*config)); | |
91 device_infos.push_back(device_info.Pass()); | |
92 } | |
93 } | |
94 callback.Run(device_infos.Pass()); | |
95 } | |
96 | |
97 void DeviceManagerImpl::OnOpenDevice( | |
98 const OpenDeviceCallback& callback, | |
99 mojo::InterfaceRequest<Device> device_request, | |
100 scoped_refptr<UsbDeviceHandle> device_handle) { | |
101 if (!device_handle) { | |
102 callback.Run(OPEN_DEVICE_ERROR_ACCESS_DENIED); | |
103 return; | |
104 } | |
105 | |
106 // Owned by its MessagePipe. | |
107 new DeviceImpl(device_handle, device_request.Pass()); | |
108 | |
109 callback.Run(OPEN_DEVICE_ERROR_OK); | |
110 } | |
111 | |
112 } // namespace usb | |
113 } // namespace device | |
OLD | NEW |