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

Side by Side Diff: device/usb/usb_device_handle_impl.cc

Issue 2752523002: Handle USB device disconnection during early setup. (Closed)
Patch Set: Created 3 years, 9 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 | « device/usb/mojo/device_impl.cc ('k') | no next file » | 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 "device/usb/usb_device_handle_impl.h" 5 #include "device/usb/usb_device_handle_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <memory> 8 #include <memory>
9 #include <numeric> 9 #include <numeric>
10 #include <utility> 10 #include <utility>
(...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 << ": " << ConvertPlatformUsbErrorToString(rv); 823 << ": " << ConvertPlatformUsbErrorToString(rv);
824 } 824 }
825 task_runner_->PostTask( 825 task_runner_->PostTask(
826 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::SetConfigurationComplete, 826 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::SetConfigurationComplete,
827 this, rv == LIBUSB_SUCCESS, callback)); 827 this, rv == LIBUSB_SUCCESS, callback));
828 } 828 }
829 829
830 void UsbDeviceHandleImpl::SetConfigurationComplete( 830 void UsbDeviceHandleImpl::SetConfigurationComplete(
831 bool success, 831 bool success,
832 const ResultCallback& callback) { 832 const ResultCallback& callback) {
833 if (!device_) {
834 callback.Run(false);
835 return;
836 }
837
833 if (success) { 838 if (success) {
834 device_->RefreshActiveConfiguration(); 839 device_->RefreshActiveConfiguration();
835 RefreshEndpointMap(); 840 RefreshEndpointMap();
836 } 841 }
837 callback.Run(success); 842 callback.Run(success);
838 } 843 }
839 844
840 void UsbDeviceHandleImpl::ClaimInterfaceOnBlockingThread( 845 void UsbDeviceHandleImpl::ClaimInterfaceOnBlockingThread(
841 int interface_number, 846 int interface_number,
842 const ResultCallback& callback) { 847 const ResultCallback& callback) {
843 int rv = libusb_claim_interface(handle_, interface_number); 848 int rv = libusb_claim_interface(handle_, interface_number);
844 scoped_refptr<InterfaceClaimer> interface_claimer; 849 scoped_refptr<InterfaceClaimer> interface_claimer;
845 if (rv == LIBUSB_SUCCESS) { 850 if (rv == LIBUSB_SUCCESS) {
846 interface_claimer = 851 interface_claimer =
847 new InterfaceClaimer(this, interface_number, task_runner_); 852 new InterfaceClaimer(this, interface_number, task_runner_);
848 } else { 853 } else {
849 USB_LOG(EVENT) << "Failed to claim interface: " 854 USB_LOG(EVENT) << "Failed to claim interface: "
850 << ConvertPlatformUsbErrorToString(rv); 855 << ConvertPlatformUsbErrorToString(rv);
851 } 856 }
852 task_runner_->PostTask( 857 task_runner_->PostTask(
853 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::ClaimInterfaceComplete, this, 858 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::ClaimInterfaceComplete, this,
854 interface_claimer, callback)); 859 interface_claimer, callback));
855 } 860 }
856 861
857 void UsbDeviceHandleImpl::ClaimInterfaceComplete( 862 void UsbDeviceHandleImpl::ClaimInterfaceComplete(
858 scoped_refptr<InterfaceClaimer> interface_claimer, 863 scoped_refptr<InterfaceClaimer> interface_claimer,
859 const ResultCallback& callback) { 864 const ResultCallback& callback) {
865 if (!device_) {
866 // Ensure that the InterfaceClaimer is released on the blocking thread.
867 InterfaceClaimer* raw_interface_claimer = interface_claimer.get();
868 interface_claimer->AddRef();
869 interface_claimer = nullptr;
870 blocking_task_runner_->ReleaseSoon(FROM_HERE, raw_interface_claimer);
871 callback.Run(false);
872 return;
873 }
874
860 if (interface_claimer) { 875 if (interface_claimer) {
861 claimed_interfaces_[interface_claimer->interface_number()] = 876 claimed_interfaces_[interface_claimer->interface_number()] =
862 interface_claimer; 877 interface_claimer;
863 RefreshEndpointMap(); 878 RefreshEndpointMap();
864 } 879 }
865 callback.Run(static_cast<bool>(interface_claimer)); 880 callback.Run(static_cast<bool>(interface_claimer));
866 } 881 }
867 882
868 void UsbDeviceHandleImpl::SetInterfaceAlternateSettingOnBlockingThread( 883 void UsbDeviceHandleImpl::SetInterfaceAlternateSettingOnBlockingThread(
869 int interface_number, 884 int interface_number,
(...skipping 11 matching lines...) Expand all
881 base::Bind(&UsbDeviceHandleImpl::SetInterfaceAlternateSettingComplete, 896 base::Bind(&UsbDeviceHandleImpl::SetInterfaceAlternateSettingComplete,
882 this, interface_number, alternate_setting, 897 this, interface_number, alternate_setting,
883 rv == LIBUSB_SUCCESS, callback)); 898 rv == LIBUSB_SUCCESS, callback));
884 } 899 }
885 900
886 void UsbDeviceHandleImpl::SetInterfaceAlternateSettingComplete( 901 void UsbDeviceHandleImpl::SetInterfaceAlternateSettingComplete(
887 int interface_number, 902 int interface_number,
888 int alternate_setting, 903 int alternate_setting,
889 bool success, 904 bool success,
890 const ResultCallback& callback) { 905 const ResultCallback& callback) {
906 if (!device_) {
907 callback.Run(false);
908 return;
909 }
910
891 if (success) { 911 if (success) {
892 claimed_interfaces_[interface_number]->set_alternate_setting( 912 claimed_interfaces_[interface_number]->set_alternate_setting(
893 alternate_setting); 913 alternate_setting);
894 RefreshEndpointMap(); 914 RefreshEndpointMap();
895 } 915 }
896 callback.Run(success); 916 callback.Run(success);
897 } 917 }
898 918
899 void UsbDeviceHandleImpl::ResetDeviceOnBlockingThread( 919 void UsbDeviceHandleImpl::ResetDeviceOnBlockingThread(
900 const ResultCallback& callback) { 920 const ResultCallback& callback) {
(...skipping 11 matching lines...) Expand all
912 int rv = libusb_clear_halt(handle_, endpoint); 932 int rv = libusb_clear_halt(handle_, endpoint);
913 if (rv != LIBUSB_SUCCESS) { 933 if (rv != LIBUSB_SUCCESS) {
914 USB_LOG(EVENT) << "Failed to clear halt: " 934 USB_LOG(EVENT) << "Failed to clear halt: "
915 << ConvertPlatformUsbErrorToString(rv); 935 << ConvertPlatformUsbErrorToString(rv);
916 } 936 }
917 task_runner_->PostTask(FROM_HERE, base::Bind(callback, rv == LIBUSB_SUCCESS)); 937 task_runner_->PostTask(FROM_HERE, base::Bind(callback, rv == LIBUSB_SUCCESS));
918 } 938 }
919 939
920 void UsbDeviceHandleImpl::RefreshEndpointMap() { 940 void UsbDeviceHandleImpl::RefreshEndpointMap() {
921 DCHECK(thread_checker_.CalledOnValidThread()); 941 DCHECK(thread_checker_.CalledOnValidThread());
942 DCHECK(device_);
922 endpoint_map_.clear(); 943 endpoint_map_.clear();
923 const UsbConfigDescriptor* config = device_->active_configuration(); 944 const UsbConfigDescriptor* config = device_->active_configuration();
924 if (config) { 945 if (config) {
925 for (const auto& map_entry : claimed_interfaces_) { 946 for (const auto& map_entry : claimed_interfaces_) {
926 int interface_number = map_entry.first; 947 int interface_number = map_entry.first;
927 const scoped_refptr<InterfaceClaimer>& claimed_iface = map_entry.second; 948 const scoped_refptr<InterfaceClaimer>& claimed_iface = map_entry.second;
928 949
929 for (const UsbInterfaceDescriptor& iface : config->interfaces) { 950 for (const UsbInterfaceDescriptor& iface : config->interfaces) {
930 if (iface.interface_number == interface_number && 951 if (iface.interface_number == interface_number &&
931 iface.alternate_setting == claimed_iface->alternate_setting()) { 952 iface.alternate_setting == claimed_iface->alternate_setting()) {
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1122 } else { 1143 } else {
1123 transfer->callback_task_runner()->PostTask(FROM_HERE, callback); 1144 transfer->callback_task_runner()->PostTask(FROM_HERE, callback);
1124 } 1145 }
1125 1146
1126 // libusb_free_transfer races with libusb_submit_transfer and only work- 1147 // libusb_free_transfer races with libusb_submit_transfer and only work-
1127 // around is to make sure to call them on the same thread. 1148 // around is to make sure to call them on the same thread.
1128 blocking_task_runner_->DeleteSoon(FROM_HERE, transfer); 1149 blocking_task_runner_->DeleteSoon(FROM_HERE, transfer);
1129 } 1150 }
1130 1151
1131 } // namespace device 1152 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/mojo/device_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698