Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 void UsbService::AddObserver(Observer* observer) { | 83 void UsbService::AddObserver(Observer* observer) { |
| 84 DCHECK(CalledOnValidThread()); | 84 DCHECK(CalledOnValidThread()); |
| 85 observer_list_.AddObserver(observer); | 85 observer_list_.AddObserver(observer); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void UsbService::RemoveObserver(Observer* observer) { | 88 void UsbService::RemoveObserver(Observer* observer) { |
| 89 DCHECK(CalledOnValidThread()); | 89 DCHECK(CalledOnValidThread()); |
| 90 observer_list_.RemoveObserver(observer); | 90 observer_list_.RemoveObserver(observer); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void UsbService::AddDeviceForTesting(scoped_refptr<UsbDevice> device) { | |
| 94 DCHECK(CalledOnValidThread()); | |
| 95 DCHECK(!ContainsKey(devices_, device->guid())); | |
|
Robert Sesek
2016/08/08 19:11:36
In production, this would allow a testing device t
Reilly Grant (use Gerrit)
2016/08/08 20:48:24
The GUIDs are generated randomly so there's no pra
| |
| 96 devices_[device->guid()] = device; | |
| 97 testing_devices_.insert(device->guid()); | |
| 98 NotifyDeviceAdded(device); | |
| 99 } | |
| 100 | |
| 101 void UsbService::RemoveDeviceForTesting(const std::string& device_guid) { | |
| 102 DCHECK(CalledOnValidThread()); | |
| 103 // Allow only devices added with AddDeviceForTesting to be removed with this | |
| 104 // method. | |
| 105 auto testing_devices_it = testing_devices_.find(device_guid); | |
| 106 if (testing_devices_it != testing_devices_.end()) { | |
| 107 auto devices_it = devices_.find(device_guid); | |
| 108 DCHECK(devices_it != devices_.end()); | |
| 109 scoped_refptr<UsbDevice> device = devices_it->second; | |
| 110 devices_.erase(devices_it); | |
| 111 testing_devices_.erase(testing_devices_it); | |
| 112 UsbService::NotifyDeviceRemoved(device); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void UsbService::GetTestDevices( | |
| 117 std::vector<scoped_refptr<UsbDevice>>* devices) { | |
| 118 devices->clear(); | |
| 119 devices->reserve(testing_devices_.size()); | |
| 120 for (const std::string& guid : testing_devices_) { | |
| 121 auto it = devices_.find(guid); | |
| 122 DCHECK(it != devices_.end()); | |
| 123 devices->push_back(it->second); | |
| 124 } | |
| 125 } | |
| 126 | |
| 93 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { | 127 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { |
| 94 DCHECK(CalledOnValidThread()); | 128 DCHECK(CalledOnValidThread()); |
| 95 | 129 |
| 96 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device)); | 130 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device)); |
| 97 } | 131 } |
| 98 | 132 |
| 99 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { | 133 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { |
| 100 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
| 101 | 135 |
| 102 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device)); | 136 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device)); |
| 103 device->NotifyDeviceRemoved(); | 137 device->NotifyDeviceRemoved(); |
| 104 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device)); | 138 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device)); |
| 105 } | 139 } |
| 106 | 140 |
| 107 } // namespace device | 141 } // namespace device |
| OLD | NEW |