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 #ifndef DEVICE_USB_DEVICE_MANAGER_IMPL_H_
6 #define DEVICE_USB_DEVICE_MANAGER_IMPL_H_
11 #include "base/containers/scoped_ptr_map.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "device/devices_app/usb/public/interfaces/device_manager.mojom.h"
17 #include "device/devices_app/usb/public/interfaces/permission_provider.mojom.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
23 class SequencedTaskRunner
;
29 class UsbDeviceFilter
;
30 class UsbDeviceHandle
;
34 class DeviceManagerDelegate
;
36 // Implementation of the public DeviceManager interface. This interface can be
37 // requested from the devices app located at "mojo:devices", if available.
38 class DeviceManagerImpl
: public DeviceManager
{
40 using DeviceList
= std::vector
<scoped_refptr
<UsbDevice
>>;
41 using DeviceMap
= std::map
<std::string
, scoped_refptr
<device::UsbDevice
>>;
44 mojo::InterfaceRequest
<DeviceManager
> request
,
45 PermissionProviderPtr permission_provider
,
46 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner
);
47 ~DeviceManagerImpl() override
;
49 void set_connection_error_handler(const mojo::Closure
& error_handler
) {
50 connection_error_handler_
= error_handler
;
54 class ServiceThreadHelper
;
56 // DeviceManager implementation:
57 void GetDevices(EnumerationOptionsPtr options
,
58 const GetDevicesCallback
& callback
) override
;
59 void GetDeviceChanges(const GetDeviceChangesCallback
& callback
) override
;
60 void GetDevice(const mojo::String
& guid
,
61 mojo::InterfaceRequest
<Device
> device_request
) override
;
63 // Callbacks to handle the async responses from the underlying UsbService.
64 void OnGetDevice(mojo::InterfaceRequest
<Device
> device_request
,
65 scoped_refptr
<UsbDevice
> device
);
66 void OnGetDevicePermissionCheckComplete(
67 scoped_refptr
<device::UsbDevice
> device
,
68 mojo::InterfaceRequest
<Device
> device_request
,
69 mojo::Array
<mojo::String
> allowed_guids
);
70 void OnGetDevices(EnumerationOptionsPtr options
,
71 const GetDevicesCallback
& callback
,
72 const DeviceList
& devices
);
74 // Methods called by |helper_| when devices are added or removed.
75 void OnDeviceAdded(scoped_refptr
<device::UsbDevice
> device
);
76 void OnDeviceRemoved(scoped_refptr
<device::UsbDevice
> device
);
77 void MaybeRunDeviceChangesCallback();
78 void OnEnumerationPermissionCheckComplete(
79 const DeviceMap
& devices_added
,
80 const DeviceMap
& devices_removed
,
81 mojo::Array
<mojo::String
> allowed_guids
);
83 PermissionProviderPtr permission_provider_
;
84 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner_
;
86 // If there are unfinished calls to GetDeviceChanges their callbacks
87 // are stored in |device_change_callbacks_|. Otherwise device changes
88 // are collected in |devices_added_| and |devices_removed_| until the
89 // next call to GetDeviceChanges.
90 std::queue
<GetDeviceChangesCallback
> device_change_callbacks_
;
91 DeviceMap devices_added_
;
92 DeviceMap devices_removed_
;
93 // To ensure that GetDeviceChangesCallbacks are called in the correct order
94 // only perform a single request to |permission_provider_| at a time.
95 bool permission_request_pending_
= false;
97 // |helper_| is owned by the service thread and holds a weak reference
98 // back to the device manager that created it.
99 ServiceThreadHelper
* helper_
;
101 mojo::Closure connection_error_handler_
;
103 mojo::Binding
<DeviceManager
> binding_
;
104 base::WeakPtrFactory
<DeviceManagerImpl
> weak_factory_
;
106 DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl
);
110 } // namespace device
112 #endif // DEVICE_USB_DEVICE_MANAGER_IMPL_H_