Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: chrome/browser/extensions/api/hid/hid_device_manager.cc

Issue 317783010: chrome.hid: enrich model with report IDs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Enrich JavaScript model (no incoming report filter) Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/hid/hid_device_manager.h" 5 #include "chrome/browser/extensions/api/hid/hid_device_manager.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 18 matching lines...) Expand all
29 } 29 }
30 30
31 scoped_ptr<base::ListValue> HidDeviceManager::GetApiDevices( 31 scoped_ptr<base::ListValue> HidDeviceManager::GetApiDevices(
32 uint16_t vendor_id, 32 uint16_t vendor_id,
33 uint16_t product_id) { 33 uint16_t product_id) {
34 UpdateDevices(); 34 UpdateDevices();
35 35
36 HidService* hid_service = HidService::GetInstance(); 36 HidService* hid_service = HidService::GetInstance();
37 DCHECK(hid_service); 37 DCHECK(hid_service);
38 base::ListValue* api_devices = new base::ListValue(); 38 base::ListValue* api_devices = new base::ListValue();
39
Ken Rockot(use gerrit already) 2014/06/06 20:04:43 nit: No need to add vertical whitespace
jracle (use Gerrit) 2014/06/07 12:56:21 OK. Though I'm a bit puzzled by this rule, for me
39 for (ResourceIdToDeviceIdMap::const_iterator device_iter = 40 for (ResourceIdToDeviceIdMap::const_iterator device_iter =
40 device_ids_.begin(); 41 device_ids_.begin();
41 device_iter != device_ids_.end(); 42 device_iter != device_ids_.end();
42 ++device_iter) { 43 ++device_iter) {
43 int resource_id = device_iter->first; 44 int resource_id = device_iter->first;
44 device::HidDeviceId device_id = device_iter->second; 45 device::HidDeviceId device_id = device_iter->second;
45 device::HidDeviceInfo device_info; 46 device::HidDeviceInfo device_info;
47
46 if (hid_service->GetDeviceInfo(device_id, &device_info)) { 48 if (hid_service->GetDeviceInfo(device_id, &device_info)) {
47 if (device_info.vendor_id == vendor_id && 49 if (device_info.vendor_id == vendor_id &&
48 device_info.product_id == product_id && 50 device_info.product_id == product_id) {
49 IsDeviceAccessible(device_info)) {
50 api::hid::HidDeviceInfo api_device_info; 51 api::hid::HidDeviceInfo api_device_info;
51 api_device_info.device_id = resource_id; 52 api_device_info.device_id = resource_id;
52 api_device_info.vendor_id = device_info.vendor_id; 53 api_device_info.vendor_id = device_info.vendor_id;
53 api_device_info.product_id = device_info.product_id; 54 api_device_info.product_id = device_info.product_id;
54 for (std::vector<device::HidUsageAndPage>::const_iterator usage_iter = 55 api_device_info.max_input_report_size =
55 device_info.usages.begin(); 56 device_info.max_input_report_size;
56 usage_iter != device_info.usages.end(); 57 api_device_info.max_output_report_size =
57 ++usage_iter) { 58 device_info.max_output_report_size;
58 api::hid::HidUsageAndPage* usage_and_page = 59 api_device_info.max_feature_report_size =
59 new api::hid::HidUsageAndPage(); 60 device_info.max_feature_report_size;
60 usage_and_page->usage_page = (*usage_iter).usage_page; 61
61 usage_and_page->usage = (*usage_iter).usage; 62 for (std::vector<device::HidCollectionInfo>::const_iterator
62 linked_ptr<api::hid::HidUsageAndPage> usage_and_page_ptr( 63 collections_iter = device_info.collections.begin();
63 usage_and_page); 64 collections_iter != device_info.collections.end();
64 api_device_info.usages.push_back(usage_and_page_ptr); 65 ++collections_iter) {
66 device::HidCollectionInfo collection = *collections_iter;
67
68 // Don't expose sensitive data.
69 if (collection.usage.IsSensitive())
Ken Rockot(use gerrit already) 2014/06/06 20:04:43 nit: Please include braces around single-line bloc
jracle (use Gerrit) 2014/06/07 12:56:21 This I sign 100%, that's safest.. I recall Apple's
70 continue;
71
72 api::hid::HidCollectionInfo* api_collection =
73 new api::hid::HidCollectionInfo();
74 api_collection->usage.usage_page = collection.usage.usage_page;
75 api_collection->usage.usage = collection.usage.usage;
76
77 for (std::set<int>::const_iterator report_ids_iter =
Ken Rockot(use gerrit already) 2014/06/06 20:04:43 I think you could just: api_collection->report_id
jracle (use Gerrit) 2014/06/07 12:56:21 Oh yes! On 2014/06/06 20:04:43, Ken Rockot wrote:
78 collection.report_ids.begin();
79 report_ids_iter != collection.report_ids.end();
80 ++report_ids_iter) {
81 api_collection->report_ids.push_back(*report_ids_iter);
82 }
83 linked_ptr<api::hid::HidCollectionInfo> collection_ptr(
84 api_collection);
85 api_device_info.collections.push_back(collection_ptr);
Ken Rockot(use gerrit already) 2014/06/06 20:04:43 Instead of naming the linked_ptr, just do api_dev
jracle (use Gerrit) 2014/06/07 12:56:21 gr8, sure. Much better. On 2014/06/06 20:04:43, K
65 } 86 }
66 api_devices->Append(api_device_info.ToValue().release()); 87
88 // Expose devices with which user can communicate.
89 if (api_device_info.collections.size() > 0)
90 api_devices->Append(api_device_info.ToValue().release());
67 } 91 }
68 } 92 }
69 } 93 }
94
70 return scoped_ptr<base::ListValue>(api_devices); 95 return scoped_ptr<base::ListValue>(api_devices);
71 } 96 }
72 97
73 bool HidDeviceManager::GetDeviceInfo(int resource_id, 98 bool HidDeviceManager::GetDeviceInfo(int resource_id,
74 device::HidDeviceInfo* device_info) { 99 device::HidDeviceInfo* device_info) {
75 UpdateDevices(); 100 UpdateDevices();
76 HidService* hid_service = HidService::GetInstance(); 101 HidService* hid_service = HidService::GetInstance();
77 DCHECK(hid_service); 102 DCHECK(hid_service);
78 103
79 ResourceIdToDeviceIdMap::const_iterator device_iter = 104 ResourceIdToDeviceIdMap::const_iterator device_iter =
(...skipping 29 matching lines...) Expand all
109 DCHECK_LT(next_resource_id_, std::numeric_limits<int>::max()); 134 DCHECK_LT(next_resource_id_, std::numeric_limits<int>::max());
110 new_id = next_resource_id_++; 135 new_id = next_resource_id_++;
111 } 136 }
112 new_resource_ids[device_info.device_id] = new_id; 137 new_resource_ids[device_info.device_id] = new_id;
113 new_device_ids[new_id] = device_info.device_id; 138 new_device_ids[new_id] = device_info.device_id;
114 } 139 }
115 device_ids_.swap(new_device_ids); 140 device_ids_.swap(new_device_ids);
116 resource_ids_.swap(new_resource_ids); 141 resource_ids_.swap(new_resource_ids);
117 } 142 }
118 143
119 // static
120 // TODO(rockot): Add some tests for this.
121 bool HidDeviceManager::IsDeviceAccessible(
122 const device::HidDeviceInfo& device_info) {
123 for (std::vector<device::HidUsageAndPage>::const_iterator iter =
124 device_info.usages.begin();
125 iter != device_info.usages.end(); ++iter) {
126 if (!IsUsageAccessible(*iter)) {
127 return false;
128 }
129 }
130 return true;
131 }
132
133 // static
134 bool HidDeviceManager::IsUsageAccessible(
135 const HidUsageAndPage& usage_and_page) {
136 if (usage_and_page.usage_page == HidUsageAndPage::kPageKeyboard)
137 return false;
138
139 if (usage_and_page.usage_page != HidUsageAndPage::kPageGenericDesktop)
140 return true;
141
142 uint16_t usage = usage_and_page.usage;
143 if (usage == HidUsageAndPage::kGenericDesktopPointer ||
144 usage == HidUsageAndPage::kGenericDesktopMouse ||
145 usage == HidUsageAndPage::kGenericDesktopKeyboard ||
146 usage == HidUsageAndPage::kGenericDesktopKeypad) {
147 return false;
148 }
149
150 if (usage >= HidUsageAndPage::kGenericDesktopSystemControl &&
151 usage <= HidUsageAndPage::kGenericDesktopSystemWarmRestart) {
152 return false;
153 }
154
155 if (usage >= HidUsageAndPage::kGenericDesktopSystemDock &&
156 usage <= HidUsageAndPage::kGenericDesktopSystemDisplaySwap) {
157 return false;
158 }
159
160 return true;
161 }
162
163 } // namespace extensions 144 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698