| 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_task_manager_win.h" | 5 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <winsock2.h> | 8 #include <winsock2.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
| 19 #include "base/win/scoped_handle.h" | |
| 20 #include "device/bluetooth/bluetooth_classic_win.h" | 19 #include "device/bluetooth/bluetooth_classic_win.h" |
| 21 #include "device/bluetooth/bluetooth_device.h" | 20 #include "device/bluetooth/bluetooth_device.h" |
| 22 #include "device/bluetooth/bluetooth_init_win.h" | 21 #include "device/bluetooth/bluetooth_init_win.h" |
| 23 #include "device/bluetooth/bluetooth_low_energy_win.h" | 22 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 24 #include "device/bluetooth/bluetooth_service_record_win.h" | 23 #include "device/bluetooth/bluetooth_service_record_win.h" |
| 25 #include "net/base/winsock_init.h" | 24 #include "net/base/winsock_init.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 const int kNumThreadsInWorkerPool = 3; | 28 const int kNumThreadsInWorkerPool = 3; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 bluetooth_class(0) { | 134 bluetooth_class(0) { |
| 136 } | 135 } |
| 137 | 136 |
| 138 BluetoothTaskManagerWin::DeviceState::~DeviceState() { | 137 BluetoothTaskManagerWin::DeviceState::~DeviceState() { |
| 139 } | 138 } |
| 140 | 139 |
| 141 BluetoothTaskManagerWin::BluetoothTaskManagerWin( | 140 BluetoothTaskManagerWin::BluetoothTaskManagerWin( |
| 142 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | 141 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 143 : ui_task_runner_(ui_task_runner), | 142 : ui_task_runner_(ui_task_runner), |
| 144 discovering_(false), | 143 discovering_(false), |
| 145 current_logging_batch_count_(0) { | 144 current_logging_batch_count_(0), |
| 146 } | 145 adapter_handle_(NULL) {} |
| 147 | 146 |
| 148 BluetoothTaskManagerWin::~BluetoothTaskManagerWin() { | 147 BluetoothTaskManagerWin::~BluetoothTaskManagerWin() { |
| 149 win::BluetoothLowEnergyWrapper::DeleteInstance(); | 148 win::BluetoothLowEnergyWrapper::DeleteInstance(); |
| 150 win::BluetoothClassicWrapper::DeleteInstance(); | 149 win::BluetoothClassicWrapper::DeleteInstance(); |
| 151 } | 150 } |
| 152 | 151 |
| 153 void BluetoothTaskManagerWin::AddObserver(Observer* observer) { | 152 void BluetoothTaskManagerWin::AddObserver(Observer* observer) { |
| 154 DCHECK(observer); | 153 DCHECK(observer); |
| 155 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 154 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 156 observers_.AddObserver(observer); | 155 observers_.AddObserver(observer); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 BluetoothTaskManagerWin::Observer, observers_, DevicesPolled(*devices)); | 289 BluetoothTaskManagerWin::Observer, observers_, DevicesPolled(*devices)); |
| 291 } | 290 } |
| 292 | 291 |
| 293 void BluetoothTaskManagerWin::PollAdapter() { | 292 void BluetoothTaskManagerWin::PollAdapter() { |
| 294 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 293 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 295 | 294 |
| 296 // Skips updating the adapter info if the adapter is in discovery mode. | 295 // Skips updating the adapter info if the adapter is in discovery mode. |
| 297 if (!discovering_) { | 296 if (!discovering_) { |
| 298 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = | 297 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = |
| 299 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | 298 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
| 300 if (adapter_handle_.IsValid()) | |
| 301 adapter_handle_.Close(); | |
| 302 HANDLE temp_adapter_handle; | 299 HANDLE temp_adapter_handle; |
| 303 HBLUETOOTH_RADIO_FIND handle = | 300 HBLUETOOTH_RADIO_FIND handle = |
| 304 win::BluetoothClassicWrapper::GetInstance()->FindFirstRadio( | 301 win::BluetoothClassicWrapper::GetInstance()->FindFirstRadio( |
| 305 &adapter_param, &temp_adapter_handle); | 302 &adapter_param, &temp_adapter_handle); |
| 306 | 303 |
| 307 if (handle) { | 304 if (handle) { |
| 308 adapter_handle_.Set(temp_adapter_handle); | 305 adapter_handle_ = temp_adapter_handle; |
| 309 GetKnownDevices(); | 306 GetKnownDevices(); |
| 310 win::BluetoothClassicWrapper::GetInstance()->FindRadioClose(handle); | 307 win::BluetoothClassicWrapper::GetInstance()->FindRadioClose(handle); |
| 311 } | 308 } |
| 312 | 309 |
| 313 PostAdapterStateToUi(); | 310 PostAdapterStateToUi(); |
| 314 } | 311 } |
| 315 | 312 |
| 316 // Re-poll. | 313 // Re-poll. |
| 317 bluetooth_task_runner_->PostDelayedTask( | 314 bluetooth_task_runner_->PostDelayedTask( |
| 318 FROM_HERE, | 315 FROM_HERE, |
| 319 base::Bind(&BluetoothTaskManagerWin::PollAdapter, | 316 base::Bind(&BluetoothTaskManagerWin::PollAdapter, |
| 320 this), | 317 this), |
| 321 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | 318 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 322 } | 319 } |
| 323 | 320 |
| 324 void BluetoothTaskManagerWin::PostAdapterStateToUi() { | 321 void BluetoothTaskManagerWin::PostAdapterStateToUi() { |
| 325 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 322 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 326 AdapterState* state = new AdapterState(); | 323 AdapterState* state = new AdapterState(); |
| 327 GetAdapterState(adapter_handle_.Get(), state); | 324 GetAdapterState(adapter_handle_, state); |
| 328 ui_task_runner_->PostTask( | 325 ui_task_runner_->PostTask( |
| 329 FROM_HERE, | 326 FROM_HERE, |
| 330 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, | 327 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, |
| 331 this, | 328 this, |
| 332 base::Owned(state))); | 329 base::Owned(state))); |
| 333 } | 330 } |
| 334 | 331 |
| 335 void BluetoothTaskManagerWin::SetPowered( | 332 void BluetoothTaskManagerWin::SetPowered( |
| 336 bool powered, | 333 bool powered, |
| 337 const base::Closure& callback, | 334 const base::Closure& callback, |
| 338 const BluetoothAdapter::ErrorCallback& error_callback) { | 335 const BluetoothAdapter::ErrorCallback& error_callback) { |
| 339 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 336 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 340 bool success = false; | 337 bool success = false; |
| 341 if (adapter_handle_.IsValid()) { | 338 if (adapter_handle_) { |
| 342 if (!powered) { | 339 if (!powered) { |
| 343 win::BluetoothClassicWrapper::GetInstance()->EnableDiscovery( | 340 win::BluetoothClassicWrapper::GetInstance()->EnableDiscovery( |
| 344 adapter_handle_.Get(), false); | 341 adapter_handle_, false); |
| 345 } | 342 } |
| 346 success = !!win::BluetoothClassicWrapper::GetInstance() | 343 success = !!win::BluetoothClassicWrapper::GetInstance() |
| 347 ->EnableIncomingConnections(adapter_handle_.Get(), powered); | 344 ->EnableIncomingConnections(adapter_handle_, powered); |
| 348 } | 345 } |
| 349 | 346 |
| 350 if (success) { | 347 if (success) { |
| 351 PostAdapterStateToUi(); | 348 PostAdapterStateToUi(); |
| 352 ui_task_runner_->PostTask(FROM_HERE, callback); | 349 ui_task_runner_->PostTask(FROM_HERE, callback); |
| 353 } else { | 350 } else { |
| 354 ui_task_runner_->PostTask(FROM_HERE, error_callback); | 351 ui_task_runner_->PostTask(FROM_HERE, error_callback); |
| 355 } | 352 } |
| 356 } | 353 } |
| 357 | 354 |
| 358 void BluetoothTaskManagerWin::StartDiscovery() { | 355 void BluetoothTaskManagerWin::StartDiscovery() { |
| 359 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 356 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 360 ui_task_runner_->PostTask( | 357 ui_task_runner_->PostTask( |
| 361 FROM_HERE, | 358 FROM_HERE, base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStarted, this, |
| 362 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStarted, | 359 !!adapter_handle_)); |
| 363 this, | 360 if (!adapter_handle_) |
| 364 adapter_handle_.IsValid())); | |
| 365 if (!adapter_handle_.IsValid()) | |
| 366 return; | 361 return; |
| 367 discovering_ = true; | 362 discovering_ = true; |
| 368 | 363 |
| 369 DiscoverDevices(1); | 364 DiscoverDevices(1); |
| 370 } | 365 } |
| 371 | 366 |
| 372 void BluetoothTaskManagerWin::StopDiscovery() { | 367 void BluetoothTaskManagerWin::StopDiscovery() { |
| 373 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 368 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 374 discovering_ = false; | 369 discovering_ = false; |
| 375 ui_task_runner_->PostTask( | 370 ui_task_runner_->PostTask( |
| 376 FROM_HERE, | 371 FROM_HERE, |
| 377 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); | 372 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); |
| 378 } | 373 } |
| 379 | 374 |
| 380 void BluetoothTaskManagerWin::DiscoverDevices(int timeout_multiplier) { | 375 void BluetoothTaskManagerWin::DiscoverDevices(int timeout_multiplier) { |
| 381 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 376 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 382 if (!discovering_ || !adapter_handle_.IsValid()) { | 377 if (!discovering_ || !adapter_handle_) { |
| 383 ui_task_runner_->PostTask( | 378 ui_task_runner_->PostTask( |
| 384 FROM_HERE, | 379 FROM_HERE, |
| 385 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); | 380 base::Bind(&BluetoothTaskManagerWin::OnDiscoveryStopped, this)); |
| 386 return; | 381 return; |
| 387 } | 382 } |
| 388 | 383 |
| 389 scoped_ptr<ScopedVector<DeviceState> > device_list( | 384 scoped_ptr<ScopedVector<DeviceState> > device_list( |
| 390 new ScopedVector<DeviceState>()); | 385 new ScopedVector<DeviceState>()); |
| 391 if (SearchDevices(timeout_multiplier, false, device_list.get())) { | 386 if (SearchDevices(timeout_multiplier, false, device_list.get())) { |
| 392 ui_task_runner_->PostTask( | 387 ui_task_runner_->PostTask( |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 break; | 717 break; |
| 723 } | 718 } |
| 724 } | 719 } |
| 725 } | 720 } |
| 726 } | 721 } |
| 727 | 722 |
| 728 return true; | 723 return true; |
| 729 } | 724 } |
| 730 | 725 |
| 731 } // namespace device | 726 } // namespace device |
| OLD | NEW |