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

Unified Diff: device/usb/usb_service_impl.cc

Issue 1903933002: Store devices that only need to be enumerated once (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed device leak at usb_service_impl.cc Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/usb/usb_service_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/usb/usb_service_impl.cc
diff --git a/device/usb/usb_service_impl.cc b/device/usb/usb_service_impl.cc
index 2624991c9401dd051eb0e20ca4eee5fb0890db67..86545d107aabde409bb153d691b21916717d6d75 100644
--- a/device/usb/usb_service_impl.cc
+++ b/device/usb/usb_service_impl.cc
@@ -321,6 +321,8 @@ UsbServiceImpl::~UsbServiceImpl() {
for (const auto& map_entry : devices_) {
map_entry.second->OnDisconnect();
}
+ for (const auto& platform_device : ignored_devices_)
+ libusb_unref_device(platform_device);
}
scoped_refptr<UsbDevice> UsbServiceImpl::GetDevice(const std::string& guid) {
@@ -417,14 +419,21 @@ void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices,
base::Bind(&UsbServiceImpl::RefreshDevicesComplete,
weak_factory_.GetWeakPtr()));
std::list<PlatformUsbDevice> new_devices;
+ std::set<PlatformUsbDevice> existing_ignored_devices;
// Look for new and existing devices.
for (size_t i = 0; i < device_count; ++i) {
PlatformUsbDevice platform_device = platform_devices[i];
+ // Ignore some devices.
+ if (ContainsValue(ignored_devices_, platform_device)) {
+ existing_ignored_devices.insert(platform_device);
+ refresh_complete.Run();
+ continue;
+ }
+
auto it = platform_devices_.find(platform_device);
if (it == platform_devices_.end()) {
- libusb_ref_device(platform_device);
new_devices.push_back(platform_device);
} else {
it->second->set_visited(true);
@@ -445,6 +454,16 @@ void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices,
}
}
+ // Remove devices not seen in this enumeration from |ignored_devices_|.
+ for (auto it = ignored_devices_.begin(); it != ignored_devices_.end();
+ /* incremented internally */) {
+ auto current = it++;
+ if (!ContainsValue(existing_ignored_devices, *current)) {
+ libusb_unref_device(*current);
+ ignored_devices_.erase(current);
+ }
+ }
+
for (PlatformUsbDevice platform_device : new_devices) {
EnumerateDevice(platform_device, refresh_complete);
}
@@ -488,6 +507,8 @@ void UsbServiceImpl::EnumerateDevice(PlatformUsbDevice platform_device,
if (rv == LIBUSB_SUCCESS) {
if (descriptor.bDeviceClass == LIBUSB_CLASS_HUB) {
// Don't try to enumerate hubs. We never want to connect to a hub.
+ libusb_ref_device(platform_device);
+ ignored_devices_.insert(platform_device);
refresh_complete.Run();
return;
}
@@ -497,13 +518,16 @@ void UsbServiceImpl::EnumerateDevice(PlatformUsbDevice platform_device,
base::Closure add_device =
base::Bind(&UsbServiceImpl::AddDevice, weak_factory_.GetWeakPtr(),
refresh_complete, device);
+ base::Closure enumeration_failed = base::Bind(
+ &UsbServiceImpl::EnumerationFailed, weak_factory_.GetWeakPtr(),
+ platform_device, refresh_complete);
bool read_bos_descriptors = descriptor.bcdUSB >= kUsbVersion2_1;
#if defined(USE_UDEV)
blocking_task_runner_->PostTask(
FROM_HERE,
base::Bind(&EnumerateUdevDevice, device, read_bos_descriptors,
- task_runner_, add_device, refresh_complete));
+ task_runner_, add_device, enumeration_failed));
#else
if (descriptor.iManufacturer == 0 && descriptor.iProduct == 0 &&
descriptor.iSerialNumber == 0 && !read_bos_descriptors) {
@@ -513,7 +537,7 @@ void UsbServiceImpl::EnumerateDevice(PlatformUsbDevice platform_device,
device->Open(base::Bind(&OnDeviceOpenedReadDescriptors,
descriptor.iManufacturer, descriptor.iProduct,
descriptor.iSerialNumber, read_bos_descriptors,
- add_device, refresh_complete));
+ add_device, enumeration_failed));
}
#endif
} else {
@@ -616,4 +640,11 @@ void UsbServiceImpl::OnPlatformDeviceRemoved(
libusb_unref_device(platform_device);
}
+void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device,
+ const base::Closure& refresh_complete) {
+ libusb_ref_device(platform_device);
+ ignored_devices_.insert(platform_device);
+ refresh_complete.Run();
+}
+
} // namespace device
« no previous file with comments | « device/usb/usb_service_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698