Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / api / usb_private / usb_private_api.cc
blob860a13a513f5f47e480bed9ce01587a88ac5c7e1
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/usb_private/usb_private_api.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/ref_counted.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/usb_service/usb_device_filter.h"
13 #include "components/usb_service/usb_device_handle.h"
14 #include "components/usb_service/usb_service.h"
15 #include "device/usb/usb_ids.h"
16 #include "extensions/common/api/usb_private.h"
18 namespace usb_private = extensions::core_api::usb_private;
19 namespace GetDevices = usb_private::GetDevices;
20 namespace GetDeviceInfo = usb_private::GetDeviceInfo;
22 using usb_service::UsbDevice;
23 using usb_service::UsbDeviceFilter;
24 using usb_service::UsbDeviceHandle;
25 using usb_service::UsbService;
27 namespace {
29 const char kErrorInitService[] = "Failed to initialize USB service.";
30 const char kErrorNoDevice[] = "No such device.";
31 const char kErrorOpen[] = "Failed to open device.";
33 } // namespace
35 namespace extensions {
37 UsbPrivateGetDevicesFunction::UsbPrivateGetDevicesFunction() {
40 UsbPrivateGetDevicesFunction::~UsbPrivateGetDevicesFunction() {
43 bool UsbPrivateGetDevicesFunction::Prepare() {
44 parameters_ = GetDevices::Params::Create(*args_);
45 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
46 return true;
49 void UsbPrivateGetDevicesFunction::AsyncWorkStart() {
50 UsbService* service = UsbService::GetInstance();
51 if (!service) {
52 CompleteWithError(kErrorInitService);
53 return;
56 std::vector<UsbDeviceFilter> filters;
57 filters.resize(parameters_->filters.size());
58 for (size_t i = 0; i < parameters_->filters.size(); ++i) {
59 UsbDeviceFilter& filter = filters[i];
60 const usb_private::DeviceFilter* filter_param =
61 parameters_->filters[i].get();
63 if (filter_param->vendor_id) {
64 filter.SetVendorId(*filter_param->vendor_id);
66 if (filter_param->product_id) {
67 filter.SetProductId(*filter_param->product_id);
69 if (filter_param->interface_class) {
70 filter.SetInterfaceClass(*filter_param->interface_class);
72 if (filter_param->interface_subclass) {
73 filter.SetInterfaceSubclass(*filter_param->interface_subclass);
75 if (filter_param->interface_protocol) {
76 filter.SetInterfaceProtocol(*filter_param->interface_protocol);
80 std::vector<scoped_refptr<UsbDevice> > devices;
81 service->GetDevices(&devices);
83 scoped_ptr<base::ListValue> result(new base::ListValue());
84 for (size_t i = 0; i < devices.size(); ++i) {
85 scoped_refptr<UsbDevice> device = devices[i];
86 bool matched = false;
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;
98 if (matched) {
99 result->Append(new base::FundamentalValue((int)device->unique_id()));
103 SetResult(result.release());
104 AsyncWorkCompleted();
107 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() {
110 UsbPrivateGetDeviceInfoFunction::~UsbPrivateGetDeviceInfoFunction() {
113 bool UsbPrivateGetDeviceInfoFunction::Prepare() {
114 parameters_ = GetDeviceInfo::Params::Create(*args_);
115 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
116 return true;
119 void UsbPrivateGetDeviceInfoFunction::AsyncWorkStart() {
120 UsbService* service = UsbService::GetInstance();
121 if (!service) {
122 CompleteWithError(kErrorInitService);
123 return;
126 scoped_refptr<UsbDevice> device =
127 service->GetDeviceById(parameters_->device_id);
128 if (!device.get()) {
129 CompleteWithError(kErrorNoDevice);
130 return;
133 usb_private::DeviceInfo device_info;
134 device_info.vendor_id = device->vendor_id();
135 device_info.product_id = device->product_id();
137 const char* name = device::UsbIds::GetVendorName(device_info.vendor_id);
138 if (name) {
139 device_info.vendor_name.reset(new std::string(name));
142 name = device::UsbIds::GetProductName(device_info.vendor_id,
143 device_info.product_id);
144 if (name) {
145 device_info.product_name.reset(new std::string(name));
148 scoped_refptr<UsbDeviceHandle> device_handle = device->Open();
149 if (!device_handle.get()) {
150 CompleteWithError(kErrorOpen);
151 return;
154 base::string16 utf16;
155 if (device_handle->GetManufacturer(&utf16)) {
156 device_info.manufacturer_string.reset(
157 new std::string(base::UTF16ToUTF8(utf16)));
160 if (device_handle->GetProduct(&utf16)) {
161 device_info.product_string.reset(new std::string(base::UTF16ToUTF8(utf16)));
164 if (device_handle->GetSerial(&utf16)) {
165 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16)));
168 SetResult(device_info.ToValue().release());
169 AsyncWorkCompleted();
172 } // namespace extensions