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

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

Issue 826283002: Add support for sending a USB SET_CONFIGURATION request. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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"
(...skipping 13 matching lines...) Expand all
24 namespace usb = extensions::core_api::usb; 24 namespace usb = extensions::core_api::usb;
25 namespace BulkTransfer = usb::BulkTransfer; 25 namespace BulkTransfer = usb::BulkTransfer;
26 namespace ClaimInterface = usb::ClaimInterface; 26 namespace ClaimInterface = usb::ClaimInterface;
27 namespace CloseDevice = usb::CloseDevice; 27 namespace CloseDevice = usb::CloseDevice;
28 namespace ControlTransfer = usb::ControlTransfer; 28 namespace ControlTransfer = usb::ControlTransfer;
29 namespace FindDevices = usb::FindDevices; 29 namespace FindDevices = usb::FindDevices;
30 namespace GetDevices = usb::GetDevices; 30 namespace GetDevices = usb::GetDevices;
31 namespace GetUserSelectedDevices = usb::GetUserSelectedDevices; 31 namespace GetUserSelectedDevices = usb::GetUserSelectedDevices;
32 namespace InterruptTransfer = usb::InterruptTransfer; 32 namespace InterruptTransfer = usb::InterruptTransfer;
33 namespace IsochronousTransfer = usb::IsochronousTransfer; 33 namespace IsochronousTransfer = usb::IsochronousTransfer;
34 namespace SetConfiguration = usb::SetConfiguration;
34 namespace GetConfiguration = usb::GetConfiguration; 35 namespace GetConfiguration = usb::GetConfiguration;
35 namespace ListInterfaces = usb::ListInterfaces; 36 namespace ListInterfaces = usb::ListInterfaces;
36 namespace OpenDevice = usb::OpenDevice; 37 namespace OpenDevice = usb::OpenDevice;
37 namespace ReleaseInterface = usb::ReleaseInterface; 38 namespace ReleaseInterface = usb::ReleaseInterface;
38 namespace RequestAccess = usb::RequestAccess; 39 namespace RequestAccess = usb::RequestAccess;
39 namespace ResetDevice = usb::ResetDevice; 40 namespace ResetDevice = usb::ResetDevice;
40 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; 41 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting;
41 42
42 using content::BrowserThread; 43 using content::BrowserThread;
43 using device::UsbConfigDescriptor; 44 using device::UsbConfigDescriptor;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 const char kDataKey[] = "data"; 78 const char kDataKey[] = "data";
78 const char kResultCodeKey[] = "resultCode"; 79 const char kResultCodeKey[] = "resultCode";
79 80
80 const char kErrorInitService[] = "Failed to initialize USB service."; 81 const char kErrorInitService[] = "Failed to initialize USB service.";
81 82
82 const char kErrorOpen[] = "Failed to open device."; 83 const char kErrorOpen[] = "Failed to open device.";
83 const char kErrorCancelled[] = "Transfer was cancelled."; 84 const char kErrorCancelled[] = "Transfer was cancelled.";
84 const char kErrorDisconnect[] = "Device disconnected."; 85 const char kErrorDisconnect[] = "Device disconnected.";
85 const char kErrorGeneric[] = "Transfer failed."; 86 const char kErrorGeneric[] = "Transfer failed.";
86 const char kErrorNotSupported[] = "Not supported on this platform."; 87 const char kErrorNotSupported[] = "Not supported on this platform.";
88 const char kErrorNotConfigured[] = "The device is not in a configured state.";
87 const char kErrorOverflow[] = "Inbound transfer overflow."; 89 const char kErrorOverflow[] = "Inbound transfer overflow.";
88 const char kErrorStalled[] = "Transfer stalled."; 90 const char kErrorStalled[] = "Transfer stalled.";
89 const char kErrorTimeout[] = "Transfer timed out."; 91 const char kErrorTimeout[] = "Transfer timed out.";
90 const char kErrorTransferLength[] = "Transfer length is insufficient."; 92 const char kErrorTransferLength[] = "Transfer length is insufficient.";
93 const char kErrorCannotSetConfiguration[] =
94 "Error setting device configuration.";
91 const char kErrorCannotClaimInterface[] = "Error claiming interface."; 95 const char kErrorCannotClaimInterface[] = "Error claiming interface.";
92 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; 96 const char kErrorCannotReleaseInterface[] = "Error releasing interface.";
93 const char kErrorCannotSetInterfaceAlternateSetting[] = 97 const char kErrorCannotSetInterfaceAlternateSetting[] =
94 "Error setting alternate interface setting."; 98 "Error setting alternate interface setting.";
95 const char kErrorConvertDirection[] = "Invalid transfer direction."; 99 const char kErrorConvertDirection[] = "Invalid transfer direction.";
96 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; 100 const char kErrorConvertRecipient[] = "Invalid transfer recipient.";
97 const char kErrorConvertRequestType[] = "Invalid request type."; 101 const char kErrorConvertRequestType[] = "Invalid request type.";
98 const char kErrorMalformedParameters[] = "Error parsing parameters."; 102 const char kErrorMalformedParameters[] = "Error parsing parameters.";
99 const char kErrorNoDevice[] = "No such device."; 103 const char kErrorNoDevice[] = "No such device.";
100 const char kErrorPermissionDenied[] = "Permission to access device was denied"; 104 const char kErrorPermissionDenied[] = "Permission to access device was denied";
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 } 386 }
383 } 387 }
384 388
385 void ConvertInterfaceDescriptor(const UsbInterfaceDescriptor& input, 389 void ConvertInterfaceDescriptor(const UsbInterfaceDescriptor& input,
386 InterfaceDescriptor* output) { 390 InterfaceDescriptor* output) {
387 output->interface_number = input.interface_number; 391 output->interface_number = input.interface_number;
388 output->alternate_setting = input.alternate_setting; 392 output->alternate_setting = input.alternate_setting;
389 output->interface_class = input.interface_class; 393 output->interface_class = input.interface_class;
390 output->interface_subclass = input.interface_subclass; 394 output->interface_subclass = input.interface_subclass;
391 output->interface_protocol = input.interface_protocol; 395 output->interface_protocol = input.interface_protocol;
392 for (UsbEndpointDescriptor::Iterator endpointIt = input.endpoints.begin(); 396 for (const UsbEndpointDescriptor& input_endpoint : input.endpoints) {
393 endpointIt != input.endpoints.end();
394 ++endpointIt) {
395 linked_ptr<EndpointDescriptor> endpoint(new EndpointDescriptor); 397 linked_ptr<EndpointDescriptor> endpoint(new EndpointDescriptor);
396 ConvertEndpointDescriptor(*endpointIt, endpoint.get()); 398 ConvertEndpointDescriptor(input_endpoint, endpoint.get());
397 output->endpoints.push_back(endpoint); 399 output->endpoints.push_back(endpoint);
398 } 400 }
399 if (input.extra_data.size() > 0) { 401 if (input.extra_data.size() > 0) {
400 output->extra_data = 402 output->extra_data =
401 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), 403 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
402 input.extra_data.size()); 404 input.extra_data.size());
403 } 405 }
404 } 406 }
405 407
406 void ConvertConfigDescriptor(const UsbConfigDescriptor& input, 408 void ConvertConfigDescriptor(const UsbConfigDescriptor& input,
407 ConfigDescriptor* output) { 409 ConfigDescriptor* output) {
408 output->configuration_value = input.configuration_value; 410 output->configuration_value = input.configuration_value;
409 output->self_powered = input.self_powered; 411 output->self_powered = input.self_powered;
410 output->remote_wakeup = input.remote_wakeup; 412 output->remote_wakeup = input.remote_wakeup;
411 output->max_power = input.maximum_power; 413 output->max_power = input.maximum_power;
412 for (UsbInterfaceDescriptor::Iterator interfaceIt = input.interfaces.begin(); 414 for (const UsbInterfaceDescriptor& input_interface : input.interfaces) {
413 interfaceIt != input.interfaces.end();
414 ++interfaceIt) {
415 linked_ptr<InterfaceDescriptor> interface(new InterfaceDescriptor); 415 linked_ptr<InterfaceDescriptor> interface(new InterfaceDescriptor);
416 ConvertInterfaceDescriptor(*interfaceIt, interface.get()); 416 ConvertInterfaceDescriptor(input_interface, interface.get());
417 output->interfaces.push_back(interface); 417 output->interfaces.push_back(interface);
418 } 418 }
419 if (input.extra_data.size() > 0) { 419 if (input.extra_data.size() > 0) {
420 output->extra_data = 420 output->extra_data =
421 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), 421 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
422 input.extra_data.size()); 422 input.extra_data.size());
423 } 423 }
424 } 424 }
425 425
426 void ConvertDeviceFilter(const usb::DeviceFilter& input, 426 void ConvertDeviceFilter(const usb::DeviceFilter& input,
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 } 829 }
830 830
831 bool UsbOpenDeviceFunction::Respond() { 831 bool UsbOpenDeviceFunction::Respond() {
832 if (permission_entry_.get()) { 832 if (permission_entry_.get()) {
833 device_permissions_manager_->UpdateLastUsed(extension_->id(), 833 device_permissions_manager_->UpdateLastUsed(extension_->id(),
834 permission_entry_); 834 permission_entry_);
835 } 835 }
836 return UsbAsyncApiFunction::Respond(); 836 return UsbAsyncApiFunction::Respond();
837 } 837 }
838 838
839 UsbSetConfigurationFunction::UsbSetConfigurationFunction() {
840 }
841
842 UsbSetConfigurationFunction::~UsbSetConfigurationFunction() {
843 }
844
845 bool UsbSetConfigurationFunction::Prepare() {
846 parameters_ = SetConfiguration::Params::Create(*args_);
847 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
848 return true;
849 }
850
851 void UsbSetConfigurationFunction::AsyncWorkStart() {
852 scoped_refptr<UsbDeviceHandle> device_handle =
853 GetDeviceHandleOrCompleteWithError(parameters_->handle);
854 if (!device_handle.get()) {
855 return;
856 }
857
858 if (device_handle->SetConfiguration(parameters_->configuration_value)) {
859 SetResult(new base::FundamentalValue(true));
860 } else {
861 SetError(kErrorCannotSetConfiguration);
862 }
863 AsyncWorkCompleted();
864 }
865
839 UsbGetConfigurationFunction::UsbGetConfigurationFunction() { 866 UsbGetConfigurationFunction::UsbGetConfigurationFunction() {
840 } 867 }
841 868
842 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() { 869 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() {
843 } 870 }
844 871
845 bool UsbGetConfigurationFunction::Prepare() { 872 bool UsbGetConfigurationFunction::Prepare() {
846 parameters_ = GetConfiguration::Params::Create(*args_); 873 parameters_ = GetConfiguration::Params::Create(*args_);
847 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 874 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
848 return true; 875 return true;
849 } 876 }
850 877
851 void UsbGetConfigurationFunction::AsyncWorkStart() { 878 void UsbGetConfigurationFunction::AsyncWorkStart() {
852 scoped_refptr<UsbDeviceHandle> device_handle = 879 scoped_refptr<UsbDeviceHandle> device_handle =
853 GetDeviceHandleOrCompleteWithError(parameters_->handle); 880 GetDeviceHandleOrCompleteWithError(parameters_->handle);
854 if (!device_handle.get()) { 881 if (!device_handle.get()) {
855 return; 882 return;
856 } 883 }
857 884
858 ConfigDescriptor config; 885 const UsbConfigDescriptor* config_descriptor =
859 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), 886 device_handle->GetDevice()->GetConfiguration();
860 &config); 887 if (config_descriptor) {
888 ConfigDescriptor config;
889 ConvertConfigDescriptor(*config_descriptor, &config);
890 SetResult(config.ToValue().release());
891 } else {
892 SetError(kErrorNotConfigured);
893 }
861 894
862 SetResult(config.ToValue().release());
863 AsyncWorkCompleted(); 895 AsyncWorkCompleted();
864 } 896 }
865 897
866 UsbListInterfacesFunction::UsbListInterfacesFunction() { 898 UsbListInterfacesFunction::UsbListInterfacesFunction() {
867 } 899 }
868 900
869 UsbListInterfacesFunction::~UsbListInterfacesFunction() { 901 UsbListInterfacesFunction::~UsbListInterfacesFunction() {
870 } 902 }
871 903
872 bool UsbListInterfacesFunction::Prepare() { 904 bool UsbListInterfacesFunction::Prepare() {
873 parameters_ = ListInterfaces::Params::Create(*args_); 905 parameters_ = ListInterfaces::Params::Create(*args_);
874 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 906 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
875 return true; 907 return true;
876 } 908 }
877 909
878 void UsbListInterfacesFunction::AsyncWorkStart() { 910 void UsbListInterfacesFunction::AsyncWorkStart() {
879 scoped_refptr<UsbDeviceHandle> device_handle = 911 scoped_refptr<UsbDeviceHandle> device_handle =
880 GetDeviceHandleOrCompleteWithError(parameters_->handle); 912 GetDeviceHandleOrCompleteWithError(parameters_->handle);
881 if (!device_handle.get()) { 913 if (!device_handle.get()) {
882 return; 914 return;
883 } 915 }
884 916
885 ConfigDescriptor config; 917 const UsbConfigDescriptor* config_descriptor =
886 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), 918 device_handle->GetDevice()->GetConfiguration();
887 &config); 919 if (config_descriptor) {
920 ConfigDescriptor config;
921 ConvertConfigDescriptor(*config_descriptor, &config);
888 922
889 scoped_ptr<base::ListValue> result(new base::ListValue); 923 scoped_ptr<base::ListValue> result(new base::ListValue);
890 for (size_t i = 0; i < config.interfaces.size(); ++i) { 924 for (size_t i = 0; i < config.interfaces.size(); ++i) {
891 result->Append(config.interfaces[i]->ToValue().release()); 925 result->Append(config.interfaces[i]->ToValue().release());
926 }
927
928 SetResult(result.release());
929 } else {
930 SetError(kErrorNotConfigured);
892 } 931 }
893 932
894 SetResult(result.release());
895 AsyncWorkCompleted(); 933 AsyncWorkCompleted();
896 } 934 }
897 935
898 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { 936 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {
899 } 937 }
900 938
901 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { 939 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {
902 } 940 }
903 941
904 bool UsbCloseDeviceFunction::Prepare() { 942 bool UsbCloseDeviceFunction::Prepare() {
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 SetResult(new base::FundamentalValue(false)); 1280 SetResult(new base::FundamentalValue(false));
1243 CompleteWithError(kErrorResetDevice); 1281 CompleteWithError(kErrorResetDevice);
1244 return; 1282 return;
1245 } 1283 }
1246 1284
1247 SetResult(new base::FundamentalValue(true)); 1285 SetResult(new base::FundamentalValue(true));
1248 AsyncWorkCompleted(); 1286 AsyncWorkCompleted();
1249 } 1287 }
1250 1288
1251 } // namespace extensions 1289 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698