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/usb/device_manager_impl.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"
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());
31 DeviceManagerImpl::DeviceManagerImpl(
32 mojo::InterfaceRequest
<DeviceManager
> request
,
33 scoped_ptr
<DeviceManagerDelegate
> delegate
)
34 : binding_(this, request
.Pass()),
35 delegate_(delegate
.Pass()),
39 DeviceManagerImpl::~DeviceManagerImpl() {
42 void DeviceManagerImpl::GetDevices(EnumerationOptionsPtr options
,
43 const GetDevicesCallback
& callback
) {
44 DCHECK(DeviceClient::Get());
45 UsbService
* usb_service
= DeviceClient::Get()->GetUsbService();
47 mojo::Array
<EnumerationResultPtr
> results(0);
48 callback
.Run(results
.Pass());
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
,
58 void DeviceManagerImpl::OnGetDevices(
59 const GetDevicesCallback
& callback
,
60 const std::vector
<UsbDeviceFilter
>& filters
,
61 const std::vector
<scoped_refptr
<UsbDevice
>>& devices
) {
62 mojo::Array
<EnumerationResultPtr
> results(0);
63 for (size_t i
= 0; i
< devices
.size(); ++i
) {
64 DeviceInfoPtr device_info
= DeviceInfo::From(*devices
[i
]);
65 if (UsbDeviceFilter::MatchesAny(devices
[i
], filters
) &&
66 delegate_
->IsDeviceAllowed(*device_info
)) {
67 EnumerationResultPtr result
= EnumerationResult::New();
69 // Owned by its message pipe.
70 new DeviceImpl(devices
[i
], mojo::GetProxy(&device
));
71 result
->device
= device
.Pass();
72 results
.push_back(result
.Pass());
75 callback
.Run(results
.Pass());