OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/usb/usb_api.h" | 5 #include "chrome/browser/extensions/api/usb/usb_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 "Number of packets must be a positive number less than 4,194,304."; | 105 "Number of packets must be a positive number less than 4,194,304."; |
106 const char kErrorInvalidPacketLength[] = "Packet length must be a " | 106 const char kErrorInvalidPacketLength[] = "Packet length must be a " |
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; | |
116 | |
117 bool ConvertDirectionToApi(const UsbEndpointDirection& input, | 115 bool ConvertDirectionToApi(const UsbEndpointDirection& input, |
118 Direction* output) { | 116 Direction* output) { |
119 switch (input) { | 117 switch (input) { |
120 case usb_service::USB_DIRECTION_INBOUND: | 118 case usb_service::USB_DIRECTION_INBOUND: |
121 *output = usb::DIRECTION_IN; | 119 *output = usb::DIRECTION_IN; |
122 return true; | 120 return true; |
123 case usb_service::USB_DIRECTION_OUTBOUND: | 121 case usb_service::USB_DIRECTION_OUTBOUND: |
124 *output = usb::DIRECTION_OUT; | 122 *output = usb::DIRECTION_OUT; |
125 return true; | 123 return true; |
126 default: | 124 default: |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 return manager_ != NULL; | 410 return manager_ != NULL; |
413 } | 411 } |
414 | 412 |
415 bool UsbAsyncApiFunction::Respond() { | 413 bool UsbAsyncApiFunction::Respond() { |
416 return error_.empty(); | 414 return error_.empty(); |
417 } | 415 } |
418 | 416 |
419 scoped_refptr<UsbDevice> | 417 scoped_refptr<UsbDevice> |
420 UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError( | 418 UsbAsyncApiFunction::GetDeviceOrOrCompleteWithError( |
421 const Device& input_device) { | 419 const Device& input_device) { |
422 if (g_device_for_test) | |
423 return g_device_for_test; | |
424 | |
425 const uint16_t vendor_id = input_device.vendor_id; | 420 const uint16_t vendor_id = input_device.vendor_id; |
426 const uint16_t product_id = input_device.product_id; | 421 const uint16_t product_id = input_device.product_id; |
427 UsbDevicePermission::CheckParam param( | 422 UsbDevicePermission::CheckParam param( |
428 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 423 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); |
429 if (!PermissionsData::CheckAPIPermissionWithParam( | 424 if (!PermissionsData::CheckAPIPermissionWithParam( |
430 GetExtension(), APIPermission::kUsbDevice, ¶m)) { | 425 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
431 LOG(WARNING) << "Insufficient permissions to access device."; | 426 LOG(WARNING) << "Insufficient permissions to access device."; |
432 CompleteWithError(kErrorPermissionDenied); | 427 CompleteWithError(kErrorPermissionDenied); |
433 return NULL; | 428 return NULL; |
434 } | 429 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 | 533 |
539 bool UsbFindDevicesFunction::Prepare() { | 534 bool UsbFindDevicesFunction::Prepare() { |
540 parameters_ = FindDevices::Params::Create(*args_); | 535 parameters_ = FindDevices::Params::Create(*args_); |
541 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 536 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
542 return true; | 537 return true; |
543 } | 538 } |
544 | 539 |
545 void UsbFindDevicesFunction::AsyncWorkStart() { | 540 void UsbFindDevicesFunction::AsyncWorkStart() { |
546 scoped_ptr<base::ListValue> result(new base::ListValue()); | 541 scoped_ptr<base::ListValue> result(new base::ListValue()); |
547 | 542 |
548 if (g_device_for_test) { | |
549 UsbDeviceResource* const resource = new UsbDeviceResource( | |
550 extension_->id(), | |
551 g_device_for_test->Open()); | |
552 | |
553 result->Append(PopulateConnectionHandle(manager_->Add(resource), 0, 0)); | |
554 SetResult(result.release()); | |
555 AsyncWorkCompleted(); | |
556 return; | |
557 } | |
558 | |
559 const uint16_t vendor_id = parameters_->options.vendor_id; | 543 const uint16_t vendor_id = parameters_->options.vendor_id; |
560 const uint16_t product_id = parameters_->options.product_id; | 544 const uint16_t product_id = parameters_->options.product_id; |
561 int interface_id = parameters_->options.interface_id.get() ? | 545 int interface_id = parameters_->options.interface_id.get() ? |
562 *parameters_->options.interface_id.get() : | 546 *parameters_->options.interface_id.get() : |
563 UsbDevicePermissionData::ANY_INTERFACE; | 547 UsbDevicePermissionData::ANY_INTERFACE; |
564 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); | 548 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); |
565 if (!PermissionsData::CheckAPIPermissionWithParam( | 549 if (!PermissionsData::CheckAPIPermissionWithParam( |
566 GetExtension(), APIPermission::kUsbDevice, ¶m)) { | 550 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
567 LOG(WARNING) << "Insufficient permissions to access device."; | 551 LOG(WARNING) << "Insufficient permissions to access device."; |
568 CompleteWithError(kErrorPermissionDenied); | 552 CompleteWithError(kErrorPermissionDenied); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 SetResult(result); | 603 SetResult(result); |
620 AsyncWorkCompleted(); | 604 AsyncWorkCompleted(); |
621 } | 605 } |
622 | 606 |
623 UsbGetDevicesFunction::UsbGetDevicesFunction() { | 607 UsbGetDevicesFunction::UsbGetDevicesFunction() { |
624 } | 608 } |
625 | 609 |
626 UsbGetDevicesFunction::~UsbGetDevicesFunction() { | 610 UsbGetDevicesFunction::~UsbGetDevicesFunction() { |
627 } | 611 } |
628 | 612 |
629 void UsbGetDevicesFunction::SetDeviceForTest(UsbDevice* device) { | |
630 g_device_for_test = device; | |
631 } | |
632 | |
633 bool UsbGetDevicesFunction::Prepare() { | 613 bool UsbGetDevicesFunction::Prepare() { |
634 parameters_ = GetDevices::Params::Create(*args_); | 614 parameters_ = GetDevices::Params::Create(*args_); |
635 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 615 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
636 return true; | 616 return true; |
637 } | 617 } |
638 | 618 |
639 void UsbGetDevicesFunction::AsyncWorkStart() { | 619 void UsbGetDevicesFunction::AsyncWorkStart() { |
640 scoped_ptr<base::ListValue> result(new base::ListValue()); | 620 scoped_ptr<base::ListValue> result(new base::ListValue()); |
641 | 621 |
642 if (g_device_for_test) { | |
643 result->Append(PopulateDevice(g_device_for_test)); | |
644 SetResult(result.release()); | |
645 AsyncWorkCompleted(); | |
646 return; | |
647 } | |
648 | |
649 const uint16_t vendor_id = parameters_->options.vendor_id; | 622 const uint16_t vendor_id = parameters_->options.vendor_id; |
650 const uint16_t product_id = parameters_->options.product_id; | 623 const uint16_t product_id = parameters_->options.product_id; |
651 UsbDevicePermission::CheckParam param( | 624 UsbDevicePermission::CheckParam param( |
652 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 625 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); |
653 if (!PermissionsData::CheckAPIPermissionWithParam( | 626 if (!PermissionsData::CheckAPIPermissionWithParam( |
654 GetExtension(), APIPermission::kUsbDevice, ¶m)) { | 627 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
655 LOG(WARNING) << "Insufficient permissions to access device."; | 628 LOG(WARNING) << "Insufficient permissions to access device."; |
656 CompleteWithError(kErrorPermissionDenied); | 629 CompleteWithError(kErrorPermissionDenied); |
657 return; | 630 return; |
658 } | 631 } |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1182 SetResult(new base::FundamentalValue(false)); | 1155 SetResult(new base::FundamentalValue(false)); |
1183 CompleteWithError(kErrorResetDevice); | 1156 CompleteWithError(kErrorResetDevice); |
1184 return; | 1157 return; |
1185 } | 1158 } |
1186 | 1159 |
1187 SetResult(new base::FundamentalValue(true)); | 1160 SetResult(new base::FundamentalValue(true)); |
1188 AsyncWorkCompleted(); | 1161 AsyncWorkCompleted(); |
1189 } | 1162 } |
1190 | 1163 |
1191 } // namespace extensions | 1164 } // namespace extensions |
OLD | NEW |