| OLD | NEW |
| 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 25 matching lines...) Expand all Loading... |
| 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 for (ResourceIdToDeviceIdMap::const_iterator device_iter = | 39 for (ResourceIdToDeviceIdMap::const_iterator device_iter = |
| 40 device_ids_.begin(); | 40 device_ids_.begin(); |
| 41 device_iter != device_ids_.end(); | 41 device_iter != device_ids_.end(); |
| 42 ++device_iter) { | 42 ++device_iter) { |
| 43 int resource_id = device_iter->first; | 43 int resource_id = device_iter->first; |
| 44 device::HidDeviceId device_id = device_iter->second; | 44 device::HidDeviceId device_id = device_iter->second; |
| 45 device::HidDeviceInfo device_info; | 45 device::HidDeviceInfo device_info; |
| 46 |
| 46 if (hid_service->GetDeviceInfo(device_id, &device_info)) { | 47 if (hid_service->GetDeviceInfo(device_id, &device_info)) { |
| 47 if (device_info.vendor_id == vendor_id && | 48 if (device_info.vendor_id == vendor_id && |
| 48 device_info.product_id == product_id && | 49 device_info.product_id == product_id) { |
| 49 IsDeviceAccessible(device_info)) { | |
| 50 api::hid::HidDeviceInfo api_device_info; | 50 api::hid::HidDeviceInfo api_device_info; |
| 51 api_device_info.device_id = resource_id; | 51 api_device_info.device_id = resource_id; |
| 52 api_device_info.vendor_id = device_info.vendor_id; | 52 api_device_info.vendor_id = device_info.vendor_id; |
| 53 api_device_info.product_id = device_info.product_id; | 53 api_device_info.product_id = device_info.product_id; |
| 54 for (std::vector<device::HidUsageAndPage>::const_iterator usage_iter = | 54 api_device_info.max_input_report_size = |
| 55 device_info.usages.begin(); | 55 device_info.max_input_report_size; |
| 56 usage_iter != device_info.usages.end(); | 56 api_device_info.max_output_report_size = |
| 57 ++usage_iter) { | 57 device_info.max_output_report_size; |
| 58 api::hid::HidUsageAndPage* usage_and_page = | 58 api_device_info.max_feature_report_size = |
| 59 new api::hid::HidUsageAndPage(); | 59 device_info.max_feature_report_size; |
| 60 usage_and_page->usage_page = (*usage_iter).usage_page; | 60 |
| 61 usage_and_page->usage = (*usage_iter).usage; | 61 for (std::vector<device::HidCollectionInfo>::const_iterator |
| 62 linked_ptr<api::hid::HidUsageAndPage> usage_and_page_ptr( | 62 collections_iter = device_info.collections.begin(); |
| 63 usage_and_page); | 63 collections_iter != device_info.collections.end(); |
| 64 api_device_info.usages.push_back(usage_and_page_ptr); | 64 ++collections_iter) { |
| 65 device::HidCollectionInfo collection = *collections_iter; |
| 66 |
| 67 // Don't expose sensitive data. |
| 68 if (collection.usage.IsProtected()) { |
| 69 continue; |
| 70 } |
| 71 |
| 72 api::hid::HidCollectionInfo* api_collection = |
| 73 new api::hid::HidCollectionInfo(); |
| 74 api_collection->usage_page = collection.usage.usage_page; |
| 75 api_collection->usage = collection.usage.usage; |
| 76 |
| 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()); |
| 81 |
| 82 api_device_info.collections.push_back( |
| 83 make_linked_ptr(api_collection)); |
| 65 } | 84 } |
| 66 api_devices->Append(api_device_info.ToValue().release()); | 85 |
| 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()); |
| 67 } | 89 } |
| 68 } | 90 } |
| 69 } | 91 } |
| 92 |
| 70 return scoped_ptr<base::ListValue>(api_devices); | 93 return scoped_ptr<base::ListValue>(api_devices); |
| 71 } | 94 } |
| 72 | 95 |
| 73 bool HidDeviceManager::GetDeviceInfo(int resource_id, | 96 bool HidDeviceManager::GetDeviceInfo(int resource_id, |
| 74 device::HidDeviceInfo* device_info) { | 97 device::HidDeviceInfo* device_info) { |
| 75 UpdateDevices(); | 98 UpdateDevices(); |
| 76 HidService* hid_service = HidService::GetInstance(); | 99 HidService* hid_service = HidService::GetInstance(); |
| 77 DCHECK(hid_service); | 100 DCHECK(hid_service); |
| 78 | 101 |
| 79 ResourceIdToDeviceIdMap::const_iterator device_iter = | 102 ResourceIdToDeviceIdMap::const_iterator device_iter = |
| (...skipping 29 matching lines...) Expand all Loading... |
| 109 DCHECK_LT(next_resource_id_, std::numeric_limits<int>::max()); | 132 DCHECK_LT(next_resource_id_, std::numeric_limits<int>::max()); |
| 110 new_id = next_resource_id_++; | 133 new_id = next_resource_id_++; |
| 111 } | 134 } |
| 112 new_resource_ids[device_info.device_id] = new_id; | 135 new_resource_ids[device_info.device_id] = new_id; |
| 113 new_device_ids[new_id] = device_info.device_id; | 136 new_device_ids[new_id] = device_info.device_id; |
| 114 } | 137 } |
| 115 device_ids_.swap(new_device_ids); | 138 device_ids_.swap(new_device_ids); |
| 116 resource_ids_.swap(new_resource_ids); | 139 resource_ids_.swap(new_resource_ids); |
| 117 } | 140 } |
| 118 | 141 |
| 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 | 142 } // namespace extensions |
| OLD | NEW |