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

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: Function comments are descriptive. 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
« no previous file with comments | « extensions/browser/api/usb/usb_api.cc ('k') | extensions/common/api/usb.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/core/device_client.h" 15 #include "device/core/device_client.h"
16 #include "device/usb/usb_ids.h" 16 #include "device/usb/usb_ids.h"
17 #include "extensions/common/api/usb_private.h" 17 #include "extensions/common/api/usb_private.h"
18 18
19 namespace usb = extensions::core_api::usb;
19 namespace usb_private = extensions::core_api::usb_private; 20 namespace usb_private = extensions::core_api::usb_private;
20 namespace GetDevices = usb_private::GetDevices; 21 namespace GetDevices = usb_private::GetDevices;
21 namespace GetDeviceInfo = usb_private::GetDeviceInfo; 22 namespace GetDeviceInfo = usb_private::GetDeviceInfo;
22 23
23 using content::BrowserThread; 24 using content::BrowserThread;
24 using usb_service::UsbDevice; 25 using usb_service::UsbDevice;
25 using usb_service::UsbDeviceFilter; 26 using usb_service::UsbDeviceFilter;
26 using usb_service::UsbDeviceHandle; 27 using usb_service::UsbDeviceHandle;
27 using usb_service::UsbService; 28 using usb_service::UsbService;
28 29
30 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector;
31
29 namespace { 32 namespace {
30 33
31 const char kErrorInitService[] = "Failed to initialize USB service."; 34 const char kErrorInitService[] = "Failed to initialize USB service.";
32 const char kErrorNoDevice[] = "No such device."; 35 const char kErrorNoDevice[] = "No such device.";
33 const char kErrorOpen[] = "Failed to open device."; 36 const char kErrorOpen[] = "Failed to open device.";
34 37
35 } // namespace 38 } // namespace
36 39
37 namespace extensions { 40 namespace extensions {
38 41
(...skipping 12 matching lines...) Expand all
51 void UsbPrivateGetDevicesFunction::AsyncWorkStart() { 54 void UsbPrivateGetDevicesFunction::AsyncWorkStart() {
52 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 55 UsbService* service = device::DeviceClient::Get()->GetUsbService();
53 if (!service) { 56 if (!service) {
54 CompleteWithError(kErrorInitService); 57 CompleteWithError(kErrorInitService);
55 return; 58 return;
56 } 59 }
57 60
58 std::vector<UsbDeviceFilter> filters; 61 std::vector<UsbDeviceFilter> filters;
59 filters.resize(parameters_->filters.size()); 62 filters.resize(parameters_->filters.size());
60 for (size_t i = 0; i < parameters_->filters.size(); ++i) { 63 for (size_t i = 0; i < parameters_->filters.size(); ++i) {
61 UsbDeviceFilter& filter = filters[i]; 64 CreateDeviceFilter(*parameters_->filters[i].get(), &filters[i]);
62 const usb_private::DeviceFilter* filter_param =
63 parameters_->filters[i].get();
64
65 if (filter_param->vendor_id) {
66 filter.SetVendorId(*filter_param->vendor_id);
67 }
68 if (filter_param->product_id) {
69 filter.SetProductId(*filter_param->product_id);
70 }
71 if (filter_param->interface_class) {
72 filter.SetInterfaceClass(*filter_param->interface_class);
73 }
74 if (filter_param->interface_subclass) {
75 filter.SetInterfaceSubclass(*filter_param->interface_subclass);
76 }
77 if (filter_param->interface_protocol) {
78 filter.SetInterfaceProtocol(*filter_param->interface_protocol);
79 }
80 } 65 }
81 66
82 std::vector<scoped_refptr<UsbDevice> > devices; 67 DeviceVector devices;
83 service->GetDevices(&devices); 68 service->GetDevices(&devices);
84 69
85 scoped_ptr<base::ListValue> result(new base::ListValue()); 70 scoped_ptr<base::ListValue> result(new base::ListValue());
86 for (size_t i = 0; i < devices.size(); ++i) { 71 for (DeviceVector::iterator it = devices.begin(); it != devices.end(); ++it) {
87 scoped_refptr<UsbDevice> device = devices[i]; 72 scoped_refptr<UsbDevice> device = *it;
88 bool matched = false; 73 if (filters.empty() || UsbDeviceFilter::MatchesAny(device, filters)) {
89
90 if (filters.empty()) {
91 matched = true;
92 } else {
93 for (size_t j = 0; !matched && j < filters.size(); ++j) {
94 if (filters[j].Matches(device)) {
95 matched = true;
96 }
97 }
98 }
99
100 if (matched) {
101 result->Append(new base::FundamentalValue((int)device->unique_id())); 74 result->Append(new base::FundamentalValue((int)device->unique_id()));
102 } 75 }
103 } 76 }
104 77
105 SetResult(result.release()); 78 SetResult(result.release());
106 AsyncWorkCompleted(); 79 AsyncWorkCompleted();
107 } 80 }
108 81
109 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() { 82 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() {
110 } 83 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 138
166 if (device_handle->GetSerial(&utf16)) { 139 if (device_handle->GetSerial(&utf16)) {
167 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16))); 140 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16)));
168 } 141 }
169 142
170 SetResult(device_info.ToValue().release()); 143 SetResult(device_info.ToValue().release());
171 AsyncWorkCompleted(); 144 AsyncWorkCompleted();
172 } 145 }
173 146
174 } // namespace extensions 147 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.cc ('k') | extensions/common/api/usb.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698