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_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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // If there are stop discovery requests, post the stop discovery again. | 126 // If there are stop discovery requests, post the stop discovery again. |
127 MaybePostStopDiscoveryTask(); | 127 MaybePostStopDiscoveryTask(); |
128 } else if (!on_stop_discovery_callbacks_.empty()) { | 128 } else if (!on_stop_discovery_callbacks_.empty()) { |
129 // If there are stop discovery requests but start discovery has failed, | 129 // If there are stop discovery requests but start discovery has failed, |
130 // notify that stop discovery has been complete. | 130 // notify that stop discovery has been complete. |
131 DiscoveryStopped(); | 131 DiscoveryStopped(); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 void BluetoothAdapterWin::DiscoveryStopped() { | 135 void BluetoothAdapterWin::DiscoveryStopped() { |
| 136 discovered_devices_.clear(); |
136 bool was_discovering = IsDiscovering(); | 137 bool was_discovering = IsDiscovering(); |
137 discovery_status_ = NOT_DISCOVERING; | 138 discovery_status_ = NOT_DISCOVERING; |
138 for (std::vector<base::Closure>::const_iterator iter = | 139 for (std::vector<base::Closure>::const_iterator iter = |
139 on_stop_discovery_callbacks_.begin(); | 140 on_stop_discovery_callbacks_.begin(); |
140 iter != on_stop_discovery_callbacks_.end(); | 141 iter != on_stop_discovery_callbacks_.end(); |
141 ++iter) { | 142 ++iter) { |
142 ui_task_runner_->PostTask(FROM_HERE, *iter); | 143 ui_task_runner_->PostTask(FROM_HERE, *iter); |
143 } | 144 } |
144 num_discovery_listeners_ = 0; | 145 num_discovery_listeners_ = 0; |
145 on_stop_discovery_callbacks_.clear(); | 146 on_stop_discovery_callbacks_.clear(); |
(...skipping 28 matching lines...) Expand all Loading... |
174 AdapterPoweredChanged(this, powered_)); | 175 AdapterPoweredChanged(this, powered_)); |
175 } | 176 } |
176 if (!initialized_) { | 177 if (!initialized_) { |
177 initialized_ = true; | 178 initialized_ = true; |
178 init_callback_.Run(); | 179 init_callback_.Run(); |
179 } | 180 } |
180 } | 181 } |
181 | 182 |
182 void BluetoothAdapterWin::DevicesDiscovered( | 183 void BluetoothAdapterWin::DevicesDiscovered( |
183 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { | 184 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { |
184 std::hash_set<std::string> device_address_list; | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
185 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = | 186 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = |
186 devices.begin(); | 187 devices.begin(); |
187 iter != devices.end(); | 188 iter != devices.end(); |
188 ++iter) { | 189 ++iter) { |
189 device_address_list.insert((*iter)->address); | 190 if (discovered_devices_.find((*iter)->address) == |
190 DevicesMap::iterator found_device_iter = devices_.find((*iter)->address); | 191 discovered_devices_.end()) { |
191 | 192 BluetoothDeviceWin device_win(**iter); |
192 if (found_device_iter == devices_.end()) { | |
193 devices_[(*iter)->address] = new BluetoothDeviceWin(**iter); | |
194 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 193 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
195 DeviceAdded(this, devices_[(*iter)->address])); | 194 DeviceAdded(this, &device_win)); |
196 continue; | 195 discovered_devices_.insert((*iter)->address); |
197 } | 196 } |
198 BluetoothDeviceWin* device_win = | |
199 static_cast<BluetoothDeviceWin*>(found_device_iter->second); | |
200 if (device_win->device_fingerprint() != | |
201 BluetoothDeviceWin::ComputeDeviceFingerprint(**iter)) { | |
202 devices_[(*iter)->address] = new BluetoothDeviceWin(**iter); | |
203 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
204 DeviceChanged(this, devices_[(*iter)->address])); | |
205 delete device_win; | |
206 } | |
207 } | |
208 | |
209 DevicesMap::iterator device_iter = devices_.begin(); | |
210 while (device_iter != devices_.end()) { | |
211 if (device_address_list.find(device_iter->first) != | |
212 device_address_list.end()) { | |
213 ++device_iter; | |
214 continue; | |
215 } | |
216 if (device_iter->second->IsConnected() || device_iter->second->IsPaired()) { | |
217 BluetoothDeviceWin* device_win = | |
218 static_cast<BluetoothDeviceWin*>(device_iter->second); | |
219 device_win->SetVisible(false); | |
220 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
221 DeviceChanged(this, device_win)); | |
222 ++device_iter; | |
223 continue; | |
224 } | |
225 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
226 DeviceRemoved(this, device_iter->second)); | |
227 delete device_iter->second; | |
228 device_iter = devices_.erase(device_iter); | |
229 } | 197 } |
230 } | 198 } |
231 | 199 |
| 200 void BluetoothAdapterWin::DevicesUpdated( |
| 201 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) { |
| 202 STLDeleteValues(&devices_); |
| 203 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter = |
| 204 devices.begin(); |
| 205 iter != devices.end(); |
| 206 ++iter) { |
| 207 devices_[(*iter)->address] = new BluetoothDeviceWin(**iter); |
| 208 } |
| 209 } |
| 210 |
232 void BluetoothAdapterWin::Init() { | 211 void BluetoothAdapterWin::Init() { |
233 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 212 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
234 task_manager_ = | 213 task_manager_ = |
235 new BluetoothTaskManagerWin(ui_task_runner_); | 214 new BluetoothTaskManagerWin(ui_task_runner_); |
236 task_manager_->AddObserver(this); | 215 task_manager_->AddObserver(this); |
237 task_manager_->Initialize(); | 216 task_manager_->Initialize(); |
238 } | 217 } |
239 | 218 |
240 void BluetoothAdapterWin::InitForTest( | 219 void BluetoothAdapterWin::InitForTest( |
241 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 220 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
(...skipping 27 matching lines...) Expand all Loading... |
269 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size(); | 248 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size(); |
270 on_stop_discovery_callbacks_.clear(); | 249 on_stop_discovery_callbacks_.clear(); |
271 return; | 250 return; |
272 } | 251 } |
273 | 252 |
274 discovery_status_ = DISCOVERY_STOPPING; | 253 discovery_status_ = DISCOVERY_STOPPING; |
275 task_manager_->PostStopDiscoveryTask(); | 254 task_manager_->PostStopDiscoveryTask(); |
276 } | 255 } |
277 | 256 |
278 } // namespace device | 257 } // namespace device |
OLD | NEW |