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_
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_device_info.h"
17 #include "device/hid/hid_service.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/common/api/hid.h"
23 class HidDeviceFilter
;
26 namespace extensions
{
30 // This service maps devices enumerated by device::HidService to resource IDs
31 // returned by the chrome.hid API.
32 class HidDeviceManager
: public BrowserContextKeyedAPI
,
33 public device::HidService::Observer
,
34 public EventRouter::Observer
{
36 typedef base::Callback
<void(scoped_ptr
<base::ListValue
>)>
37 GetApiDevicesCallback
;
39 explicit HidDeviceManager(content::BrowserContext
* context
);
40 ~HidDeviceManager() override
;
42 // BrowserContextKeyedAPI implementation.
43 static BrowserContextKeyedAPIFactory
<HidDeviceManager
>* GetFactoryInstance();
45 // Convenience method to get the HidDeviceManager for a profile.
46 static HidDeviceManager
* Get(content::BrowserContext
* context
) {
47 return BrowserContextKeyedAPIFactory
<HidDeviceManager
>::Get(context
);
50 // Enumerates available devices, taking into account the permissions held by
51 // the given extension and the filters provided. The provided callback will
52 // be posted to the calling thread's task runner with a list of device info
54 void GetApiDevices(const Extension
* extension
,
55 const std::vector
<device::HidDeviceFilter
>& filters
,
56 const GetApiDevicesCallback
& callback
);
58 scoped_refptr
<device::HidDeviceInfo
> GetDeviceInfo(int resource_id
);
60 static bool HasPermission(const Extension
* extension
,
61 scoped_refptr
<device::HidDeviceInfo
> device_info
);
64 friend class BrowserContextKeyedAPIFactory
<HidDeviceManager
>;
66 typedef std::map
<int, device::HidDeviceId
> ResourceIdToDeviceIdMap
;
67 typedef std::map
<device::HidDeviceId
, int> DeviceIdToResourceIdMap
;
69 struct GetApiDevicesParams
;
72 void Shutdown() override
;
74 // BrowserContextKeyedAPI:
75 static const char* service_name() { return "HidDeviceManager"; }
76 static const bool kServiceHasOwnInstanceInIncognito
= true;
77 static const bool kServiceIsNULLWhileTesting
= true;
79 // EventRouter::Observer:
80 void OnListenerAdded(const EventListenerInfo
& details
) override
;
82 // HidService::Observer:
83 void OnDeviceAdded(scoped_refptr
<device::HidDeviceInfo
> device_info
) override
;
85 scoped_refptr
<device::HidDeviceInfo
> device_info
) override
;
87 // Wait to perform an initial enumeration and register a HidService::Observer
88 // until the first API customer makes a request or registers an event
90 void LazyInitialize();
92 // Builds a list of device info objects representing the currently enumerated
93 // devices, taking into account the permissions held by the given extension
94 // and the filters provided.
95 scoped_ptr
<base::ListValue
> CreateApiDeviceList(
96 const Extension
* extension
,
97 const std::vector
<device::HidDeviceFilter
>& filters
);
98 void OnEnumerationComplete(
99 const std::vector
<scoped_refptr
<device::HidDeviceInfo
>>& devices
);
101 void DispatchEvent(const std::string
& event_name
,
102 scoped_ptr
<base::ListValue
> event_args
,
103 scoped_refptr
<device::HidDeviceInfo
> device_info
);
105 base::ThreadChecker thread_checker_
;
106 EventRouter
* event_router_
;
108 ScopedObserver
<device::HidService
, device::HidService::Observer
>
109 hid_service_observer_
;
110 bool enumeration_ready_
;
111 ScopedVector
<GetApiDevicesParams
> pending_enumerations_
;
112 int next_resource_id_
;
113 ResourceIdToDeviceIdMap device_ids_
;
114 DeviceIdToResourceIdMap resource_ids_
;
115 base::WeakPtrFactory
<HidDeviceManager
> weak_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(HidDeviceManager
);
120 } // namespace extensions
122 #endif // EXTENSIONS_BROWSER_API_HID_HID_DEVICE_MANAGER_H_