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"
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
;
29 const char kErrorInitService
[] = "Failed to initialize USB service.";
30 const char kErrorNoDevice
[] = "No such device.";
31 const char kErrorOpen
[] = "Failed to open device.";
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());
49 void UsbPrivateGetDevicesFunction::AsyncWorkStart() {
50 UsbService
* service
= UsbService::GetInstance();
52 CompleteWithError(kErrorInitService
);
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
];
88 if (filters
.empty()) {
91 for (size_t j
= 0; !matched
&& j
< filters
.size(); ++j
) {
92 if (filters
[j
].Matches(device
)) {
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());
119 void UsbPrivateGetDeviceInfoFunction::AsyncWorkStart() {
120 UsbService
* service
= UsbService::GetInstance();
122 CompleteWithError(kErrorInitService
);
126 scoped_refptr
<UsbDevice
> device
=
127 service
->GetDeviceById(parameters_
->device_id
);
129 CompleteWithError(kErrorNoDevice
);
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
);
139 device_info
.vendor_name
.reset(new std::string(name
));
142 name
= device::UsbIds::GetProductName(device_info
.vendor_id
,
143 device_info
.product_id
);
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
);
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