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

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

Issue 599303004: Add getUserSelectedDevices to the USB extensions API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add device_permissions_prompt.* to BUILD.gn. Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/usb/usb_api.h" 5 #include "extensions/browser/api/usb/usb_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop_proxy.h" 11 #include "base/message_loop/message_loop_proxy.h"
12 #include "device/core/device_client.h" 12 #include "device/core/device_client.h"
13 #include "device/usb/usb_device_handle.h" 13 #include "device/usb/usb_device_handle.h"
14 #include "device/usb/usb_service.h" 14 #include "device/usb/usb_service.h"
15 #include "extensions/browser/api/device_permissions_manager.h"
16 #include "extensions/browser/api/device_permissions_prompt.h"
17 #include "extensions/browser/api/extensions_api_client.h"
15 #include "extensions/browser/api/usb/usb_device_resource.h" 18 #include "extensions/browser/api/usb/usb_device_resource.h"
16 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
17 #include "extensions/common/api/usb.h" 20 #include "extensions/common/api/usb.h"
18 #include "extensions/common/permissions/permissions_data.h" 21 #include "extensions/common/permissions/permissions_data.h"
19 #include "extensions/common/permissions/usb_device_permission.h" 22 #include "extensions/common/permissions/usb_device_permission.h"
20 23
21 namespace usb = extensions::core_api::usb; 24 namespace usb = extensions::core_api::usb;
22 namespace BulkTransfer = usb::BulkTransfer; 25 namespace BulkTransfer = usb::BulkTransfer;
23 namespace ClaimInterface = usb::ClaimInterface; 26 namespace ClaimInterface = usb::ClaimInterface;
24 namespace CloseDevice = usb::CloseDevice; 27 namespace CloseDevice = usb::CloseDevice;
25 namespace ControlTransfer = usb::ControlTransfer; 28 namespace ControlTransfer = usb::ControlTransfer;
26 namespace FindDevices = usb::FindDevices; 29 namespace FindDevices = usb::FindDevices;
27 namespace GetDevices = usb::GetDevices; 30 namespace GetDevices = usb::GetDevices;
31 namespace GetUserSelectedDevices = usb::GetUserSelectedDevices;
28 namespace InterruptTransfer = usb::InterruptTransfer; 32 namespace InterruptTransfer = usb::InterruptTransfer;
29 namespace IsochronousTransfer = usb::IsochronousTransfer; 33 namespace IsochronousTransfer = usb::IsochronousTransfer;
30 namespace GetConfiguration = usb::GetConfiguration; 34 namespace GetConfiguration = usb::GetConfiguration;
31 namespace ListInterfaces = usb::ListInterfaces; 35 namespace ListInterfaces = usb::ListInterfaces;
32 namespace OpenDevice = usb::OpenDevice; 36 namespace OpenDevice = usb::OpenDevice;
33 namespace ReleaseInterface = usb::ReleaseInterface; 37 namespace ReleaseInterface = usb::ReleaseInterface;
34 namespace RequestAccess = usb::RequestAccess; 38 namespace RequestAccess = usb::RequestAccess;
35 namespace ResetDevice = usb::ResetDevice; 39 namespace ResetDevice = usb::ResetDevice;
36 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; 40 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting;
37 41
38 using content::BrowserThread; 42 using content::BrowserThread;
43 using device::UsbConfigDescriptor;
44 using device::UsbDevice;
45 using device::UsbDeviceFilter;
46 using device::UsbDeviceHandle;
47 using device::UsbEndpointDescriptor;
48 using device::UsbEndpointDirection;
49 using device::UsbInterfaceDescriptor;
50 using device::UsbService;
51 using device::UsbSynchronizationType;
52 using device::UsbTransferStatus;
53 using device::UsbTransferType;
54 using device::UsbUsageType;
55 using std::string;
56 using std::vector;
39 using usb::ConfigDescriptor; 57 using usb::ConfigDescriptor;
40 using usb::ControlTransferInfo; 58 using usb::ControlTransferInfo;
41 using usb::ConnectionHandle; 59 using usb::ConnectionHandle;
42 using usb::Device; 60 using usb::Device;
43 using usb::Direction; 61 using usb::Direction;
44 using usb::EndpointDescriptor; 62 using usb::EndpointDescriptor;
45 using usb::GenericTransferInfo; 63 using usb::GenericTransferInfo;
46 using usb::InterfaceDescriptor; 64 using usb::InterfaceDescriptor;
47 using usb::IsochronousTransferInfo; 65 using usb::IsochronousTransferInfo;
48 using usb::Recipient; 66 using usb::Recipient;
49 using usb::RequestType; 67 using usb::RequestType;
50 using usb::SynchronizationType; 68 using usb::SynchronizationType;
51 using usb::TransferType; 69 using usb::TransferType;
52 using usb::UsageType; 70 using usb::UsageType;
53 using device::UsbConfigDescriptor;
54 using device::UsbDevice;
55 using device::UsbDeviceFilter;
56 using device::UsbDeviceHandle;
57 using device::UsbEndpointDescriptor;
58 using device::UsbEndpointDirection;
59 using device::UsbInterfaceDescriptor;
60 using device::UsbService;
61 using device::UsbSynchronizationType;
62 using device::UsbTransferStatus;
63 using device::UsbTransferType;
64 using device::UsbUsageType;
65 71
66 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; 72 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector;
67 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; 73 typedef scoped_ptr<DeviceVector> ScopedDeviceVector;
68 74
69 namespace { 75 namespace {
70 76
71 const char kDataKey[] = "data"; 77 const char kDataKey[] = "data";
72 const char kResultCodeKey[] = "resultCode"; 78 const char kResultCodeKey[] = "resultCode";
73 79
74 const char kErrorInitService[] = "Failed to initialize USB service."; 80 const char kErrorInitService[] = "Failed to initialize USB service.";
75 81
76 const char kErrorOpen[] = "Failed to open device."; 82 const char kErrorOpen[] = "Failed to open device.";
77 const char kErrorCancelled[] = "Transfer was cancelled."; 83 const char kErrorCancelled[] = "Transfer was cancelled.";
78 const char kErrorDisconnect[] = "Device disconnected."; 84 const char kErrorDisconnect[] = "Device disconnected.";
79 const char kErrorGeneric[] = "Transfer failed."; 85 const char kErrorGeneric[] = "Transfer failed.";
80 #if !defined(OS_CHROMEOS)
81 const char kErrorNotSupported[] = "Not supported on this platform."; 86 const char kErrorNotSupported[] = "Not supported on this platform.";
82 #endif
83 const char kErrorOverflow[] = "Inbound transfer overflow."; 87 const char kErrorOverflow[] = "Inbound transfer overflow.";
84 const char kErrorStalled[] = "Transfer stalled."; 88 const char kErrorStalled[] = "Transfer stalled.";
85 const char kErrorTimeout[] = "Transfer timed out."; 89 const char kErrorTimeout[] = "Transfer timed out.";
86 const char kErrorTransferLength[] = "Transfer length is insufficient."; 90 const char kErrorTransferLength[] = "Transfer length is insufficient.";
87 const char kErrorCannotClaimInterface[] = "Error claiming interface."; 91 const char kErrorCannotClaimInterface[] = "Error claiming interface.";
88 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; 92 const char kErrorCannotReleaseInterface[] = "Error releasing interface.";
89 const char kErrorCannotSetInterfaceAlternateSetting[] = 93 const char kErrorCannotSetInterfaceAlternateSetting[] =
90 "Error setting alternate interface setting."; 94 "Error setting alternate interface setting.";
91 const char kErrorConvertDirection[] = "Invalid transfer direction."; 95 const char kErrorConvertDirection[] = "Invalid transfer direction.";
92 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; 96 const char kErrorConvertRecipient[] = "Invalid transfer recipient.";
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 base::Value* PopulateConnectionHandle(int handle, 291 base::Value* PopulateConnectionHandle(int handle,
288 int vendor_id, 292 int vendor_id,
289 int product_id) { 293 int product_id) {
290 ConnectionHandle result; 294 ConnectionHandle result;
291 result.handle = handle; 295 result.handle = handle;
292 result.vendor_id = vendor_id; 296 result.vendor_id = vendor_id;
293 result.product_id = product_id; 297 result.product_id = product_id;
294 return result.ToValue().release(); 298 return result.ToValue().release();
295 } 299 }
296 300
297 base::Value* PopulateDevice(UsbDevice* device) { 301 base::Value* PopulateDevice(const UsbDevice* device) {
298 Device result; 302 Device result;
299 result.device = device->unique_id(); 303 result.device = device->unique_id();
300 result.vendor_id = device->vendor_id(); 304 result.vendor_id = device->vendor_id();
301 result.product_id = device->product_id(); 305 result.product_id = device->product_id();
302 return result.ToValue().release(); 306 return result.ToValue().release();
303 } 307 }
304 308
305 TransferType ConvertTransferTypeToApi(const UsbTransferType& input) { 309 TransferType ConvertTransferTypeToApi(const UsbTransferType& input) {
306 switch (input) { 310 switch (input) {
307 case device::USB_TRANSFER_CONTROL: 311 case device::USB_TRANSFER_CONTROL:
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 ConvertInterfaceDescriptor(*interfaceIt, interface.get()); 416 ConvertInterfaceDescriptor(*interfaceIt, interface.get());
413 output->interfaces.push_back(interface); 417 output->interfaces.push_back(interface);
414 } 418 }
415 if (input.extra_data.size() > 0) { 419 if (input.extra_data.size() > 0) {
416 output->extra_data = 420 output->extra_data =
417 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), 421 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
418 input.extra_data.size()); 422 input.extra_data.size());
419 } 423 }
420 } 424 }
421 425
426 void ConvertDeviceFilter(const usb::DeviceFilter& input,
427 UsbDeviceFilter* output) {
428 if (input.vendor_id) {
429 output->SetVendorId(*input.vendor_id);
430 }
431 if (input.product_id) {
432 output->SetProductId(*input.product_id);
433 }
434 if (input.interface_class) {
435 output->SetInterfaceClass(*input.interface_class);
436 }
437 if (input.interface_subclass) {
438 output->SetInterfaceSubclass(*input.interface_subclass);
439 }
440 if (input.interface_protocol) {
441 output->SetInterfaceProtocol(*input.interface_protocol);
442 }
443 }
444
422 } // namespace 445 } // namespace
423 446
424 namespace extensions { 447 namespace extensions {
425 448
426 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { 449 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) {
427 } 450 }
428 451
429 UsbAsyncApiFunction::~UsbAsyncApiFunction() { 452 UsbAsyncApiFunction::~UsbAsyncApiFunction() {
430 } 453 }
431 454
432 bool UsbAsyncApiFunction::PrePrepare() { 455 bool UsbAsyncApiFunction::PrePrepare() {
433 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context()); 456 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context());
434 set_work_thread_id(BrowserThread::FILE); 457 set_work_thread_id(BrowserThread::FILE);
435 return manager_ != NULL; 458 return manager_ != NULL;
436 } 459 }
437 460
438 bool UsbAsyncApiFunction::Respond() { 461 bool UsbAsyncApiFunction::Respond() {
439 return error_.empty(); 462 return error_.empty();
440 } 463 }
441 464
442 // static
443 void UsbAsyncApiFunction::CreateDeviceFilter(const usb::DeviceFilter& input,
444 UsbDeviceFilter* output) {
445 if (input.vendor_id) {
446 output->SetVendorId(*input.vendor_id);
447 }
448 if (input.product_id) {
449 output->SetProductId(*input.product_id);
450 }
451 if (input.interface_class) {
452 output->SetInterfaceClass(*input.interface_class);
453 }
454 if (input.interface_subclass) {
455 output->SetInterfaceSubclass(*input.interface_subclass);
456 }
457 if (input.interface_protocol) {
458 output->SetInterfaceProtocol(*input.interface_protocol);
459 }
460 }
461
462 bool UsbAsyncApiFunction::HasDevicePermission(scoped_refptr<UsbDevice> device) { 465 bool UsbAsyncApiFunction::HasDevicePermission(scoped_refptr<UsbDevice> device) {
463 UsbDevicePermission::CheckParam param( 466 UsbDevicePermission::CheckParam param(
464 device->vendor_id(), 467 device->vendor_id(),
465 device->product_id(), 468 device->product_id(),
466 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); 469 UsbDevicePermissionData::UNSPECIFIED_INTERFACE);
467 return extension()->permissions_data()->CheckAPIPermissionWithParam( 470 if (extension()->permissions_data()->CheckAPIPermissionWithParam(
468 APIPermission::kUsbDevice, &param); 471 APIPermission::kUsbDevice, &param)) {
472 return true;
473 }
474
475 if (device_permissions_.get()) {
476 return device_permissions_->CheckUsbDevice(device);
477 }
478
479 return false;
469 } 480 }
470 481
471 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError( 482 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError(
472 const Device& input_device) { 483 const Device& input_device) {
473 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 484 UsbService* service = device::DeviceClient::Get()->GetUsbService();
474 if (!service) { 485 if (!service) {
475 CompleteWithError(kErrorInitService); 486 CompleteWithError(kErrorInitService);
476 return NULL; 487 return NULL;
477 } 488 }
478 489
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 661
651 UsbGetDevicesFunction::UsbGetDevicesFunction() { 662 UsbGetDevicesFunction::UsbGetDevicesFunction() {
652 } 663 }
653 664
654 UsbGetDevicesFunction::~UsbGetDevicesFunction() { 665 UsbGetDevicesFunction::~UsbGetDevicesFunction() {
655 } 666 }
656 667
657 bool UsbGetDevicesFunction::Prepare() { 668 bool UsbGetDevicesFunction::Prepare() {
658 parameters_ = GetDevices::Params::Create(*args_); 669 parameters_ = GetDevices::Params::Create(*args_);
659 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 670 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
671 device_permissions_ = DevicePermissionsManager::Get(browser_context())
672 ->GetForExtension(extension()->id());
660 return true; 673 return true;
661 } 674 }
662 675
663 void UsbGetDevicesFunction::AsyncWorkStart() { 676 void UsbGetDevicesFunction::AsyncWorkStart() {
664 std::vector<UsbDeviceFilter> filters; 677 std::vector<UsbDeviceFilter> filters;
665 if (parameters_->options.filters) { 678 if (parameters_->options.filters) {
666 filters.resize(parameters_->options.filters->size()); 679 filters.resize(parameters_->options.filters->size());
667 for (size_t i = 0; i < parameters_->options.filters->size(); ++i) { 680 for (size_t i = 0; i < parameters_->options.filters->size(); ++i) {
668 CreateDeviceFilter(*parameters_->options.filters->at(i).get(), 681 ConvertDeviceFilter(*parameters_->options.filters->at(i).get(),
669 &filters[i]); 682 &filters[i]);
670 } 683 }
671 } 684 }
672 if (parameters_->options.vendor_id) { 685 if (parameters_->options.vendor_id) {
673 filters.resize(filters.size() + 1); 686 filters.resize(filters.size() + 1);
674 filters.back().SetVendorId(*parameters_->options.vendor_id); 687 filters.back().SetVendorId(*parameters_->options.vendor_id);
675 if (parameters_->options.product_id) { 688 if (parameters_->options.product_id) {
676 filters.back().SetProductId(*parameters_->options.product_id); 689 filters.back().SetProductId(*parameters_->options.product_id);
677 } 690 }
678 } 691 }
679 692
(...skipping 12 matching lines...) Expand all
692 if ((filters.empty() || UsbDeviceFilter::MatchesAny(device, filters)) && 705 if ((filters.empty() || UsbDeviceFilter::MatchesAny(device, filters)) &&
693 HasDevicePermission(device)) { 706 HasDevicePermission(device)) {
694 result->Append(PopulateDevice(it->get())); 707 result->Append(PopulateDevice(it->get()));
695 } 708 }
696 } 709 }
697 710
698 SetResult(result.release()); 711 SetResult(result.release());
699 AsyncWorkCompleted(); 712 AsyncWorkCompleted();
700 } 713 }
701 714
715 UsbGetUserSelectedDevicesFunction::UsbGetUserSelectedDevicesFunction() {
716 }
717
718 UsbGetUserSelectedDevicesFunction::~UsbGetUserSelectedDevicesFunction() {
719 }
720
721 AsyncApiFunction::ResponseAction UsbGetUserSelectedDevicesFunction::Run() {
722 scoped_ptr<extensions::core_api::usb::GetUserSelectedDevices::Params>
723 parameters = GetUserSelectedDevices::Params::Create(*args_);
724 EXTENSION_FUNCTION_VALIDATE(parameters.get());
725
726 if (!user_gesture()) {
727 return RespondNow(OneArgument(new base::ListValue()));
728 }
729
730 bool multiple = false;
731 if (parameters->options.multiple) {
732 multiple = *parameters->options.multiple;
733 }
734
735 std::vector<UsbDeviceFilter> filters;
736 if (parameters->options.filters) {
737 filters.resize(parameters->options.filters->size());
738 for (size_t i = 0; i < parameters->options.filters->size(); ++i) {
739 ConvertDeviceFilter(*parameters->options.filters->at(i).get(),
740 &filters[i]);
741 }
742 }
743
744 prompt_ = ExtensionsAPIClient::Get()->CreateDevicePermissionsPrompt(
745 GetAssociatedWebContents());
746 if (!prompt_) {
747 return RespondNow(Error(kErrorNotSupported));
748 }
749
750 AddRef();
751 prompt_->AskForUsbDevices(
752 this, extension(), browser_context(), multiple, filters);
753 return RespondLater();
754 }
755
756 void UsbGetUserSelectedDevicesFunction::OnUsbDevicesChosen(
757 const std::vector<scoped_refptr<UsbDevice>>& devices) {
758 scoped_ptr<base::ListValue> result(new base::ListValue());
759 for (const auto& device : devices) {
760 result->Append(PopulateDevice(device.get()));
761 }
762
763 SetResult(result.release());
764 SendResponse(true);
765 Release();
766 }
767
702 UsbRequestAccessFunction::UsbRequestAccessFunction() { 768 UsbRequestAccessFunction::UsbRequestAccessFunction() {
703 } 769 }
704 770
705 UsbRequestAccessFunction::~UsbRequestAccessFunction() { 771 UsbRequestAccessFunction::~UsbRequestAccessFunction() {
706 } 772 }
707 773
708 bool UsbRequestAccessFunction::Prepare() { 774 bool UsbRequestAccessFunction::Prepare() {
709 parameters_ = RequestAccess::Params::Create(*args_); 775 parameters_ = RequestAccess::Params::Create(*args_);
710 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 776 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
777 device_permissions_ = DevicePermissionsManager::Get(browser_context())
778 ->GetForExtension(extension()->id());
711 return true; 779 return true;
712 } 780 }
713 781
714 void UsbRequestAccessFunction::AsyncWorkStart() { 782 void UsbRequestAccessFunction::AsyncWorkStart() {
715 #if defined(OS_CHROMEOS) 783 #if defined(OS_CHROMEOS)
716 scoped_refptr<UsbDevice> device = 784 scoped_refptr<UsbDevice> device =
717 GetDeviceOrCompleteWithError(parameters_->device); 785 GetDeviceOrCompleteWithError(parameters_->device);
718 if (!device.get()) 786 if (!device.get())
719 return; 787 return;
720 788
(...skipping 13 matching lines...) Expand all
734 802
735 UsbOpenDeviceFunction::UsbOpenDeviceFunction() { 803 UsbOpenDeviceFunction::UsbOpenDeviceFunction() {
736 } 804 }
737 805
738 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() { 806 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() {
739 } 807 }
740 808
741 bool UsbOpenDeviceFunction::Prepare() { 809 bool UsbOpenDeviceFunction::Prepare() {
742 parameters_ = OpenDevice::Params::Create(*args_); 810 parameters_ = OpenDevice::Params::Create(*args_);
743 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 811 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
812 device_permissions_ = DevicePermissionsManager::Get(browser_context())
813 ->GetForExtension(extension()->id());
744 return true; 814 return true;
745 } 815 }
746 816
747 void UsbOpenDeviceFunction::AsyncWorkStart() { 817 void UsbOpenDeviceFunction::AsyncWorkStart() {
748 scoped_refptr<UsbDevice> device = 818 scoped_refptr<UsbDevice> device =
749 GetDeviceOrCompleteWithError(parameters_->device); 819 GetDeviceOrCompleteWithError(parameters_->device);
750 if (!device.get()) 820 if (!device.get())
751 return; 821 return;
752 822
753 handle_ = device->Open(); 823 handle_ = device->Open();
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
1170 SetResult(new base::FundamentalValue(false)); 1240 SetResult(new base::FundamentalValue(false));
1171 CompleteWithError(kErrorResetDevice); 1241 CompleteWithError(kErrorResetDevice);
1172 return; 1242 return;
1173 } 1243 }
1174 1244
1175 SetResult(new base::FundamentalValue(true)); 1245 SetResult(new base::FundamentalValue(true));
1176 AsyncWorkCompleted(); 1246 AsyncWorkCompleted();
1177 } 1247 }
1178 1248
1179 } // namespace extensions 1249 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/extension_function_histogram_value.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698