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

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

Issue 1672843002: Implement fake Bluetooth adapter for BluetoothTest.ConstructFakeAdapter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 10 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_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
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
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 device_search_params.fIssueInquiry = (search_cached_devices_only ? 0 : 1); 436 device_search_params.fIssueInquiry = (search_cached_devices_only ? 0 : 1);
442 device_search_params.cTimeoutMultiplier = timeout_multiplier; 437 device_search_params.cTimeoutMultiplier = timeout_multiplier;
443 438
444 BLUETOOTH_DEVICE_INFO device_info; 439 BLUETOOTH_DEVICE_INFO device_info;
445 ZeroMemory(&device_info, sizeof(device_info)); 440 ZeroMemory(&device_info, sizeof(device_info));
446 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); 441 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
447 HBLUETOOTH_DEVICE_FIND handle = 442 HBLUETOOTH_DEVICE_FIND handle =
448 win::BluetoothClassicWrapper::GetInstance()->FindFirstDevice( 443 win::BluetoothClassicWrapper::GetInstance()->FindFirstDevice(
449 &device_search_params, &device_info); 444 &device_search_params, &device_info);
450 if (!handle) { 445 if (!handle) {
451 int last_error = GetLastError(); 446 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError();
452 if (last_error == ERROR_NO_MORE_ITEMS) { 447 if (last_error == ERROR_NO_MORE_ITEMS) {
453 return true; // No devices is not an error. 448 return true; // No devices is not an error.
454 } 449 }
455 LogPollingError("Error calling BluetoothFindFirstDevice", last_error); 450 LogPollingError("Error calling BluetoothFindFirstDevice", last_error);
456 return false; 451 return false;
457 } 452 }
458 453
459 while (true) { 454 while (true) {
460 DeviceState* device_state = new DeviceState(); 455 DeviceState* device_state = new DeviceState();
461 GetDeviceState(device_info, device_state); 456 GetDeviceState(device_info, device_state);
462 device_list->push_back(device_state); 457 device_list->push_back(device_state);
463 458
464 // Reset device info before next call (as a safety precaution). 459 // Reset device info before next call (as a safety precaution).
465 ZeroMemory(&device_info, sizeof(device_info)); 460 ZeroMemory(&device_info, sizeof(device_info));
466 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); 461 device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
467 if (!win::BluetoothClassicWrapper::GetInstance()->FindNextDevice( 462 if (!win::BluetoothClassicWrapper::GetInstance()->FindNextDevice(
468 handle, &device_info)) { 463 handle, &device_info)) {
469 int last_error = GetLastError(); 464 int last_error = win::BluetoothClassicWrapper::GetInstance()->LastError();
470 if (last_error == ERROR_NO_MORE_ITEMS) { 465 if (last_error == ERROR_NO_MORE_ITEMS) {
471 break; // No more items is expected error when done enumerating. 466 break; // No more items is expected error when done enumerating.
472 } 467 }
473 LogPollingError("Error calling BluetoothFindNextDevice", last_error); 468 LogPollingError("Error calling BluetoothFindNextDevice", last_error);
474 win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle); 469 win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle);
475 return false; 470 return false;
476 } 471 }
477 } 472 }
478 473
479 if (!win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle)) { 474 if (!win::BluetoothClassicWrapper::GetInstance()->FindDeviceClose(handle)) {
480 LogPollingError("Error calling BluetoothFindDeviceClose", GetLastError()); 475 LogPollingError("Error calling BluetoothFindDeviceClose",
476 win::BluetoothClassicWrapper::GetInstance()->LastError());
481 return false; 477 return false;
482 } 478 }
483 return true; 479 return true;
484 } 480 }
485 481
486 bool BluetoothTaskManagerWin::SearchLowEnergyDevices( 482 bool BluetoothTaskManagerWin::SearchLowEnergyDevices(
487 ScopedVector<DeviceState>* device_list) { 483 ScopedVector<DeviceState>* device_list) {
488 if (!win::IsBluetoothLowEnergySupported()) 484 if (!win::IsBluetoothLowEnergySupported())
489 return true; // Bluetooth LE not supported is not an error. 485 return true; // Bluetooth LE not supported is not an error.
490 486
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 break; 718 break;
723 } 719 }
724 } 720 }
725 } 721 }
726 } 722 }
727 723
728 return true; 724 return true;
729 } 725 }
730 726
731 } // namespace device 727 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_task_manager_win.h ('k') | device/bluetooth/test/bluetooth_test_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698