Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / hid / hid_device_manager.h
blob8479901f0f0cc5dd5fc78b1f8f24307c2a95f49b
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.
5 #ifndef EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_
6 #define EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/scoped_observer.h"
15 #include "base/threading/thread_checker.h"
16 #include "device/hid/hid_service.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/browser/extension_event_histogram_value.h"
20 #include "extensions/common/api/hid.h"
22 namespace device {
23 class HidDeviceFilter;
24 class HidDeviceInfo;
27 namespace extensions {
29 class Extension;
31 // This service maps devices enumerated by device::HidService to resource IDs
32 // returned by the chrome.hid API.
33 class HidDeviceManager : public BrowserContextKeyedAPI,
34 public device::HidService::Observer,
35 public EventRouter::Observer {
36 public:
37 typedef base::Callback<void(scoped_ptr<base::ListValue>)>
38 GetApiDevicesCallback;
40 explicit HidDeviceManager(content::BrowserContext* context);
41 ~HidDeviceManager() override;
43 // BrowserContextKeyedAPI implementation.
44 static BrowserContextKeyedAPIFactory<HidDeviceManager>* GetFactoryInstance();
46 // Convenience method to get the HidDeviceManager for a profile.
47 static HidDeviceManager* Get(content::BrowserContext* context) {
48 return BrowserContextKeyedAPIFactory<HidDeviceManager>::Get(context);
51 // Enumerates available devices, taking into account the permissions held by
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
54 // objects.
55 void GetApiDevices(const Extension* extension,
56 const std::vector<device::HidDeviceFilter>& filters,
57 const GetApiDevicesCallback& callback);
59 // Converts a list of HidDeviceInfo objects into a value that can be returned
60 // through the API.
61 scoped_ptr<base::ListValue> GetApiDevicesFromList(
62 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices);
64 scoped_refptr<device::HidDeviceInfo> GetDeviceInfo(int resource_id);
66 // Checks if |extension| has permission to open |device_info|. Set
67 // |update_last_used| to update the timestamp in the DevicePermissionsManager.
68 bool HasPermission(const Extension* extension,
69 scoped_refptr<device::HidDeviceInfo> device_info,
70 bool update_last_used);
72 private:
73 friend class BrowserContextKeyedAPIFactory<HidDeviceManager>;
75 typedef std::map<int, device::HidDeviceId> ResourceIdToDeviceIdMap;
76 typedef std::map<device::HidDeviceId, int> DeviceIdToResourceIdMap;
78 struct GetApiDevicesParams;
80 // KeyedService:
81 void Shutdown() override;
83 // BrowserContextKeyedAPI:
84 static const char* service_name() { return "HidDeviceManager"; }
85 static const bool kServiceHasOwnInstanceInIncognito = true;
86 static const bool kServiceIsNULLWhileTesting = true;
88 // EventRouter::Observer:
89 void OnListenerAdded(const EventListenerInfo& details) override;
91 // HidService::Observer:
92 void OnDeviceAdded(scoped_refptr<device::HidDeviceInfo> device_info) override;
93 void OnDeviceRemoved(
94 scoped_refptr<device::HidDeviceInfo> device_info) override;
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
98 // listener.
99 void LazyInitialize();
101 // Builds a list of device info objects representing the currently enumerated
102 // devices, taking into account the permissions held by the given extension
103 // and the filters provided.
104 scoped_ptr<base::ListValue> CreateApiDeviceList(
105 const Extension* extension,
106 const std::vector<device::HidDeviceFilter>& filters);
107 void OnEnumerationComplete(
108 const std::vector<scoped_refptr<device::HidDeviceInfo>>& devices);
110 void DispatchEvent(events::HistogramValue histogram_value,
111 const std::string& event_name,
112 scoped_ptr<base::ListValue> event_args,
113 scoped_refptr<device::HidDeviceInfo> device_info);
115 base::ThreadChecker thread_checker_;
116 content::BrowserContext* browser_context_ = nullptr;
117 EventRouter* event_router_ = nullptr;
118 bool initialized_ = false;
119 ScopedObserver<device::HidService, device::HidService::Observer>
120 hid_service_observer_;
121 bool enumeration_ready_ = false;
122 ScopedVector<GetApiDevicesParams> pending_enumerations_;
123 int next_resource_id_ = 0;
124 ResourceIdToDeviceIdMap device_ids_;
125 DeviceIdToResourceIdMap resource_ids_;
126 base::WeakPtrFactory<HidDeviceManager> weak_factory_;
128 DISALLOW_COPY_AND_ASSIGN(HidDeviceManager);
131 } // namespace extensions
133 #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_