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

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

Issue 2204713006: Add chrome://usb-internals page for adding and removing test devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses dpapad@'s nits. Created 4 years, 4 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
OLDNEW
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698