| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 } | 832 } |
| 833 | 833 |
| 834 bool UsbOpenDeviceFunction::Respond() { | 834 bool UsbOpenDeviceFunction::Respond() { |
| 835 if (permission_entry_.get()) { | 835 if (permission_entry_.get()) { |
| 836 device_permissions_manager_->UpdateLastUsed(extension_->id(), | 836 device_permissions_manager_->UpdateLastUsed(extension_->id(), |
| 837 permission_entry_); | 837 permission_entry_); |
| 838 } | 838 } |
| 839 return UsbAsyncApiFunction::Respond(); | 839 return UsbAsyncApiFunction::Respond(); |
| 840 } | 840 } |
| 841 | 841 |
| 842 UsbSetConfigurationFunction::UsbSetConfigurationFunction() { |
| 843 } |
| 844 |
| 845 UsbSetConfigurationFunction::~UsbSetConfigurationFunction() { |
| 846 } |
| 847 |
| 848 bool UsbSetConfigurationFunction::Prepare() { |
| 849 parameters_ = SetConfiguration::Params::Create(*args_); |
| 850 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 851 return true; |
| 852 } |
| 853 |
| 854 void UsbSetConfigurationFunction::AsyncWorkStart() { |
| 855 scoped_refptr<UsbDeviceHandle> device_handle = |
| 856 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
| 857 if (!device_handle.get()) { |
| 858 return; |
| 859 } |
| 860 |
| 861 if (device_handle->SetConfiguration(parameters_->configuration_value)) { |
| 862 SetResult(new base::FundamentalValue(true)); |
| 863 } else { |
| 864 SetError(kErrorCannotSetConfiguration); |
| 865 } |
| 866 AsyncWorkCompleted(); |
| 867 } |
| 868 |
| 842 UsbGetConfigurationFunction::UsbGetConfigurationFunction() { | 869 UsbGetConfigurationFunction::UsbGetConfigurationFunction() { |
| 843 } | 870 } |
| 844 | 871 |
| 845 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() { | 872 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() { |
| 846 } | 873 } |
| 847 | 874 |
| 848 bool UsbGetConfigurationFunction::Prepare() { | 875 bool UsbGetConfigurationFunction::Prepare() { |
| 849 parameters_ = GetConfiguration::Params::Create(*args_); | 876 parameters_ = GetConfiguration::Params::Create(*args_); |
| 850 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 877 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 851 return true; | 878 return true; |
| 852 } | 879 } |
| 853 | 880 |
| 854 void UsbGetConfigurationFunction::AsyncWorkStart() { | 881 void UsbGetConfigurationFunction::AsyncWorkStart() { |
| 855 scoped_refptr<UsbDeviceHandle> device_handle = | 882 scoped_refptr<UsbDeviceHandle> device_handle = |
| 856 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 883 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
| 857 if (!device_handle.get()) { | 884 if (!device_handle.get()) { |
| 858 return; | 885 return; |
| 859 } | 886 } |
| 860 | 887 |
| 861 ConfigDescriptor config; | 888 const UsbConfigDescriptor* config_descriptor = |
| 862 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), | 889 device_handle->GetDevice()->GetConfiguration(); |
| 863 &config); | 890 if (config_descriptor) { |
| 891 ConfigDescriptor config; |
| 892 ConvertConfigDescriptor(*config_descriptor, &config); |
| 893 SetResult(config.ToValue().release()); |
| 894 } else { |
| 895 SetError(kErrorNotConfigured); |
| 896 } |
| 864 | 897 |
| 865 SetResult(config.ToValue().release()); | |
| 866 AsyncWorkCompleted(); | 898 AsyncWorkCompleted(); |
| 867 } | 899 } |
| 868 | 900 |
| 869 UsbListInterfacesFunction::UsbListInterfacesFunction() { | 901 UsbListInterfacesFunction::UsbListInterfacesFunction() { |
| 870 } | 902 } |
| 871 | 903 |
| 872 UsbListInterfacesFunction::~UsbListInterfacesFunction() { | 904 UsbListInterfacesFunction::~UsbListInterfacesFunction() { |
| 873 } | 905 } |
| 874 | 906 |
| 875 bool UsbListInterfacesFunction::Prepare() { | 907 bool UsbListInterfacesFunction::Prepare() { |
| 876 parameters_ = ListInterfaces::Params::Create(*args_); | 908 parameters_ = ListInterfaces::Params::Create(*args_); |
| 877 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 909 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 878 return true; | 910 return true; |
| 879 } | 911 } |
| 880 | 912 |
| 881 void UsbListInterfacesFunction::AsyncWorkStart() { | 913 void UsbListInterfacesFunction::AsyncWorkStart() { |
| 882 scoped_refptr<UsbDeviceHandle> device_handle = | 914 scoped_refptr<UsbDeviceHandle> device_handle = |
| 883 GetDeviceHandleOrCompleteWithError(parameters_->handle); | 915 GetDeviceHandleOrCompleteWithError(parameters_->handle); |
| 884 if (!device_handle.get()) { | 916 if (!device_handle.get()) { |
| 885 return; | 917 return; |
| 886 } | 918 } |
| 887 | 919 |
| 888 ConfigDescriptor config; | 920 const UsbConfigDescriptor* config_descriptor = |
| 889 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(), | 921 device_handle->GetDevice()->GetConfiguration(); |
| 890 &config); | 922 if (config_descriptor) { |
| 923 ConfigDescriptor config; |
| 924 ConvertConfigDescriptor(*config_descriptor, &config); |
| 891 | 925 |
| 892 scoped_ptr<base::ListValue> result(new base::ListValue); | 926 scoped_ptr<base::ListValue> result(new base::ListValue); |
| 893 for (size_t i = 0; i < config.interfaces.size(); ++i) { | 927 for (size_t i = 0; i < config.interfaces.size(); ++i) { |
| 894 result->Append(config.interfaces[i]->ToValue().release()); | 928 result->Append(config.interfaces[i]->ToValue().release()); |
| 929 } |
| 930 |
| 931 SetResult(result.release()); |
| 932 } else { |
| 933 SetError(kErrorNotConfigured); |
| 895 } | 934 } |
| 896 | 935 |
| 897 SetResult(result.release()); | |
| 898 AsyncWorkCompleted(); | 936 AsyncWorkCompleted(); |
| 899 } | 937 } |
| 900 | 938 |
| 901 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { | 939 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { |
| 902 } | 940 } |
| 903 | 941 |
| 904 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { | 942 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { |
| 905 } | 943 } |
| 906 | 944 |
| 907 bool UsbCloseDeviceFunction::Prepare() { | 945 bool UsbCloseDeviceFunction::Prepare() { |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1245 SetResult(new base::FundamentalValue(false)); | 1283 SetResult(new base::FundamentalValue(false)); |
| 1246 CompleteWithError(kErrorResetDevice); | 1284 CompleteWithError(kErrorResetDevice); |
| 1247 return; | 1285 return; |
| 1248 } | 1286 } |
| 1249 | 1287 |
| 1250 SetResult(new base::FundamentalValue(true)); | 1288 SetResult(new base::FundamentalValue(true)); |
| 1251 AsyncWorkCompleted(); | 1289 AsyncWorkCompleted(); |
| 1252 } | 1290 } |
| 1253 | 1291 |
| 1254 } // namespace extensions | 1292 } // namespace extensions |
| OLD | NEW |