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 "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 "chrome/browser/extensions/api/usb/usb_device_resource.h" | |
13 #include "chrome/common/extensions/api/usb.h" | |
14 #include "components/usb_service/usb_device_handle.h" | 12 #include "components/usb_service/usb_device_handle.h" |
15 #include "components/usb_service/usb_service.h" | 13 #include "components/usb_service/usb_service.h" |
14 #include "extensions/browser/api/usb/usb_device_resource.h" | |
16 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
16 #include "extensions/common/api/usb.h" | |
17 #include "extensions/common/permissions/permissions_data.h" | 17 #include "extensions/common/permissions/permissions_data.h" |
18 #include "extensions/common/permissions/usb_device_permission.h" | 18 #include "extensions/common/permissions/usb_device_permission.h" |
19 | 19 |
20 namespace usb = extensions::api::usb; | 20 namespace usb = extensions::core_api::usb; |
21 namespace BulkTransfer = usb::BulkTransfer; | 21 namespace BulkTransfer = usb::BulkTransfer; |
22 namespace ClaimInterface = usb::ClaimInterface; | 22 namespace ClaimInterface = usb::ClaimInterface; |
23 namespace CloseDevice = usb::CloseDevice; | 23 namespace CloseDevice = usb::CloseDevice; |
24 namespace ControlTransfer = usb::ControlTransfer; | 24 namespace ControlTransfer = usb::ControlTransfer; |
25 namespace FindDevices = usb::FindDevices; | 25 namespace FindDevices = usb::FindDevices; |
26 namespace GetDevices = usb::GetDevices; | 26 namespace GetDevices = usb::GetDevices; |
27 namespace InterruptTransfer = usb::InterruptTransfer; | 27 namespace InterruptTransfer = usb::InterruptTransfer; |
28 namespace IsochronousTransfer = usb::IsochronousTransfer; | 28 namespace IsochronousTransfer = usb::IsochronousTransfer; |
29 namespace ListInterfaces = usb::ListInterfaces; | 29 namespace ListInterfaces = usb::ListInterfaces; |
30 namespace OpenDevice = usb::OpenDevice; | 30 namespace OpenDevice = usb::OpenDevice; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 const char kErrorCannotSetInterfaceAlternateSetting[] = | 90 const char kErrorCannotSetInterfaceAlternateSetting[] = |
91 "Error setting alternate interface setting."; | 91 "Error setting alternate interface setting."; |
92 const char kErrorConvertDirection[] = "Invalid transfer direction."; | 92 const char kErrorConvertDirection[] = "Invalid transfer direction."; |
93 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; | 93 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; |
94 const char kErrorConvertRequestType[] = "Invalid request type."; | 94 const char kErrorConvertRequestType[] = "Invalid request type."; |
95 const char kErrorConvertSynchronizationType[] = "Invalid synchronization type"; | 95 const char kErrorConvertSynchronizationType[] = "Invalid synchronization type"; |
96 const char kErrorConvertTransferType[] = "Invalid endpoint type."; | 96 const char kErrorConvertTransferType[] = "Invalid endpoint type."; |
97 const char kErrorConvertUsageType[] = "Invalid usage type."; | 97 const char kErrorConvertUsageType[] = "Invalid usage type."; |
98 const char kErrorMalformedParameters[] = "Error parsing parameters."; | 98 const char kErrorMalformedParameters[] = "Error parsing parameters."; |
99 const char kErrorNoDevice[] = "No such device."; | 99 const char kErrorNoDevice[] = "No such device."; |
100 const char kErrorPermissionDenied[] = | 100 const char kErrorPermissionDenied[] = "Permission to access device was denied"; |
101 "Permission to access device was denied"; | |
102 const char kErrorInvalidTransferLength[] = | 101 const char kErrorInvalidTransferLength[] = |
103 "Transfer length must be a positive number less than 104,857,600."; | 102 "Transfer length must be a positive number less than 104,857,600."; |
104 const char kErrorInvalidNumberOfPackets[] = | 103 const char kErrorInvalidNumberOfPackets[] = |
105 "Number of packets must be a positive number less than 4,194,304."; | 104 "Number of packets must be a positive number less than 4,194,304."; |
106 const char kErrorInvalidPacketLength[] = "Packet length must be a " | 105 const char kErrorInvalidPacketLength[] = |
106 "Packet length must be a " | |
rpaquay
2014/05/05 15:58:57
Shouldn't these strings be merged into a single li
Ken Rockot(use gerrit already)
2014/05/05 16:24:34
Weird. Yes. Done.
| |
107 "positive number less than 65,536."; | 107 "positive number less than 65,536."; |
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 UsbDevice* g_device_for_test = NULL; | 115 UsbDevice* g_device_for_test = NULL; |
116 | 116 |
(...skipping 26 matching lines...) Expand all Loading... | |
143 return true; | 143 return true; |
144 case usb_service::USB_SYNCHRONIZATION_SYNCHRONOUS: | 144 case usb_service::USB_SYNCHRONIZATION_SYNCHRONOUS: |
145 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | 145 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; |
146 return true; | 146 return true; |
147 default: | 147 default: |
148 NOTREACHED(); | 148 NOTREACHED(); |
149 return false; | 149 return false; |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 bool ConvertTransferTypeToApi( | 153 bool ConvertTransferTypeToApi(const UsbTransferType& input, |
154 const UsbTransferType& input, | 154 usb::TransferType* output) { |
155 usb::TransferType* output) { | |
156 switch (input) { | 155 switch (input) { |
157 case usb_service::USB_TRANSFER_CONTROL: | 156 case usb_service::USB_TRANSFER_CONTROL: |
158 *output = usb::TRANSFER_TYPE_CONTROL; | 157 *output = usb::TRANSFER_TYPE_CONTROL; |
159 return true; | 158 return true; |
160 case usb_service::USB_TRANSFER_INTERRUPT: | 159 case usb_service::USB_TRANSFER_INTERRUPT: |
161 *output = usb::TRANSFER_TYPE_INTERRUPT; | 160 *output = usb::TRANSFER_TYPE_INTERRUPT; |
162 return true; | 161 return true; |
163 case usb_service::USB_TRANSFER_ISOCHRONOUS: | 162 case usb_service::USB_TRANSFER_ISOCHRONOUS: |
164 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; | 163 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; |
165 return true; | 164 return true; |
(...skipping 16 matching lines...) Expand all Loading... | |
182 return true; | 181 return true; |
183 case usb_service::USB_USAGE_EXPLICIT_FEEDBACK: | 182 case usb_service::USB_USAGE_EXPLICIT_FEEDBACK: |
184 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | 183 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; |
185 return true; | 184 return true; |
186 default: | 185 default: |
187 NOTREACHED(); | 186 NOTREACHED(); |
188 return false; | 187 return false; |
189 } | 188 } |
190 } | 189 } |
191 | 190 |
192 bool ConvertDirection(const Direction& input, | 191 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { |
193 UsbEndpointDirection* output) { | |
194 switch (input) { | 192 switch (input) { |
195 case usb::DIRECTION_IN: | 193 case usb::DIRECTION_IN: |
196 *output = usb_service::USB_DIRECTION_INBOUND; | 194 *output = usb_service::USB_DIRECTION_INBOUND; |
197 return true; | 195 return true; |
198 case usb::DIRECTION_OUT: | 196 case usb::DIRECTION_OUT: |
199 *output = usb_service::USB_DIRECTION_OUTBOUND; | 197 *output = usb_service::USB_DIRECTION_OUTBOUND; |
200 return true; | 198 return true; |
201 default: | 199 default: |
202 NOTREACHED(); | 200 NOTREACHED(); |
203 return false; | 201 return false; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 return true; | 237 return true; |
240 case usb::RECIPIENT_OTHER: | 238 case usb::RECIPIENT_OTHER: |
241 *output = UsbDeviceHandle::OTHER; | 239 *output = UsbDeviceHandle::OTHER; |
242 return true; | 240 return true; |
243 default: | 241 default: |
244 NOTREACHED(); | 242 NOTREACHED(); |
245 return false; | 243 return false; |
246 } | 244 } |
247 } | 245 } |
248 | 246 |
249 template<class T> | 247 template <class T> |
250 bool GetTransferSize(const T& input, size_t* output) { | 248 bool GetTransferSize(const T& input, size_t* output) { |
251 if (input.direction == usb::DIRECTION_IN) { | 249 if (input.direction == usb::DIRECTION_IN) { |
252 const int* length = input.length.get(); | 250 const int* length = input.length.get(); |
253 if (length && *length >= 0 && | 251 if (length && *length >= 0 && |
254 static_cast<size_t>(*length) < kMaxTransferLength) { | 252 static_cast<size_t>(*length) < kMaxTransferLength) { |
255 *output = *length; | 253 *output = *length; |
256 return true; | 254 return true; |
257 } | 255 } |
258 } else if (input.direction == usb::DIRECTION_OUT) { | 256 } else if (input.direction == usb::DIRECTION_OUT) { |
259 if (input.data.get()) { | 257 if (input.data.get()) { |
260 *output = input.data->size(); | 258 *output = input.data->size(); |
261 return true; | 259 return true; |
262 } | 260 } |
263 } | 261 } |
264 return false; | 262 return false; |
265 } | 263 } |
266 | 264 |
267 template<class T> | 265 template <class T> |
268 scoped_refptr<net::IOBuffer> CreateBufferForTransfer( | 266 scoped_refptr<net::IOBuffer> CreateBufferForTransfer( |
269 const T& input, UsbEndpointDirection direction, size_t size) { | 267 const T& input, |
270 | 268 UsbEndpointDirection direction, |
269 size_t size) { | |
271 if (size >= kMaxTransferLength) | 270 if (size >= kMaxTransferLength) |
272 return NULL; | 271 return NULL; |
273 | 272 |
274 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 273 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
275 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 274 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
276 // cannot represent a zero-length buffer, while an URB can. | 275 // cannot represent a zero-length buffer, while an URB can. |
277 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( | 276 scoped_refptr<net::IOBuffer> buffer = |
278 static_cast<size_t>(1), size)); | 277 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); |
279 | 278 |
280 if (direction == usb_service::USB_DIRECTION_INBOUND) { | 279 if (direction == usb_service::USB_DIRECTION_INBOUND) { |
281 return buffer; | 280 return buffer; |
282 } else if (direction == usb_service::USB_DIRECTION_OUTBOUND) { | 281 } else if (direction == usb_service::USB_DIRECTION_OUTBOUND) { |
283 if (input.data.get() && size <= input.data->size()) { | 282 if (input.data.get() && size <= input.data->size()) { |
284 memcpy(buffer->data(), input.data->data(), size); | 283 memcpy(buffer->data(), input.data->data(), size); |
285 return buffer; | 284 return buffer; |
286 } | 285 } |
287 } | 286 } |
288 NOTREACHED(); | 287 NOTREACHED(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 bool success) { | 321 bool success) { |
323 if (success) { | 322 if (success) { |
324 ++i; | 323 ++i; |
325 } else { | 324 } else { |
326 i = devices->erase(i); | 325 i = devices->erase(i); |
327 } | 326 } |
328 if (i == devices->end()) { | 327 if (i == devices->end()) { |
329 callback.Run(devices.Pass()); | 328 callback.Run(devices.Pass()); |
330 return; | 329 return; |
331 } | 330 } |
332 (*i)->RequestUsbAcess(interface_id, base::Bind(RequestUsbDevicesAccessHelper, | 331 (*i)->RequestUsbAcess(interface_id, |
333 base::Passed(devices.Pass()), | 332 base::Bind(RequestUsbDevicesAccessHelper, |
334 i, interface_id, callback)); | 333 base::Passed(devices.Pass()), |
334 i, | |
335 interface_id, | |
336 callback)); | |
335 } | 337 } |
336 | 338 |
337 void RequestUsbDevicesAccess( | 339 void RequestUsbDevicesAccess( |
338 ScopedDeviceVector devices, | 340 ScopedDeviceVector devices, |
339 int interface_id, | 341 int interface_id, |
340 const base::Callback<void(ScopedDeviceVector result)>& callback) { | 342 const base::Callback<void(ScopedDeviceVector result)>& callback) { |
341 if (devices->empty()) { | 343 if (devices->empty()) { |
342 callback.Run(devices.Pass()); | 344 callback.Run(devices.Pass()); |
343 return; | 345 return; |
344 } | 346 } |
345 std::vector<scoped_refptr<UsbDevice> >::iterator i = devices->begin(); | 347 std::vector<scoped_refptr<UsbDevice> >::iterator i = devices->begin(); |
346 (*i)->RequestUsbAcess( | 348 (*i)->RequestUsbAcess(interface_id, |
347 interface_id, | 349 base::Bind(RequestUsbDevicesAccessHelper, |
348 base::Bind(RequestUsbDevicesAccessHelper, base::Passed(devices.Pass()), | 350 base::Passed(devices.Pass()), |
349 i, interface_id, callback)); | 351 i, |
352 interface_id, | |
353 callback)); | |
350 } | 354 } |
351 #endif // OS_CHROMEOS | 355 #endif // OS_CHROMEOS |
352 | 356 |
353 base::DictionaryValue* CreateTransferInfo( | 357 base::DictionaryValue* CreateTransferInfo(UsbTransferStatus status, |
354 UsbTransferStatus status, | 358 scoped_refptr<net::IOBuffer> data, |
355 scoped_refptr<net::IOBuffer> data, | 359 size_t length) { |
356 size_t length) { | |
357 base::DictionaryValue* result = new base::DictionaryValue(); | 360 base::DictionaryValue* result = new base::DictionaryValue(); |
358 result->SetInteger(kResultCodeKey, status); | 361 result->SetInteger(kResultCodeKey, status); |
359 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(), | 362 result->Set(kDataKey, |
360 length)); | 363 base::BinaryValue::CreateWithCopiedBuffer(data->data(), length)); |
361 return result; | 364 return result; |
362 } | 365 } |
363 | 366 |
364 base::Value* PopulateConnectionHandle(int handle, int vendor_id, | 367 base::Value* PopulateConnectionHandle(int handle, |
368 int vendor_id, | |
365 int product_id) { | 369 int product_id) { |
366 ConnectionHandle result; | 370 ConnectionHandle result; |
367 result.handle = handle; | 371 result.handle = handle; |
368 result.vendor_id = vendor_id; | 372 result.vendor_id = vendor_id; |
369 result.product_id = product_id; | 373 result.product_id = product_id; |
370 return result.ToValue().release(); | 374 return result.ToValue().release(); |
371 } | 375 } |
372 | 376 |
373 base::Value* PopulateDevice(UsbDevice* device) { | 377 base::Value* PopulateDevice(UsbDevice* device) { |
374 Device result; | 378 Device result; |
(...skipping 17 matching lines...) Expand all Loading... | |
392 descriptor.interface_subclass = interface_subclass; | 396 descriptor.interface_subclass = interface_subclass; |
393 descriptor.interface_protocol = interface_protocol; | 397 descriptor.interface_protocol = interface_protocol; |
394 descriptor.endpoints = *endpoints; | 398 descriptor.endpoints = *endpoints; |
395 return descriptor.ToValue().release(); | 399 return descriptor.ToValue().release(); |
396 } | 400 } |
397 | 401 |
398 } // namespace | 402 } // namespace |
399 | 403 |
400 namespace extensions { | 404 namespace extensions { |
401 | 405 |
402 UsbAsyncApiFunction::UsbAsyncApiFunction() | 406 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { |
403 : manager_(NULL) { | |
404 } | 407 } |
405 | 408 |
406 UsbAsyncApiFunction::~UsbAsyncApiFunction() { | 409 UsbAsyncApiFunction::~UsbAsyncApiFunction() { |
407 } | 410 } |
408 | 411 |
409 bool UsbAsyncApiFunction::PrePrepare() { | 412 bool UsbAsyncApiFunction::PrePrepare() { |
410 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context()); | 413 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context()); |
411 set_work_thread_id(BrowserThread::FILE); | 414 set_work_thread_id(BrowserThread::FILE); |
412 return manager_ != NULL; | 415 return manager_ != NULL; |
413 } | 416 } |
414 | 417 |
415 bool UsbAsyncApiFunction::Respond() { | 418 bool UsbAsyncApiFunction::Respond() { |
416 return error_.empty(); | 419 return error_.empty(); |
417 } | 420 } |
418 | 421 |
419 scoped_refptr<UsbDevice> | 422 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError( |
420 UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError( | |
421 const Device& input_device) { | 423 const Device& input_device) { |
422 if (g_device_for_test) | 424 if (g_device_for_test) |
423 return g_device_for_test; | 425 return g_device_for_test; |
424 | 426 |
425 const uint16_t vendor_id = input_device.vendor_id; | 427 const uint16_t vendor_id = input_device.vendor_id; |
426 const uint16_t product_id = input_device.product_id; | 428 const uint16_t product_id = input_device.product_id; |
427 UsbDevicePermission::CheckParam param( | 429 UsbDevicePermission::CheckParam param( |
428 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 430 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); |
429 if (!PermissionsData::CheckAPIPermissionWithParam( | 431 if (!PermissionsData::CheckAPIPermissionWithParam( |
430 GetExtension(), APIPermission::kUsbDevice, ¶m)) { | 432 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 | 489 |
488 void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) { | 490 void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) { |
489 manager_->Remove(extension_->id(), api_resource_id); | 491 manager_->Remove(extension_->id(), api_resource_id); |
490 } | 492 } |
491 | 493 |
492 void UsbAsyncApiFunction::CompleteWithError(const std::string& error) { | 494 void UsbAsyncApiFunction::CompleteWithError(const std::string& error) { |
493 SetError(error); | 495 SetError(error); |
494 AsyncWorkCompleted(); | 496 AsyncWorkCompleted(); |
495 } | 497 } |
496 | 498 |
497 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() {} | 499 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { |
500 } | |
498 | 501 |
499 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() {} | 502 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { |
503 } | |
500 | 504 |
501 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, | 505 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, |
502 scoped_refptr<net::IOBuffer> data, | 506 scoped_refptr<net::IOBuffer> data, |
503 size_t length) { | 507 size_t length) { |
504 if (status != usb_service::USB_TRANSFER_COMPLETED) | 508 if (status != usb_service::USB_TRANSFER_COMPLETED) |
505 SetError(ConvertTransferStatusToErrorString(status)); | 509 SetError(ConvertTransferStatusToErrorString(status)); |
506 | 510 |
507 SetResult(CreateTransferInfo(status, data, length)); | 511 SetResult(CreateTransferInfo(status, data, length)); |
508 AsyncWorkCompleted(); | 512 AsyncWorkCompleted(); |
509 } | 513 } |
510 | 514 |
511 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( | 515 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( |
512 const Direction& input, UsbEndpointDirection* output) { | 516 const Direction& input, |
517 UsbEndpointDirection* output) { | |
513 const bool converted = ConvertDirection(input, output); | 518 const bool converted = ConvertDirection(input, output); |
514 if (!converted) | 519 if (!converted) |
515 SetError(kErrorConvertDirection); | 520 SetError(kErrorConvertDirection); |
516 return converted; | 521 return converted; |
517 } | 522 } |
518 | 523 |
519 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( | 524 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( |
520 const RequestType& input, UsbDeviceHandle::TransferRequestType* output) { | 525 const RequestType& input, |
526 UsbDeviceHandle::TransferRequestType* output) { | |
521 const bool converted = ConvertRequestType(input, output); | 527 const bool converted = ConvertRequestType(input, output); |
522 if (!converted) | 528 if (!converted) |
523 SetError(kErrorConvertRequestType); | 529 SetError(kErrorConvertRequestType); |
524 return converted; | 530 return converted; |
525 } | 531 } |
526 | 532 |
527 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely( | 533 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely( |
528 const Recipient& input, UsbDeviceHandle::TransferRecipient* output) { | 534 const Recipient& input, |
535 UsbDeviceHandle::TransferRecipient* output) { | |
529 const bool converted = ConvertRecipient(input, output); | 536 const bool converted = ConvertRecipient(input, output); |
530 if (!converted) | 537 if (!converted) |
531 SetError(kErrorConvertRecipient); | 538 SetError(kErrorConvertRecipient); |
532 return converted; | 539 return converted; |
533 } | 540 } |
534 | 541 |
535 UsbFindDevicesFunction::UsbFindDevicesFunction() {} | 542 UsbFindDevicesFunction::UsbFindDevicesFunction() { |
543 } | |
536 | 544 |
537 UsbFindDevicesFunction::~UsbFindDevicesFunction() {} | 545 UsbFindDevicesFunction::~UsbFindDevicesFunction() { |
546 } | |
538 | 547 |
539 bool UsbFindDevicesFunction::Prepare() { | 548 bool UsbFindDevicesFunction::Prepare() { |
540 parameters_ = FindDevices::Params::Create(*args_); | 549 parameters_ = FindDevices::Params::Create(*args_); |
541 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 550 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
542 return true; | 551 return true; |
543 } | 552 } |
544 | 553 |
545 void UsbFindDevicesFunction::AsyncWorkStart() { | 554 void UsbFindDevicesFunction::AsyncWorkStart() { |
546 scoped_ptr<base::ListValue> result(new base::ListValue()); | 555 scoped_ptr<base::ListValue> result(new base::ListValue()); |
547 | 556 |
548 if (g_device_for_test) { | 557 if (g_device_for_test) { |
549 UsbDeviceResource* const resource = new UsbDeviceResource( | 558 UsbDeviceResource* const resource = |
550 extension_->id(), | 559 new UsbDeviceResource(extension_->id(), g_device_for_test->Open()); |
551 g_device_for_test->Open()); | |
552 | 560 |
553 result->Append(PopulateConnectionHandle(manager_->Add(resource), 0, 0)); | 561 result->Append(PopulateConnectionHandle(manager_->Add(resource), 0, 0)); |
554 SetResult(result.release()); | 562 SetResult(result.release()); |
555 AsyncWorkCompleted(); | 563 AsyncWorkCompleted(); |
556 return; | 564 return; |
557 } | 565 } |
558 | 566 |
559 const uint16_t vendor_id = parameters_->options.vendor_id; | 567 const uint16_t vendor_id = parameters_->options.vendor_id; |
560 const uint16_t product_id = parameters_->options.product_id; | 568 const uint16_t product_id = parameters_->options.product_id; |
561 int interface_id = parameters_->options.interface_id.get() ? | 569 int interface_id = parameters_->options.interface_id.get() |
562 *parameters_->options.interface_id.get() : | 570 ? *parameters_->options.interface_id.get() |
563 UsbDevicePermissionData::ANY_INTERFACE; | 571 : UsbDevicePermissionData::ANY_INTERFACE; |
564 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); | 572 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); |
565 if (!PermissionsData::CheckAPIPermissionWithParam( | 573 if (!PermissionsData::CheckAPIPermissionWithParam( |
566 GetExtension(), APIPermission::kUsbDevice, ¶m)) { | 574 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
567 LOG(WARNING) << "Insufficient permissions to access device."; | 575 LOG(WARNING) << "Insufficient permissions to access device."; |
568 CompleteWithError(kErrorPermissionDenied); | 576 CompleteWithError(kErrorPermissionDenied); |
569 return; | 577 return; |
570 } | 578 } |
571 | 579 |
572 UsbService *service = UsbService::GetInstance(); | 580 UsbService* service = UsbService::GetInstance(); |
573 if (!service) { | 581 if (!service) { |
574 CompleteWithError(kErrorInitService); | 582 CompleteWithError(kErrorInitService); |
575 return; | 583 return; |
576 } | 584 } |
577 | 585 |
578 ScopedDeviceVector devices(new DeviceVector()); | 586 ScopedDeviceVector devices(new DeviceVector()); |
579 service->GetDevices(devices.get()); | 587 service->GetDevices(devices.get()); |
580 | 588 |
581 for (DeviceVector::iterator it = devices->begin(); | 589 for (DeviceVector::iterator it = devices->begin(); it != devices->end();) { |
582 it != devices->end();) { | |
583 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) { | 590 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) { |
584 it = devices->erase(it); | 591 it = devices->erase(it); |
585 } else { | 592 } else { |
586 ++it; | 593 ++it; |
587 } | 594 } |
588 } | 595 } |
589 | 596 |
590 #if defined(OS_CHROMEOS) | 597 #if defined(OS_CHROMEOS) |
591 RequestUsbDevicesAccess( | 598 RequestUsbDevicesAccess( |
592 devices.Pass(), interface_id, | 599 devices.Pass(), |
600 interface_id, | |
593 base::Bind(&UsbFindDevicesFunction::OpenDevices, this)); | 601 base::Bind(&UsbFindDevicesFunction::OpenDevices, this)); |
594 #else | 602 #else |
595 OpenDevices(devices.Pass()); | 603 OpenDevices(devices.Pass()); |
596 #endif // OS_CHROMEOS | 604 #endif // OS_CHROMEOS |
597 } | 605 } |
598 | 606 |
599 void UsbFindDevicesFunction::OpenDevices(ScopedDeviceVector devices) { | 607 void UsbFindDevicesFunction::OpenDevices(ScopedDeviceVector devices) { |
600 base::ListValue* result = new base::ListValue(); | 608 base::ListValue* result = new base::ListValue(); |
601 | 609 |
602 for (size_t i = 0; i < devices->size(); ++i) { | 610 for (size_t i = 0; i < devices->size(); ++i) { |
603 scoped_refptr<UsbDeviceHandle> device_handle = | 611 scoped_refptr<UsbDeviceHandle> device_handle = devices->at(i)->Open(); |
604 devices->at(i)->Open(); | |
605 if (device_handle) | 612 if (device_handle) |
606 device_handles_.push_back(device_handle); | 613 device_handles_.push_back(device_handle); |
607 } | 614 } |
608 | 615 |
609 for (size_t i = 0; i < device_handles_.size(); ++i) { | 616 for (size_t i = 0; i < device_handles_.size(); ++i) { |
610 UsbDeviceHandle* const device_handle = device_handles_[i].get(); | 617 UsbDeviceHandle* const device_handle = device_handles_[i].get(); |
611 UsbDeviceResource* const resource = | 618 UsbDeviceResource* const resource = |
612 new UsbDeviceResource(extension_->id(), device_handle); | 619 new UsbDeviceResource(extension_->id(), device_handle); |
613 | 620 |
614 result->Append(PopulateConnectionHandle(manager_->Add(resource), | 621 result->Append(PopulateConnectionHandle(manager_->Add(resource), |
615 parameters_->options.vendor_id, | 622 parameters_->options.vendor_id, |
616 parameters_->options.product_id)); | 623 parameters_->options.product_id)); |
617 } | 624 } |
618 | 625 |
619 SetResult(result); | 626 SetResult(result); |
620 AsyncWorkCompleted(); | 627 AsyncWorkCompleted(); |
621 } | 628 } |
622 | 629 |
623 UsbGetDevicesFunction::UsbGetDevicesFunction() { | 630 UsbGetDevicesFunction::UsbGetDevicesFunction() { |
624 } | 631 } |
625 | 632 |
626 UsbGetDevicesFunction::~UsbGetDevicesFunction() { | 633 UsbGetDevicesFunction::~UsbGetDevicesFunction() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
675 } | 682 } |
676 | 683 |
677 for (size_t i = 0; i < devices.size(); ++i) { | 684 for (size_t i = 0; i < devices.size(); ++i) { |
678 result->Append(PopulateDevice(devices[i].get())); | 685 result->Append(PopulateDevice(devices[i].get())); |
679 } | 686 } |
680 | 687 |
681 SetResult(result.release()); | 688 SetResult(result.release()); |
682 AsyncWorkCompleted(); | 689 AsyncWorkCompleted(); |
683 } | 690 } |
684 | 691 |
685 UsbRequestAccessFunction::UsbRequestAccessFunction() {} | 692 UsbRequestAccessFunction::UsbRequestAccessFunction() { |
693 } | |
686 | 694 |
687 UsbRequestAccessFunction::~UsbRequestAccessFunction() {} | 695 UsbRequestAccessFunction::~UsbRequestAccessFunction() { |
696 } | |
688 | 697 |
689 bool UsbRequestAccessFunction::Prepare() { | 698 bool UsbRequestAccessFunction::Prepare() { |
690 parameters_ = RequestAccess::Params::Create(*args_); | 699 parameters_ = RequestAccess::Params::Create(*args_); |
691 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 700 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
692 return true; | 701 return true; |
693 } | 702 } |
694 | 703 |
695 void UsbRequestAccessFunction::AsyncWorkStart() { | 704 void UsbRequestAccessFunction::AsyncWorkStart() { |
696 #if defined(OS_CHROMEOS) | 705 #if defined(OS_CHROMEOS) |
697 scoped_refptr<UsbDevice> device = | 706 scoped_refptr<UsbDevice> device = |
698 GetDeviceOrOrCompleteWithError(parameters_->device); | 707 GetDeviceOrOrCompleteWithError(parameters_->device); |
699 if (!device) return; | 708 if (!device) |
709 return; | |
700 | 710 |
701 device->RequestUsbAcess(parameters_->interface_id, | 711 device->RequestUsbAcess( |
702 base::Bind(&UsbRequestAccessFunction::OnCompleted, | 712 parameters_->interface_id, |
703 this)); | 713 base::Bind(&UsbRequestAccessFunction::OnCompleted, this)); |
704 #else | 714 #else |
705 SetResult(new base::FundamentalValue(false)); | 715 SetResult(new base::FundamentalValue(false)); |
706 CompleteWithError(kErrorNotSupported); | 716 CompleteWithError(kErrorNotSupported); |
707 #endif // OS_CHROMEOS | 717 #endif // OS_CHROMEOS |
708 } | 718 } |
709 | 719 |
710 void UsbRequestAccessFunction::OnCompleted(bool success) { | 720 void UsbRequestAccessFunction::OnCompleted(bool success) { |
711 SetResult(new base::FundamentalValue(success)); | 721 SetResult(new base::FundamentalValue(success)); |
712 AsyncWorkCompleted(); | 722 AsyncWorkCompleted(); |
713 } | 723 } |
714 | 724 |
715 UsbOpenDeviceFunction::UsbOpenDeviceFunction() {} | 725 UsbOpenDeviceFunction::UsbOpenDeviceFunction() { |
726 } | |
716 | 727 |
717 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() {} | 728 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() { |
729 } | |
718 | 730 |
719 bool UsbOpenDeviceFunction::Prepare() { | 731 bool UsbOpenDeviceFunction::Prepare() { |
720 parameters_ = OpenDevice::Params::Create(*args_); | 732 parameters_ = OpenDevice::Params::Create(*args_); |
721 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 733 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
722 return true; | 734 return true; |
723 } | 735 } |
724 | 736 |
725 void UsbOpenDeviceFunction::AsyncWorkStart() { | 737 void UsbOpenDeviceFunction::AsyncWorkStart() { |
726 scoped_refptr<UsbDevice> device = | 738 scoped_refptr<UsbDevice> device = |
727 GetDeviceOrOrCompleteWithError(parameters_->device); | 739 GetDeviceOrOrCompleteWithError(parameters_->device); |
728 if (!device) return; | 740 if (!device) |
741 return; | |
729 | 742 |
730 handle_ = device->Open(); | 743 handle_ = device->Open(); |
731 if (!handle_) { | 744 if (!handle_) { |
732 SetError(kErrorOpen); | 745 SetError(kErrorOpen); |
733 AsyncWorkCompleted(); | 746 AsyncWorkCompleted(); |
734 return; | 747 return; |
735 } | 748 } |
736 | 749 |
737 SetResult(PopulateConnectionHandle( | 750 SetResult(PopulateConnectionHandle( |
738 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), | 751 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), |
739 handle_->device()->vendor_id(), | 752 handle_->device()->vendor_id(), |
740 handle_->device()->product_id())); | 753 handle_->device()->product_id())); |
741 AsyncWorkCompleted(); | 754 AsyncWorkCompleted(); |
742 } | 755 } |
743 | 756 |
744 UsbListInterfacesFunction::UsbListInterfacesFunction() {} | 757 UsbListInterfacesFunction::UsbListInterfacesFunction() { |
758 } | |
745 | 759 |
746 UsbListInterfacesFunction::~UsbListInterfacesFunction() {} | 760 UsbListInterfacesFunction::~UsbListInterfacesFunction() { |
761 } | |
747 | 762 |
748 bool UsbListInterfacesFunction::Prepare() { | 763 bool UsbListInterfacesFunction::Prepare() { |
749 parameters_ = ListInterfaces::Params::Create(*args_); | 764 parameters_ = ListInterfaces::Params::Create(*args_); |
750 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 765 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
751 return true; | 766 return true; |
752 } | 767 } |
753 | 768 |
754 void UsbListInterfacesFunction::AsyncWorkStart() { | 769 void UsbListInterfacesFunction::AsyncWorkStart() { |
755 scoped_refptr<UsbDeviceHandle> device_handle = | 770 scoped_refptr<UsbDeviceHandle> device_handle = |
756 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 771 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
757 if (!device_handle) return; | 772 if (!device_handle) |
773 return; | |
758 | 774 |
759 scoped_refptr<UsbConfigDescriptor> config = | 775 scoped_refptr<UsbConfigDescriptor> config = |
760 device_handle->device()->ListInterfaces(); | 776 device_handle->device()->ListInterfaces(); |
761 | 777 |
762 if (!config) { | 778 if (!config) { |
763 SetError(kErrorCannotListInterfaces); | 779 SetError(kErrorCannotListInterfaces); |
764 AsyncWorkCompleted(); | 780 AsyncWorkCompleted(); |
765 return; | 781 return; |
766 } | 782 } |
767 | 783 |
768 result_.reset(new base::ListValue()); | 784 result_.reset(new base::ListValue()); |
769 | 785 |
770 for (size_t i = 0, num_interfaces = config->GetNumInterfaces(); | 786 for (size_t i = 0, num_interfaces = config->GetNumInterfaces(); |
771 i < num_interfaces; ++i) { | 787 i < num_interfaces; |
772 scoped_refptr<const UsbInterfaceDescriptor> | 788 ++i) { |
773 usb_interface(config->GetInterface(i)); | 789 scoped_refptr<const UsbInterfaceDescriptor> usb_interface( |
790 config->GetInterface(i)); | |
774 for (size_t j = 0, num_descriptors = usb_interface->GetNumAltSettings(); | 791 for (size_t j = 0, num_descriptors = usb_interface->GetNumAltSettings(); |
775 j < num_descriptors; ++j) { | 792 j < num_descriptors; |
776 scoped_refptr<const UsbInterfaceAltSettingDescriptor> descriptor | 793 ++j) { |
777 = usb_interface->GetAltSetting(j); | 794 scoped_refptr<const UsbInterfaceAltSettingDescriptor> descriptor = |
795 usb_interface->GetAltSetting(j); | |
778 std::vector<linked_ptr<EndpointDescriptor> > endpoints; | 796 std::vector<linked_ptr<EndpointDescriptor> > endpoints; |
779 for (size_t k = 0, num_endpoints = descriptor->GetNumEndpoints(); | 797 for (size_t k = 0, num_endpoints = descriptor->GetNumEndpoints(); |
780 k < num_endpoints; k++) { | 798 k < num_endpoints; |
781 scoped_refptr<const UsbEndpointDescriptor> endpoint | 799 k++) { |
782 = descriptor->GetEndpoint(k); | 800 scoped_refptr<const UsbEndpointDescriptor> endpoint = |
801 descriptor->GetEndpoint(k); | |
783 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor()); | 802 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor()); |
784 | 803 |
785 TransferType type; | 804 TransferType type; |
786 Direction direction; | 805 Direction direction; |
787 SynchronizationType synchronization; | 806 SynchronizationType synchronization; |
788 UsageType usage; | 807 UsageType usage; |
789 | 808 |
790 if (!ConvertTransferTypeSafely(endpoint->GetTransferType(), &type) || | 809 if (!ConvertTransferTypeSafely(endpoint->GetTransferType(), &type) || |
791 !ConvertDirectionSafely(endpoint->GetDirection(), &direction) || | 810 !ConvertDirectionSafely(endpoint->GetDirection(), &direction) || |
792 !ConvertSynchronizationTypeSafely( | 811 !ConvertSynchronizationTypeSafely( |
(...skipping 11 matching lines...) Expand all Loading... | |
804 endpoint_desc->synchronization = synchronization; | 823 endpoint_desc->synchronization = synchronization; |
805 endpoint_desc->usage = usage; | 824 endpoint_desc->usage = usage; |
806 | 825 |
807 int* polling_interval = new int; | 826 int* polling_interval = new int; |
808 endpoint_desc->polling_interval.reset(polling_interval); | 827 endpoint_desc->polling_interval.reset(polling_interval); |
809 *polling_interval = endpoint->GetPollingInterval(); | 828 *polling_interval = endpoint->GetPollingInterval(); |
810 | 829 |
811 endpoints.push_back(endpoint_desc); | 830 endpoints.push_back(endpoint_desc); |
812 } | 831 } |
813 | 832 |
814 result_->Append(PopulateInterfaceDescriptor( | 833 result_->Append( |
815 descriptor->GetInterfaceNumber(), | 834 PopulateInterfaceDescriptor(descriptor->GetInterfaceNumber(), |
816 descriptor->GetAlternateSetting(), | 835 descriptor->GetAlternateSetting(), |
817 descriptor->GetInterfaceClass(), | 836 descriptor->GetInterfaceClass(), |
818 descriptor->GetInterfaceSubclass(), | 837 descriptor->GetInterfaceSubclass(), |
819 descriptor->GetInterfaceProtocol(), | 838 descriptor->GetInterfaceProtocol(), |
820 &endpoints)); | 839 &endpoints)); |
821 } | 840 } |
822 } | 841 } |
823 | 842 |
824 SetResult(result_.release()); | 843 SetResult(result_.release()); |
825 AsyncWorkCompleted(); | 844 AsyncWorkCompleted(); |
826 } | 845 } |
827 | 846 |
828 bool UsbListInterfacesFunction::ConvertDirectionSafely( | 847 bool UsbListInterfacesFunction::ConvertDirectionSafely( |
829 const UsbEndpointDirection& input, | 848 const UsbEndpointDirection& input, |
830 usb::Direction* output) { | 849 usb::Direction* output) { |
(...skipping 23 matching lines...) Expand all Loading... | |
854 | 873 |
855 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( | 874 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( |
856 const UsbUsageType& input, | 875 const UsbUsageType& input, |
857 usb::UsageType* output) { | 876 usb::UsageType* output) { |
858 const bool converted = ConvertUsageTypeToApi(input, output); | 877 const bool converted = ConvertUsageTypeToApi(input, output); |
859 if (!converted) | 878 if (!converted) |
860 SetError(kErrorConvertUsageType); | 879 SetError(kErrorConvertUsageType); |
861 return converted; | 880 return converted; |
862 } | 881 } |
863 | 882 |
864 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} | 883 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { |
884 } | |
865 | 885 |
866 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} | 886 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { |
887 } | |
867 | 888 |
868 bool UsbCloseDeviceFunction::Prepare() { | 889 bool UsbCloseDeviceFunction::Prepare() { |
869 parameters_ = CloseDevice::Params::Create(*args_); | 890 parameters_ = CloseDevice::Params::Create(*args_); |
870 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 891 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
871 return true; | 892 return true; |
872 } | 893 } |
873 | 894 |
874 void UsbCloseDeviceFunction::AsyncWorkStart() { | 895 void UsbCloseDeviceFunction::AsyncWorkStart() { |
875 scoped_refptr<UsbDeviceHandle> device_handle = | 896 scoped_refptr<UsbDeviceHandle> device_handle = |
876 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 897 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
877 if (!device_handle) return; | 898 if (!device_handle) |
899 return; | |
878 | 900 |
879 device_handle->Close(); | 901 device_handle->Close(); |
880 RemoveUsbDeviceResource(parameters_->handle.handle); | 902 RemoveUsbDeviceResource(parameters_->handle.handle); |
881 AsyncWorkCompleted(); | 903 AsyncWorkCompleted(); |
882 } | 904 } |
883 | 905 |
884 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() {} | 906 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() { |
907 } | |
885 | 908 |
886 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() {} | 909 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() { |
910 } | |
887 | 911 |
888 bool UsbClaimInterfaceFunction::Prepare() { | 912 bool UsbClaimInterfaceFunction::Prepare() { |
889 parameters_ = ClaimInterface::Params::Create(*args_); | 913 parameters_ = ClaimInterface::Params::Create(*args_); |
890 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 914 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
891 return true; | 915 return true; |
892 } | 916 } |
893 | 917 |
894 void UsbClaimInterfaceFunction::AsyncWorkStart() { | 918 void UsbClaimInterfaceFunction::AsyncWorkStart() { |
895 scoped_refptr<UsbDeviceHandle> device_handle = | 919 scoped_refptr<UsbDeviceHandle> device_handle = |
896 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 920 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
897 if (!device_handle) return; | 921 if (!device_handle) |
922 return; | |
898 | 923 |
899 bool success = device_handle->ClaimInterface(parameters_->interface_number); | 924 bool success = device_handle->ClaimInterface(parameters_->interface_number); |
900 | 925 |
901 if (!success) | 926 if (!success) |
902 SetError(kErrorCannotClaimInterface); | 927 SetError(kErrorCannotClaimInterface); |
903 AsyncWorkCompleted(); | 928 AsyncWorkCompleted(); |
904 } | 929 } |
905 | 930 |
906 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() {} | 931 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() { |
932 } | |
907 | 933 |
908 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() {} | 934 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() { |
935 } | |
909 | 936 |
910 bool UsbReleaseInterfaceFunction::Prepare() { | 937 bool UsbReleaseInterfaceFunction::Prepare() { |
911 parameters_ = ReleaseInterface::Params::Create(*args_); | 938 parameters_ = ReleaseInterface::Params::Create(*args_); |
912 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 939 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
913 return true; | 940 return true; |
914 } | 941 } |
915 | 942 |
916 void UsbReleaseInterfaceFunction::AsyncWorkStart() { | 943 void UsbReleaseInterfaceFunction::AsyncWorkStart() { |
917 scoped_refptr<UsbDeviceHandle> device_handle = | 944 scoped_refptr<UsbDeviceHandle> device_handle = |
918 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 945 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
919 if (!device_handle) return; | 946 if (!device_handle) |
947 return; | |
920 | 948 |
921 bool success = device_handle->ReleaseInterface(parameters_->interface_number); | 949 bool success = device_handle->ReleaseInterface(parameters_->interface_number); |
922 if (!success) | 950 if (!success) |
923 SetError(kErrorCannotReleaseInterface); | 951 SetError(kErrorCannotReleaseInterface); |
924 AsyncWorkCompleted(); | 952 AsyncWorkCompleted(); |
925 } | 953 } |
926 | 954 |
927 UsbSetInterfaceAlternateSettingFunction:: | 955 UsbSetInterfaceAlternateSettingFunction:: |
928 UsbSetInterfaceAlternateSettingFunction() {} | 956 UsbSetInterfaceAlternateSettingFunction() { |
957 } | |
929 | 958 |
930 UsbSetInterfaceAlternateSettingFunction:: | 959 UsbSetInterfaceAlternateSettingFunction:: |
931 ~UsbSetInterfaceAlternateSettingFunction() {} | 960 ~UsbSetInterfaceAlternateSettingFunction() { |
961 } | |
932 | 962 |
933 bool UsbSetInterfaceAlternateSettingFunction::Prepare() { | 963 bool UsbSetInterfaceAlternateSettingFunction::Prepare() { |
934 parameters_ = SetInterfaceAlternateSetting::Params::Create(*args_); | 964 parameters_ = SetInterfaceAlternateSetting::Params::Create(*args_); |
935 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 965 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
936 return true; | 966 return true; |
937 } | 967 } |
938 | 968 |
939 void UsbSetInterfaceAlternateSettingFunction::AsyncWorkStart() { | 969 void UsbSetInterfaceAlternateSettingFunction::AsyncWorkStart() { |
940 scoped_refptr<UsbDeviceHandle> device_handle = | 970 scoped_refptr<UsbDeviceHandle> device_handle = |
941 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 971 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
942 if (!device_handle) return; | 972 if (!device_handle) |
973 return; | |
943 | 974 |
944 bool success = device_handle->SetInterfaceAlternateSetting( | 975 bool success = device_handle->SetInterfaceAlternateSetting( |
945 parameters_->interface_number, | 976 parameters_->interface_number, parameters_->alternate_setting); |
946 parameters_->alternate_setting); | |
947 if (!success) | 977 if (!success) |
948 SetError(kErrorCannotSetInterfaceAlternateSetting); | 978 SetError(kErrorCannotSetInterfaceAlternateSetting); |
949 | 979 |
950 AsyncWorkCompleted(); | 980 AsyncWorkCompleted(); |
951 } | 981 } |
952 | 982 |
953 UsbControlTransferFunction::UsbControlTransferFunction() {} | 983 UsbControlTransferFunction::UsbControlTransferFunction() { |
984 } | |
954 | 985 |
955 UsbControlTransferFunction::~UsbControlTransferFunction() {} | 986 UsbControlTransferFunction::~UsbControlTransferFunction() { |
987 } | |
956 | 988 |
957 bool UsbControlTransferFunction::Prepare() { | 989 bool UsbControlTransferFunction::Prepare() { |
958 parameters_ = ControlTransfer::Params::Create(*args_); | 990 parameters_ = ControlTransfer::Params::Create(*args_); |
959 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 991 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
960 return true; | 992 return true; |
961 } | 993 } |
962 | 994 |
963 void UsbControlTransferFunction::AsyncWorkStart() { | 995 void UsbControlTransferFunction::AsyncWorkStart() { |
964 scoped_refptr<UsbDeviceHandle> device_handle = | 996 scoped_refptr<UsbDeviceHandle> device_handle = |
965 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 997 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
966 if (!device_handle) return; | 998 if (!device_handle) |
999 return; | |
967 | 1000 |
968 const ControlTransferInfo& transfer = parameters_->transfer_info; | 1001 const ControlTransferInfo& transfer = parameters_->transfer_info; |
969 | 1002 |
970 UsbEndpointDirection direction; | 1003 UsbEndpointDirection direction; |
971 UsbDeviceHandle::TransferRequestType request_type; | 1004 UsbDeviceHandle::TransferRequestType request_type; |
972 UsbDeviceHandle::TransferRecipient recipient; | 1005 UsbDeviceHandle::TransferRecipient recipient; |
973 size_t size = 0; | 1006 size_t size = 0; |
974 | 1007 |
975 if (!ConvertDirectionSafely(transfer.direction, &direction) || | 1008 if (!ConvertDirectionSafely(transfer.direction, &direction) || |
976 !ConvertRequestTypeSafely(transfer.request_type, &request_type) || | 1009 !ConvertRequestTypeSafely(transfer.request_type, &request_type) || |
977 !ConvertRecipientSafely(transfer.recipient, &recipient)) { | 1010 !ConvertRecipientSafely(transfer.recipient, &recipient)) { |
978 AsyncWorkCompleted(); | 1011 AsyncWorkCompleted(); |
979 return; | 1012 return; |
980 } | 1013 } |
981 | 1014 |
982 if (!GetTransferSize(transfer, &size)) { | 1015 if (!GetTransferSize(transfer, &size)) { |
983 CompleteWithError(kErrorInvalidTransferLength); | 1016 CompleteWithError(kErrorInvalidTransferLength); |
984 return; | 1017 return; |
985 } | 1018 } |
986 | 1019 |
987 scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer( | 1020 scoped_refptr<net::IOBuffer> buffer = |
988 transfer, direction, size); | 1021 CreateBufferForTransfer(transfer, direction, size); |
989 if (!buffer.get()) { | 1022 if (!buffer.get()) { |
990 CompleteWithError(kErrorMalformedParameters); | 1023 CompleteWithError(kErrorMalformedParameters); |
991 return; | 1024 return; |
992 } | 1025 } |
993 | 1026 |
994 device_handle->ControlTransfer( | 1027 device_handle->ControlTransfer( |
995 direction, | 1028 direction, |
996 request_type, | 1029 request_type, |
997 recipient, | 1030 recipient, |
998 transfer.request, | 1031 transfer.request, |
999 transfer.value, | 1032 transfer.value, |
1000 transfer.index, | 1033 transfer.index, |
1001 buffer.get(), | 1034 buffer.get(), |
1002 size, | 1035 size, |
1003 0, | 1036 0, |
1004 base::Bind(&UsbControlTransferFunction::OnCompleted, this)); | 1037 base::Bind(&UsbControlTransferFunction::OnCompleted, this)); |
1005 } | 1038 } |
1006 | 1039 |
1007 UsbBulkTransferFunction::UsbBulkTransferFunction() {} | 1040 UsbBulkTransferFunction::UsbBulkTransferFunction() { |
1041 } | |
1008 | 1042 |
1009 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} | 1043 UsbBulkTransferFunction::~UsbBulkTransferFunction() { |
1044 } | |
1010 | 1045 |
1011 bool UsbBulkTransferFunction::Prepare() { | 1046 bool UsbBulkTransferFunction::Prepare() { |
1012 parameters_ = BulkTransfer::Params::Create(*args_); | 1047 parameters_ = BulkTransfer::Params::Create(*args_); |
1013 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 1048 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
1014 return true; | 1049 return true; |
1015 } | 1050 } |
1016 | 1051 |
1017 void UsbBulkTransferFunction::AsyncWorkStart() { | 1052 void UsbBulkTransferFunction::AsyncWorkStart() { |
1018 scoped_refptr<UsbDeviceHandle> device_handle = | 1053 scoped_refptr<UsbDeviceHandle> device_handle = |
1019 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 1054 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
1020 if (!device_handle) return; | 1055 if (!device_handle) |
1056 return; | |
1021 | 1057 |
1022 const GenericTransferInfo& transfer = parameters_->transfer_info; | 1058 const GenericTransferInfo& transfer = parameters_->transfer_info; |
1023 | 1059 |
1024 UsbEndpointDirection direction; | 1060 UsbEndpointDirection direction; |
1025 size_t size = 0; | 1061 size_t size = 0; |
1026 | 1062 |
1027 if (!ConvertDirectionSafely(transfer.direction, &direction)) { | 1063 if (!ConvertDirectionSafely(transfer.direction, &direction)) { |
1028 AsyncWorkCompleted(); | 1064 AsyncWorkCompleted(); |
1029 return; | 1065 return; |
1030 } | 1066 } |
1031 | 1067 |
1032 if (!GetTransferSize(transfer, &size)) { | 1068 if (!GetTransferSize(transfer, &size)) { |
1033 CompleteWithError(kErrorInvalidTransferLength); | 1069 CompleteWithError(kErrorInvalidTransferLength); |
1034 return; | 1070 return; |
1035 } | 1071 } |
1036 | 1072 |
1037 scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer( | 1073 scoped_refptr<net::IOBuffer> buffer = |
1038 transfer, direction, size); | 1074 CreateBufferForTransfer(transfer, direction, size); |
1039 if (!buffer.get()) { | 1075 if (!buffer.get()) { |
1040 CompleteWithError(kErrorMalformedParameters); | 1076 CompleteWithError(kErrorMalformedParameters); |
1041 return; | 1077 return; |
1042 } | 1078 } |
1043 | 1079 |
1044 device_handle->BulkTransfer( | 1080 device_handle->BulkTransfer( |
1045 direction, | 1081 direction, |
1046 transfer.endpoint, | 1082 transfer.endpoint, |
1047 buffer.get(), | 1083 buffer.get(), |
1048 size, | 1084 size, |
1049 0, | 1085 0, |
1050 base::Bind(&UsbBulkTransferFunction::OnCompleted, this)); | 1086 base::Bind(&UsbBulkTransferFunction::OnCompleted, this)); |
1051 } | 1087 } |
1052 | 1088 |
1053 UsbInterruptTransferFunction::UsbInterruptTransferFunction() {} | 1089 UsbInterruptTransferFunction::UsbInterruptTransferFunction() { |
1090 } | |
1054 | 1091 |
1055 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} | 1092 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() { |
1093 } | |
1056 | 1094 |
1057 bool UsbInterruptTransferFunction::Prepare() { | 1095 bool UsbInterruptTransferFunction::Prepare() { |
1058 parameters_ = InterruptTransfer::Params::Create(*args_); | 1096 parameters_ = InterruptTransfer::Params::Create(*args_); |
1059 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 1097 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
1060 return true; | 1098 return true; |
1061 } | 1099 } |
1062 | 1100 |
1063 void UsbInterruptTransferFunction::AsyncWorkStart() { | 1101 void UsbInterruptTransferFunction::AsyncWorkStart() { |
1064 scoped_refptr<UsbDeviceHandle> device_handle = | 1102 scoped_refptr<UsbDeviceHandle> device_handle = |
1065 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 1103 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
1066 if (!device_handle) return; | 1104 if (!device_handle) |
1105 return; | |
1067 | 1106 |
1068 const GenericTransferInfo& transfer = parameters_->transfer_info; | 1107 const GenericTransferInfo& transfer = parameters_->transfer_info; |
1069 | 1108 |
1070 UsbEndpointDirection direction; | 1109 UsbEndpointDirection direction; |
1071 size_t size = 0; | 1110 size_t size = 0; |
1072 | 1111 |
1073 if (!ConvertDirectionSafely(transfer.direction, &direction)) { | 1112 if (!ConvertDirectionSafely(transfer.direction, &direction)) { |
1074 AsyncWorkCompleted(); | 1113 AsyncWorkCompleted(); |
1075 return; | 1114 return; |
1076 } | 1115 } |
1077 | 1116 |
1078 if (!GetTransferSize(transfer, &size)) { | 1117 if (!GetTransferSize(transfer, &size)) { |
1079 CompleteWithError(kErrorInvalidTransferLength); | 1118 CompleteWithError(kErrorInvalidTransferLength); |
1080 return; | 1119 return; |
1081 } | 1120 } |
1082 | 1121 |
1083 scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer( | 1122 scoped_refptr<net::IOBuffer> buffer = |
1084 transfer, direction, size); | 1123 CreateBufferForTransfer(transfer, direction, size); |
1085 if (!buffer.get()) { | 1124 if (!buffer.get()) { |
1086 CompleteWithError(kErrorMalformedParameters); | 1125 CompleteWithError(kErrorMalformedParameters); |
1087 return; | 1126 return; |
1088 } | 1127 } |
1089 | 1128 |
1090 device_handle->InterruptTransfer( | 1129 device_handle->InterruptTransfer( |
1091 direction, | 1130 direction, |
1092 transfer.endpoint, | 1131 transfer.endpoint, |
1093 buffer.get(), | 1132 buffer.get(), |
1094 size, | 1133 size, |
1095 0, | 1134 0, |
1096 base::Bind(&UsbInterruptTransferFunction::OnCompleted, this)); | 1135 base::Bind(&UsbInterruptTransferFunction::OnCompleted, this)); |
1097 } | 1136 } |
1098 | 1137 |
1099 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() {} | 1138 UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() { |
1139 } | |
1100 | 1140 |
1101 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {} | 1141 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() { |
1142 } | |
1102 | 1143 |
1103 bool UsbIsochronousTransferFunction::Prepare() { | 1144 bool UsbIsochronousTransferFunction::Prepare() { |
1104 parameters_ = IsochronousTransfer::Params::Create(*args_); | 1145 parameters_ = IsochronousTransfer::Params::Create(*args_); |
1105 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 1146 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
1106 return true; | 1147 return true; |
1107 } | 1148 } |
1108 | 1149 |
1109 void UsbIsochronousTransferFunction::AsyncWorkStart() { | 1150 void UsbIsochronousTransferFunction::AsyncWorkStart() { |
1110 scoped_refptr<UsbDeviceHandle> device_handle = | 1151 scoped_refptr<UsbDeviceHandle> device_handle = |
1111 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 1152 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
1112 if (!device_handle) return; | 1153 if (!device_handle) |
1154 return; | |
1113 | 1155 |
1114 const IsochronousTransferInfo& transfer = parameters_->transfer_info; | 1156 const IsochronousTransferInfo& transfer = parameters_->transfer_info; |
1115 const GenericTransferInfo& generic_transfer = transfer.transfer_info; | 1157 const GenericTransferInfo& generic_transfer = transfer.transfer_info; |
1116 | 1158 |
1117 size_t size = 0; | 1159 size_t size = 0; |
1118 UsbEndpointDirection direction; | 1160 UsbEndpointDirection direction; |
1119 | 1161 |
1120 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) { | 1162 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) { |
1121 AsyncWorkCompleted(); | 1163 AsyncWorkCompleted(); |
1122 return; | 1164 return; |
(...skipping 12 matching lines...) Expand all Loading... | |
1135 CompleteWithError(kErrorInvalidPacketLength); | 1177 CompleteWithError(kErrorInvalidPacketLength); |
1136 return; | 1178 return; |
1137 } | 1179 } |
1138 unsigned int packet_length = transfer.packet_length; | 1180 unsigned int packet_length = transfer.packet_length; |
1139 const uint64 total_length = packets * packet_length; | 1181 const uint64 total_length = packets * packet_length; |
1140 if (packets > size || total_length > size) { | 1182 if (packets > size || total_length > size) { |
1141 CompleteWithError(kErrorTransferLength); | 1183 CompleteWithError(kErrorTransferLength); |
1142 return; | 1184 return; |
1143 } | 1185 } |
1144 | 1186 |
1145 scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer( | 1187 scoped_refptr<net::IOBuffer> buffer = |
1146 generic_transfer, direction, size); | 1188 CreateBufferForTransfer(generic_transfer, direction, size); |
1147 if (!buffer.get()) { | 1189 if (!buffer.get()) { |
1148 CompleteWithError(kErrorMalformedParameters); | 1190 CompleteWithError(kErrorMalformedParameters); |
1149 return; | 1191 return; |
1150 } | 1192 } |
1151 | 1193 |
1152 device_handle->IsochronousTransfer( | 1194 device_handle->IsochronousTransfer( |
1153 direction, | 1195 direction, |
1154 generic_transfer.endpoint, | 1196 generic_transfer.endpoint, |
1155 buffer.get(), | 1197 buffer.get(), |
1156 size, | 1198 size, |
1157 packets, | 1199 packets, |
1158 packet_length, | 1200 packet_length, |
1159 0, | 1201 0, |
1160 base::Bind(&UsbIsochronousTransferFunction::OnCompleted, this)); | 1202 base::Bind(&UsbIsochronousTransferFunction::OnCompleted, this)); |
1161 } | 1203 } |
1162 | 1204 |
1163 UsbResetDeviceFunction::UsbResetDeviceFunction() {} | 1205 UsbResetDeviceFunction::UsbResetDeviceFunction() { |
1206 } | |
1164 | 1207 |
1165 UsbResetDeviceFunction::~UsbResetDeviceFunction() {} | 1208 UsbResetDeviceFunction::~UsbResetDeviceFunction() { |
1209 } | |
1166 | 1210 |
1167 bool UsbResetDeviceFunction::Prepare() { | 1211 bool UsbResetDeviceFunction::Prepare() { |
1168 parameters_ = ResetDevice::Params::Create(*args_); | 1212 parameters_ = ResetDevice::Params::Create(*args_); |
1169 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 1213 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
1170 return true; | 1214 return true; |
1171 } | 1215 } |
1172 | 1216 |
1173 void UsbResetDeviceFunction::AsyncWorkStart() { | 1217 void UsbResetDeviceFunction::AsyncWorkStart() { |
1174 scoped_refptr<UsbDeviceHandle> device_handle = | 1218 scoped_refptr<UsbDeviceHandle> device_handle = |
1175 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 1219 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
1176 if (!device_handle) return; | 1220 if (!device_handle) |
1221 return; | |
1177 | 1222 |
1178 bool success = device_handle->ResetDevice(); | 1223 bool success = device_handle->ResetDevice(); |
1179 if (!success) { | 1224 if (!success) { |
1180 device_handle->Close(); | 1225 device_handle->Close(); |
1181 RemoveUsbDeviceResource(parameters_->handle.handle); | 1226 RemoveUsbDeviceResource(parameters_->handle.handle); |
1182 SetResult(new base::FundamentalValue(false)); | 1227 SetResult(new base::FundamentalValue(false)); |
1183 CompleteWithError(kErrorResetDevice); | 1228 CompleteWithError(kErrorResetDevice); |
1184 return; | 1229 return; |
1185 } | 1230 } |
1186 | 1231 |
1187 SetResult(new base::FundamentalValue(true)); | 1232 SetResult(new base::FundamentalValue(true)); |
1188 AsyncWorkCompleted(); | 1233 AsyncWorkCompleted(); |
1189 } | 1234 } |
1190 | 1235 |
1191 } // namespace extensions | 1236 } // namespace extensions |
OLD | NEW |