OLD | NEW |
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 void BluetoothDeviceWin::UpdateServices( |
| 60 const BluetoothTaskManagerWin::DeviceState& device_state) { |
| 61 uuids_.clear(); |
| 62 service_record_list_.clear(); |
45 | 63 |
46 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator | 64 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
47 iter = state.service_record_states.begin(); | 65 iter = device_state.service_record_states.begin(); |
48 iter != state.service_record_states.end(); | 66 iter != device_state.service_record_states.end(); |
49 ++iter) { | 67 ++iter) { |
50 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( | 68 BluetoothServiceRecordWin* service_record = |
51 state.address, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); | 69 new BluetoothServiceRecordWin(device_state.address, |
| 70 (*iter)->name, |
| 71 (*iter)->sdp_bytes, |
| 72 (*iter)->gatt_uuid); |
52 service_record_list_.push_back(service_record); | 73 service_record_list_.push_back(service_record); |
53 uuids_.push_back(service_record->uuid()); | 74 uuids_.push_back(service_record->uuid()); |
54 } | 75 } |
55 } | 76 } |
56 | 77 |
57 BluetoothDeviceWin::~BluetoothDeviceWin() { | 78 bool BluetoothDeviceWin::IsEqual( |
| 79 const BluetoothTaskManagerWin::DeviceState& device_state) { |
| 80 if (address_ != device_state.address || name_ != device_state.name || |
| 81 bluetooth_class_ != device_state.bluetooth_class || |
| 82 visible_ != device_state.visible || |
| 83 connected_ != device_state.connected || |
| 84 paired_ != device_state.authenticated) { |
| 85 return false; |
| 86 } |
| 87 |
| 88 // Checks service collection |
| 89 typedef std::set<BluetoothUUID> UUIDSet; |
| 90 typedef base::ScopedPtrHashMap<std::string, BluetoothServiceRecordWin> |
| 91 ServiceRecordMap; |
| 92 |
| 93 UUIDSet known_services; |
| 94 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end(); |
| 95 ++iter) { |
| 96 known_services.insert((*iter)); |
| 97 } |
| 98 |
| 99 UUIDSet new_services; |
| 100 ServiceRecordMap new_service_records; |
| 101 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| 102 iter = device_state.service_record_states.begin(); |
| 103 iter != device_state.service_record_states.end(); |
| 104 ++iter) { |
| 105 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin( |
| 106 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid); |
| 107 new_services.insert(service_record->uuid()); |
| 108 new_service_records.set( |
| 109 service_record->uuid().canonical_value(), |
| 110 scoped_ptr<BluetoothServiceRecordWin>(service_record)); |
| 111 } |
| 112 |
| 113 UUIDSet removed_services = |
| 114 base::STLSetDifference<UUIDSet>(known_services, new_services); |
| 115 if (!removed_services.empty()) { |
| 116 return false; |
| 117 } |
| 118 UUIDSet added_devices = |
| 119 base::STLSetDifference<UUIDSet>(new_services, known_services); |
| 120 if (!added_devices.empty()) { |
| 121 return false; |
| 122 } |
| 123 |
| 124 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
| 125 iter != service_record_list_.end(); |
| 126 ++iter) { |
| 127 BluetoothServiceRecordWin* service_record = (*iter); |
| 128 BluetoothServiceRecordWin* new_service_record = |
| 129 new_service_records.get((*iter)->uuid().canonical_value()); |
| 130 if (!service_record->IsEqual(*new_service_record)) |
| 131 return false; |
| 132 } |
| 133 return true; |
58 } | 134 } |
59 | 135 |
60 void BluetoothDeviceWin::SetVisible(bool visible) { | 136 void BluetoothDeviceWin::SetVisible(bool visible) { |
61 visible_ = visible; | 137 visible_ = visible; |
62 } | 138 } |
63 | 139 |
64 uint32 BluetoothDeviceWin::GetBluetoothClass() const { | 140 uint32 BluetoothDeviceWin::GetBluetoothClass() const { |
65 return bluetooth_class_; | 141 return bluetooth_class_; |
66 } | 142 } |
67 | 143 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); | 281 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); |
206 iter != service_record_list_.end(); | 282 iter != service_record_list_.end(); |
207 ++iter) { | 283 ++iter) { |
208 if ((*iter)->uuid() == uuid) | 284 if ((*iter)->uuid() == uuid) |
209 return *iter; | 285 return *iter; |
210 } | 286 } |
211 return NULL; | 287 return NULL; |
212 } | 288 } |
213 | 289 |
214 } // namespace device | 290 } // namespace device |
OLD | NEW |