Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/usb_api.h" | 5 #include "extensions/browser/api/usb/usb_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "device/core/device_client.h" | 12 #include "device/core/device_client.h" |
| 13 #include "device/usb/usb_device_handle.h" | 13 #include "device/usb/usb_device_handle.h" |
| 14 #include "device/usb/usb_service.h" | 14 #include "device/usb/usb_service.h" |
| 15 #include "extensions/browser/api/usb/usb_device_resource.h" | 15 #include "extensions/browser/api/usb/usb_device_resource.h" |
| 16 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
| 17 #include "extensions/common/api/usb.h" | 17 #include "extensions/common/api/usb.h" |
| 18 #include "extensions/common/permissions/permissions_data.h" | 18 #include "extensions/common/permissions/permissions_data.h" |
| 19 #include "extensions/common/permissions/usb_device_permission.h" | 19 #include "extensions/common/permissions/usb_device_permission.h" |
| 20 | 20 |
| 21 namespace usb = extensions::core_api::usb; | 21 namespace usb = extensions::core_api::usb; |
| 22 namespace BulkTransfer = usb::BulkTransfer; | 22 namespace BulkTransfer = usb::BulkTransfer; |
| 23 namespace ClaimInterface = usb::ClaimInterface; | 23 namespace ClaimInterface = usb::ClaimInterface; |
| 24 namespace CloseDevice = usb::CloseDevice; | 24 namespace CloseDevice = usb::CloseDevice; |
| 25 namespace ControlTransfer = usb::ControlTransfer; | 25 namespace ControlTransfer = usb::ControlTransfer; |
| 26 namespace FindDevices = usb::FindDevices; | 26 namespace FindDevices = usb::FindDevices; |
| 27 namespace GetDevices = usb::GetDevices; | 27 namespace GetDevices = usb::GetDevices; |
| 28 namespace InterruptTransfer = usb::InterruptTransfer; | 28 namespace InterruptTransfer = usb::InterruptTransfer; |
| 29 namespace IsochronousTransfer = usb::IsochronousTransfer; | 29 namespace IsochronousTransfer = usb::IsochronousTransfer; |
| 30 namespace GetConfiguration = usb::GetConfiguration; | |
| 30 namespace ListInterfaces = usb::ListInterfaces; | 31 namespace ListInterfaces = usb::ListInterfaces; |
| 31 namespace OpenDevice = usb::OpenDevice; | 32 namespace OpenDevice = usb::OpenDevice; |
| 32 namespace ReleaseInterface = usb::ReleaseInterface; | 33 namespace ReleaseInterface = usb::ReleaseInterface; |
| 33 namespace RequestAccess = usb::RequestAccess; | 34 namespace RequestAccess = usb::RequestAccess; |
| 34 namespace ResetDevice = usb::ResetDevice; | 35 namespace ResetDevice = usb::ResetDevice; |
| 35 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; | 36 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; |
| 36 | 37 |
| 37 using content::BrowserThread; | 38 using content::BrowserThread; |
| 38 using std::string; | 39 using usb::ConfigDescriptor; |
| 39 using std::vector; | |
| 40 using usb::ControlTransferInfo; | 40 using usb::ControlTransferInfo; |
| 41 using usb::ConnectionHandle; | 41 using usb::ConnectionHandle; |
| 42 using usb::Device; | 42 using usb::Device; |
| 43 using usb::Direction; | 43 using usb::Direction; |
| 44 using usb::EndpointDescriptor; | 44 using usb::EndpointDescriptor; |
| 45 using usb::GenericTransferInfo; | 45 using usb::GenericTransferInfo; |
| 46 using usb::InterfaceDescriptor; | 46 using usb::InterfaceDescriptor; |
| 47 using usb::IsochronousTransferInfo; | 47 using usb::IsochronousTransferInfo; |
| 48 using usb::Recipient; | 48 using usb::Recipient; |
| 49 using usb::RequestType; | 49 using usb::RequestType; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 77 const char kErrorCancelled[] = "Transfer was cancelled."; | 77 const char kErrorCancelled[] = "Transfer was cancelled."; |
| 78 const char kErrorDisconnect[] = "Device disconnected."; | 78 const char kErrorDisconnect[] = "Device disconnected."; |
| 79 const char kErrorGeneric[] = "Transfer failed."; | 79 const char kErrorGeneric[] = "Transfer failed."; |
| 80 #if !defined(OS_CHROMEOS) | 80 #if !defined(OS_CHROMEOS) |
| 81 const char kErrorNotSupported[] = "Not supported on this platform."; | 81 const char kErrorNotSupported[] = "Not supported on this platform."; |
| 82 #endif | 82 #endif |
| 83 const char kErrorOverflow[] = "Inbound transfer overflow."; | 83 const char kErrorOverflow[] = "Inbound transfer overflow."; |
| 84 const char kErrorStalled[] = "Transfer stalled."; | 84 const char kErrorStalled[] = "Transfer stalled."; |
| 85 const char kErrorTimeout[] = "Transfer timed out."; | 85 const char kErrorTimeout[] = "Transfer timed out."; |
| 86 const char kErrorTransferLength[] = "Transfer length is insufficient."; | 86 const char kErrorTransferLength[] = "Transfer length is insufficient."; |
| 87 | |
| 88 const char kErrorCannotListInterfaces[] = "Error listing interfaces."; | |
| 89 const char kErrorCannotClaimInterface[] = "Error claiming interface."; | 87 const char kErrorCannotClaimInterface[] = "Error claiming interface."; |
| 90 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; | 88 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; |
| 91 const char kErrorCannotSetInterfaceAlternateSetting[] = | 89 const char kErrorCannotSetInterfaceAlternateSetting[] = |
| 92 "Error setting alternate interface setting."; | 90 "Error setting alternate interface setting."; |
| 93 const char kErrorConvertDirection[] = "Invalid transfer direction."; | 91 const char kErrorConvertDirection[] = "Invalid transfer direction."; |
| 94 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; | 92 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; |
| 95 const char kErrorConvertRequestType[] = "Invalid request type."; | 93 const char kErrorConvertRequestType[] = "Invalid request type."; |
| 96 const char kErrorConvertSynchronizationType[] = "Invalid synchronization type"; | |
| 97 const char kErrorConvertTransferType[] = "Invalid endpoint type."; | |
| 98 const char kErrorConvertUsageType[] = "Invalid usage type."; | |
| 99 const char kErrorMalformedParameters[] = "Error parsing parameters."; | 94 const char kErrorMalformedParameters[] = "Error parsing parameters."; |
| 100 const char kErrorNoDevice[] = "No such device."; | 95 const char kErrorNoDevice[] = "No such device."; |
| 101 const char kErrorPermissionDenied[] = "Permission to access device was denied"; | 96 const char kErrorPermissionDenied[] = "Permission to access device was denied"; |
| 102 const char kErrorInvalidTransferLength[] = | 97 const char kErrorInvalidTransferLength[] = |
| 103 "Transfer length must be a positive number less than 104,857,600."; | 98 "Transfer length must be a positive number less than 104,857,600."; |
| 104 const char kErrorInvalidNumberOfPackets[] = | 99 const char kErrorInvalidNumberOfPackets[] = |
| 105 "Number of packets must be a positive number less than 4,194,304."; | 100 "Number of packets must be a positive number less than 4,194,304."; |
| 106 const char kErrorInvalidPacketLength[] = | 101 const char kErrorInvalidPacketLength[] = |
| 107 "Packet length must be a positive number less than 65,536."; | 102 "Packet length must be a positive number less than 65,536."; |
| 108 const char kErrorResetDevice[] = | 103 const char kErrorResetDevice[] = |
| 109 "Error resetting the device. The device has been closed."; | 104 "Error resetting the device. The device has been closed."; |
| 110 | 105 |
| 111 const size_t kMaxTransferLength = 100 * 1024 * 1024; | 106 const size_t kMaxTransferLength = 100 * 1024 * 1024; |
| 112 const int kMaxPackets = 4 * 1024 * 1024; | 107 const int kMaxPackets = 4 * 1024 * 1024; |
| 113 const int kMaxPacketLength = 64 * 1024; | 108 const int kMaxPacketLength = 64 * 1024; |
| 114 | 109 |
| 115 bool ConvertDirectionToApi(const UsbEndpointDirection& input, | 110 bool ConvertDirectionFromApi(const Direction& input, |
| 116 Direction* output) { | 111 UsbEndpointDirection* output) { |
| 117 switch (input) { | |
| 118 case device::USB_DIRECTION_INBOUND: | |
| 119 *output = usb::DIRECTION_IN; | |
| 120 return true; | |
| 121 case device::USB_DIRECTION_OUTBOUND: | |
| 122 *output = usb::DIRECTION_OUT; | |
| 123 return true; | |
| 124 default: | |
| 125 NOTREACHED(); | |
| 126 return false; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, | |
| 131 usb::SynchronizationType* output) { | |
| 132 switch (input) { | |
| 133 case device::USB_SYNCHRONIZATION_NONE: | |
| 134 *output = usb::SYNCHRONIZATION_TYPE_NONE; | |
| 135 return true; | |
| 136 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS: | |
| 137 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | |
| 138 return true; | |
| 139 case device::USB_SYNCHRONIZATION_ADAPTIVE: | |
| 140 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | |
| 141 return true; | |
| 142 case device::USB_SYNCHRONIZATION_SYNCHRONOUS: | |
| 143 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | |
| 144 return true; | |
| 145 default: | |
| 146 NOTREACHED(); | |
| 147 return false; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 bool ConvertTransferTypeToApi(const UsbTransferType& input, | |
| 152 usb::TransferType* output) { | |
| 153 switch (input) { | |
| 154 case device::USB_TRANSFER_CONTROL: | |
| 155 *output = usb::TRANSFER_TYPE_CONTROL; | |
| 156 return true; | |
| 157 case device::USB_TRANSFER_INTERRUPT: | |
| 158 *output = usb::TRANSFER_TYPE_INTERRUPT; | |
| 159 return true; | |
| 160 case device::USB_TRANSFER_ISOCHRONOUS: | |
| 161 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; | |
| 162 return true; | |
| 163 case device::USB_TRANSFER_BULK: | |
| 164 *output = usb::TRANSFER_TYPE_BULK; | |
| 165 return true; | |
| 166 default: | |
| 167 NOTREACHED(); | |
| 168 return false; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { | |
| 173 switch (input) { | |
| 174 case device::USB_USAGE_DATA: | |
| 175 *output = usb::USAGE_TYPE_DATA; | |
| 176 return true; | |
| 177 case device::USB_USAGE_FEEDBACK: | |
| 178 *output = usb::USAGE_TYPE_FEEDBACK; | |
| 179 return true; | |
| 180 case device::USB_USAGE_EXPLICIT_FEEDBACK: | |
| 181 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | |
| 182 return true; | |
| 183 default: | |
| 184 NOTREACHED(); | |
| 185 return false; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { | |
| 190 switch (input) { | 112 switch (input) { |
| 191 case usb::DIRECTION_IN: | 113 case usb::DIRECTION_IN: |
| 192 *output = device::USB_DIRECTION_INBOUND; | 114 *output = device::USB_DIRECTION_INBOUND; |
| 193 return true; | 115 return true; |
| 194 case usb::DIRECTION_OUT: | 116 case usb::DIRECTION_OUT: |
| 195 *output = device::USB_DIRECTION_OUTBOUND; | 117 *output = device::USB_DIRECTION_OUTBOUND; |
| 196 return true; | 118 return true; |
| 197 default: | 119 default: |
| 198 NOTREACHED(); | 120 NOTREACHED(); |
| 199 return false; | 121 return false; |
| 200 } | 122 } |
| 201 } | 123 } |
| 202 | 124 |
| 203 bool ConvertRequestType(const RequestType& input, | 125 bool ConvertRequestTypeFromApi(const RequestType& input, |
| 204 UsbDeviceHandle::TransferRequestType* output) { | 126 UsbDeviceHandle::TransferRequestType* output) { |
| 205 switch (input) { | 127 switch (input) { |
| 206 case usb::REQUEST_TYPE_STANDARD: | 128 case usb::REQUEST_TYPE_STANDARD: |
| 207 *output = UsbDeviceHandle::STANDARD; | 129 *output = UsbDeviceHandle::STANDARD; |
| 208 return true; | 130 return true; |
| 209 case usb::REQUEST_TYPE_CLASS: | 131 case usb::REQUEST_TYPE_CLASS: |
| 210 *output = UsbDeviceHandle::CLASS; | 132 *output = UsbDeviceHandle::CLASS; |
| 211 return true; | 133 return true; |
| 212 case usb::REQUEST_TYPE_VENDOR: | 134 case usb::REQUEST_TYPE_VENDOR: |
| 213 *output = UsbDeviceHandle::VENDOR; | 135 *output = UsbDeviceHandle::VENDOR; |
| 214 return true; | 136 return true; |
| 215 case usb::REQUEST_TYPE_RESERVED: | 137 case usb::REQUEST_TYPE_RESERVED: |
| 216 *output = UsbDeviceHandle::RESERVED; | 138 *output = UsbDeviceHandle::RESERVED; |
| 217 return true; | 139 return true; |
| 218 default: | 140 default: |
| 219 NOTREACHED(); | 141 NOTREACHED(); |
| 220 return false; | 142 return false; |
| 221 } | 143 } |
| 222 } | 144 } |
| 223 | 145 |
| 224 bool ConvertRecipient(const Recipient& input, | 146 bool ConvertRecipientFromApi(const Recipient& input, |
| 225 UsbDeviceHandle::TransferRecipient* output) { | 147 UsbDeviceHandle::TransferRecipient* output) { |
| 226 switch (input) { | 148 switch (input) { |
| 227 case usb::RECIPIENT_DEVICE: | 149 case usb::RECIPIENT_DEVICE: |
| 228 *output = UsbDeviceHandle::DEVICE; | 150 *output = UsbDeviceHandle::DEVICE; |
| 229 return true; | 151 return true; |
| 230 case usb::RECIPIENT_INTERFACE: | 152 case usb::RECIPIENT_INTERFACE: |
| 231 *output = UsbDeviceHandle::INTERFACE; | 153 *output = UsbDeviceHandle::INTERFACE; |
| 232 return true; | 154 return true; |
| 233 case usb::RECIPIENT_ENDPOINT: | 155 case usb::RECIPIENT_ENDPOINT: |
| 234 *output = UsbDeviceHandle::ENDPOINT; | 156 *output = UsbDeviceHandle::ENDPOINT; |
| 235 return true; | 157 return true; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 } else if (direction == device::USB_DIRECTION_OUTBOUND) { | 201 } else if (direction == device::USB_DIRECTION_OUTBOUND) { |
| 280 if (input.data.get() && size <= input.data->size()) { | 202 if (input.data.get() && size <= input.data->size()) { |
| 281 memcpy(buffer->data(), input.data->data(), size); | 203 memcpy(buffer->data(), input.data->data(), size); |
| 282 return buffer; | 204 return buffer; |
| 283 } | 205 } |
| 284 } | 206 } |
| 285 NOTREACHED(); | 207 NOTREACHED(); |
| 286 return NULL; | 208 return NULL; |
| 287 } | 209 } |
| 288 | 210 |
| 289 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { | 211 const char* ConvertTransferStatusToApi(const UsbTransferStatus status) { |
| 290 switch (status) { | 212 switch (status) { |
| 291 case device::USB_TRANSFER_COMPLETED: | 213 case device::USB_TRANSFER_COMPLETED: |
| 292 return ""; | 214 return ""; |
| 293 case device::USB_TRANSFER_ERROR: | 215 case device::USB_TRANSFER_ERROR: |
| 294 return kErrorGeneric; | 216 return kErrorGeneric; |
| 295 case device::USB_TRANSFER_TIMEOUT: | 217 case device::USB_TRANSFER_TIMEOUT: |
| 296 return kErrorTimeout; | 218 return kErrorTimeout; |
| 297 case device::USB_TRANSFER_CANCELLED: | 219 case device::USB_TRANSFER_CANCELLED: |
| 298 return kErrorCancelled; | 220 return kErrorCancelled; |
| 299 case device::USB_TRANSFER_STALLED: | 221 case device::USB_TRANSFER_STALLED: |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 373 } | 295 } |
| 374 | 296 |
| 375 base::Value* PopulateDevice(UsbDevice* device) { | 297 base::Value* PopulateDevice(UsbDevice* device) { |
| 376 Device result; | 298 Device result; |
| 377 result.device = device->unique_id(); | 299 result.device = device->unique_id(); |
| 378 result.vendor_id = device->vendor_id(); | 300 result.vendor_id = device->vendor_id(); |
| 379 result.product_id = device->product_id(); | 301 result.product_id = device->product_id(); |
| 380 return result.ToValue().release(); | 302 return result.ToValue().release(); |
| 381 } | 303 } |
| 382 | 304 |
| 383 base::Value* PopulateInterfaceDescriptor( | 305 TransferType ConvertTransferTypeToApi(const UsbTransferType& input) { |
| 384 int interface_number, | 306 switch (input) { |
| 385 int alternate_setting, | 307 case device::USB_TRANSFER_CONTROL: |
| 386 int interface_class, | 308 return usb::TRANSFER_TYPE_CONTROL; |
| 387 int interface_subclass, | 309 case device::USB_TRANSFER_INTERRUPT: |
| 388 int interface_protocol, | 310 return usb::TRANSFER_TYPE_INTERRUPT; |
| 389 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) { | 311 case device::USB_TRANSFER_ISOCHRONOUS: |
| 390 InterfaceDescriptor descriptor; | 312 return usb::TRANSFER_TYPE_ISOCHRONOUS; |
| 391 descriptor.interface_number = interface_number; | 313 case device::USB_TRANSFER_BULK: |
| 392 descriptor.alternate_setting = alternate_setting; | 314 return usb::TRANSFER_TYPE_BULK; |
| 393 descriptor.interface_class = interface_class; | 315 default: |
| 394 descriptor.interface_subclass = interface_subclass; | 316 NOTREACHED(); |
| 395 descriptor.interface_protocol = interface_protocol; | 317 return usb::TRANSFER_TYPE_NONE; |
| 396 descriptor.endpoints = *endpoints; | 318 } |
| 397 return descriptor.ToValue().release(); | 319 } |
| 320 | |
| 321 Direction ConvertDirectionToApi(const UsbEndpointDirection& input) { | |
| 322 switch (input) { | |
| 323 case device::USB_DIRECTION_INBOUND: | |
| 324 return usb::DIRECTION_IN; | |
| 325 case device::USB_DIRECTION_OUTBOUND: | |
| 326 return usb::DIRECTION_OUT; | |
| 327 default: | |
| 328 NOTREACHED(); | |
| 329 return usb::DIRECTION_NONE; | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 SynchronizationType ConvertSynchronizationTypeToApi( | |
| 334 const UsbSynchronizationType& input) { | |
| 335 switch (input) { | |
| 336 case device::USB_SYNCHRONIZATION_NONE: | |
| 337 return usb::SYNCHRONIZATION_TYPE_NONE; | |
| 338 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS: | |
| 339 return usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | |
| 340 case device::USB_SYNCHRONIZATION_ADAPTIVE: | |
| 341 return usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | |
| 342 case device::USB_SYNCHRONIZATION_SYNCHRONOUS: | |
| 343 return usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | |
| 344 default: | |
| 345 NOTREACHED(); | |
| 346 return usb::SYNCHRONIZATION_TYPE_NONE; | |
| 347 } | |
| 348 } | |
| 349 | |
| 350 UsageType ConvertUsageTypeToApi(const UsbUsageType& input) { | |
| 351 switch (input) { | |
| 352 case device::USB_USAGE_DATA: | |
| 353 return usb::USAGE_TYPE_DATA; | |
| 354 case device::USB_USAGE_FEEDBACK: | |
| 355 return usb::USAGE_TYPE_FEEDBACK; | |
| 356 case device::USB_USAGE_EXPLICIT_FEEDBACK: | |
| 357 return usb::USAGE_TYPE_EXPLICITFEEDBACK; | |
| 358 default: | |
| 359 NOTREACHED(); | |
| 360 return usb::USAGE_TYPE_NONE; | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 void ConvertEndpointDescriptor(const UsbEndpointDescriptor& input, | |
| 365 EndpointDescriptor* output) { | |
| 366 output->address = input.address; | |
| 367 output->type = ConvertTransferTypeToApi(input.transfer_type); | |
| 368 output->direction = ConvertDirectionToApi(input.direction); | |
| 369 output->maximum_packet_size = input.maximum_packet_size; | |
| 370 output->synchronization = | |
| 371 ConvertSynchronizationTypeToApi(input.synchronization_type); | |
| 372 output->usage = ConvertUsageTypeToApi(input.usage_type); | |
| 373 output->polling_interval.reset(new int(input.polling_interval)); | |
| 374 output->extra_data = | |
| 375 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), | |
| 376 input.extra_data.size()); | |
| 377 } | |
| 378 | |
| 379 void ConvertInterfaceDescriptor(const UsbInterfaceDescriptor& input, | |
| 380 InterfaceDescriptor* output) { | |
| 381 output->interface_number = input.interface_number; | |
| 382 output->alternate_setting = input.alternate_setting; | |
| 383 output->interface_class = input.interface_class; | |
| 384 output->interface_subclass = input.interface_subclass; | |
| 385 output->interface_protocol = input.interface_protocol; | |
| 386 for (size_t i = 0; i < input.endpoints.size(); ++i) { | |
|
meacer
2014/09/11 19:17:20
nit: Why not this?
for (input::iterator it = inpu
Reilly Grant (use Gerrit)
2014/09/11 19:38:47
That's not valid C++ but I wish it were. I'm going
meacer
2014/09/11 19:55:04
Yes, fail on my part. I meant |EndpointIterator it
| |
| 387 linked_ptr<EndpointDescriptor> endpoint(new EndpointDescriptor); | |
| 388 ConvertEndpointDescriptor(input.endpoints[i], endpoint.get()); | |
| 389 output->endpoints.push_back(endpoint); | |
| 390 } | |
| 391 output->extra_data = | |
| 392 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), | |
| 393 input.extra_data.size()); | |
| 394 } | |
| 395 | |
| 396 void ConvertConfigDescriptor(const UsbConfigDescriptor& input, | |
| 397 ConfigDescriptor* output) { | |
| 398 output->configuration_value = input.configuration_value; | |
| 399 output->self_powered = input.self_powered; | |
| 400 output->remote_wakeup = input.remote_wakeup; | |
| 401 output->max_power = input.maximum_power; | |
| 402 for (size_t i = 0; i < input.interfaces.size(); ++i) { | |
|
meacer
2014/09/11 19:17:20
Same as above
| |
| 403 linked_ptr<InterfaceDescriptor> interface(new InterfaceDescriptor); | |
| 404 ConvertInterfaceDescriptor(input.interfaces[i], interface.get()); | |
| 405 output->interfaces.push_back(interface); | |
| 406 } | |
| 407 output->extra_data = | |
| 408 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), | |
| 409 input.extra_data.size()); | |
| 398 } | 410 } |
| 399 | 411 |
| 400 } // namespace | 412 } // namespace |
| 401 | 413 |
| 402 namespace extensions { | 414 namespace extensions { |
| 403 | 415 |
| 404 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { | 416 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { |
| 405 } | 417 } |
| 406 | 418 |
| 407 UsbAsyncApiFunction::~UsbAsyncApiFunction() { | 419 UsbAsyncApiFunction::~UsbAsyncApiFunction() { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { | 521 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { |
| 510 } | 522 } |
| 511 | 523 |
| 512 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { | 524 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { |
| 513 } | 525 } |
| 514 | 526 |
| 515 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, | 527 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, |
| 516 scoped_refptr<net::IOBuffer> data, | 528 scoped_refptr<net::IOBuffer> data, |
| 517 size_t length) { | 529 size_t length) { |
| 518 if (status != device::USB_TRANSFER_COMPLETED) | 530 if (status != device::USB_TRANSFER_COMPLETED) |
| 519 SetError(ConvertTransferStatusToErrorString(status)); | 531 SetError(ConvertTransferStatusToApi(status)); |
| 520 | 532 |
| 521 SetResult(CreateTransferInfo(status, data, length)); | 533 SetResult(CreateTransferInfo(status, data, length)); |
| 522 AsyncWorkCompleted(); | 534 AsyncWorkCompleted(); |
| 523 } | 535 } |
| 524 | 536 |
| 525 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( | 537 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( |
| 526 const Direction& input, | 538 const Direction& input, |
| 527 UsbEndpointDirection* output) { | 539 UsbEndpointDirection* output) { |
| 528 const bool converted = ConvertDirection(input, output); | 540 const bool converted = ConvertDirectionFromApi(input, output); |
| 529 if (!converted) | 541 if (!converted) |
| 530 SetError(kErrorConvertDirection); | 542 SetError(kErrorConvertDirection); |
| 531 return converted; | 543 return converted; |
| 532 } | 544 } |
| 533 | 545 |
| 534 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( | 546 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( |
| 535 const RequestType& input, | 547 const RequestType& input, |
| 536 UsbDeviceHandle::TransferRequestType* output) { | 548 UsbDeviceHandle::TransferRequestType* output) { |
| 537 const bool converted = ConvertRequestType(input, output); | 549 const bool converted = ConvertRequestTypeFromApi(input, output); |
| 538 if (!converted) | 550 if (!converted) |
| 539 SetError(kErrorConvertRequestType); | 551 SetError(kErrorConvertRequestType); |
| 540 return converted; | 552 return converted; |
| 541 } | 553 } |
| 542 | 554 |
| 543 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely( | 555 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely( |
| 544 const Recipient& input, | 556 const Recipient& input, |
| 545 UsbDeviceHandle::TransferRecipient* output) { | 557 UsbDeviceHandle::TransferRecipient* output) { |
| 546 const bool converted = ConvertRecipient(input, output); | 558 const bool converted = ConvertRecipientFromApi(input, output); |
| 547 if (!converted) | 559 if (!converted) |
| 548 SetError(kErrorConvertRecipient); | 560 SetError(kErrorConvertRecipient); |
| 549 return converted; | 561 return converted; |
| 550 } | 562 } |
| 551 | 563 |
| 552 UsbFindDevicesFunction::UsbFindDevicesFunction() { | 564 UsbFindDevicesFunction::UsbFindDevicesFunction() { |
| 553 } | 565 } |
| 554 | 566 |
| 555 UsbFindDevicesFunction::~UsbFindDevicesFunction() { | 567 UsbFindDevicesFunction::~UsbFindDevicesFunction() { |
| 556 } | 568 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 return; | 747 return; |
| 736 } | 748 } |
| 737 | 749 |
| 738 SetResult(PopulateConnectionHandle( | 750 SetResult(PopulateConnectionHandle( |
| 739 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), | 751 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), |
| 740 handle_->GetDevice()->vendor_id(), | 752 handle_->GetDevice()->vendor_id(), |
| 741 handle_->GetDevice()->product_id())); | 753 handle_->GetDevice()->product_id())); |
| 742 AsyncWorkCompleted(); | 754 AsyncWorkCompleted(); |
| 743 } | 755 } |
| 744 | 756 |
| 757 UsbGetConfigurationFunction::UsbGetConfigurationFunction() { | |
| 758 } | |
| 759 | |
| 760 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() { | |
| 761 } | |
| 762 | |
| 763 bool UsbGetConfigurationFunction::Prepare() { | |
| 764 parameters_ = GetConfiguration::Params::Create(*args_); | |
| 765 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
| 766 return true; | |
| 767 } | |
| 768 | |
| 769 void UsbGetConfigurationFunction::AsyncWorkStart() { | |
| 770 scoped_refptr<UsbDeviceHandle> device_handle = | |
| 771 GetDeviceHandleOrCompleteWithError(parameters_->handle); | |
| 772 if (!device_handle.get()) { | |
| 773 return; | |
| 774 } | |
| 775 | |
| 776 ConfigDescriptor config; | |
| 777 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), | |
| 778 &config); | |
| 779 | |
| 780 SetResult(config.ToValue().release()); | |
| 781 AsyncWorkCompleted(); | |
| 782 } | |
| 783 | |
| 745 UsbListInterfacesFunction::UsbListInterfacesFunction() { | 784 UsbListInterfacesFunction::UsbListInterfacesFunction() { |
| 746 } | 785 } |
| 747 | 786 |
| 748 UsbListInterfacesFunction::~UsbListInterfacesFunction() { | 787 UsbListInterfacesFunction::~UsbListInterfacesFunction() { |
| 749 } | 788 } |
| 750 | 789 |
| 751 bool UsbListInterfacesFunction::Prepare() { | 790 bool UsbListInterfacesFunction::Prepare() { |
| 752 parameters_ = ListInterfaces::Params::Create(*args_); | 791 parameters_ = ListInterfaces::Params::Create(*args_); |
| 753 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 792 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 754 return true; | 793 return true; |
| 755 } | 794 } |
| 756 | 795 |
| 757 void UsbListInterfacesFunction::AsyncWorkStart() { | 796 void UsbListInterfacesFunction::AsyncWorkStart() { |
| 758 scoped_refptr<UsbDeviceHandle> device_handle = | 797 scoped_refptr<UsbDeviceHandle> device_handle = |
| 759 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 798 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
| 760 if (!device_handle.get()) | 799 if (!device_handle.get()) { |
| 761 return; | 800 return; |
| 801 } | |
| 762 | 802 |
| 763 const UsbConfigDescriptor& config = | 803 ConfigDescriptor config; |
| 764 device_handle->GetDevice()->GetConfiguration(); | 804 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), |
| 805 &config); | |
| 765 | 806 |
| 766 scoped_ptr<base::ListValue> result(new base::ListValue()); | 807 scoped_ptr<base::ListValue> result(new base::ListValue); |
| 767 | |
| 768 for (size_t i = 0; i < config.interfaces.size(); ++i) { | 808 for (size_t i = 0; i < config.interfaces.size(); ++i) { |
| 769 const UsbInterfaceDescriptor& usb_interface = config.interfaces[i]; | 809 result->Append(config.interfaces[i]->ToValue().release()); |
| 770 std::vector<linked_ptr<EndpointDescriptor> > endpoints; | |
| 771 | |
| 772 for (size_t j = 0; j < usb_interface.endpoints.size(); ++j) { | |
| 773 const UsbEndpointDescriptor& endpoint = usb_interface.endpoints[j]; | |
| 774 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor()); | |
| 775 | |
| 776 TransferType type; | |
| 777 Direction direction; | |
| 778 SynchronizationType synchronization; | |
| 779 UsageType usage; | |
| 780 | |
| 781 if (!ConvertTransferTypeSafely(endpoint.transfer_type, &type) || | |
| 782 !ConvertDirectionSafely(endpoint.direction, &direction) || | |
| 783 !ConvertSynchronizationTypeSafely(endpoint.synchronization_type, | |
| 784 &synchronization) || | |
| 785 !ConvertUsageTypeSafely(endpoint.usage_type, &usage)) { | |
| 786 SetError(kErrorCannotListInterfaces); | |
| 787 AsyncWorkCompleted(); | |
| 788 return; | |
| 789 } | |
| 790 | |
| 791 endpoint_desc->address = endpoint.address; | |
| 792 endpoint_desc->type = type; | |
| 793 endpoint_desc->direction = direction; | |
| 794 endpoint_desc->maximum_packet_size = endpoint.maximum_packet_size; | |
| 795 endpoint_desc->synchronization = synchronization; | |
| 796 endpoint_desc->usage = usage; | |
| 797 endpoint_desc->polling_interval.reset(new int(endpoint.polling_interval)); | |
| 798 | |
| 799 endpoints.push_back(endpoint_desc); | |
| 800 } | |
| 801 | |
| 802 result->Append(PopulateInterfaceDescriptor(usb_interface.interface_number, | |
| 803 usb_interface.alternate_setting, | |
| 804 usb_interface.interface_class, | |
| 805 usb_interface.interface_subclass, | |
| 806 usb_interface.interface_protocol, | |
| 807 &endpoints)); | |
| 808 } | 810 } |
| 809 | 811 |
| 810 SetResult(result.release()); | 812 SetResult(result.release()); |
| 811 AsyncWorkCompleted(); | 813 AsyncWorkCompleted(); |
| 812 } | 814 } |
| 813 | 815 |
| 814 bool UsbListInterfacesFunction::ConvertDirectionSafely( | |
| 815 const UsbEndpointDirection& input, | |
| 816 usb::Direction* output) { | |
| 817 const bool converted = ConvertDirectionToApi(input, output); | |
| 818 if (!converted) | |
| 819 SetError(kErrorConvertDirection); | |
| 820 return converted; | |
| 821 } | |
| 822 | |
| 823 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely( | |
| 824 const UsbSynchronizationType& input, | |
| 825 usb::SynchronizationType* output) { | |
| 826 const bool converted = ConvertSynchronizationTypeToApi(input, output); | |
| 827 if (!converted) | |
| 828 SetError(kErrorConvertSynchronizationType); | |
| 829 return converted; | |
| 830 } | |
| 831 | |
| 832 bool UsbListInterfacesFunction::ConvertTransferTypeSafely( | |
| 833 const UsbTransferType& input, | |
| 834 usb::TransferType* output) { | |
| 835 const bool converted = ConvertTransferTypeToApi(input, output); | |
| 836 if (!converted) | |
| 837 SetError(kErrorConvertTransferType); | |
| 838 return converted; | |
| 839 } | |
| 840 | |
| 841 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( | |
| 842 const UsbUsageType& input, | |
| 843 usb::UsageType* output) { | |
| 844 const bool converted = ConvertUsageTypeToApi(input, output); | |
| 845 if (!converted) | |
| 846 SetError(kErrorConvertUsageType); | |
| 847 return converted; | |
| 848 } | |
| 849 | |
| 850 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { | 816 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { |
| 851 } | 817 } |
| 852 | 818 |
| 853 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { | 819 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { |
| 854 } | 820 } |
| 855 | 821 |
| 856 bool UsbCloseDeviceFunction::Prepare() { | 822 bool UsbCloseDeviceFunction::Prepare() { |
| 857 parameters_ = CloseDevice::Params::Create(*args_); | 823 parameters_ = CloseDevice::Params::Create(*args_); |
| 858 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 824 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 859 return true; | 825 return true; |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1194 SetResult(new base::FundamentalValue(false)); | 1160 SetResult(new base::FundamentalValue(false)); |
| 1195 CompleteWithError(kErrorResetDevice); | 1161 CompleteWithError(kErrorResetDevice); |
| 1196 return; | 1162 return; |
| 1197 } | 1163 } |
| 1198 | 1164 |
| 1199 SetResult(new base::FundamentalValue(true)); | 1165 SetResult(new base::FundamentalValue(true)); |
| 1200 AsyncWorkCompleted(); | 1166 AsyncWorkCompleted(); |
| 1201 } | 1167 } |
| 1202 | 1168 |
| 1203 } // namespace extensions | 1169 } // namespace extensions |
| OLD | NEW |