| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ |
| 6 #define EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ | 6 #define EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "device/hid/hid_service.h" | 16 #include "device/hid/hid_service.h" |
| 17 #include "extensions/browser/browser_context_keyed_api_factory.h" | 17 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 18 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/browser/extension_event_histogram_value.h" | 19 #include "extensions/browser/extension_event_histogram_value.h" |
| 20 #include "extensions/common/api/hid.h" | 20 #include "extensions/common/api/hid.h" |
| 21 | 21 |
| 22 namespace device { | 22 namespace device { |
| 23 class HidDeviceFilter; | 23 class HidDeviceFilter; |
| 24 class HidDeviceInfo; | 24 class HidDeviceInfo; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 | 28 |
| 29 class Extension; | 29 class Extension; |
| 30 | 30 |
| 31 // This service maps devices enumerated by device::HidService to resource IDs | 31 // This service maps devices enumerated by device::HidService to resource IDs |
| 32 // returned by the chrome.hid API. | 32 // returned by the chrome.hid API. |
| 33 class HidDeviceManager : public BrowserContextKeyedAPI, | 33 class HidDeviceManager : public BrowserContextKeyedAPI, |
| 34 public device::HidService::Observer, | 34 public device::HidService::Observer, |
| 35 public EventRouter::Observer { | 35 public EventRouter::Observer { |
| 36 public: | 36 public: |
| 37 typedef base::Callback<void(scoped_ptr<base::ListValue>)> | 37 typedef base::Callback<void(std::unique_ptr<base::ListValue>)> |
| 38 GetApiDevicesCallback; | 38 GetApiDevicesCallback; |
| 39 | 39 |
| 40 explicit HidDeviceManager(content::BrowserContext* context); | 40 explicit HidDeviceManager(content::BrowserContext* context); |
| 41 ~HidDeviceManager() override; | 41 ~HidDeviceManager() override; |
| 42 | 42 |
| 43 // BrowserContextKeyedAPI implementation. | 43 // BrowserContextKeyedAPI implementation. |
| 44 static BrowserContextKeyedAPIFactory<HidDeviceManager>* GetFactoryInstance(); | 44 static BrowserContextKeyedAPIFactory<HidDeviceManager>* GetFactoryInstance(); |
| 45 | 45 |
| 46 // Convenience method to get the HidDeviceManager for a profile. | 46 // Convenience method to get the HidDeviceManager for a profile. |
| 47 static HidDeviceManager* Get(content::BrowserContext* context) { | 47 static HidDeviceManager* Get(content::BrowserContext* context) { |
| 48 return BrowserContextKeyedAPIFactory<HidDeviceManager>::Get(context); | 48 return BrowserContextKeyedAPIFactory<HidDeviceManager>::Get(context); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Enumerates available devices, taking into account the permissions held by | 51 // Enumerates available devices, taking into account the permissions held by |
| 52 // the given extension and the filters provided. The provided callback will | 52 // the given extension and the filters provided. The provided callback will |
| 53 // be posted to the calling thread's task runner with a list of device info | 53 // be posted to the calling thread's task runner with a list of device info |
| 54 // objects. | 54 // objects. |
| 55 void GetApiDevices(const Extension* extension, | 55 void GetApiDevices(const Extension* extension, |
| 56 const std::vector<device::HidDeviceFilter>& filters, | 56 const std::vector<device::HidDeviceFilter>& filters, |
| 57 const GetApiDevicesCallback& callback); | 57 const GetApiDevicesCallback& callback); |
| 58 | 58 |
| 59 // Converts a list of HidDeviceInfo objects into a value that can be returned | 59 // Converts a list of HidDeviceInfo objects into a value that can be returned |
| 60 // through the API. | 60 // through the API. |
| 61 scoped_ptr<base::ListValue> GetApiDevicesFromList( | 61 std::unique_ptr<base::ListValue> GetApiDevicesFromList( |
| 62 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices); | 62 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices); |
| 63 | 63 |
| 64 scoped_refptr<device::HidDeviceInfo> GetDeviceInfo(int resource_id); | 64 scoped_refptr<device::HidDeviceInfo> GetDeviceInfo(int resource_id); |
| 65 | 65 |
| 66 // Checks if |extension| has permission to open |device_info|. Set | 66 // Checks if |extension| has permission to open |device_info|. Set |
| 67 // |update_last_used| to update the timestamp in the DevicePermissionsManager. | 67 // |update_last_used| to update the timestamp in the DevicePermissionsManager. |
| 68 bool HasPermission(const Extension* extension, | 68 bool HasPermission(const Extension* extension, |
| 69 scoped_refptr<device::HidDeviceInfo> device_info, | 69 scoped_refptr<device::HidDeviceInfo> device_info, |
| 70 bool update_last_used); | 70 bool update_last_used); |
| 71 | 71 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 94 scoped_refptr<device::HidDeviceInfo> device_info) override; | 94 scoped_refptr<device::HidDeviceInfo> device_info) override; |
| 95 | 95 |
| 96 // Wait to perform an initial enumeration and register a HidService::Observer | 96 // Wait to perform an initial enumeration and register a HidService::Observer |
| 97 // until the first API customer makes a request or registers an event | 97 // until the first API customer makes a request or registers an event |
| 98 // listener. | 98 // listener. |
| 99 void LazyInitialize(); | 99 void LazyInitialize(); |
| 100 | 100 |
| 101 // Builds a list of device info objects representing the currently enumerated | 101 // Builds a list of device info objects representing the currently enumerated |
| 102 // devices, taking into account the permissions held by the given extension | 102 // devices, taking into account the permissions held by the given extension |
| 103 // and the filters provided. | 103 // and the filters provided. |
| 104 scoped_ptr<base::ListValue> CreateApiDeviceList( | 104 std::unique_ptr<base::ListValue> CreateApiDeviceList( |
| 105 const Extension* extension, | 105 const Extension* extension, |
| 106 const std::vector<device::HidDeviceFilter>& filters); | 106 const std::vector<device::HidDeviceFilter>& filters); |
| 107 void OnEnumerationComplete( | 107 void OnEnumerationComplete( |
| 108 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices); | 108 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices); |
| 109 | 109 |
| 110 void DispatchEvent(events::HistogramValue histogram_value, | 110 void DispatchEvent(events::HistogramValue histogram_value, |
| 111 const std::string& event_name, | 111 const std::string& event_name, |
| 112 scoped_ptr<base::ListValue> event_args, | 112 std::unique_ptr<base::ListValue> event_args, |
| 113 scoped_refptr<device::HidDeviceInfo> device_info); | 113 scoped_refptr<device::HidDeviceInfo> device_info); |
| 114 | 114 |
| 115 base::ThreadChecker thread_checker_; | 115 base::ThreadChecker thread_checker_; |
| 116 content::BrowserContext* browser_context_ = nullptr; | 116 content::BrowserContext* browser_context_ = nullptr; |
| 117 EventRouter* event_router_ = nullptr; | 117 EventRouter* event_router_ = nullptr; |
| 118 bool initialized_ = false; | 118 bool initialized_ = false; |
| 119 ScopedObserver<device::HidService, device::HidService::Observer> | 119 ScopedObserver<device::HidService, device::HidService::Observer> |
| 120 hid_service_observer_; | 120 hid_service_observer_; |
| 121 bool enumeration_ready_ = false; | 121 bool enumeration_ready_ = false; |
| 122 std::vector<scoped_ptr<GetApiDevicesParams>> pending_enumerations_; | 122 std::vector<std::unique_ptr<GetApiDevicesParams>> pending_enumerations_; |
| 123 int next_resource_id_ = 0; | 123 int next_resource_id_ = 0; |
| 124 ResourceIdToDeviceIdMap device_ids_; | 124 ResourceIdToDeviceIdMap device_ids_; |
| 125 DeviceIdToResourceIdMap resource_ids_; | 125 DeviceIdToResourceIdMap resource_ids_; |
| 126 base::WeakPtrFactory<HidDeviceManager> weak_factory_; | 126 base::WeakPtrFactory<HidDeviceManager> weak_factory_; |
| 127 | 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(HidDeviceManager); | 128 DISALLOW_COPY_AND_ASSIGN(HidDeviceManager); |
| 129 }; | 129 }; |
| 130 | 130 |
| 131 } // namespace extensions | 131 } // namespace extensions |
| 132 | 132 |
| 133 #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ | 133 #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_ |
| OLD | NEW |