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 OpenDevice(const mojo::String
& guid
,
61 mojo::InterfaceRequest
<Device
> device_request
,
62 const OpenDeviceCallback
& callback
) override
;
64 void OnGotDeviceInfoForOpen(mojo::InterfaceRequest
<Device
> device_request
,
65 const OpenDeviceCallback
& callback
,
66 DeviceInfoPtr device_info
);
67 void OnOpenDevicePermissionCheckComplete(
68 mojo::InterfaceRequest
<Device
> device_request
,
69 const OpenDeviceCallback
& callback
,
70 mojo::Array
<mojo::String
> allowed_guids
);
72 // Callbacks to handle the async responses from the underlying UsbService.
73 void OnGetDevices(EnumerationOptionsPtr options
,
74 const GetDevicesCallback
& callback
,
75 const DeviceList
& devices
);
77 // Methods called by |helper_| when devices are added or removed.
78 void OnDeviceAdded(scoped_refptr
<device::UsbDevice
> device
);
79 void OnDeviceRemoved(scoped_refptr
<device::UsbDevice
> device
);
80 void MaybeRunDeviceChangesCallback();
81 void OnEnumerationPermissionCheckComplete(
82 const DeviceMap
& devices_added
,
83 const DeviceMap
& devices_removed
,
84 mojo::Array
<mojo::String
> allowed_guids
);
86 PermissionProviderPtr permission_provider_
;
87 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner_
;
89 // If there are unfinished calls to GetDeviceChanges their callbacks
90 // are stored in |device_change_callbacks_|. Otherwise device changes
91 // are collected in |devices_added_| and |devices_removed_| until the
92 // next call to GetDeviceChanges.
93 std::queue
<GetDeviceChangesCallback
> device_change_callbacks_
;
94 DeviceMap devices_added_
;
95 DeviceMap devices_removed_
;
96 // To ensure that GetDeviceChangesCallbacks are called in the correct order
97 // only perform a single request to |permission_provider_| at a time.
98 bool permission_request_pending_
= false;
100 // |helper_| is owned by the service thread and holds a weak reference
101 // back to the device manager that created it.
102 ServiceThreadHelper
* helper_
;
104 mojo::Closure connection_error_handler_
;
106 mojo::Binding
<DeviceManager
> binding_
;
107 base::WeakPtrFactory
<DeviceManagerImpl
> weak_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl
);
113 } // namespace device
115 #endif // DEVICE_USB_DEVICE_MANAGER_IMPL_H_