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 "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
22 class SequencedTaskRunner
;
28 class UsbDeviceFilter
;
29 class UsbDeviceHandle
;
33 class DeviceManagerDelegate
;
35 // Implementation of the public DeviceManager interface. This interface can be
36 // requested from the devices app located at "mojo:devices", if available.
37 class DeviceManagerImpl
: public DeviceManager
{
40 mojo::InterfaceRequest
<DeviceManager
> request
,
41 scoped_ptr
<DeviceManagerDelegate
> delegate
,
42 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner
);
43 ~DeviceManagerImpl() override
;
45 void set_connection_error_handler(const mojo::Closure
& error_handler
);
48 class ServiceThreadHelper
;
50 // DeviceManager implementation:
51 void GetDevices(EnumerationOptionsPtr options
,
52 const GetDevicesCallback
& callback
) override
;
53 void GetDeviceChanges(const GetDeviceChangesCallback
& callback
) override
;
54 void OpenDevice(const mojo::String
& guid
,
55 mojo::InterfaceRequest
<Device
> device_request
,
56 const OpenDeviceCallback
& callback
) override
;
58 // Callbacks to handle the async responses from the underlying UsbService.
59 void OnGetDevices(const GetDevicesCallback
& callback
,
60 mojo::Array
<DeviceInfoPtr
> devices
);
61 void OnGetInitialDevices(const GetDeviceChangesCallback
& callback
,
62 mojo::Array
<DeviceInfoPtr
> devices
);
64 // Methods called by |helper_| when devices are added or removed.
65 void OnDeviceAdded(DeviceInfoPtr device
);
66 void OnDeviceRemoved(std::string device_guid
);
67 void MaybeRunDeviceChangesCallback();
69 mojo::StrongBinding
<DeviceManager
> binding_
;
71 scoped_ptr
<DeviceManagerDelegate
> delegate_
;
72 scoped_refptr
<base::SequencedTaskRunner
> service_task_runner_
;
74 // If there are unfinished calls to GetDeviceChanges their callbacks
75 // are stored in |device_change_callbacks_|. Otherwise device changes
76 // are collected in |devices_added_| and |devices_removed_| until the
77 // next call to GetDeviceChanges.
78 std::queue
<GetDeviceChangesCallback
> device_change_callbacks_
;
79 mojo::Array
<DeviceInfoPtr
> devices_added_
;
80 std::set
<std::string
> devices_removed_
;
82 // |helper_| is owned by the service thread and holds a weak reference
83 // back to the device manager that created it.
84 ServiceThreadHelper
* helper_
= nullptr;
86 base::WeakPtrFactory
<DeviceManagerImpl
> weak_factory_
;
88 DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl
);
94 #endif // DEVICE_USB_DEVICE_MANAGER_IMPL_H_