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

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

Issue 2905153002: Move off of deprecated base::NonThreadSafe in device/ (Closed)
Patch Set: ThreadChecker => SequenceChecker Created 3 years, 6 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
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/feature_list.h" 8 #include "base/feature_list.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 #endif 64 #endif
65 } 65 }
66 66
67 // static 67 // static
68 scoped_refptr<base::SequencedTaskRunner> 68 scoped_refptr<base::SequencedTaskRunner>
69 UsbService::CreateBlockingTaskRunner() { 69 UsbService::CreateBlockingTaskRunner() {
70 return base::CreateSequencedTaskRunnerWithTraits(kBlockingTaskTraits); 70 return base::CreateSequencedTaskRunnerWithTraits(kBlockingTaskTraits);
71 } 71 }
72 72
73 UsbService::~UsbService() { 73 UsbService::~UsbService() {
74 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
74 for (auto& observer : observer_list_) 75 for (auto& observer : observer_list_)
75 observer.WillDestroyUsbService(); 76 observer.WillDestroyUsbService();
76 } 77 }
77 78
78 UsbService::UsbService( 79 UsbService::UsbService(
79 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) 80 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
80 : blocking_task_runner_(std::move(blocking_task_runner)) { 81 : blocking_task_runner_(std::move(blocking_task_runner)) {
81 if (base::ThreadTaskRunnerHandle::IsSet()) 82 if (base::ThreadTaskRunnerHandle::IsSet())
82 task_runner_ = base::ThreadTaskRunnerHandle::Get(); 83 task_runner_ = base::ThreadTaskRunnerHandle::Get();
83 } 84 }
84 85
85 scoped_refptr<UsbDevice> UsbService::GetDevice(const std::string& guid) { 86 scoped_refptr<UsbDevice> UsbService::GetDevice(const std::string& guid) {
86 DCHECK(CalledOnValidThread()); 87 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
87 auto it = devices_.find(guid); 88 auto it = devices_.find(guid);
88 if (it == devices_.end()) 89 if (it == devices_.end())
89 return nullptr; 90 return nullptr;
90 return it->second; 91 return it->second;
91 } 92 }
92 93
93 void UsbService::GetDevices(const GetDevicesCallback& callback) { 94 void UsbService::GetDevices(const GetDevicesCallback& callback) {
94 std::vector<scoped_refptr<UsbDevice>> devices; 95 std::vector<scoped_refptr<UsbDevice>> devices;
95 devices.reserve(devices_.size()); 96 devices.reserve(devices_.size());
96 for (const auto& map_entry : devices_) 97 for (const auto& map_entry : devices_)
97 devices.push_back(map_entry.second); 98 devices.push_back(map_entry.second);
98 if (task_runner_) 99 if (task_runner_)
99 task_runner_->PostTask(FROM_HERE, base::Bind(callback, devices)); 100 task_runner_->PostTask(FROM_HERE, base::Bind(callback, devices));
100 else 101 else
101 callback.Run(devices); 102 callback.Run(devices);
102 } 103 }
103 104
104 void UsbService::AddObserver(Observer* observer) { 105 void UsbService::AddObserver(Observer* observer) {
105 DCHECK(CalledOnValidThread()); 106 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
106 observer_list_.AddObserver(observer); 107 observer_list_.AddObserver(observer);
107 } 108 }
108 109
109 void UsbService::RemoveObserver(Observer* observer) { 110 void UsbService::RemoveObserver(Observer* observer) {
110 DCHECK(CalledOnValidThread()); 111 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
111 observer_list_.RemoveObserver(observer); 112 observer_list_.RemoveObserver(observer);
112 } 113 }
113 114
114 void UsbService::AddDeviceForTesting(scoped_refptr<UsbDevice> device) { 115 void UsbService::AddDeviceForTesting(scoped_refptr<UsbDevice> device) {
115 DCHECK(CalledOnValidThread()); 116 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
116 DCHECK(!ContainsKey(devices_, device->guid())); 117 DCHECK(!ContainsKey(devices_, device->guid()));
117 devices_[device->guid()] = device; 118 devices_[device->guid()] = device;
118 testing_devices_.insert(device->guid()); 119 testing_devices_.insert(device->guid());
119 NotifyDeviceAdded(device); 120 NotifyDeviceAdded(device);
120 } 121 }
121 122
122 void UsbService::RemoveDeviceForTesting(const std::string& device_guid) { 123 void UsbService::RemoveDeviceForTesting(const std::string& device_guid) {
123 DCHECK(CalledOnValidThread()); 124 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
124 // Allow only devices added with AddDeviceForTesting to be removed with this 125 // Allow only devices added with AddDeviceForTesting to be removed with this
125 // method. 126 // method.
126 auto testing_devices_it = testing_devices_.find(device_guid); 127 auto testing_devices_it = testing_devices_.find(device_guid);
127 if (testing_devices_it != testing_devices_.end()) { 128 if (testing_devices_it != testing_devices_.end()) {
128 auto devices_it = devices_.find(device_guid); 129 auto devices_it = devices_.find(device_guid);
129 DCHECK(devices_it != devices_.end()); 130 DCHECK(devices_it != devices_.end());
130 scoped_refptr<UsbDevice> device = devices_it->second; 131 scoped_refptr<UsbDevice> device = devices_it->second;
131 devices_.erase(devices_it); 132 devices_.erase(devices_it);
132 testing_devices_.erase(testing_devices_it); 133 testing_devices_.erase(testing_devices_it);
133 UsbService::NotifyDeviceRemoved(device); 134 UsbService::NotifyDeviceRemoved(device);
134 } 135 }
135 } 136 }
136 137
137 void UsbService::GetTestDevices( 138 void UsbService::GetTestDevices(
138 std::vector<scoped_refptr<UsbDevice>>* devices) { 139 std::vector<scoped_refptr<UsbDevice>>* devices) {
139 devices->clear(); 140 devices->clear();
140 devices->reserve(testing_devices_.size()); 141 devices->reserve(testing_devices_.size());
141 for (const std::string& guid : testing_devices_) { 142 for (const std::string& guid : testing_devices_) {
142 auto it = devices_.find(guid); 143 auto it = devices_.find(guid);
143 DCHECK(it != devices_.end()); 144 DCHECK(it != devices_.end());
144 devices->push_back(it->second); 145 devices->push_back(it->second);
145 } 146 }
146 } 147 }
147 148
148 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { 149 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
149 DCHECK(CalledOnValidThread()); 150 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
150 151
151 for (auto& observer : observer_list_) 152 for (auto& observer : observer_list_)
152 observer.OnDeviceAdded(device); 153 observer.OnDeviceAdded(device);
153 } 154 }
154 155
155 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { 156 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
156 DCHECK(CalledOnValidThread()); 157 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
157 158
158 for (auto& observer : observer_list_) 159 for (auto& observer : observer_list_)
159 observer.OnDeviceRemoved(device); 160 observer.OnDeviceRemoved(device);
160 device->NotifyDeviceRemoved(); 161 device->NotifyDeviceRemoved();
161 for (auto& observer : observer_list_) 162 for (auto& observer : observer_list_)
162 observer.OnDeviceRemovedCleanup(device); 163 observer.OnDeviceRemovedCleanup(device);
163 } 164 }
164 165
165 } // namespace device 166 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698