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

Side by Side Diff: device/bluetooth/bluetooth_device_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: Rebasing, code cleanup, git cl format. 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_device_win.h" 5 #include "device/bluetooth/bluetooth_device_win.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
12 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "device/bluetooth/bluetooth_service_record_win.h" 15 #include "device/bluetooth/bluetooth_service_record_win.h"
15 #include "device/bluetooth/bluetooth_socket_thread.h" 16 #include "device/bluetooth/bluetooth_socket_thread.h"
16 #include "device/bluetooth/bluetooth_socket_win.h" 17 #include "device/bluetooth/bluetooth_socket_win.h"
17 #include "device/bluetooth/bluetooth_task_manager_win.h" 18 #include "device/bluetooth/bluetooth_task_manager_win.h"
18 #include "device/bluetooth/bluetooth_uuid.h" 19 #include "device/bluetooth/bluetooth_uuid.h"
19 20
20 namespace { 21 namespace {
21 22
22 const int kSdpBytesBufferSize = 1024; 23 const int kSdpBytesBufferSize = 1024;
23 24
24 } // namespace 25 } // namespace
25 26
26 namespace device { 27 namespace device {
27 28
28 BluetoothDeviceWin::BluetoothDeviceWin( 29 BluetoothDeviceWin::BluetoothDeviceWin(
29 const BluetoothTaskManagerWin::DeviceState& state, 30 const BluetoothTaskManagerWin::DeviceState& device_state,
30 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 31 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
31 scoped_refptr<BluetoothSocketThread> socket_thread, 32 const scoped_refptr<BluetoothSocketThread>& socket_thread,
32 net::NetLog* net_log, 33 net::NetLog* net_log,
33 const net::NetLog::Source& net_log_source) 34 const net::NetLog::Source& net_log_source)
34 : BluetoothDevice(), 35 : BluetoothDevice(),
35 ui_task_runner_(ui_task_runner), 36 ui_task_runner_(ui_task_runner),
36 socket_thread_(socket_thread), 37 socket_thread_(socket_thread),
37 net_log_(net_log), 38 net_log_(net_log),
38 net_log_source_(net_log_source) { 39 net_log_source_(net_log_source) {
39 name_ = state.name; 40 Update(device_state);
40 address_ = CanonicalizeAddress(state.address); 41 }
41 bluetooth_class_ = state.bluetooth_class; 42
42 visible_ = state.visible; 43 BluetoothDeviceWin::~BluetoothDeviceWin() {
43 connected_ = state.connected; 44 }
44 paired_ = state.authenticated; 45
46 void BluetoothDeviceWin::Update(
47 const BluetoothTaskManagerWin::DeviceState& device_state) {
48 address_ = device_state.address;
49 // Note: Callers are responsible for providing a canonicalized address.
50 DCHECK_EQ(address_, BluetoothDevice::CanonicalizeAddress(address_));
51 name_ = device_state.name;
52 bluetooth_class_ = device_state.bluetooth_class;
53 visible_ = device_state.visible;
54 connected_ = device_state.connected;
55 paired_ = device_state.authenticated;
56 UpdateServices(device_state);
57 }
58
59
60 void BluetoothDeviceWin::UpdateServices(
61 const BluetoothTaskManagerWin::DeviceState& device_state) {
62 uuids_.clear();
63 service_record_list_.clear();
45 64
46 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 65 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
47 iter = state.service_record_states.begin(); 66 iter = device_state.service_record_states.begin();
48 iter != state.service_record_states.end(); 67 iter != device_state.service_record_states.end();
49 ++iter) { 68 ++iter) {
50 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( 69 BluetoothServiceRecordWin* service_record =
51 state.address, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); 70 new BluetoothServiceRecordWin(device_state.address,
71 (*iter)->name,
72 (*iter)->sdp_bytes,
73 (*iter)->gatt_uuid);
52 service_record_list_.push_back(service_record); 74 service_record_list_.push_back(service_record);
53 uuids_.push_back(service_record->uuid()); 75 uuids_.push_back(service_record->uuid());
54 } 76 }
55 } 77 }
56 78
57 BluetoothDeviceWin::~BluetoothDeviceWin() { 79 bool BluetoothDeviceWin::IsEqual(
80 const BluetoothTaskManagerWin::DeviceState& device_state) {
81 if (address_ != device_state.address || name_ != device_state.name ||
82 bluetooth_class_ != device_state.bluetooth_class ||
83 visible_ != device_state.visible ||
84 connected_ != device_state.connected ||
85 paired_ != device_state.authenticated) {
86 return false;
87 }
88
89 // Checks service collection
90 typedef std::set<BluetoothUUID> UUIDSet;
91 typedef base::ScopedPtrHashMap<std::string, BluetoothServiceRecordWin>
92 ServiceRecordMap;
93
94 UUIDSet known_services;
95 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
96 ++iter) {
97 known_services.insert((*iter));
98 }
99
100 UUIDSet new_services;
101 ServiceRecordMap new_service_records;
102 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
103 iter = device_state.service_record_states.begin();
104 iter != device_state.service_record_states.end();
105 ++iter) {
106 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
107 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid);
108 new_services.insert(service_record->uuid());
109 new_service_records.set(
110 service_record->uuid().canonical_value(),
111 scoped_ptr<BluetoothServiceRecordWin>(service_record));
112 }
113
114 UUIDSet removed_services =
115 base::STLSetDifference<UUIDSet>(known_services, new_services);
116 if (removed_services.size()) {
xiyuan 2014/07/30 21:41:47 nit: !removed_services.empty()
rpaquay 2014/07/30 22:34:25 Done.
117 return false;
118 }
119 UUIDSet added_devices =
120 base::STLSetDifference<UUIDSet>(new_services, known_services);
121 if (added_devices.size()) {
xiyuan 2014/07/30 21:41:47 nit: !added_devices.empty()
rpaquay 2014/07/30 22:34:25 Done.
122 return false;
123 }
124
125 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
126 iter != service_record_list_.end();
127 ++iter) {
128 BluetoothServiceRecordWin* service_record = (*iter);
129 BluetoothServiceRecordWin* new_service_record =
130 new_service_records.get((*iter)->uuid().canonical_value());
131 if (!service_record->IsEqual(*new_service_record))
132 return false;
133 }
134 return true;
58 } 135 }
59 136
60 void BluetoothDeviceWin::SetVisible(bool visible) { 137 void BluetoothDeviceWin::SetVisible(bool visible) {
61 visible_ = visible; 138 visible_ = visible;
62 } 139 }
63 140
64 uint32 BluetoothDeviceWin::GetBluetoothClass() const { 141 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
65 return bluetooth_class_; 142 return bluetooth_class_;
66 } 143 }
67 144
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); 282 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
206 iter != service_record_list_.end(); 283 iter != service_record_list_.end();
207 ++iter) { 284 ++iter) {
208 if ((*iter)->uuid() == uuid) 285 if ((*iter)->uuid() == uuid)
209 return *iter; 286 return *iter;
210 } 287 }
211 return NULL; 288 return NULL;
212 } 289 }
213 290
214 } // namespace device 291 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698