Extensions: Move bluetooth APIs to extensions/.
[chromium-blink-merge.git] / extensions / browser / api / hid / hid_device_manager.cc
blobc14783c790c39b15eafee8dd51a6f7bc0f7744ce
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"
7 #include <limits>
8 #include <vector>
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() {}
23 // static
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(
32 uint16_t vendor_id,
33 uint16_t product_id) {
34 UpdateDevices();
36 HidService* hid_service = HidService::GetInstance();
37 DCHECK(hid_service);
38 base::ListValue* api_devices = new base::ListValue();
39 for (ResourceIdToDeviceIdMap::const_iterator device_iter =
40 device_ids_.begin();
41 device_iter != device_ids_.end();
42 ++device_iter) {
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();
64 ++collections_iter) {
65 device::HidCollectionInfo collection = *collections_iter;
67 // Don't expose sensitive data.
68 if (collection.usage.IsProtected()) {
69 continue;
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) {
98 UpdateDevices();
99 HidService* hid_service = HidService::GetInstance();
100 DCHECK(hid_service);
102 ResourceIdToDeviceIdMap::const_iterator device_iter =
103 device_ids_.find(resource_id);
104 if (device_iter == device_ids_.end())
105 return false;
107 return hid_service->GetDeviceInfo(device_iter->second, device_info);
110 void HidDeviceManager::UpdateDevices() {
111 thread_checker_.CalledOnValidThread();
112 HidService* hid_service = HidService::GetInstance();
113 DCHECK(hid_service);
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 =
122 devices.begin();
123 iter != devices.end();
124 ++iter) {
125 const device::HidDeviceInfo& device_info = *iter;
126 DeviceIdToResourceIdMap::iterator resource_iter =
127 resource_ids_.find(device_info.device_id);
128 int new_id;
129 if (resource_iter != resource_ids_.end()) {
130 new_id = resource_iter->second;
131 } else {
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