OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "extensions/browser/api/usb_private/usb_private_api.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 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" |
| 17 |
| 18 namespace usb_private = extensions::core_api::usb_private; |
| 19 namespace GetDevices = usb_private::GetDevices; |
| 20 namespace GetDeviceInfo = usb_private::GetDeviceInfo; |
| 21 |
| 22 using usb_service::UsbDevice; |
| 23 using usb_service::UsbDeviceFilter; |
| 24 using usb_service::UsbDeviceHandle; |
| 25 using usb_service::UsbService; |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kErrorInitService[] = "Failed to initialize USB service."; |
| 30 const char kErrorNoDevice[] = "No such device."; |
| 31 const char kErrorOpen[] = "Failed to open device."; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 namespace extensions { |
| 36 |
| 37 UsbPrivateGetDevicesFunction::UsbPrivateGetDevicesFunction() { |
| 38 } |
| 39 |
| 40 UsbPrivateGetDevicesFunction::~UsbPrivateGetDevicesFunction() { |
| 41 } |
| 42 |
| 43 bool UsbPrivateGetDevicesFunction::Prepare() { |
| 44 parameters_ = GetDevices::Params::Create(*args_); |
| 45 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 46 return true; |
| 47 } |
| 48 |
| 49 void UsbPrivateGetDevicesFunction::AsyncWorkStart() { |
| 50 UsbService* service = UsbService::GetInstance(); |
| 51 if (!service) { |
| 52 CompleteWithError(kErrorInitService); |
| 53 return; |
| 54 } |
| 55 |
| 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(); |
| 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 } |
| 79 |
| 80 std::vector<scoped_refptr<UsbDevice> > devices; |
| 81 service->GetDevices(&devices); |
| 82 |
| 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; |
| 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())); |
| 100 } |
| 101 } |
| 102 |
| 103 SetResult(result.release()); |
| 104 AsyncWorkCompleted(); |
| 105 } |
| 106 |
| 107 UsbPrivateGetDeviceInfoFunction::UsbPrivateGetDeviceInfoFunction() { |
| 108 } |
| 109 |
| 110 UsbPrivateGetDeviceInfoFunction::~UsbPrivateGetDeviceInfoFunction() { |
| 111 } |
| 112 |
| 113 bool UsbPrivateGetDeviceInfoFunction::Prepare() { |
| 114 parameters_ = GetDeviceInfo::Params::Create(*args_); |
| 115 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 116 return true; |
| 117 } |
| 118 |
| 119 void UsbPrivateGetDeviceInfoFunction::AsyncWorkStart() { |
| 120 UsbService* service = UsbService::GetInstance(); |
| 121 if (!service) { |
| 122 CompleteWithError(kErrorInitService); |
| 123 return; |
| 124 } |
| 125 |
| 126 scoped_refptr<UsbDevice> device = |
| 127 service->GetDeviceById(parameters_->device_id); |
| 128 if (!device) { |
| 129 CompleteWithError(kErrorNoDevice); |
| 130 return; |
| 131 } |
| 132 |
| 133 usb_private::DeviceInfo device_info; |
| 134 device_info.vendor_id = device->vendor_id(); |
| 135 device_info.product_id = device->product_id(); |
| 136 |
| 137 const char* name = device::UsbIds::GetVendorName(device_info.vendor_id); |
| 138 if (name) { |
| 139 device_info.vendor_name.reset(new std::string(name)); |
| 140 } |
| 141 |
| 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)); |
| 146 } |
| 147 |
| 148 scoped_refptr<UsbDeviceHandle> device_handle = device->Open(); |
| 149 if (!device_handle) { |
| 150 CompleteWithError(kErrorOpen); |
| 151 return; |
| 152 } |
| 153 |
| 154 base::string16 utf16; |
| 155 if (device_handle->GetManufacturer(&utf16)) { |
| 156 device_info.manufacturer_string.reset( |
| 157 new std::string(base::UTF16ToUTF8(utf16))); |
| 158 } |
| 159 |
| 160 if (device_handle->GetProduct(&utf16)) { |
| 161 device_info.product_string.reset(new std::string(base::UTF16ToUTF8(utf16))); |
| 162 } |
| 163 |
| 164 if (device_handle->GetSerial(&utf16)) { |
| 165 device_info.serial_string.reset(new std::string(base::UTF16ToUTF8(utf16))); |
| 166 } |
| 167 |
| 168 SetResult(device_info.ToValue().release()); |
| 169 AsyncWorkCompleted(); |
| 170 } |
| 171 |
| 172 } // namespace extensions |
OLD | NEW |