OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_USB_DEVICE_MANAGER_IMPL_H_ | 5 #ifndef DEVICE_USB_DEVICE_MANAGER_IMPL_H_ |
6 #define DEVICE_USB_DEVICE_MANAGER_IMPL_H_ | 6 #define DEVICE_USB_DEVICE_MANAGER_IMPL_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/containers/scoped_ptr_map.h" | 11 #include "base/containers/scoped_ptr_map.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/scoped_observer.h" | |
17 #include "device/devices_app/usb/public/interfaces/device_manager.mojom.h" | 16 #include "device/devices_app/usb/public/interfaces/device_manager.mojom.h" |
18 #include "device/devices_app/usb/public/interfaces/permission_provider.mojom.h" | 17 #include "device/devices_app/usb/public/interfaces/permission_provider.mojom.h" |
19 #include "device/usb/usb_service.h" | |
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | 18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" |
21 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | 19 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
22 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
23 | 21 |
24 namespace base { | 22 namespace base { |
25 class SequencedTaskRunner; | 23 class SequencedTaskRunner; |
26 } | 24 } |
27 | 25 |
28 namespace device { | 26 namespace device { |
29 | 27 |
30 class UsbDevice; | 28 class UsbDevice; |
31 class UsbDeviceFilter; | 29 class UsbDeviceFilter; |
32 class UsbDeviceHandle; | 30 class UsbDeviceHandle; |
33 | 31 |
34 namespace usb { | 32 namespace usb { |
35 | 33 |
36 class DeviceManagerDelegate; | 34 class DeviceManagerDelegate; |
37 | 35 |
38 // Implementation of the public DeviceManager interface. This interface can be | 36 // Implementation of the public DeviceManager interface. This interface can be |
39 // requested from the devices app located at "mojo:devices", if available. | 37 // requested from the devices app located at "mojo:devices", if available. |
40 class DeviceManagerImpl : public DeviceManager, | 38 class DeviceManagerImpl : public DeviceManager { |
41 public device::UsbService::Observer { | |
42 public: | 39 public: |
43 using DeviceList = std::vector<scoped_refptr<UsbDevice>>; | 40 using DeviceList = std::vector<scoped_refptr<UsbDevice>>; |
44 using DeviceMap = std::map<std::string, scoped_refptr<device::UsbDevice>>; | 41 using DeviceMap = std::map<std::string, scoped_refptr<device::UsbDevice>>; |
45 | 42 |
46 static void Create(PermissionProviderPtr permission_provider, | 43 DeviceManagerImpl( |
47 mojo::InterfaceRequest<DeviceManager> request); | 44 mojo::InterfaceRequest<DeviceManager> request, |
48 | 45 PermissionProviderPtr permission_provider, |
49 DeviceManagerImpl(PermissionProviderPtr permission_provider, | 46 scoped_refptr<base::SequencedTaskRunner> service_task_runner); |
50 mojo::InterfaceRequest<DeviceManager> request); | |
51 ~DeviceManagerImpl() override; | 47 ~DeviceManagerImpl() override; |
52 | 48 |
53 void set_connection_error_handler(const mojo::Closure& error_handler) { | 49 void set_connection_error_handler(const mojo::Closure& error_handler) { |
54 connection_error_handler_ = error_handler; | 50 connection_error_handler_ = error_handler; |
55 } | 51 } |
56 | 52 |
57 private: | 53 private: |
| 54 class ServiceThreadHelper; |
| 55 |
58 // DeviceManager implementation: | 56 // DeviceManager implementation: |
59 void GetDevices(EnumerationOptionsPtr options, | 57 void GetDevices(EnumerationOptionsPtr options, |
60 const GetDevicesCallback& callback) override; | 58 const GetDevicesCallback& callback) override; |
61 void GetDeviceChanges(const GetDeviceChangesCallback& callback) override; | 59 void GetDeviceChanges(const GetDeviceChangesCallback& callback) override; |
62 void GetDevice(const mojo::String& guid, | 60 void GetDevice(const mojo::String& guid, |
63 mojo::InterfaceRequest<Device> device_request) override; | 61 mojo::InterfaceRequest<Device> device_request) override; |
64 | 62 |
65 // Callbacks to handle the async responses from the underlying UsbService. | 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( | 66 void OnGetDevicePermissionCheckComplete( |
67 scoped_refptr<device::UsbDevice> device, | 67 scoped_refptr<device::UsbDevice> device, |
68 mojo::InterfaceRequest<Device> device_request, | 68 mojo::InterfaceRequest<Device> device_request, |
69 mojo::Array<mojo::String> allowed_guids); | 69 mojo::Array<mojo::String> allowed_guids); |
70 void OnGetDevices(EnumerationOptionsPtr options, | 70 void OnGetDevices(EnumerationOptionsPtr options, |
71 const GetDevicesCallback& callback, | 71 const GetDevicesCallback& callback, |
72 const DeviceList& devices); | 72 const DeviceList& devices); |
73 | 73 |
74 // UsbService::Observer implementation: | 74 // Methods called by |helper_| when devices are added or removed. |
75 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; | 75 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device); |
76 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; | 76 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device); |
77 void WillDestroyUsbService() override; | |
78 | |
79 void MaybeRunDeviceChangesCallback(); | 77 void MaybeRunDeviceChangesCallback(); |
80 void OnEnumerationPermissionCheckComplete( | 78 void OnEnumerationPermissionCheckComplete( |
81 const DeviceMap& devices_added, | 79 const DeviceMap& devices_added, |
82 const DeviceMap& devices_removed, | 80 const DeviceMap& devices_removed, |
83 mojo::Array<mojo::String> allowed_guids); | 81 mojo::Array<mojo::String> allowed_guids); |
84 | 82 |
85 PermissionProviderPtr permission_provider_; | 83 PermissionProviderPtr permission_provider_; |
| 84 scoped_refptr<base::SequencedTaskRunner> service_task_runner_; |
86 | 85 |
87 // If there are unfinished calls to GetDeviceChanges their callbacks | 86 // If there are unfinished calls to GetDeviceChanges their callbacks |
88 // are stored in |device_change_callbacks_|. Otherwise device changes | 87 // are stored in |device_change_callbacks_|. Otherwise device changes |
89 // are collected in |devices_added_| and |devices_removed_| until the | 88 // are collected in |devices_added_| and |devices_removed_| until the |
90 // next call to GetDeviceChanges. | 89 // next call to GetDeviceChanges. |
91 std::queue<GetDeviceChangesCallback> device_change_callbacks_; | 90 std::queue<GetDeviceChangesCallback> device_change_callbacks_; |
92 DeviceMap devices_added_; | 91 DeviceMap devices_added_; |
93 DeviceMap devices_removed_; | 92 DeviceMap devices_removed_; |
94 // To ensure that GetDeviceChangesCallbacks are called in the correct order | 93 // To ensure that GetDeviceChangesCallbacks are called in the correct order |
95 // only perform a single request to |permission_provider_| at a time. | 94 // only perform a single request to |permission_provider_| at a time. |
96 bool permission_request_pending_ = false; | 95 bool permission_request_pending_ = false; |
97 | 96 |
98 UsbService* usb_service_; | 97 // |helper_| is owned by the service thread and holds a weak reference |
99 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; | 98 // back to the device manager that created it. |
| 99 ServiceThreadHelper* helper_; |
100 | 100 |
101 mojo::Closure connection_error_handler_; | 101 mojo::Closure connection_error_handler_; |
102 | 102 |
103 mojo::Binding<DeviceManager> binding_; | 103 mojo::Binding<DeviceManager> binding_; |
104 base::WeakPtrFactory<DeviceManagerImpl> weak_factory_; | 104 base::WeakPtrFactory<DeviceManagerImpl> weak_factory_; |
105 | 105 |
106 DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl); | 106 DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl); |
107 }; | 107 }; |
108 | 108 |
109 } // namespace usb | 109 } // namespace usb |
110 } // namespace device | 110 } // namespace device |
111 | 111 |
112 #endif // DEVICE_USB_DEVICE_MANAGER_IMPL_H_ | 112 #endif // DEVICE_USB_DEVICE_MANAGER_IMPL_H_ |
OLD | NEW |