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::UsbDeviceFilter; | 55 using device::UsbDeviceFilter; |
56 using usb_service::UsbDeviceHandle; | 56 using device::UsbDeviceHandle; |
57 using usb_service::UsbEndpointDescriptor; | 57 using device::UsbEndpointDescriptor; |
58 using usb_service::UsbEndpointDirection; | 58 using device::UsbEndpointDirection; |
59 using usb_service::UsbInterfaceAltSettingDescriptor; | 59 using device::UsbInterfaceAltSettingDescriptor; |
60 using usb_service::UsbInterfaceDescriptor; | 60 using device::UsbInterfaceDescriptor; |
61 using usb_service::UsbService; | 61 using device::UsbService; |
62 using usb_service::UsbSynchronizationType; | 62 using device::UsbSynchronizationType; |
63 using usb_service::UsbTransferStatus; | 63 using device::UsbTransferStatus; |
64 using usb_service::UsbTransferType; | 64 using device::UsbTransferType; |
65 using usb_service::UsbUsageType; | 65 using device::UsbUsageType; |
66 | 66 |
67 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; | 67 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; |
68 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; | 68 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; |
69 | 69 |
70 namespace { | 70 namespace { |
71 | 71 |
72 const char kDataKey[] = "data"; | 72 const char kDataKey[] = "data"; |
73 const char kResultCodeKey[] = "resultCode"; | 73 const char kResultCodeKey[] = "resultCode"; |
74 | 74 |
75 const char kErrorInitService[] = "Failed to initialize USB service."; | 75 const char kErrorInitService[] = "Failed to initialize USB service."; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 const char kErrorResetDevice[] = | 109 const char kErrorResetDevice[] = |
110 "Error resetting the device. The device has been closed."; | 110 "Error resetting the device. The device has been closed."; |
111 | 111 |
112 const size_t kMaxTransferLength = 100 * 1024 * 1024; | 112 const size_t kMaxTransferLength = 100 * 1024 * 1024; |
113 const int kMaxPackets = 4 * 1024 * 1024; | 113 const int kMaxPackets = 4 * 1024 * 1024; |
114 const int kMaxPacketLength = 64 * 1024; | 114 const int kMaxPacketLength = 64 * 1024; |
115 | 115 |
116 bool ConvertDirectionToApi(const UsbEndpointDirection& input, | 116 bool ConvertDirectionToApi(const UsbEndpointDirection& input, |
117 Direction* output) { | 117 Direction* output) { |
118 switch (input) { | 118 switch (input) { |
119 case usb_service::USB_DIRECTION_INBOUND: | 119 case device::USB_DIRECTION_INBOUND: |
120 *output = usb::DIRECTION_IN; | 120 *output = usb::DIRECTION_IN; |
121 return true; | 121 return true; |
122 case usb_service::USB_DIRECTION_OUTBOUND: | 122 case device::USB_DIRECTION_OUTBOUND: |
123 *output = usb::DIRECTION_OUT; | 123 *output = usb::DIRECTION_OUT; |
124 return true; | 124 return true; |
125 default: | 125 default: |
126 NOTREACHED(); | 126 NOTREACHED(); |
127 return false; | 127 return false; |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, | 131 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, |
132 usb::SynchronizationType* output) { | 132 usb::SynchronizationType* output) { |
133 switch (input) { | 133 switch (input) { |
134 case usb_service::USB_SYNCHRONIZATION_NONE: | 134 case device::USB_SYNCHRONIZATION_NONE: |
135 *output = usb::SYNCHRONIZATION_TYPE_NONE; | 135 *output = usb::SYNCHRONIZATION_TYPE_NONE; |
136 return true; | 136 return true; |
137 case usb_service::USB_SYNCHRONIZATION_ASYNCHRONOUS: | 137 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS: |
138 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | 138 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; |
139 return true; | 139 return true; |
140 case usb_service::USB_SYNCHRONIZATION_ADAPTIVE: | 140 case device::USB_SYNCHRONIZATION_ADAPTIVE: |
141 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | 141 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; |
142 return true; | 142 return true; |
143 case usb_service::USB_SYNCHRONIZATION_SYNCHRONOUS: | 143 case device::USB_SYNCHRONIZATION_SYNCHRONOUS: |
144 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | 144 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; |
145 return true; | 145 return true; |
146 default: | 146 default: |
147 NOTREACHED(); | 147 NOTREACHED(); |
148 return false; | 148 return false; |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 bool ConvertTransferTypeToApi(const UsbTransferType& input, | 152 bool ConvertTransferTypeToApi(const UsbTransferType& input, |
153 usb::TransferType* output) { | 153 usb::TransferType* output) { |
154 switch (input) { | 154 switch (input) { |
155 case usb_service::USB_TRANSFER_CONTROL: | 155 case device::USB_TRANSFER_CONTROL: |
156 *output = usb::TRANSFER_TYPE_CONTROL; | 156 *output = usb::TRANSFER_TYPE_CONTROL; |
157 return true; | 157 return true; |
158 case usb_service::USB_TRANSFER_INTERRUPT: | 158 case device::USB_TRANSFER_INTERRUPT: |
159 *output = usb::TRANSFER_TYPE_INTERRUPT; | 159 *output = usb::TRANSFER_TYPE_INTERRUPT; |
160 return true; | 160 return true; |
161 case usb_service::USB_TRANSFER_ISOCHRONOUS: | 161 case device::USB_TRANSFER_ISOCHRONOUS: |
162 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; | 162 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; |
163 return true; | 163 return true; |
164 case usb_service::USB_TRANSFER_BULK: | 164 case device::USB_TRANSFER_BULK: |
165 *output = usb::TRANSFER_TYPE_BULK; | 165 *output = usb::TRANSFER_TYPE_BULK; |
166 return true; | 166 return true; |
167 default: | 167 default: |
168 NOTREACHED(); | 168 NOTREACHED(); |
169 return false; | 169 return false; |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { | 173 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { |
174 switch (input) { | 174 switch (input) { |
175 case usb_service::USB_USAGE_DATA: | 175 case device::USB_USAGE_DATA: |
176 *output = usb::USAGE_TYPE_DATA; | 176 *output = usb::USAGE_TYPE_DATA; |
177 return true; | 177 return true; |
178 case usb_service::USB_USAGE_FEEDBACK: | 178 case device::USB_USAGE_FEEDBACK: |
179 *output = usb::USAGE_TYPE_FEEDBACK; | 179 *output = usb::USAGE_TYPE_FEEDBACK; |
180 return true; | 180 return true; |
181 case usb_service::USB_USAGE_EXPLICIT_FEEDBACK: | 181 case device::USB_USAGE_EXPLICIT_FEEDBACK: |
182 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | 182 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; |
183 return true; | 183 return true; |
184 default: | 184 default: |
185 NOTREACHED(); | 185 NOTREACHED(); |
186 return false; | 186 return false; |
187 } | 187 } |
188 } | 188 } |
189 | 189 |
190 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { | 190 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { |
191 switch (input) { | 191 switch (input) { |
192 case usb::DIRECTION_IN: | 192 case usb::DIRECTION_IN: |
193 *output = usb_service::USB_DIRECTION_INBOUND; | 193 *output = device::USB_DIRECTION_INBOUND; |
194 return true; | 194 return true; |
195 case usb::DIRECTION_OUT: | 195 case usb::DIRECTION_OUT: |
196 *output = usb_service::USB_DIRECTION_OUTBOUND; | 196 *output = device::USB_DIRECTION_OUTBOUND; |
197 return true; | 197 return true; |
198 default: | 198 default: |
199 NOTREACHED(); | 199 NOTREACHED(); |
200 return false; | 200 return false; |
201 } | 201 } |
202 } | 202 } |
203 | 203 |
204 bool ConvertRequestType(const RequestType& input, | 204 bool ConvertRequestType(const RequestType& input, |
205 UsbDeviceHandle::TransferRequestType* output) { | 205 UsbDeviceHandle::TransferRequestType* output) { |
206 switch (input) { | 206 switch (input) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 size_t size) { | 268 size_t size) { |
269 if (size >= kMaxTransferLength) | 269 if (size >= kMaxTransferLength) |
270 return NULL; | 270 return NULL; |
271 | 271 |
272 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 272 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
273 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 273 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
274 // cannot represent a zero-length buffer, while an URB can. | 274 // cannot represent a zero-length buffer, while an URB can. |
275 scoped_refptr<net::IOBuffer> buffer = | 275 scoped_refptr<net::IOBuffer> buffer = |
276 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); | 276 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); |
277 | 277 |
278 if (direction == usb_service::USB_DIRECTION_INBOUND) { | 278 if (direction == device::USB_DIRECTION_INBOUND) { |
279 return buffer; | 279 return buffer; |
280 } else if (direction == usb_service::USB_DIRECTION_OUTBOUND) { | 280 } else if (direction == device::USB_DIRECTION_OUTBOUND) { |
281 if (input.data.get() && size <= input.data->size()) { | 281 if (input.data.get() && size <= input.data->size()) { |
282 memcpy(buffer->data(), input.data->data(), size); | 282 memcpy(buffer->data(), input.data->data(), size); |
283 return buffer; | 283 return buffer; |
284 } | 284 } |
285 } | 285 } |
286 NOTREACHED(); | 286 NOTREACHED(); |
287 return NULL; | 287 return NULL; |
288 } | 288 } |
289 | 289 |
290 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { | 290 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { |
291 switch (status) { | 291 switch (status) { |
292 case usb_service::USB_TRANSFER_COMPLETED: | 292 case device::USB_TRANSFER_COMPLETED: |
293 return ""; | 293 return ""; |
294 case usb_service::USB_TRANSFER_ERROR: | 294 case device::USB_TRANSFER_ERROR: |
295 return kErrorGeneric; | 295 return kErrorGeneric; |
296 case usb_service::USB_TRANSFER_TIMEOUT: | 296 case device::USB_TRANSFER_TIMEOUT: |
297 return kErrorTimeout; | 297 return kErrorTimeout; |
298 case usb_service::USB_TRANSFER_CANCELLED: | 298 case device::USB_TRANSFER_CANCELLED: |
299 return kErrorCancelled; | 299 return kErrorCancelled; |
300 case usb_service::USB_TRANSFER_STALLED: | 300 case device::USB_TRANSFER_STALLED: |
301 return kErrorStalled; | 301 return kErrorStalled; |
302 case usb_service::USB_TRANSFER_DISCONNECT: | 302 case device::USB_TRANSFER_DISCONNECT: |
303 return kErrorDisconnect; | 303 return kErrorDisconnect; |
304 case usb_service::USB_TRANSFER_OVERFLOW: | 304 case device::USB_TRANSFER_OVERFLOW: |
305 return kErrorOverflow; | 305 return kErrorOverflow; |
306 case usb_service::USB_TRANSFER_LENGTH_SHORT: | 306 case device::USB_TRANSFER_LENGTH_SHORT: |
307 return kErrorTransferLength; | 307 return kErrorTransferLength; |
308 default: | 308 default: |
309 NOTREACHED(); | 309 NOTREACHED(); |
310 return ""; | 310 return ""; |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 #if defined(OS_CHROMEOS) | 314 #if defined(OS_CHROMEOS) |
315 void RequestUsbDevicesAccessHelper( | 315 void RequestUsbDevicesAccessHelper( |
316 ScopedDeviceVector devices, | 316 ScopedDeviceVector devices, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 output->SetInterfaceClass(*input.interface_class); | 431 output->SetInterfaceClass(*input.interface_class); |
432 } | 432 } |
433 if (input.interface_subclass) { | 433 if (input.interface_subclass) { |
434 output->SetInterfaceSubclass(*input.interface_subclass); | 434 output->SetInterfaceSubclass(*input.interface_subclass); |
435 } | 435 } |
436 if (input.interface_protocol) { | 436 if (input.interface_protocol) { |
437 output->SetInterfaceProtocol(*input.interface_protocol); | 437 output->SetInterfaceProtocol(*input.interface_protocol); |
438 } | 438 } |
439 } | 439 } |
440 | 440 |
441 bool UsbAsyncApiFunction::HasDevicePermission( | 441 bool UsbAsyncApiFunction::HasDevicePermission(scoped_refptr<UsbDevice> device) { |
442 scoped_refptr<usb_service::UsbDevice> device) { | |
443 UsbDevicePermission::CheckParam param( | 442 UsbDevicePermission::CheckParam param( |
444 device->vendor_id(), | 443 device->vendor_id(), |
445 device->product_id(), | 444 device->product_id(), |
446 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 445 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); |
447 return extension()->permissions_data()->CheckAPIPermissionWithParam( | 446 return extension()->permissions_data()->CheckAPIPermissionWithParam( |
448 APIPermission::kUsbDevice, ¶m); | 447 APIPermission::kUsbDevice, ¶m); |
449 } | 448 } |
450 | 449 |
451 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError( | 450 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError( |
452 const Device& input_device) { | 451 const Device& input_device) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 | 509 |
511 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { | 510 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { |
512 } | 511 } |
513 | 512 |
514 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { | 513 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { |
515 } | 514 } |
516 | 515 |
517 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, | 516 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, |
518 scoped_refptr<net::IOBuffer> data, | 517 scoped_refptr<net::IOBuffer> data, |
519 size_t length) { | 518 size_t length) { |
520 if (status != usb_service::USB_TRANSFER_COMPLETED) | 519 if (status != device::USB_TRANSFER_COMPLETED) |
521 SetError(ConvertTransferStatusToErrorString(status)); | 520 SetError(ConvertTransferStatusToErrorString(status)); |
522 | 521 |
523 SetResult(CreateTransferInfo(status, data, length)); | 522 SetResult(CreateTransferInfo(status, data, length)); |
524 AsyncWorkCompleted(); | 523 AsyncWorkCompleted(); |
525 } | 524 } |
526 | 525 |
527 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( | 526 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( |
528 const Direction& input, | 527 const Direction& input, |
529 UsbEndpointDirection* output) { | 528 UsbEndpointDirection* output) { |
530 const bool converted = ConvertDirection(input, output); | 529 const bool converted = ConvertDirection(input, output); |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1217 SetResult(new base::FundamentalValue(false)); | 1216 SetResult(new base::FundamentalValue(false)); |
1218 CompleteWithError(kErrorResetDevice); | 1217 CompleteWithError(kErrorResetDevice); |
1219 return; | 1218 return; |
1220 } | 1219 } |
1221 | 1220 |
1222 SetResult(new base::FundamentalValue(true)); | 1221 SetResult(new base::FundamentalValue(true)); |
1223 AsyncWorkCompleted(); | 1222 AsyncWorkCompleted(); |
1224 } | 1223 } |
1225 | 1224 |
1226 } // namespace extensions | 1225 } // namespace extensions |
OLD | NEW |