| 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" |
| 16 #include "device/devices_app/usb/public/interfaces/device_manager.mojom.h" | 17 #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 "device/devices_app/usb/public/interfaces/permission_provider.mojom.h" |
| 19 #include "device/usb/usb_service.h" |
| 18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" |
| 19 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | 21 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
| 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | 22 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 21 | 23 |
| 22 namespace base { | 24 namespace base { |
| 23 class SequencedTaskRunner; | 25 class SequencedTaskRunner; |
| 24 } | 26 } |
| 25 | 27 |
| 26 namespace device { | 28 namespace device { |
| 27 | 29 |
| 28 class UsbDevice; | 30 class UsbDevice; |
| 29 class UsbDeviceFilter; | 31 class UsbDeviceFilter; |
| 30 class UsbDeviceHandle; | 32 class UsbDeviceHandle; |
| 31 | 33 |
| 32 namespace usb { | 34 namespace usb { |
| 33 | 35 |
| 34 class DeviceManagerDelegate; | 36 class DeviceManagerDelegate; |
| 35 | 37 |
| 36 // Implementation of the public DeviceManager interface. This interface can be | 38 // Implementation of the public DeviceManager interface. This interface can be |
| 37 // requested from the devices app located at "mojo:devices", if available. | 39 // requested from the devices app located at "mojo:devices", if available. |
| 38 class DeviceManagerImpl : public DeviceManager { | 40 class DeviceManagerImpl : public DeviceManager, |
| 41 public device::UsbService::Observer { |
| 39 public: | 42 public: |
| 40 using DeviceList = std::vector<scoped_refptr<UsbDevice>>; | 43 using DeviceList = std::vector<scoped_refptr<UsbDevice>>; |
| 41 using DeviceMap = std::map<std::string, scoped_refptr<device::UsbDevice>>; | 44 using DeviceMap = std::map<std::string, scoped_refptr<device::UsbDevice>>; |
| 42 | 45 |
| 43 DeviceManagerImpl( | 46 static void Create(PermissionProviderPtr permission_provider, |
| 44 mojo::InterfaceRequest<DeviceManager> request, | 47 mojo::InterfaceRequest<DeviceManager> request); |
| 45 PermissionProviderPtr permission_provider, | 48 |
| 46 scoped_refptr<base::SequencedTaskRunner> service_task_runner); | 49 DeviceManagerImpl(PermissionProviderPtr permission_provider, |
| 50 mojo::InterfaceRequest<DeviceManager> request); |
| 47 ~DeviceManagerImpl() override; | 51 ~DeviceManagerImpl() override; |
| 48 | 52 |
| 49 void set_connection_error_handler(const mojo::Closure& error_handler) { | 53 void set_connection_error_handler(const mojo::Closure& error_handler) { |
| 50 connection_error_handler_ = error_handler; | 54 connection_error_handler_ = error_handler; |
| 51 } | 55 } |
| 52 | 56 |
| 53 private: | 57 private: |
| 54 class ServiceThreadHelper; | |
| 55 | |
| 56 // DeviceManager implementation: | 58 // DeviceManager implementation: |
| 57 void GetDevices(EnumerationOptionsPtr options, | 59 void GetDevices(EnumerationOptionsPtr options, |
| 58 const GetDevicesCallback& callback) override; | 60 const GetDevicesCallback& callback) override; |
| 59 void GetDeviceChanges(const GetDeviceChangesCallback& callback) override; | 61 void GetDeviceChanges(const GetDeviceChangesCallback& callback) override; |
| 60 void GetDevice(const mojo::String& guid, | 62 void GetDevice(const mojo::String& guid, |
| 61 mojo::InterfaceRequest<Device> device_request) override; | 63 mojo::InterfaceRequest<Device> device_request) override; |
| 62 | 64 |
| 63 // Callbacks to handle the async responses from the underlying UsbService. | 65 // 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 // Methods called by |helper_| when devices are added or removed. | 74 // UsbService::Observer implementation: |
| 75 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device); | 75 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override; |
| 76 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device); | 76 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override; |
| 77 void WillDestroyUsbService() override; |
| 78 |
| 77 void MaybeRunDeviceChangesCallback(); | 79 void MaybeRunDeviceChangesCallback(); |
| 78 void OnEnumerationPermissionCheckComplete( | 80 void OnEnumerationPermissionCheckComplete( |
| 79 const DeviceMap& devices_added, | 81 const DeviceMap& devices_added, |
| 80 const DeviceMap& devices_removed, | 82 const DeviceMap& devices_removed, |
| 81 mojo::Array<mojo::String> allowed_guids); | 83 mojo::Array<mojo::String> allowed_guids); |
| 82 | 84 |
| 83 PermissionProviderPtr permission_provider_; | 85 PermissionProviderPtr permission_provider_; |
| 84 scoped_refptr<base::SequencedTaskRunner> service_task_runner_; | |
| 85 | 86 |
| 86 // If there are unfinished calls to GetDeviceChanges their callbacks | 87 // If there are unfinished calls to GetDeviceChanges their callbacks |
| 87 // are stored in |device_change_callbacks_|. Otherwise device changes | 88 // are stored in |device_change_callbacks_|. Otherwise device changes |
| 88 // are collected in |devices_added_| and |devices_removed_| until the | 89 // are collected in |devices_added_| and |devices_removed_| until the |
| 89 // next call to GetDeviceChanges. | 90 // next call to GetDeviceChanges. |
| 90 std::queue<GetDeviceChangesCallback> device_change_callbacks_; | 91 std::queue<GetDeviceChangesCallback> device_change_callbacks_; |
| 91 DeviceMap devices_added_; | 92 DeviceMap devices_added_; |
| 92 DeviceMap devices_removed_; | 93 DeviceMap devices_removed_; |
| 93 // To ensure that GetDeviceChangesCallbacks are called in the correct order | 94 // To ensure that GetDeviceChangesCallbacks are called in the correct order |
| 94 // only perform a single request to |permission_provider_| at a time. | 95 // only perform a single request to |permission_provider_| at a time. |
| 95 bool permission_request_pending_ = false; | 96 bool permission_request_pending_ = false; |
| 96 | 97 |
| 97 // |helper_| is owned by the service thread and holds a weak reference | 98 UsbService* usb_service_; |
| 98 // back to the device manager that created it. | 99 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; |
| 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 |