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

Side by Side Diff: extensions/browser/api/usb_private/usb_private_api.cc

Issue 517923002: Add more generic filters to the chrome.usb.getDevices API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up the USB API documentation. Created 6 years, 3 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 "extensions/browser/api/usb_private/usb_private_api.h" 5 #include "extensions/browser/api/usb_private/usb_private_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/usb_service/usb_device_filter.h" 12 #include "components/usb_service/usb_device_filter.h"
13 #include "components/usb_service/usb_device_handle.h" 13 #include "components/usb_service/usb_device_handle.h"
14 #include "components/usb_service/usb_service.h" 14 #include "components/usb_service/usb_service.h"
15 #include "device/usb/usb_ids.h" 15 #include "device/usb/usb_ids.h"
16 #include "extensions/common/api/usb_private.h" 16 #include "extensions/common/api/usb_private.h"
17 17
18 namespace usb = extensions::core_api::usb;
18 namespace usb_private = extensions::core_api::usb_private; 19 namespace usb_private = extensions::core_api::usb_private;
19 namespace GetDevices = usb_private::GetDevices; 20 namespace GetDevices = usb_private::GetDevices;
20 namespace GetDeviceInfo = usb_private::GetDeviceInfo; 21 namespace GetDeviceInfo = usb_private::GetDeviceInfo;
21 22
22 using usb_service::UsbDevice; 23 using usb_service::UsbDevice;
23 using usb_service::UsbDeviceFilter; 24 using usb_service::UsbDeviceFilter;
24 using usb_service::UsbDeviceHandle; 25 using usb_service::UsbDeviceHandle;
25 using usb_service::UsbService; 26 using usb_service::UsbService;
26 27
28 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector;
29
27 namespace { 30 namespace {
28 31
29 const char kErrorInitService[] = "Failed to initialize USB service."; 32 const char kErrorInitService[] = "Failed to initialize USB service.";
30 const char kErrorNoDevice[] = "No such device."; 33 const char kErrorNoDevice[] = "No such device.";
31 const char kErrorOpen[] = "Failed to open device."; 34 const char kErrorOpen[] = "Failed to open device.";
32 35
33 } // namespace 36 } // namespace
34 37
35 namespace extensions { 38 namespace extensions {
36 39
(...skipping 12 matching lines...) Expand all
49 void UsbPrivateGetDevicesFunction::AsyncWorkStart() { 52 void UsbPrivateGetDevicesFunction::AsyncWorkStart() {
50 UsbService* service = UsbService::GetInstance(); 53 UsbService* service = UsbService::GetInstance();
51 if (!service) { 54 if (!service) {
52 CompleteWithError(kErrorInitService); 55 CompleteWithError(kErrorInitService);
53 return; 56 return;
54 } 57 }
55 58
56 std::vector<UsbDeviceFilter> filters; 59 std::vector<UsbDeviceFilter> filters;
57 filters.resize(parameters_->filters.size()); 60 filters.resize(parameters_->filters.size());
58 for (size_t i = 0; i < parameters_->filters.size(); ++i) { 61 for (size_t i = 0; i < parameters_->filters.size(); ++i) {
59 UsbDeviceFilter& filter = filters[i]; 62 CreateDeviceFilter(*parameters_->filters[i].get(), &filters[i]);
60 const usb_private::DeviceFilter* filter_param =
61 parameters_->filters[i].get();
62
63 if (filter_param->vendor_id) {
64 filter.SetVendorId(*filter_param->vendor_id);
65 }
66 if (filter_param->product_id) {
67 filter.SetProductId(*filter_param->product_id);
68 }
69 if (filter_param->interface_class) {
70 filter.SetInterfaceClass(*filter_param->interface_class);
71 }
72 if (filter_param->interface_subclass) {
73 filter.SetInterfaceSubclass(*filter_param->interface_subclass);
74 }
75 if (filter_param->interface_protocol) {
76 filter.SetInterfaceProtocol(*filter_param->interface_protocol);
77 }
78 } 63 }
79 64
80 std::vector<scoped_refptr<UsbDevice> > devices; 65 DeviceVector devices;
81 service->GetDevices(&devices); 66 service->GetDevices(&devices);
82 67
83 scoped_ptr<base::ListValue> result(new base::ListValue()); 68 scoped_ptr<base::ListValue> result(new base::ListValue());
84 for (size_t i = 0; i < devices.size(); ++i) { 69 for (DeviceVector::iterator it = devices.begin(); it != devices.end(); ++it) {
85 scoped_refptr<UsbDevice> device = devices[i]; 70 scoped_refptr<UsbDevice> device = *it;
86 bool matched = false; 71 if (filters.empty() || UsbDeviceFilter::MatchesAny(device, filters)) {
87
88 if (filters.empty()) {
89 matched = true;
90 } else {
91 for (size_t j = 0; !matched && j < filters.size(); ++j) {
92 if (filters[j].Matches(device)) {
93 matched = true;
94 }
95 }
96 }
97
98 if (matched) {
99 result->Append(new base::FundamentalValue((int)device->unique_id())); 72 result->Append(new base::FundamentalValue((int)device->unique_id()));
100 } 73 }
101 } 74 }
102 75
103 SetResult(result.release()); 76 SetResult(result.release());
104 AsyncWorkCompleted(); 77 AsyncWorkCompleted();
105 } 78 }
106 79
107 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() { 80 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() {
108 } 81 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 136
164 if (device_handle->GetSerial(&utf16)) { 137 if (device_handle->GetSerial(&utf16)) {
165 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16))); 138 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16)));
166 } 139 }
167 140
168 SetResult(device_info.ToValue().release()); 141 SetResult(device_info.ToValue().release());
169 AsyncWorkCompleted(); 142 AsyncWorkCompleted();
170 } 143 }
171 144
172 } // namespace extensions 145 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698