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

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: Add interface and endpoint descriptor iterator typedefs. 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
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/api/usb/usb_apitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 (UsbInterfaceDescriptor::Iterator interfaceIt = config.interfaces.begin();
770 return; 769 interfaceIt != config.interfaces.end();
770 ++interfaceIt) {
771 std::vector<linked_ptr<EndpointDescriptor> > endpoints;
772
773 for (UsbEndpointDescriptor::Iterator endpointIt =
774 interfaceIt->endpoints.begin();
775 endpointIt != interfaceIt->endpoints.end();
776 ++endpointIt) {
777 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
778
779 TransferType type;
780 Direction direction;
781 SynchronizationType synchronization;
782 UsageType usage;
783
784 if (!ConvertTransferTypeSafely(endpointIt->transfer_type, &type) ||
785 !ConvertDirectionSafely(endpointIt->direction, &direction) ||
786 !ConvertSynchronizationTypeSafely(endpointIt->synchronization_type,
787 &synchronization) ||
788 !ConvertUsageTypeSafely(endpointIt->usage_type, &usage)) {
789 SetError(kErrorCannotListInterfaces);
790 AsyncWorkCompleted();
791 return;
792 }
793
794 endpoint_desc->address = endpointIt->address;
795 endpoint_desc->type = type;
796 endpoint_desc->direction = direction;
797 endpoint_desc->maximum_packet_size = endpointIt->maximum_packet_size;
798 endpoint_desc->synchronization = synchronization;
799 endpoint_desc->usage = usage;
800 endpoint_desc->polling_interval.reset(
801 new int(endpointIt->polling_interval));
802
803 endpoints.push_back(endpoint_desc);
804 }
805
806 result->Append(PopulateInterfaceDescriptor(interfaceIt->interface_number,
807 interfaceIt->alternate_setting,
808 interfaceIt->interface_class,
809 interfaceIt->interface_subclass,
810 interfaceIt->interface_protocol,
811 &endpoints));
771 } 812 }
772 813
773 result_.reset(new base::ListValue()); 814 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(); 815 AsyncWorkCompleted();
834 } 816 }
835 817
836 bool UsbListInterfacesFunction::ConvertDirectionSafely( 818 bool UsbListInterfacesFunction::ConvertDirectionSafely(
837 const UsbEndpointDirection& input, 819 const UsbEndpointDirection& input,
838 usb::Direction* output) { 820 usb::Direction* output) {
839 const bool converted = ConvertDirectionToApi(input, output); 821 const bool converted = ConvertDirectionToApi(input, output);
840 if (!converted) 822 if (!converted)
841 SetError(kErrorConvertDirection); 823 SetError(kErrorConvertDirection);
842 return converted; 824 return converted;
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 SetResult(new base::FundamentalValue(false)); 1198 SetResult(new base::FundamentalValue(false));
1217 CompleteWithError(kErrorResetDevice); 1199 CompleteWithError(kErrorResetDevice);
1218 return; 1200 return;
1219 } 1201 }
1220 1202
1221 SetResult(new base::FundamentalValue(true)); 1203 SetResult(new base::FundamentalValue(true));
1222 AsyncWorkCompleted(); 1204 AsyncWorkCompleted();
1223 } 1205 }
1224 1206
1225 } // namespace extensions 1207 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/api/usb/usb_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698