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

Side by Side Diff: device/bluetooth/bluetooth_adapter_win.cc

Issue 424093004: Improve processing of Bluetooth device discovery on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address code review feedback (nits and memory leak). Created 6 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bluetooth/bluetooth_adapter_win.h" 5 #include "device/bluetooth/bluetooth_adapter_win.h"
6 6
7 #include <hash_set> 7 #include <hash_set>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 powered_ = state.powered; 202 powered_ = state.powered;
203 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 203 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
204 AdapterPoweredChanged(this, powered_)); 204 AdapterPoweredChanged(this, powered_));
205 } 205 }
206 if (!initialized_) { 206 if (!initialized_) {
207 initialized_ = true; 207 initialized_ = true;
208 init_callback_.Run(); 208 init_callback_.Run();
209 } 209 }
210 } 210 }
211 211
212 void BluetoothAdapterWin::DevicesDiscovered( 212 void BluetoothAdapterWin::DevicesPolled(
213 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { 213 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
214 DCHECK(thread_checker_.CalledOnValidThread()); 214 DCHECK(thread_checker_.CalledOnValidThread());
215
216 // We are receiving a new list of all devices known to the system. Merge this
217 // new list with the list we know of (|devices_|) and raise corresponding
218 // DeviceAdded, DeviceRemoved and DeviceChanged events.
219
220 typedef std::set<std::string> DeviceAddressSet;
221 DeviceAddressSet known_devices;
222 for (DevicesMap::const_iterator iter = devices_.begin();
223 iter != devices_.end();
224 ++iter) {
225 known_devices.insert((*iter).first);
226 }
227
228 DeviceAddressSet new_devices;
215 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = 229 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
216 devices.begin(); 230 devices.begin();
217 iter != devices.end(); 231 iter != devices.end();
218 ++iter) { 232 ++iter) {
219 if (discovered_devices_.find((*iter)->address) == 233 new_devices.insert((*iter)->address);
220 discovered_devices_.end()) { 234 }
221 BluetoothDeviceWin device_win( 235
222 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source()); 236 // Process device removal first
223 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 237 DeviceAddressSet removed_devices =
224 DeviceAdded(this, &device_win)); 238 base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices);
225 discovered_devices_.insert((*iter)->address); 239 for (DeviceAddressSet::const_iterator iter = removed_devices.begin();
240 iter != removed_devices.end();
241 ++iter) {
242 BluetoothDevice* device_win = devices_[(*iter)];
243 devices_.erase(*iter);
244 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
245 observers_,
246 DeviceRemoved(this, device_win));
247 delete device_win;
248 }
249
250 // Process added and (maybe) changed devices in one pass
251 DeviceAddressSet added_devices =
252 base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices);
253 DeviceAddressSet changed_devices =
254 base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices);
255 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
256 devices.begin();
257 iter != devices.end();
258 ++iter) {
259 BluetoothTaskManagerWin::DeviceState* device_state = (*iter);
260 if (added_devices.find(device_state->address) != added_devices.end()) {
261 BluetoothDeviceWin* device_win =
262 new BluetoothDeviceWin(*device_state,
263 ui_task_runner_,
264 socket_thread_,
265 NULL,
266 net::NetLog::Source());
267 devices_[device_state->address] = device_win;
268 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
269 observers_,
270 DeviceAdded(this, device_win));
271 } else if (changed_devices.find(device_state->address) !=
272 changed_devices.end()) {
273 BluetoothDeviceWin* device_win =
274 static_cast<BluetoothDeviceWin*>(devices_[device_state->address]);
275 if (!device_win->IsEqual(*device_state)) {
276 device_win->Update(*device_state);
277 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
278 observers_,
279 DeviceChanged(this, device_win));
280 }
226 } 281 }
227 } 282 }
228 } 283 }
229 284
230 void BluetoothAdapterWin::DevicesUpdated(
231 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
232 STLDeleteValues(&devices_);
233 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
234 devices.begin();
235 iter != devices.end();
236 ++iter) {
237 devices_[(*iter)->address] = new BluetoothDeviceWin(
238 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source());
239 }
240 }
241
242 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING, 285 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING,
243 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped(). 286 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped().
244 void BluetoothAdapterWin::AddDiscoverySession( 287 void BluetoothAdapterWin::AddDiscoverySession(
245 const base::Closure& callback, 288 const base::Closure& callback,
246 const ErrorCallback& error_callback) { 289 const ErrorCallback& error_callback) {
247 if (discovery_status_ == DISCOVERING) { 290 if (discovery_status_ == DISCOVERING) {
248 num_discovery_listeners_++; 291 num_discovery_listeners_++;
249 callback.Run(); 292 callback.Run();
250 return; 293 return;
251 } 294 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size(); 349 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size();
307 on_stop_discovery_callbacks_.clear(); 350 on_stop_discovery_callbacks_.clear();
308 return; 351 return;
309 } 352 }
310 353
311 discovery_status_ = DISCOVERY_STOPPING; 354 discovery_status_ = DISCOVERY_STOPPING;
312 task_manager_->PostStopDiscoveryTask(); 355 task_manager_->PostStopDiscoveryTask();
313 } 356 }
314 357
315 } // namespace device 358 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_win.h ('k') | device/bluetooth/bluetooth_adapter_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698