| 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 "components/usb_service/usb_device_handle.h" | |
| 13 #include "components/usb_service/usb_service.h" | |
| 14 #include "device/core/device_client.h" | 12 #include "device/core/device_client.h" |
| 13 #include "device/usb/usb_device_handle.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; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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; |
| 50 using usb::SynchronizationType; | 50 using usb::SynchronizationType; |
| 51 using usb::TransferType; | 51 using usb::TransferType; |
| 52 using usb::UsageType; | 52 using usb::UsageType; |
| 53 using usb_service::UsbConfigDescriptor; | 53 using device::UsbConfigDescriptor; |
| 54 using usb_service::UsbDevice; | 54 using device::UsbDevice; |
| 55 using usb_service::UsbDeviceHandle; | 55 using device::UsbDeviceHandle; |
| 56 using usb_service::UsbEndpointDescriptor; | 56 using device::UsbEndpointDescriptor; |
| 57 using usb_service::UsbEndpointDirection; | 57 using device::UsbEndpointDirection; |
| 58 using usb_service::UsbInterfaceAltSettingDescriptor; | 58 using device::UsbInterfaceAltSettingDescriptor; |
| 59 using usb_service::UsbInterfaceDescriptor; | 59 using device::UsbInterfaceDescriptor; |
| 60 using usb_service::UsbService; | 60 using device::UsbService; |
| 61 using usb_service::UsbSynchronizationType; | 61 using device::UsbSynchronizationType; |
| 62 using usb_service::UsbTransferStatus; | 62 using device::UsbTransferStatus; |
| 63 using usb_service::UsbTransferType; | 63 using device::UsbTransferType; |
| 64 using usb_service::UsbUsageType; | 64 using device::UsbUsageType; |
| 65 | 65 |
| 66 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; | 66 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; |
| 67 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; | 67 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; |
| 68 | 68 |
| 69 namespace { | 69 namespace { |
| 70 | 70 |
| 71 const char kDataKey[] = "data"; | 71 const char kDataKey[] = "data"; |
| 72 const char kResultCodeKey[] = "resultCode"; | 72 const char kResultCodeKey[] = "resultCode"; |
| 73 | 73 |
| 74 const char kErrorInitService[] = "Failed to initialize USB service."; | 74 const char kErrorInitService[] = "Failed to initialize USB service."; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 const char kErrorResetDevice[] = | 108 const char kErrorResetDevice[] = |
| 109 "Error resetting the device. The device has been closed."; | 109 "Error resetting the device. The device has been closed."; |
| 110 | 110 |
| 111 const size_t kMaxTransferLength = 100 * 1024 * 1024; | 111 const size_t kMaxTransferLength = 100 * 1024 * 1024; |
| 112 const int kMaxPackets = 4 * 1024 * 1024; | 112 const int kMaxPackets = 4 * 1024 * 1024; |
| 113 const int kMaxPacketLength = 64 * 1024; | 113 const int kMaxPacketLength = 64 * 1024; |
| 114 | 114 |
| 115 bool ConvertDirectionToApi(const UsbEndpointDirection& input, | 115 bool ConvertDirectionToApi(const UsbEndpointDirection& input, |
| 116 Direction* output) { | 116 Direction* output) { |
| 117 switch (input) { | 117 switch (input) { |
| 118 case usb_service::USB_DIRECTION_INBOUND: | 118 case device::USB_DIRECTION_INBOUND: |
| 119 *output = usb::DIRECTION_IN; | 119 *output = usb::DIRECTION_IN; |
| 120 return true; | 120 return true; |
| 121 case usb_service::USB_DIRECTION_OUTBOUND: | 121 case device::USB_DIRECTION_OUTBOUND: |
| 122 *output = usb::DIRECTION_OUT; | 122 *output = usb::DIRECTION_OUT; |
| 123 return true; | 123 return true; |
| 124 default: | 124 default: |
| 125 NOTREACHED(); | 125 NOTREACHED(); |
| 126 return false; | 126 return false; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, | 130 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, |
| 131 usb::SynchronizationType* output) { | 131 usb::SynchronizationType* output) { |
| 132 switch (input) { | 132 switch (input) { |
| 133 case usb_service::USB_SYNCHRONIZATION_NONE: | 133 case device::USB_SYNCHRONIZATION_NONE: |
| 134 *output = usb::SYNCHRONIZATION_TYPE_NONE; | 134 *output = usb::SYNCHRONIZATION_TYPE_NONE; |
| 135 return true; | 135 return true; |
| 136 case usb_service::USB_SYNCHRONIZATION_ASYNCHRONOUS: | 136 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS: |
| 137 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | 137 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; |
| 138 return true; | 138 return true; |
| 139 case usb_service::USB_SYNCHRONIZATION_ADAPTIVE: | 139 case device::USB_SYNCHRONIZATION_ADAPTIVE: |
| 140 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | 140 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; |
| 141 return true; | 141 return true; |
| 142 case usb_service::USB_SYNCHRONIZATION_SYNCHRONOUS: | 142 case device::USB_SYNCHRONIZATION_SYNCHRONOUS: |
| 143 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | 143 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; |
| 144 return true; | 144 return true; |
| 145 default: | 145 default: |
| 146 NOTREACHED(); | 146 NOTREACHED(); |
| 147 return false; | 147 return false; |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 bool ConvertTransferTypeToApi(const UsbTransferType& input, | 151 bool ConvertTransferTypeToApi(const UsbTransferType& input, |
| 152 usb::TransferType* output) { | 152 usb::TransferType* output) { |
| 153 switch (input) { | 153 switch (input) { |
| 154 case usb_service::USB_TRANSFER_CONTROL: | 154 case device::USB_TRANSFER_CONTROL: |
| 155 *output = usb::TRANSFER_TYPE_CONTROL; | 155 *output = usb::TRANSFER_TYPE_CONTROL; |
| 156 return true; | 156 return true; |
| 157 case usb_service::USB_TRANSFER_INTERRUPT: | 157 case device::USB_TRANSFER_INTERRUPT: |
| 158 *output = usb::TRANSFER_TYPE_INTERRUPT; | 158 *output = usb::TRANSFER_TYPE_INTERRUPT; |
| 159 return true; | 159 return true; |
| 160 case usb_service::USB_TRANSFER_ISOCHRONOUS: | 160 case device::USB_TRANSFER_ISOCHRONOUS: |
| 161 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; | 161 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; |
| 162 return true; | 162 return true; |
| 163 case usb_service::USB_TRANSFER_BULK: | 163 case device::USB_TRANSFER_BULK: |
| 164 *output = usb::TRANSFER_TYPE_BULK; | 164 *output = usb::TRANSFER_TYPE_BULK; |
| 165 return true; | 165 return true; |
| 166 default: | 166 default: |
| 167 NOTREACHED(); | 167 NOTREACHED(); |
| 168 return false; | 168 return false; |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { | 172 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { |
| 173 switch (input) { | 173 switch (input) { |
| 174 case usb_service::USB_USAGE_DATA: | 174 case device::USB_USAGE_DATA: |
| 175 *output = usb::USAGE_TYPE_DATA; | 175 *output = usb::USAGE_TYPE_DATA; |
| 176 return true; | 176 return true; |
| 177 case usb_service::USB_USAGE_FEEDBACK: | 177 case device::USB_USAGE_FEEDBACK: |
| 178 *output = usb::USAGE_TYPE_FEEDBACK; | 178 *output = usb::USAGE_TYPE_FEEDBACK; |
| 179 return true; | 179 return true; |
| 180 case usb_service::USB_USAGE_EXPLICIT_FEEDBACK: | 180 case device::USB_USAGE_EXPLICIT_FEEDBACK: |
| 181 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | 181 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; |
| 182 return true; | 182 return true; |
| 183 default: | 183 default: |
| 184 NOTREACHED(); | 184 NOTREACHED(); |
| 185 return false; | 185 return false; |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { | 189 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { |
| 190 switch (input) { | 190 switch (input) { |
| 191 case usb::DIRECTION_IN: | 191 case usb::DIRECTION_IN: |
| 192 *output = usb_service::USB_DIRECTION_INBOUND; | 192 *output = device::USB_DIRECTION_INBOUND; |
| 193 return true; | 193 return true; |
| 194 case usb::DIRECTION_OUT: | 194 case usb::DIRECTION_OUT: |
| 195 *output = usb_service::USB_DIRECTION_OUTBOUND; | 195 *output = device::USB_DIRECTION_OUTBOUND; |
| 196 return true; | 196 return true; |
| 197 default: | 197 default: |
| 198 NOTREACHED(); | 198 NOTREACHED(); |
| 199 return false; | 199 return false; |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool ConvertRequestType(const RequestType& input, | 203 bool ConvertRequestType(const RequestType& input, |
| 204 UsbDeviceHandle::TransferRequestType* output) { | 204 UsbDeviceHandle::TransferRequestType* output) { |
| 205 switch (input) { | 205 switch (input) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 size_t size) { | 267 size_t size) { |
| 268 if (size >= kMaxTransferLength) | 268 if (size >= kMaxTransferLength) |
| 269 return NULL; | 269 return NULL; |
| 270 | 270 |
| 271 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 271 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
| 272 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 272 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
| 273 // cannot represent a zero-length buffer, while an URB can. | 273 // cannot represent a zero-length buffer, while an URB can. |
| 274 scoped_refptr<net::IOBuffer> buffer = | 274 scoped_refptr<net::IOBuffer> buffer = |
| 275 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); | 275 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); |
| 276 | 276 |
| 277 if (direction == usb_service::USB_DIRECTION_INBOUND) { | 277 if (direction == device::USB_DIRECTION_INBOUND) { |
| 278 return buffer; | 278 return buffer; |
| 279 } else if (direction == usb_service::USB_DIRECTION_OUTBOUND) { | 279 } else if (direction == device::USB_DIRECTION_OUTBOUND) { |
| 280 if (input.data.get() && size <= input.data->size()) { | 280 if (input.data.get() && size <= input.data->size()) { |
| 281 memcpy(buffer->data(), input.data->data(), size); | 281 memcpy(buffer->data(), input.data->data(), size); |
| 282 return buffer; | 282 return buffer; |
| 283 } | 283 } |
| 284 } | 284 } |
| 285 NOTREACHED(); | 285 NOTREACHED(); |
| 286 return NULL; | 286 return NULL; |
| 287 } | 287 } |
| 288 | 288 |
| 289 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { | 289 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { |
| 290 switch (status) { | 290 switch (status) { |
| 291 case usb_service::USB_TRANSFER_COMPLETED: | 291 case device::USB_TRANSFER_COMPLETED: |
| 292 return ""; | 292 return ""; |
| 293 case usb_service::USB_TRANSFER_ERROR: | 293 case device::USB_TRANSFER_ERROR: |
| 294 return kErrorGeneric; | 294 return kErrorGeneric; |
| 295 case usb_service::USB_TRANSFER_TIMEOUT: | 295 case device::USB_TRANSFER_TIMEOUT: |
| 296 return kErrorTimeout; | 296 return kErrorTimeout; |
| 297 case usb_service::USB_TRANSFER_CANCELLED: | 297 case device::USB_TRANSFER_CANCELLED: |
| 298 return kErrorCancelled; | 298 return kErrorCancelled; |
| 299 case usb_service::USB_TRANSFER_STALLED: | 299 case device::USB_TRANSFER_STALLED: |
| 300 return kErrorStalled; | 300 return kErrorStalled; |
| 301 case usb_service::USB_TRANSFER_DISCONNECT: | 301 case device::USB_TRANSFER_DISCONNECT: |
| 302 return kErrorDisconnect; | 302 return kErrorDisconnect; |
| 303 case usb_service::USB_TRANSFER_OVERFLOW: | 303 case device::USB_TRANSFER_OVERFLOW: |
| 304 return kErrorOverflow; | 304 return kErrorOverflow; |
| 305 case usb_service::USB_TRANSFER_LENGTH_SHORT: | 305 case device::USB_TRANSFER_LENGTH_SHORT: |
| 306 return kErrorTransferLength; | 306 return kErrorTransferLength; |
| 307 default: | 307 default: |
| 308 NOTREACHED(); | 308 NOTREACHED(); |
| 309 return ""; | 309 return ""; |
| 310 } | 310 } |
| 311 } | 311 } |
| 312 | 312 |
| 313 #if defined(OS_CHROMEOS) | 313 #if defined(OS_CHROMEOS) |
| 314 void RequestUsbDevicesAccessHelper( | 314 void RequestUsbDevicesAccessHelper( |
| 315 ScopedDeviceVector devices, | 315 ScopedDeviceVector devices, |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 | 493 |
| 494 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { | 494 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { |
| 495 } | 495 } |
| 496 | 496 |
| 497 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { | 497 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { |
| 498 } | 498 } |
| 499 | 499 |
| 500 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, | 500 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, |
| 501 scoped_refptr<net::IOBuffer> data, | 501 scoped_refptr<net::IOBuffer> data, |
| 502 size_t length) { | 502 size_t length) { |
| 503 if (status != usb_service::USB_TRANSFER_COMPLETED) | 503 if (status != device::USB_TRANSFER_COMPLETED) |
| 504 SetError(ConvertTransferStatusToErrorString(status)); | 504 SetError(ConvertTransferStatusToErrorString(status)); |
| 505 | 505 |
| 506 SetResult(CreateTransferInfo(status, data, length)); | 506 SetResult(CreateTransferInfo(status, data, length)); |
| 507 AsyncWorkCompleted(); | 507 AsyncWorkCompleted(); |
| 508 } | 508 } |
| 509 | 509 |
| 510 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( | 510 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( |
| 511 const Direction& input, | 511 const Direction& input, |
| 512 UsbEndpointDirection* output) { | 512 UsbEndpointDirection* output) { |
| 513 const bool converted = ConvertDirection(input, output); | 513 const bool converted = ConvertDirection(input, output); |
| (...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 SetResult(new base::FundamentalValue(false)); | 1200 SetResult(new base::FundamentalValue(false)); |
| 1201 CompleteWithError(kErrorResetDevice); | 1201 CompleteWithError(kErrorResetDevice); |
| 1202 return; | 1202 return; |
| 1203 } | 1203 } |
| 1204 | 1204 |
| 1205 SetResult(new base::FundamentalValue(true)); | 1205 SetResult(new base::FundamentalValue(true)); |
| 1206 AsyncWorkCompleted(); | 1206 AsyncWorkCompleted(); |
| 1207 } | 1207 } |
| 1208 | 1208 |
| 1209 } // namespace extensions | 1209 } // namespace extensions |
| OLD | NEW |