Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(471)

Side by Side Diff: extensions/browser/api/usb/usb_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698