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

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

Issue 562763002: Convert device::UsbConfigDescriptor and friends to structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 using usb::RequestType; 49 using usb::RequestType;
50 using usb::SynchronizationType; 50 using usb::SynchronizationType;
51 using usb::TransferType; 51 using usb::TransferType;
52 using usb::UsageType; 52 using usb::UsageType;
53 using device::UsbConfigDescriptor; 53 using device::UsbConfigDescriptor;
54 using device::UsbDevice; 54 using device::UsbDevice;
55 using device::UsbDeviceFilter; 55 using device::UsbDeviceFilter;
56 using device::UsbDeviceHandle; 56 using device::UsbDeviceHandle;
57 using device::UsbEndpointDescriptor; 57 using device::UsbEndpointDescriptor;
58 using device::UsbEndpointDirection; 58 using device::UsbEndpointDirection;
59 using device::UsbInterfaceAltSettingDescriptor;
60 using device::UsbInterfaceDescriptor; 59 using device::UsbInterfaceDescriptor;
61 using device::UsbService; 60 using device::UsbService;
62 using device::UsbSynchronizationType; 61 using device::UsbSynchronizationType;
63 using device::UsbTransferStatus; 62 using device::UsbTransferStatus;
64 using device::UsbTransferType; 63 using device::UsbTransferType;
65 using device::UsbUsageType; 64 using device::UsbUsageType;
66 65
67 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; 66 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector;
68 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; 67 typedef scoped_ptr<DeviceVector> ScopedDeviceVector;
69 68
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 753 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
755 return true; 754 return true;
756 } 755 }
757 756
758 void UsbListInterfacesFunction::AsyncWorkStart() { 757 void UsbListInterfacesFunction::AsyncWorkStart() {
759 scoped_refptr<UsbDeviceHandle> device_handle = 758 scoped_refptr<UsbDeviceHandle> device_handle =
760 GetDeviceHandleOrCompleteWithError(parameters_->handle); 759 GetDeviceHandleOrCompleteWithError(parameters_->handle);
761 if (!device_handle.get()) 760 if (!device_handle.get())
762 return; 761 return;
763 762
764 scoped_refptr<UsbConfigDescriptor> config = 763 const UsbConfigDescriptor& config =
765 device_handle->GetDevice()->ListInterfaces(); 764 device_handle->GetDevice()->GetConfiguration();
766 765
767 if (!config.get()) { 766 scoped_ptr<base::ListValue> result(new base::ListValue());
768 SetError(kErrorCannotListInterfaces); 767
769 AsyncWorkCompleted(); 768 for (size_t i = 0; i < config.interfaces.size(); ++i) {
770 return; 769 const UsbInterfaceDescriptor& usb_interface = config.interfaces[i];
770 std::vector<linked_ptr<EndpointDescriptor> > endpoints;
771
772 for (size_t j = 0; j < usb_interface.endpoints.size(); ++j) {
773 const UsbEndpointDescriptor& endpoint = usb_interface.endpoints[j];
774 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
775
776 TransferType type;
777 Direction direction;
778 SynchronizationType synchronization;
779 UsageType usage;
780
781 if (!ConvertTransferTypeSafely(endpoint.transfer_type, &type) ||
782 !ConvertDirectionSafely(endpoint.direction, &direction) ||
783 !ConvertSynchronizationTypeSafely(endpoint.synchronization_type,
784 &synchronization) ||
785 !ConvertUsageTypeSafely(endpoint.usage_type, &usage)) {
786 SetError(kErrorCannotListInterfaces);
787 AsyncWorkCompleted();
788 return;
789 }
790
791 endpoint_desc->address = endpoint.address;
792 endpoint_desc->type = type;
793 endpoint_desc->direction = direction;
794 endpoint_desc->maximum_packet_size = endpoint.maximum_packet_size;
795 endpoint_desc->synchronization = synchronization;
796 endpoint_desc->usage = usage;
797 endpoint_desc->polling_interval.reset(new int(endpoint.polling_interval));
798
799 endpoints.push_back(endpoint_desc);
800 }
801
802 result->Append(PopulateInterfaceDescriptor(usb_interface.interface_number,
803 usb_interface.alternate_setting,
804 usb_interface.interface_class,
805 usb_interface.interface_subclass,
806 usb_interface.interface_protocol,
807 &endpoints));
771 } 808 }
772 809
773 result_.reset(new base::ListValue()); 810 SetResult(result.release());
774
775 for (size_t i = 0, num_interfaces = config->GetNumInterfaces();
776 i < num_interfaces;
777 ++i) {
778 scoped_refptr<const UsbInterfaceDescriptor> usb_interface(
779 config->GetInterface(i));
780 for (size_t j = 0, num_descriptors = usb_interface->GetNumAltSettings();
781 j < num_descriptors;
782 ++j) {
783 scoped_refptr<const UsbInterfaceAltSettingDescriptor> descriptor =
784 usb_interface->GetAltSetting(j);
785 std::vector<linked_ptr<EndpointDescriptor> > endpoints;
786 for (size_t k = 0, num_endpoints = descriptor->GetNumEndpoints();
787 k < num_endpoints;
788 k++) {
789 scoped_refptr<const UsbEndpointDescriptor> endpoint =
790 descriptor->GetEndpoint(k);
791 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
792
793 TransferType type;
794 Direction direction;
795 SynchronizationType synchronization;
796 UsageType usage;
797
798 if (!ConvertTransferTypeSafely(endpoint->GetTransferType(), &type) ||
799 !ConvertDirectionSafely(endpoint->GetDirection(), &direction) ||
800 !ConvertSynchronizationTypeSafely(
801 endpoint->GetSynchronizationType(), &synchronization) ||
802 !ConvertUsageTypeSafely(endpoint->GetUsageType(), &usage)) {
803 SetError(kErrorCannotListInterfaces);
804 AsyncWorkCompleted();
805 return;
806 }
807
808 endpoint_desc->address = endpoint->GetAddress();
809 endpoint_desc->type = type;
810 endpoint_desc->direction = direction;
811 endpoint_desc->maximum_packet_size = endpoint->GetMaximumPacketSize();
812 endpoint_desc->synchronization = synchronization;
813 endpoint_desc->usage = usage;
814
815 int* polling_interval = new int;
816 endpoint_desc->polling_interval.reset(polling_interval);
817 *polling_interval = endpoint->GetPollingInterval();
818
819 endpoints.push_back(endpoint_desc);
820 }
821
822 result_->Append(
823 PopulateInterfaceDescriptor(descriptor->GetInterfaceNumber(),
824 descriptor->GetAlternateSetting(),
825 descriptor->GetInterfaceClass(),
826 descriptor->GetInterfaceSubclass(),
827 descriptor->GetInterfaceProtocol(),
828 &endpoints));
829 }
830 }
831
832 SetResult(result_.release());
833 AsyncWorkCompleted(); 811 AsyncWorkCompleted();
834 } 812 }
835 813
836 bool UsbListInterfacesFunction::ConvertDirectionSafely( 814 bool UsbListInterfacesFunction::ConvertDirectionSafely(
837 const UsbEndpointDirection& input, 815 const UsbEndpointDirection& input,
838 usb::Direction* output) { 816 usb::Direction* output) {
839 const bool converted = ConvertDirectionToApi(input, output); 817 const bool converted = ConvertDirectionToApi(input, output);
840 if (!converted) 818 if (!converted)
841 SetError(kErrorConvertDirection); 819 SetError(kErrorConvertDirection);
842 return converted; 820 return converted;
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 SetResult(new base::FundamentalValue(false)); 1194 SetResult(new base::FundamentalValue(false));
1217 CompleteWithError(kErrorResetDevice); 1195 CompleteWithError(kErrorResetDevice);
1218 return; 1196 return;
1219 } 1197 }
1220 1198
1221 SetResult(new base::FundamentalValue(true)); 1199 SetResult(new base::FundamentalValue(true));
1222 AsyncWorkCompleted(); 1200 AsyncWorkCompleted();
1223 } 1201 }
1224 1202
1225 } // namespace extensions 1203 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698