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 #include "extensions/browser/api/hid/hid_device_manager.h"
10 #include "base/lazy_instance.h"
11 #include "device/hid/hid_service.h"
13 using device::HidService
;
14 using device::HidUsageAndPage
;
16 namespace extensions
{
18 HidDeviceManager::HidDeviceManager(content::BrowserContext
* context
)
19 : next_resource_id_(0) {}
21 HidDeviceManager::~HidDeviceManager() {}
24 BrowserContextKeyedAPIFactory
<HidDeviceManager
>*
25 HidDeviceManager::GetFactoryInstance() {
26 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<HidDeviceManager
> >
27 factory
= LAZY_INSTANCE_INITIALIZER
;
28 return &factory
.Get();
31 scoped_ptr
<base::ListValue
> HidDeviceManager::GetApiDevices(
33 uint16_t product_id
) {
36 HidService
* hid_service
= HidService::GetInstance();
38 base::ListValue
* api_devices
= new base::ListValue();
39 for (ResourceIdToDeviceIdMap::const_iterator device_iter
=
41 device_iter
!= device_ids_
.end();
43 int resource_id
= device_iter
->first
;
44 device::HidDeviceId device_id
= device_iter
->second
;
45 device::HidDeviceInfo device_info
;
47 if (hid_service
->GetDeviceInfo(device_id
, &device_info
)) {
48 if (device_info
.vendor_id
== vendor_id
&&
49 device_info
.product_id
== product_id
) {
50 core_api::hid::HidDeviceInfo api_device_info
;
51 api_device_info
.device_id
= resource_id
;
52 api_device_info
.vendor_id
= device_info
.vendor_id
;
53 api_device_info
.product_id
= device_info
.product_id
;
54 api_device_info
.max_input_report_size
=
55 device_info
.max_input_report_size
;
56 api_device_info
.max_output_report_size
=
57 device_info
.max_output_report_size
;
58 api_device_info
.max_feature_report_size
=
59 device_info
.max_feature_report_size
;
61 for (std::vector
<device::HidCollectionInfo
>::const_iterator
62 collections_iter
= device_info
.collections
.begin();
63 collections_iter
!= device_info
.collections
.end();
65 device::HidCollectionInfo collection
= *collections_iter
;
67 // Don't expose sensitive data.
68 if (collection
.usage
.IsProtected()) {
72 core_api::hid::HidCollectionInfo
* api_collection
=
73 new core_api::hid::HidCollectionInfo();
74 api_collection
->usage_page
= collection
.usage
.usage_page
;
75 api_collection
->usage
= collection
.usage
.usage
;
77 api_collection
->report_ids
.resize(collection
.report_ids
.size());
78 std::copy(collection
.report_ids
.begin(),
79 collection
.report_ids
.end(),
80 api_collection
->report_ids
.begin());
82 api_device_info
.collections
.push_back(
83 make_linked_ptr(api_collection
));
86 // Expose devices with which user can communicate.
87 if (api_device_info
.collections
.size() > 0)
88 api_devices
->Append(api_device_info
.ToValue().release());
93 return scoped_ptr
<base::ListValue
>(api_devices
);
96 bool HidDeviceManager::GetDeviceInfo(int resource_id
,
97 device::HidDeviceInfo
* device_info
) {
99 HidService
* hid_service
= HidService::GetInstance();
102 ResourceIdToDeviceIdMap::const_iterator device_iter
=
103 device_ids_
.find(resource_id
);
104 if (device_iter
== device_ids_
.end())
107 return hid_service
->GetDeviceInfo(device_iter
->second
, device_info
);
110 void HidDeviceManager::UpdateDevices() {
111 thread_checker_
.CalledOnValidThread();
112 HidService
* hid_service
= HidService::GetInstance();
115 std::vector
<device::HidDeviceInfo
> devices
;
116 hid_service
->GetDevices(&devices
);
118 // Build an updated bidi mapping between resource ID and underlying device ID.
119 DeviceIdToResourceIdMap new_resource_ids
;
120 ResourceIdToDeviceIdMap new_device_ids
;
121 for (std::vector
<device::HidDeviceInfo
>::const_iterator iter
=
123 iter
!= devices
.end();
125 const device::HidDeviceInfo
& device_info
= *iter
;
126 DeviceIdToResourceIdMap::iterator resource_iter
=
127 resource_ids_
.find(device_info
.device_id
);
129 if (resource_iter
!= resource_ids_
.end()) {
130 new_id
= resource_iter
->second
;
132 DCHECK_LT(next_resource_id_
, std::numeric_limits
<int>::max());
133 new_id
= next_resource_id_
++;
135 new_resource_ids
[device_info
.device_id
] = new_id
;
136 new_device_ids
[new_id
] = device_info
.device_id
;
138 device_ids_
.swap(new_device_ids
);
139 resource_ids_
.swap(new_resource_ids
);
142 } // namespace extensions