Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/usb/usb_api.h" | 5 #include "chrome/browser/extensions/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 "chrome/browser/extensions/api/usb/usb_device_resource.h" | 11 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/usb/usb_service.h" | 14 #include "chrome/browser/usb/usb_service.h" |
| 15 #include "chrome/browser/usb/usb_service_factory.h" | 15 #include "chrome/browser/usb/usb_service_factory.h" |
| 16 #include "chrome/common/extensions/api/usb.h" | 16 #include "chrome/common/extensions/api/usb.h" |
| 17 #include "chrome/common/extensions/permissions/usb_device_permission.h" | 17 #include "chrome/common/extensions/permissions/usb_device_permission.h" |
| 18 | 18 |
| 19 namespace BulkTransfer = extensions::api::usb::BulkTransfer; | 19 namespace BulkTransfer = extensions::api::usb::BulkTransfer; |
| 20 namespace ClaimInterface = extensions::api::usb::ClaimInterface; | 20 namespace ClaimInterface = extensions::api::usb::ClaimInterface; |
| 21 namespace ListInterfaces = extensions::api::usb::ListInterfaces; | |
| 21 namespace CloseDevice = extensions::api::usb::CloseDevice; | 22 namespace CloseDevice = extensions::api::usb::CloseDevice; |
| 22 namespace ControlTransfer = extensions::api::usb::ControlTransfer; | 23 namespace ControlTransfer = extensions::api::usb::ControlTransfer; |
| 23 namespace FindDevices = extensions::api::usb::FindDevices; | 24 namespace FindDevices = extensions::api::usb::FindDevices; |
| 24 namespace InterruptTransfer = extensions::api::usb::InterruptTransfer; | 25 namespace InterruptTransfer = extensions::api::usb::InterruptTransfer; |
| 25 namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer; | 26 namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer; |
| 26 namespace ReleaseInterface = extensions::api::usb::ReleaseInterface; | 27 namespace ReleaseInterface = extensions::api::usb::ReleaseInterface; |
| 27 namespace SetInterfaceAlternateSetting = | 28 namespace SetInterfaceAlternateSetting = |
| 28 extensions::api::usb::SetInterfaceAlternateSetting; | 29 extensions::api::usb::SetInterfaceAlternateSetting; |
| 29 namespace usb = extensions::api::usb; | 30 namespace usb = extensions::api::usb; |
| 30 | 31 |
| 31 using std::string; | 32 using std::string; |
| 32 using std::vector; | 33 using std::vector; |
| 33 using usb::ControlTransferInfo; | 34 using usb::ControlTransferInfo; |
| 34 using usb::Device; | 35 using usb::Device; |
| 35 using usb::Direction; | 36 using usb::Direction; |
| 37 using usb::EndpointDescriptor; | |
| 38 using usb::EndpointType; | |
| 36 using usb::GenericTransferInfo; | 39 using usb::GenericTransferInfo; |
| 40 using usb::InterfaceDescriptor; | |
| 37 using usb::IsochronousTransferInfo; | 41 using usb::IsochronousTransferInfo; |
| 38 using usb::Recipient; | 42 using usb::Recipient; |
| 39 using usb::RequestType; | 43 using usb::RequestType; |
| 44 using usb::SynchronizationType; | |
| 45 using usb::UsageType; | |
| 40 | 46 |
| 41 namespace { | 47 namespace { |
| 42 | 48 |
| 43 static const char* kDataKey = "data"; | 49 static const char* kDataKey = "data"; |
| 44 static const char* kResultCodeKey = "resultCode"; | 50 static const char* kResultCodeKey = "resultCode"; |
| 45 | 51 |
| 46 static const char* kErrorCancelled = "Transfer was cancelled."; | 52 static const char* kErrorCancelled = "Transfer was cancelled."; |
| 47 static const char* kErrorDisconnect = "Device disconnected."; | 53 static const char* kErrorDisconnect = "Device disconnected."; |
| 48 static const char* kErrorGeneric = "Transfer failed."; | 54 static const char* kErrorGeneric = "Transfer failed."; |
| 49 static const char* kErrorOverflow = "Inbound transfer overflow."; | 55 static const char* kErrorOverflow = "Inbound transfer overflow."; |
| 50 static const char* kErrorStalled = "Transfer stalled."; | 56 static const char* kErrorStalled = "Transfer stalled."; |
| 51 static const char* kErrorTimeout = "Transfer timed out."; | 57 static const char* kErrorTimeout = "Transfer timed out."; |
| 52 static const char* kErrorTransferLength = "Transfer length is insufficient."; | 58 static const char* kErrorTransferLength = "Transfer length is insufficient."; |
| 53 | 59 |
| 60 static const char* kErrorCannotListInterfaces = "Error listing interfaces."; | |
| 54 static const char* kErrorCannotClaimInterface = "Error claiming interface."; | 61 static const char* kErrorCannotClaimInterface = "Error claiming interface."; |
| 55 static const char* kErrorCannotReleaseInterface = "Error releasing interface."; | 62 static const char* kErrorCannotReleaseInterface = "Error releasing interface."; |
| 56 static const char* kErrorCannotSetInterfaceAlternateSetting = | 63 static const char* kErrorCannotSetInterfaceAlternateSetting = |
| 57 "Error setting alternate interface setting."; | 64 "Error setting alternate interface setting."; |
| 58 static const char* kErrorConvertDirection = "Invalid transfer direction."; | 65 static const char* kErrorConvertDirection = "Invalid transfer direction."; |
| 66 static const char* kErrorConvertEndpointType = "Invalid endpoint type."; | |
| 59 static const char* kErrorConvertRecipient = "Invalid transfer recipient."; | 67 static const char* kErrorConvertRecipient = "Invalid transfer recipient."; |
| 60 static const char* kErrorConvertRequestType = "Invalid request type."; | 68 static const char* kErrorConvertRequestType = "Invalid request type."; |
| 69 static const char* kErrorConvertSynchronizationType = | |
| 70 "Invalid synchronization type"; | |
| 71 static const char* kErrorConvertUsageType = "Invalid usage type."; | |
| 61 static const char* kErrorMalformedParameters = "Error parsing parameters."; | 72 static const char* kErrorMalformedParameters = "Error parsing parameters."; |
| 62 static const char* kErrorNoDevice = "No such device."; | 73 static const char* kErrorNoDevice = "No such device."; |
| 63 static const char* kErrorPermissionDenied = | 74 static const char* kErrorPermissionDenied = |
| 64 "Permission to access device was denied"; | 75 "Permission to access device was denied"; |
| 65 static const char* kErrorInvalidTransferLength = "Transfer length must be a " | 76 static const char* kErrorInvalidTransferLength = "Transfer length must be a " |
| 66 "positive number less than 104,857,600."; | 77 "positive number less than 104,857,600."; |
| 67 static const char* kErrorInvalidNumberOfPackets = "Number of packets must be a " | 78 static const char* kErrorInvalidNumberOfPackets = "Number of packets must be a " |
| 68 "positive number less than 4,194,304."; | 79 "positive number less than 4,194,304."; |
| 69 static const char* kErrorInvalidPacketLength = "Packet length must be a " | 80 static const char* kErrorInvalidPacketLength = "Packet length must be a " |
| 70 "positive number less than 65,536."; | 81 "positive number less than 65,536."; |
| 71 | 82 |
| 72 static const size_t kMaxTransferLength = 100 * 1024 * 1024; | 83 static const size_t kMaxTransferLength = 100 * 1024 * 1024; |
| 73 static const int kMaxPackets = 4 * 1024 * 1024; | 84 static const int kMaxPackets = 4 * 1024 * 1024; |
| 74 static const int kMaxPacketLength = 64 * 1024; | 85 static const int kMaxPacketLength = 64 * 1024; |
| 75 | 86 |
| 76 static UsbDevice* device_for_test_ = NULL; | 87 static UsbDevice* device_for_test_ = NULL; |
| 77 | 88 |
| 78 static bool ConvertDirection(const Direction& input, | 89 static bool ConvertDirectionToApi(const UsbInterfaceDirection& input, |
| 79 UsbDevice::TransferDirection* output) { | 90 Direction* output) { |
| 80 switch (input) { | 91 switch (input) { |
| 81 case usb::DIRECTION_IN: | 92 case USB_DIRECTION_INBOUND: |
| 82 *output = UsbDevice::INBOUND; | 93 *output = usb::DIRECTION_IN; |
| 83 return true; | 94 return true; |
| 84 case usb::DIRECTION_OUT: | 95 case USB_DIRECTION_OUTBOUND: |
| 85 *output = UsbDevice::OUTBOUND; | 96 *output = usb::DIRECTION_OUT; |
| 86 return true; | 97 return true; |
| 87 default: | 98 default: |
|
Bei Zhang
2013/04/26 22:38:01
I believe this should be:
default:
NOTREACHED()
Kenny Root (Google)
2013/05/08 01:15:08
Done.
| |
| 88 return false; | 99 return false; |
| 100 } | |
| 101 NOTREACHED(); | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 static bool ConvertEndpointTypeToApi( | |
| 106 const UsbEndpointType& input, | |
| 107 extensions::api::usb::EndpointType* output) { | |
| 108 switch (input) { | |
| 109 case USB_ENDPOINT_CONTROL: | |
| 110 *output = usb::ENDPOINT_TYPE_CONTROL; | |
| 111 return true; | |
| 112 case USB_ENDPOINT_INTERRUPT: | |
| 113 *output = usb::ENDPOINT_TYPE_INTERRUPT; | |
| 114 return true; | |
| 115 case USB_ENDPOINT_ISOCHRONOUS: | |
| 116 *output = usb::ENDPOINT_TYPE_ISOCHRONOUS; | |
| 117 return true; | |
| 118 case USB_ENDPOINT_BULK: | |
| 119 *output = usb::ENDPOINT_TYPE_BULK; | |
| 120 return true; | |
| 121 default: | |
| 122 return false; | |
| 123 } | |
| 124 NOTREACHED(); | |
| 125 return false; | |
| 126 } | |
| 127 | |
| 128 static bool ConvertSynchronizationTypeToApi( | |
| 129 const UsbSynchronizationType& input, | |
| 130 extensions::api::usb::SynchronizationType* output) { | |
| 131 switch (input) { | |
| 132 case USB_SYNCHRONIZATION_NONE: | |
| 133 *output = usb::SYNCHRONIZATION_TYPE_NONE; | |
| 134 return true; | |
| 135 case USB_SYNCHRONIZATION_ASYNCHRONOUS: | |
| 136 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | |
| 137 return true; | |
| 138 case USB_SYNCHRONIZATION_ADAPTIVE: | |
| 139 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | |
| 140 return true; | |
| 141 case USB_SYNCHRONIZATION_SYNCHRONOUS: | |
| 142 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | |
| 143 return true; | |
| 144 default: | |
| 145 return false; | |
| 146 } | |
| 147 NOTREACHED(); | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 static bool ConvertUsageTypeToApi(const UsbUsageType& input, | |
| 152 extensions::api::usb::UsageType* output) { | |
| 153 switch (input) { | |
| 154 case USB_USAGE_DATA: | |
| 155 *output = usb::USAGE_TYPE_DATA; | |
| 156 return true; | |
| 157 case USB_USAGE_FEEDBACK: | |
| 158 *output = usb::USAGE_TYPE_FEEDBACK; | |
| 159 return true; | |
| 160 case USB_USAGE_EXPLICIT_FEEDBACK: | |
| 161 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | |
| 162 return true; | |
| 163 default: | |
| 164 return false; | |
| 165 } | |
| 166 NOTREACHED(); | |
| 167 return false; | |
| 168 } | |
| 169 | |
| 170 static bool ConvertDirection(const Direction& input, | |
| 171 UsbInterfaceDirection* output) { | |
| 172 switch (input) { | |
| 173 case usb::DIRECTION_IN: | |
| 174 *output = USB_DIRECTION_INBOUND; | |
| 175 return true; | |
| 176 case usb::DIRECTION_OUT: | |
| 177 *output = USB_DIRECTION_OUTBOUND; | |
| 178 return true; | |
| 179 default: | |
| 180 return false; | |
| 89 } | 181 } |
| 90 NOTREACHED(); | 182 NOTREACHED(); |
| 91 return false; | 183 return false; |
| 92 } | 184 } |
| 93 | 185 |
| 94 static bool ConvertRequestType(const RequestType& input, | 186 static bool ConvertRequestType(const RequestType& input, |
| 95 UsbDevice::TransferRequestType* output) { | 187 UsbDevice::TransferRequestType* output) { |
| 96 switch (input) { | 188 switch (input) { |
| 97 case usb::REQUEST_TYPE_STANDARD: | 189 case usb::REQUEST_TYPE_STANDARD: |
| 98 *output = UsbDevice::STANDARD; | 190 *output = UsbDevice::STANDARD; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 if (input.data.get()) { | 240 if (input.data.get()) { |
| 149 *output = input.data->size(); | 241 *output = input.data->size(); |
| 150 return true; | 242 return true; |
| 151 } | 243 } |
| 152 } | 244 } |
| 153 return false; | 245 return false; |
| 154 } | 246 } |
| 155 | 247 |
| 156 template<class T> | 248 template<class T> |
| 157 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer( | 249 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer( |
| 158 const T& input, UsbDevice::TransferDirection direction, size_t size) { | 250 const T& input, UsbInterfaceDirection direction, size_t size) { |
| 159 | 251 |
| 160 if (size >= kMaxTransferLength) | 252 if (size >= kMaxTransferLength) |
| 161 return NULL; | 253 return NULL; |
| 162 | 254 |
| 163 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 255 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
| 164 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 256 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
| 165 // cannot represent a zero-length buffer, while an URB can. | 257 // cannot represent a zero-length buffer, while an URB can. |
| 166 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( | 258 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( |
| 167 static_cast<size_t>(1), size)); | 259 static_cast<size_t>(1), size)); |
| 168 | 260 |
| 169 if (direction == UsbDevice::INBOUND) { | 261 if (direction == USB_DIRECTION_INBOUND) { |
| 170 return buffer; | 262 return buffer; |
| 171 } else if (direction == UsbDevice::OUTBOUND) { | 263 } else if (direction == USB_DIRECTION_OUTBOUND) { |
| 172 if (input.data.get() && size <= input.data->size()) { | 264 if (input.data.get() && size <= input.data->size()) { |
| 173 memcpy(buffer->data(), input.data->data(), size); | 265 memcpy(buffer->data(), input.data->data(), size); |
| 174 return buffer; | 266 return buffer; |
| 175 } | 267 } |
| 176 } | 268 } |
| 177 NOTREACHED(); | 269 NOTREACHED(); |
| 178 return NULL; | 270 return NULL; |
| 179 } | 271 } |
| 180 | 272 |
| 181 static const char* ConvertTransferStatusToErrorString( | 273 static const char* ConvertTransferStatusToErrorString( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 } | 307 } |
| 216 | 308 |
| 217 static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) { | 309 static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) { |
| 218 Device device; | 310 Device device; |
| 219 device.handle = handle; | 311 device.handle = handle; |
| 220 device.vendor_id = vendor_id; | 312 device.vendor_id = vendor_id; |
| 221 device.product_id = product_id; | 313 device.product_id = product_id; |
| 222 return device.ToValue().release(); | 314 return device.ToValue().release(); |
| 223 } | 315 } |
| 224 | 316 |
| 317 static base::Value* PopulateInterfaceDescriptor(int interface_number, | |
| 318 int alternate_setting, int interface_class, int interface_subclass, | |
| 319 int interface_protocol, | |
| 320 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) { | |
| 321 InterfaceDescriptor descriptor; | |
| 322 descriptor.interface_number = interface_number; | |
| 323 descriptor.alternate_setting = alternate_setting; | |
| 324 descriptor.interface_class = interface_class; | |
| 325 descriptor.interface_subclass = interface_subclass; | |
| 326 descriptor.interface_protocol = interface_subclass; | |
| 327 descriptor.endpoints = *endpoints; | |
| 328 return descriptor.ToValue().release(); | |
| 329 } | |
| 330 | |
| 225 } // namespace | 331 } // namespace |
| 226 | 332 |
| 227 namespace extensions { | 333 namespace extensions { |
| 228 | 334 |
| 229 UsbAsyncApiFunction::UsbAsyncApiFunction() | 335 UsbAsyncApiFunction::UsbAsyncApiFunction() |
| 230 : manager_(NULL) { | 336 : manager_(NULL) { |
| 231 } | 337 } |
| 232 | 338 |
| 233 UsbAsyncApiFunction::~UsbAsyncApiFunction() { | 339 UsbAsyncApiFunction::~UsbAsyncApiFunction() { |
| 234 } | 340 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 264 scoped_refptr<net::IOBuffer> data, | 370 scoped_refptr<net::IOBuffer> data, |
| 265 size_t length) { | 371 size_t length) { |
| 266 if (status != USB_TRANSFER_COMPLETED) | 372 if (status != USB_TRANSFER_COMPLETED) |
| 267 SetError(ConvertTransferStatusToErrorString(status)); | 373 SetError(ConvertTransferStatusToErrorString(status)); |
| 268 | 374 |
| 269 SetResult(CreateTransferInfo(status, data, length)); | 375 SetResult(CreateTransferInfo(status, data, length)); |
| 270 AsyncWorkCompleted(); | 376 AsyncWorkCompleted(); |
| 271 } | 377 } |
| 272 | 378 |
| 273 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( | 379 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( |
| 274 const Direction& input, UsbDevice::TransferDirection* output) { | 380 const Direction& input, UsbInterfaceDirection* output) { |
| 275 const bool converted = ConvertDirection(input, output); | 381 const bool converted = ConvertDirection(input, output); |
| 276 if (!converted) | 382 if (!converted) |
| 277 SetError(kErrorConvertDirection); | 383 SetError(kErrorConvertDirection); |
| 278 return converted; | 384 return converted; |
| 279 } | 385 } |
| 280 | 386 |
| 281 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( | 387 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( |
| 282 const RequestType& input, UsbDevice::TransferRequestType* output) { | 388 const RequestType& input, UsbDevice::TransferRequestType* output) { |
| 283 const bool converted = ConvertRequestType(input, output); | 389 const bool converted = ConvertRequestType(input, output); |
| 284 if (!converted) | 390 if (!converted) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 Device js_device; | 460 Device js_device; |
| 355 result_->Append(PopulateDevice(manager_->Add(resource), | 461 result_->Append(PopulateDevice(manager_->Add(resource), |
| 356 parameters_->options.vendor_id, | 462 parameters_->options.vendor_id, |
| 357 parameters_->options.product_id)); | 463 parameters_->options.product_id)); |
| 358 } | 464 } |
| 359 | 465 |
| 360 SetResult(result_.release()); | 466 SetResult(result_.release()); |
| 361 AsyncWorkCompleted(); | 467 AsyncWorkCompleted(); |
| 362 } | 468 } |
| 363 | 469 |
| 470 UsbListInterfacesFunction::UsbListInterfacesFunction() {} | |
| 471 | |
| 472 UsbListInterfacesFunction::~UsbListInterfacesFunction() {} | |
| 473 | |
| 474 bool UsbListInterfacesFunction::Prepare() { | |
| 475 parameters_ = ListInterfaces::Params::Create(*args_); | |
| 476 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | |
| 477 return true; | |
| 478 } | |
| 479 | |
| 480 void UsbListInterfacesFunction::AsyncWorkStart() { | |
| 481 UsbDeviceResource* const device = GetUsbDeviceResource( | |
| 482 parameters_->device.handle); | |
| 483 if (!device) { | |
| 484 CompleteWithError(kErrorNoDevice); | |
| 485 return; | |
| 486 } | |
| 487 | |
| 488 PlatformUsbConfigDescriptor platform_config = NULL; | |
| 489 | |
| 490 device->device()->ListInterfaces(&platform_config, base::Bind( | |
| 491 &UsbListInterfacesFunction::OnCompleted, this)); | |
| 492 | |
| 493 config_ = new UsbConfigDescriptor(platform_config); | |
|
Bei Zhang
2013/04/26 22:38:01
Can you put some comment to explain these two line
Kenny Root (Google)
2013/05/08 01:15:08
Oh, I see your point. This doesn't actually work.
| |
| 494 | |
| 495 RemoveUsbDeviceResource(parameters_->device.handle); | |
| 496 } | |
| 497 | |
| 498 void UsbListInterfacesFunction::OnCompleted(bool success) { | |
| 499 if (!success) { | |
| 500 SetError(kErrorCannotListInterfaces); | |
| 501 AsyncWorkCompleted(); | |
| 502 return; | |
| 503 } | |
| 504 | |
| 505 for (size_t i = 0, numInterfaces = config_->numInterfaces(); | |
|
Kenny Root (Google)
2013/05/08 01:15:08
I might be able to move this all into AsyncWorkSta
| |
| 506 i < numInterfaces; ++i) { | |
| 507 scoped_refptr<const UsbInterface> usbInterface(config_->getInterface(i)); | |
| 508 for (size_t j = 0, numDescriptors = usbInterface->numAltSettings(); | |
| 509 i < numDescriptors; ++i) { | |
| 510 scoped_refptr<const UsbInterfaceDescriptor> descriptor | |
| 511 = usbInterface->getAltSetting(j); | |
| 512 std::vector<linked_ptr<EndpointDescriptor> > endpoints; | |
| 513 for (size_t k = 0, numEndpoints = descriptor->numEndpoints(); | |
| 514 k < numEndpoints; k++) { | |
| 515 scoped_refptr<const UsbEndpointDescriptor> endpoint | |
| 516 = descriptor->getEndpoint(k); | |
| 517 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor()); | |
| 518 | |
| 519 EndpointType type; | |
| 520 Direction direction; | |
| 521 SynchronizationType synchronization; | |
| 522 UsageType usage; | |
| 523 | |
| 524 if (!ConvertEndpointTypeSafely(endpoint->GetEndpointType(), &type) || | |
| 525 !ConvertDirectionSafely(endpoint->GetDirection(), &direction) || | |
| 526 !ConvertSynchronizationTypeSafely( | |
| 527 endpoint->GetSynchronizationType(), &synchronization) || | |
| 528 !ConvertUsageTypeSafely(endpoint->GetUsageType(), &usage)) { | |
| 529 AsyncWorkCompleted(); | |
| 530 return; | |
| 531 } | |
| 532 | |
| 533 endpoint_desc->address = endpoint->GetAddress(); | |
| 534 endpoint_desc->type = type; | |
| 535 endpoint_desc->direction = direction; | |
| 536 endpoint_desc->maximum_packet_size = endpoint->GetMaximumPacketSize(); | |
| 537 endpoint_desc->synchronization = synchronization; | |
| 538 endpoint_desc->usage = usage; | |
| 539 | |
| 540 int* polling_interval = new int; | |
| 541 endpoint_desc->polling_interval.reset(polling_interval); | |
| 542 *polling_interval = endpoint->GetPollingInterval(); | |
| 543 | |
| 544 endpoints.push_back(endpoint_desc); | |
| 545 } | |
| 546 | |
| 547 result_->Append(PopulateInterfaceDescriptor( | |
| 548 descriptor->GetInterfaceNumber(), | |
| 549 descriptor->GetAlternateSetting(), | |
| 550 descriptor->GetInterfaceClass(), | |
| 551 descriptor->GetInterfaceSubclass(), | |
| 552 descriptor->GetInterfaceProtocol(), | |
| 553 &endpoints)); | |
| 554 } | |
| 555 } | |
| 556 | |
| 557 SetResult(result_.release()); | |
| 558 AsyncWorkCompleted(); | |
| 559 } | |
| 560 | |
| 561 bool UsbListInterfacesFunction::ConvertDirectionSafely( | |
| 562 const UsbInterfaceDirection& input, | |
| 563 extensions::api::usb::Direction* output) { | |
| 564 const bool converted = ConvertDirectionToApi(input, output); | |
| 565 if (!converted) | |
| 566 SetError(kErrorConvertDirection); | |
| 567 return converted; | |
| 568 } | |
| 569 | |
| 570 bool UsbListInterfacesFunction::ConvertEndpointTypeSafely( | |
| 571 const UsbEndpointType& input, | |
| 572 extensions::api::usb::EndpointType* output) { | |
| 573 const bool converted = ConvertEndpointTypeToApi(input, output); | |
| 574 if (!converted) | |
| 575 SetError(kErrorConvertEndpointType); | |
| 576 return converted; | |
| 577 } | |
| 578 | |
| 579 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely( | |
| 580 const UsbSynchronizationType& input, | |
| 581 extensions::api::usb::SynchronizationType* output) { | |
| 582 const bool converted = ConvertSynchronizationTypeToApi(input, output); | |
| 583 if (!converted) | |
| 584 SetError(kErrorConvertSynchronizationType); | |
| 585 return converted; | |
| 586 } | |
| 587 | |
| 588 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( | |
| 589 const UsbUsageType& input, | |
| 590 extensions::api::usb::UsageType* output) { | |
| 591 const bool converted = ConvertUsageTypeToApi(input, output); | |
| 592 if (!converted) | |
| 593 SetError(kErrorConvertUsageType); | |
| 594 return converted; | |
| 595 } | |
| 596 | |
| 364 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} | 597 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} |
| 365 | 598 |
| 366 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} | 599 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} |
| 367 | 600 |
| 368 bool UsbCloseDeviceFunction::Prepare() { | 601 bool UsbCloseDeviceFunction::Prepare() { |
| 369 parameters_ = CloseDevice::Params::Create(*args_); | 602 parameters_ = CloseDevice::Params::Create(*args_); |
| 370 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 603 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 371 return true; | 604 return true; |
| 372 } | 605 } |
| 373 | 606 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 void UsbControlTransferFunction::AsyncWorkStart() { | 719 void UsbControlTransferFunction::AsyncWorkStart() { |
| 487 UsbDeviceResource* const device = GetUsbDeviceResource( | 720 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 488 parameters_->device.handle); | 721 parameters_->device.handle); |
| 489 if (!device) { | 722 if (!device) { |
| 490 CompleteWithError(kErrorNoDevice); | 723 CompleteWithError(kErrorNoDevice); |
| 491 return; | 724 return; |
| 492 } | 725 } |
| 493 | 726 |
| 494 const ControlTransferInfo& transfer = parameters_->transfer_info; | 727 const ControlTransferInfo& transfer = parameters_->transfer_info; |
| 495 | 728 |
| 496 UsbDevice::TransferDirection direction; | 729 UsbInterfaceDirection direction; |
| 497 UsbDevice::TransferRequestType request_type; | 730 UsbDevice::TransferRequestType request_type; |
| 498 UsbDevice::TransferRecipient recipient; | 731 UsbDevice::TransferRecipient recipient; |
| 499 size_t size = 0; | 732 size_t size = 0; |
| 500 | 733 |
| 501 if (!ConvertDirectionSafely(transfer.direction, &direction) || | 734 if (!ConvertDirectionSafely(transfer.direction, &direction) || |
| 502 !ConvertRequestTypeSafely(transfer.request_type, &request_type) || | 735 !ConvertRequestTypeSafely(transfer.request_type, &request_type) || |
| 503 !ConvertRecipientSafely(transfer.recipient, &recipient)) { | 736 !ConvertRecipientSafely(transfer.recipient, &recipient)) { |
| 504 AsyncWorkCompleted(); | 737 AsyncWorkCompleted(); |
| 505 return; | 738 return; |
| 506 } | 739 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 535 void UsbBulkTransferFunction::AsyncWorkStart() { | 768 void UsbBulkTransferFunction::AsyncWorkStart() { |
| 536 UsbDeviceResource* const device = GetUsbDeviceResource( | 769 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 537 parameters_->device.handle); | 770 parameters_->device.handle); |
| 538 if (!device) { | 771 if (!device) { |
| 539 CompleteWithError(kErrorNoDevice); | 772 CompleteWithError(kErrorNoDevice); |
| 540 return; | 773 return; |
| 541 } | 774 } |
| 542 | 775 |
| 543 const GenericTransferInfo& transfer = parameters_->transfer_info; | 776 const GenericTransferInfo& transfer = parameters_->transfer_info; |
| 544 | 777 |
| 545 UsbDevice::TransferDirection direction; | 778 UsbInterfaceDirection direction; |
| 546 size_t size = 0; | 779 size_t size = 0; |
| 547 | 780 |
| 548 if (!ConvertDirectionSafely(transfer.direction, &direction)) { | 781 if (!ConvertDirectionSafely(transfer.direction, &direction)) { |
| 549 AsyncWorkCompleted(); | 782 AsyncWorkCompleted(); |
| 550 return; | 783 return; |
| 551 } | 784 } |
| 552 | 785 |
| 553 if (!GetTransferSize(transfer, &size)) { | 786 if (!GetTransferSize(transfer, &size)) { |
| 554 CompleteWithError(kErrorInvalidTransferLength); | 787 CompleteWithError(kErrorInvalidTransferLength); |
| 555 return; | 788 return; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 579 void UsbInterruptTransferFunction::AsyncWorkStart() { | 812 void UsbInterruptTransferFunction::AsyncWorkStart() { |
| 580 UsbDeviceResource* const device = GetUsbDeviceResource( | 813 UsbDeviceResource* const device = GetUsbDeviceResource( |
| 581 parameters_->device.handle); | 814 parameters_->device.handle); |
| 582 if (!device) { | 815 if (!device) { |
| 583 CompleteWithError(kErrorNoDevice); | 816 CompleteWithError(kErrorNoDevice); |
| 584 return; | 817 return; |
| 585 } | 818 } |
| 586 | 819 |
| 587 const GenericTransferInfo& transfer = parameters_->transfer_info; | 820 const GenericTransferInfo& transfer = parameters_->transfer_info; |
| 588 | 821 |
| 589 UsbDevice::TransferDirection direction; | 822 UsbInterfaceDirection direction; |
| 590 size_t size = 0; | 823 size_t size = 0; |
| 591 | 824 |
| 592 if (!ConvertDirectionSafely(transfer.direction, &direction)) { | 825 if (!ConvertDirectionSafely(transfer.direction, &direction)) { |
| 593 AsyncWorkCompleted(); | 826 AsyncWorkCompleted(); |
| 594 return; | 827 return; |
| 595 } | 828 } |
| 596 | 829 |
| 597 if (!GetTransferSize(transfer, &size)) { | 830 if (!GetTransferSize(transfer, &size)) { |
| 598 CompleteWithError(kErrorInvalidTransferLength); | 831 CompleteWithError(kErrorInvalidTransferLength); |
| 599 return; | 832 return; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 625 parameters_->device.handle); | 858 parameters_->device.handle); |
| 626 if (!device) { | 859 if (!device) { |
| 627 CompleteWithError(kErrorNoDevice); | 860 CompleteWithError(kErrorNoDevice); |
| 628 return; | 861 return; |
| 629 } | 862 } |
| 630 | 863 |
| 631 const IsochronousTransferInfo& transfer = parameters_->transfer_info; | 864 const IsochronousTransferInfo& transfer = parameters_->transfer_info; |
| 632 const GenericTransferInfo& generic_transfer = transfer.transfer_info; | 865 const GenericTransferInfo& generic_transfer = transfer.transfer_info; |
| 633 | 866 |
| 634 size_t size = 0; | 867 size_t size = 0; |
| 635 UsbDevice::TransferDirection direction; | 868 UsbInterfaceDirection direction; |
| 636 | 869 |
| 637 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) { | 870 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) { |
| 638 AsyncWorkCompleted(); | 871 AsyncWorkCompleted(); |
| 639 return; | 872 return; |
| 640 } | 873 } |
| 641 if (!GetTransferSize(generic_transfer, &size)) { | 874 if (!GetTransferSize(generic_transfer, &size)) { |
| 642 CompleteWithError(kErrorInvalidTransferLength); | 875 CompleteWithError(kErrorInvalidTransferLength); |
| 643 return; | 876 return; |
| 644 } | 877 } |
| 645 if (transfer.packets < 0 || transfer.packets >= kMaxPackets) { | 878 if (transfer.packets < 0 || transfer.packets >= kMaxPackets) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 665 CompleteWithError(kErrorMalformedParameters); | 898 CompleteWithError(kErrorMalformedParameters); |
| 666 return; | 899 return; |
| 667 } | 900 } |
| 668 | 901 |
| 669 device->device()->IsochronousTransfer(direction, generic_transfer.endpoint, | 902 device->device()->IsochronousTransfer(direction, generic_transfer.endpoint, |
| 670 buffer, size, packets, packet_length, 0, base::Bind( | 903 buffer, size, packets, packet_length, 0, base::Bind( |
| 671 &UsbIsochronousTransferFunction::OnCompleted, this)); | 904 &UsbIsochronousTransferFunction::OnCompleted, this)); |
| 672 } | 905 } |
| 673 | 906 |
| 674 } // namespace extensions | 907 } // namespace extensions |
| OLD | NEW |