/extensions/browser/api/hid/hid_device_manager.h

https://gitlab.com/jonnialva90/iridium-browser · C Header · 133 lines · 80 code · 28 blank · 25 comment · 0 complexity · 036c7cec85b9e45aa0ac3e339fa2c2de MD5 · raw file

  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
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_
  5. #define EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_
  6. #include <map>
  7. #include "base/basictypes.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/scoped_ptr.h"
  10. #include "base/memory/scoped_vector.h"
  11. #include "base/scoped_observer.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "device/hid/hid_service.h"
  14. #include "extensions/browser/browser_context_keyed_api_factory.h"
  15. #include "extensions/browser/event_router.h"
  16. #include "extensions/browser/extension_event_histogram_value.h"
  17. #include "extensions/common/api/hid.h"
  18. namespace device {
  19. class HidDeviceFilter;
  20. class HidDeviceInfo;
  21. }
  22. namespace extensions {
  23. class Extension;
  24. // This service maps devices enumerated by device::HidService to resource IDs
  25. // returned by the chrome.hid API.
  26. class HidDeviceManager : public BrowserContextKeyedAPI,
  27. public device::HidService::Observer,
  28. public EventRouter::Observer {
  29. public:
  30. typedef base::Callback<void(scoped_ptr<base::ListValue>)>
  31. GetApiDevicesCallback;
  32. explicit HidDeviceManager(content::BrowserContext* context);
  33. ~HidDeviceManager() override;
  34. // BrowserContextKeyedAPI implementation.
  35. static BrowserContextKeyedAPIFactory<HidDeviceManager>* GetFactoryInstance();
  36. // Convenience method to get the HidDeviceManager for a profile.
  37. static HidDeviceManager* Get(content::BrowserContext* context) {
  38. return BrowserContextKeyedAPIFactory<HidDeviceManager>::Get(context);
  39. }
  40. // Enumerates available devices, taking into account the permissions held by
  41. // the given extension and the filters provided. The provided callback will
  42. // be posted to the calling thread's task runner with a list of device info
  43. // objects.
  44. void GetApiDevices(const Extension* extension,
  45. const std::vector<device::HidDeviceFilter>& filters,
  46. const GetApiDevicesCallback& callback);
  47. // Converts a list of HidDeviceInfo objects into a value that can be returned
  48. // through the API.
  49. scoped_ptr<base::ListValue> GetApiDevicesFromList(
  50. const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices);
  51. scoped_refptr<device::HidDeviceInfo> GetDeviceInfo(int resource_id);
  52. // Checks if |extension| has permission to open |device_info|. Set
  53. // |update_last_used| to update the timestamp in the DevicePermissionsManager.
  54. bool HasPermission(const Extension* extension,
  55. scoped_refptr<device::HidDeviceInfo> device_info,
  56. bool update_last_used);
  57. private:
  58. friend class BrowserContextKeyedAPIFactory<HidDeviceManager>;
  59. typedef std::map<int, device::HidDeviceId> ResourceIdToDeviceIdMap;
  60. typedef std::map<device::HidDeviceId, int> DeviceIdToResourceIdMap;
  61. struct GetApiDevicesParams;
  62. // KeyedService:
  63. void Shutdown() override;
  64. // BrowserContextKeyedAPI:
  65. static const char* service_name() { return "HidDeviceManager"; }
  66. static const bool kServiceHasOwnInstanceInIncognito = true;
  67. static const bool kServiceIsNULLWhileTesting = true;
  68. // EventRouter::Observer:
  69. void OnListenerAdded(const EventListenerInfo& details) override;
  70. // HidService::Observer:
  71. void OnDeviceAdded(scoped_refptr<device::HidDeviceInfo> device_info) override;
  72. void OnDeviceRemoved(
  73. scoped_refptr<device::HidDeviceInfo> device_info) override;
  74. // Wait to perform an initial enumeration and register a HidService::Observer
  75. // until the first API customer makes a request or registers an event
  76. // listener.
  77. void LazyInitialize();
  78. // Builds a list of device info objects representing the currently enumerated
  79. // devices, taking into account the permissions held by the given extension
  80. // and the filters provided.
  81. scoped_ptr<base::ListValue> CreateApiDeviceList(
  82. const Extension* extension,
  83. const std::vector<device::HidDeviceFilter>& filters);
  84. void OnEnumerationComplete(
  85. const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices);
  86. void DispatchEvent(events::HistogramValue histogram_value,
  87. const std::string& event_name,
  88. scoped_ptr<base::ListValue> event_args,
  89. scoped_refptr<device::HidDeviceInfo> device_info);
  90. base::ThreadChecker thread_checker_;
  91. content::BrowserContext* browser_context_ = nullptr;
  92. EventRouter* event_router_ = nullptr;
  93. bool initialized_ = false;
  94. ScopedObserver<device::HidService, device::HidService::Observer>
  95. hid_service_observer_;
  96. bool enumeration_ready_ = false;
  97. ScopedVector<GetApiDevicesParams> pending_enumerations_;
  98. int next_resource_id_ = 0;
  99. ResourceIdToDeviceIdMap device_ids_;
  100. DeviceIdToResourceIdMap resource_ids_;
  101. base::WeakPtrFactory<HidDeviceManager> weak_factory_;
  102. DISALLOW_COPY_AND_ASSIGN(HidDeviceManager);
  103. };
  104. } // namespace extensions
  105. #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_