OLD | NEW |
---|---|
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_service.h" | 5 #include "device/usb/usb_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 scoped_refptr<UsbDeviceImpl> new_device = AddDevice(platform_devices[i]); | 166 scoped_refptr<UsbDeviceImpl> new_device = AddDevice(platform_devices[i]); |
167 if (new_device) { | 167 if (new_device) { |
168 connected_devices.insert(new_device.get()); | 168 connected_devices.insert(new_device.get()); |
169 } | 169 } |
170 } else { | 170 } else { |
171 connected_devices.insert(platform_devices_[platform_devices[i]].get()); | 171 connected_devices.insert(platform_devices_[platform_devices[i]].get()); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 // Find disconnected devices. | 175 // Find disconnected devices. |
176 for (PlatformDeviceMap::iterator it = platform_devices_.begin(); | 176 for (const auto& map_entry : platform_devices_) { |
177 it != platform_devices_.end(); | 177 PlatformUsbDevice platform_device = map_entry.first; |
178 ++it) { | 178 scoped_refptr<UsbDeviceImpl> device = map_entry.second; |
179 if (!ContainsKey(connected_devices, it->second.get())) { | 179 if (!ContainsKey(connected_devices, device.get())) { |
180 disconnected_devices.push_back(it->first); | 180 disconnected_devices.push_back(platform_device); |
181 devices_.erase(it->second->unique_id()); | 181 devices_.erase(device->unique_id()); |
182 it->second->OnDisconnect(); | 182 NotifyDeviceRemoved(device); |
183 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
| |
183 } | 184 } |
184 } | 185 } |
185 | 186 |
186 // Remove disconnected devices from platform_devices_. | 187 // Remove disconnected devices from platform_devices_. |
187 for (size_t i = 0; i < disconnected_devices.size(); ++i) { | 188 for (const PlatformUsbDevice& platform_device : disconnected_devices) { |
188 // UsbDevice will be destroyed after this. The corresponding | 189 // UsbDevice will be destroyed after this. The corresponding |
189 // PlatformUsbDevice will be unref'ed during this process. | 190 // PlatformUsbDevice will be unref'ed during this process. |
190 platform_devices_.erase(disconnected_devices[i]); | 191 platform_devices_.erase(platform_device); |
191 } | 192 } |
192 | 193 |
193 libusb_free_device_list(platform_devices, true); | 194 libusb_free_device_list(platform_devices, true); |
194 } | 195 } |
195 | 196 |
196 scoped_refptr<UsbDeviceImpl> UsbServiceImpl::AddDevice( | 197 scoped_refptr<UsbDeviceImpl> UsbServiceImpl::AddDevice( |
197 PlatformUsbDevice platform_device) { | 198 PlatformUsbDevice platform_device) { |
198 libusb_device_descriptor descriptor; | 199 libusb_device_descriptor descriptor; |
199 int rv = libusb_get_device_descriptor(platform_device, &descriptor); | 200 int rv = libusb_get_device_descriptor(platform_device, &descriptor); |
200 if (rv == LIBUSB_SUCCESS) { | 201 if (rv == LIBUSB_SUCCESS) { |
201 uint32 unique_id; | 202 uint32 unique_id; |
202 do { | 203 do { |
203 unique_id = ++next_unique_id_; | 204 unique_id = ++next_unique_id_; |
204 } while (devices_.find(unique_id) != devices_.end()); | 205 } while (devices_.find(unique_id) != devices_.end()); |
205 | 206 |
206 scoped_refptr<UsbDeviceImpl> new_device(new UsbDeviceImpl( | 207 scoped_refptr<UsbDeviceImpl> new_device(new UsbDeviceImpl( |
207 context_, ui_task_runner_, platform_device, descriptor.idVendor, | 208 context_, ui_task_runner_, platform_device, descriptor.idVendor, |
208 descriptor.idProduct, unique_id)); | 209 descriptor.idProduct, unique_id)); |
209 platform_devices_[platform_device] = new_device; | 210 platform_devices_[platform_device] = new_device; |
210 devices_[unique_id] = new_device; | 211 devices_[unique_id] = new_device; |
212 NotifyDeviceAdded(new_device); | |
211 return new_device; | 213 return new_device; |
212 } else { | 214 } else { |
213 VLOG(1) << "Failed to get device descriptor: " | 215 VLOG(1) << "Failed to get device descriptor: " |
214 << ConvertPlatformUsbErrorToString(rv); | 216 << ConvertPlatformUsbErrorToString(rv); |
215 return nullptr; | 217 return nullptr; |
216 } | 218 } |
217 } | 219 } |
218 | 220 |
219 // static | 221 // static |
220 int LIBUSB_CALL UsbServiceImpl::HotplugCallback(libusb_context* context, | 222 int LIBUSB_CALL UsbServiceImpl::HotplugCallback(libusb_context* context, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 | 269 |
268 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); | 270 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
269 if (it != platform_devices_.end()) { | 271 if (it != platform_devices_.end()) { |
270 scoped_refptr<UsbDeviceImpl> device = it->second; | 272 scoped_refptr<UsbDeviceImpl> device = it->second; |
271 DeviceMap::iterator dev_it = devices_.find(device->unique_id()); | 273 DeviceMap::iterator dev_it = devices_.find(device->unique_id()); |
272 if (dev_it != devices_.end()) { | 274 if (dev_it != devices_.end()) { |
273 devices_.erase(dev_it); | 275 devices_.erase(dev_it); |
274 } else { | 276 } else { |
275 NOTREACHED(); | 277 NOTREACHED(); |
276 } | 278 } |
279 NotifyDeviceRemoved(device); | |
277 device->OnDisconnect(); | 280 device->OnDisconnect(); |
278 platform_devices_.erase(it); | 281 platform_devices_.erase(it); |
279 } else { | 282 } else { |
280 NOTREACHED(); | 283 NOTREACHED(); |
281 } | 284 } |
282 | 285 |
283 libusb_unref_device(platform_device); | 286 libusb_unref_device(platform_device); |
284 } | 287 } |
285 | 288 |
286 // static | 289 // static |
(...skipping 16 matching lines...) Expand all Loading... | |
303 g_usb_service_instance.Get().reset(instance); | 306 g_usb_service_instance.Get().reset(instance); |
304 } | 307 } |
305 return instance; | 308 return instance; |
306 } | 309 } |
307 | 310 |
308 // static | 311 // static |
309 void UsbService::SetInstanceForTest(UsbService* instance) { | 312 void UsbService::SetInstanceForTest(UsbService* instance) { |
310 g_usb_service_instance.Get().reset(instance); | 313 g_usb_service_instance.Get().reset(instance); |
311 } | 314 } |
312 | 315 |
316 UsbService::UsbService() { | |
317 } | |
318 | |
319 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
| |
320 } | |
321 | |
322 void UsbService::AddObserver(Observer* observer) { | |
323 observer_list_.AddObserver(observer); | |
324 } | |
325 | |
326 void UsbService::RemoveObserver(Observer* observer) { | |
327 observer_list_.RemoveObserver(observer); | |
328 } | |
329 | |
330 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { | |
331 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device)); | |
332 } | |
333 | |
334 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { | |
335 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device)); | |
336 } | |
337 | |
313 } // namespace device | 338 } // namespace device |
OLD | NEW |