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

Unified Diff: device/usb/usb_service_impl.cc

Issue 809743006: Add an Observer interface to UsbService for device add/remove. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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.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 a23a4a1548b913efe763a5b140951f056b6d424c..66244d742e4c0479c5dcd10dca27f540fb47f79c 100644
--- a/device/usb/usb_service_impl.cc
+++ b/device/usb/usb_service_impl.cc
@@ -173,21 +173,22 @@ void UsbServiceImpl::RefreshDevices() {
}
// Find disconnected devices.
- for (PlatformDeviceMap::iterator it = platform_devices_.begin();
- it != platform_devices_.end();
- ++it) {
- if (!ContainsKey(connected_devices, it->second.get())) {
- disconnected_devices.push_back(it->first);
- devices_.erase(it->second->unique_id());
- it->second->OnDisconnect();
+ for (const auto& map_entry : platform_devices_) {
+ PlatformUsbDevice platform_device = map_entry.first;
+ scoped_refptr<UsbDeviceImpl> device = map_entry.second;
+ if (!ContainsKey(connected_devices, device.get())) {
+ disconnected_devices.push_back(platform_device);
+ devices_.erase(device->unique_id());
+ NotifyDeviceRemoved(device);
+ device->OnDisconnect();
asargent_no_longer_on_chrome 2015/01/05 22:01:58 I assume you've already thought about the ramifica
Reilly Grant (use Gerrit) 2015/01/05 22:10:34 I'm actually about to remove the observer list fro
}
}
// Remove disconnected devices from platform_devices_.
- for (size_t i = 0; i < disconnected_devices.size(); ++i) {
+ for (const PlatformUsbDevice& platform_device : disconnected_devices) {
// UsbDevice will be destroyed after this. The corresponding
// PlatformUsbDevice will be unref'ed during this process.
- platform_devices_.erase(disconnected_devices[i]);
+ platform_devices_.erase(platform_device);
}
libusb_free_device_list(platform_devices, true);
@@ -208,6 +209,7 @@ scoped_refptr<UsbDeviceImpl> UsbServiceImpl::AddDevice(
descriptor.idProduct, unique_id));
platform_devices_[platform_device] = new_device;
devices_[unique_id] = new_device;
+ NotifyDeviceAdded(new_device);
return new_device;
} else {
VLOG(1) << "Failed to get device descriptor: "
@@ -274,6 +276,7 @@ void UsbServiceImpl::OnDeviceRemoved(PlatformUsbDevice platform_device) {
} else {
NOTREACHED();
}
+ NotifyDeviceRemoved(device);
device->OnDisconnect();
platform_devices_.erase(it);
} else {
@@ -310,4 +313,26 @@ void UsbService::SetInstanceForTest(UsbService* instance) {
g_usb_service_instance.Get().reset(instance);
}
+UsbService::UsbService() {
+}
+
+UsbService::~UsbService() {
asargent_no_longer_on_chrome 2015/01/05 22:01:58 Consider adding a DCHECK in the destructor here th
Reilly Grant (use Gerrit) 2015/01/05 22:10:34 There's a template flag I can pass to ObserverList
+}
+
+void UsbService::AddObserver(Observer* observer) {
+ observer_list_.AddObserver(observer);
+}
+
+void UsbService::RemoveObserver(Observer* observer) {
+ observer_list_.RemoveObserver(observer);
+}
+
+void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
+ FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device));
+}
+
+void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
+ FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
+}
+
} // namespace device
« no previous file with comments | « device/usb/usb_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698