diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-16 00:28:17 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-16 00:28:17 +0000 |
commit | 0216e79b17c2d45f596f374d34fde579d131cc46 (patch) | |
tree | c82284c61d196416febffc6f6d58939ea32a39c4 /device/bluetooth/bluetooth_adapter_win.cc | |
parent | d39eefeb284e5ef5ec1012a2e27d97697678fc28 (diff) | |
download | chromium_src-0216e79b17c2d45f596f374d34fde579d131cc46.zip chromium_src-0216e79b17c2d45f596f374d34fde579d131cc46.tar.gz chromium_src-0216e79b17c2d45f596f374d34fde579d131cc46.tar.bz2 |
Implemented BluetoothTaskManagerWin class.
BluetoothAdapterWin owns BluetoothTaskManagerWin and registers itself as an observer to BluetoothTaskManagerWin. The BluetoothTaskManagerWin polls the Bluetooth Windows API to get the up-to-date adapter status, and notifies BluetoothAdapterWin upon changes.
BUG=135470
Review URL: https://chromiumcodereview.appspot.com/11411130
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@177024 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device/bluetooth/bluetooth_adapter_win.cc')
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win.cc | 116 |
1 files changed, 17 insertions, 99 deletions
diff --git a/device/bluetooth/bluetooth_adapter_win.cc b/device/bluetooth/bluetooth_adapter_win.cc index 0df985cb..1f0efce 100644 --- a/device/bluetooth/bluetooth_adapter_win.cc +++ b/device/bluetooth/bluetooth_adapter_win.cc @@ -6,65 +6,14 @@ #include "device/bluetooth/bluetooth_adapter_win.h" -#include <BluetoothAPIs.h> -#if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 -// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h. -#undef FACILITY_VISUALCPP -#endif -#include <delayimp.h> #include <string> -#include "base/bind.h" #include "base/logging.h" -#include "base/message_loop.h" -#include "base/stringprintf.h" -#include "base/sys_string_conversions.h" -#include "base/threading/thread_restrictions.h" - -#pragma comment(lib, "Bthprops.lib") -#pragma comment(lib, "delayimp.lib") - -namespace { - -const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param = - { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; - -// A frame-based exception handler filter function for a handler for exceptions -// generated by the Visual C++ delay loader helper function. -int FilterVisualCPPExceptions(DWORD exception_code) { - return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ? - EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; -} - -// Returns true if the machine has a bluetooth stack available. The first call -// to this function will involve file IO, so it should be done on an -// appropriate thread. This function is not threadsafe. -bool HasBluetoothStack() { - static enum { - HBS_UNKNOWN, - HBS_YES, - HBS_NO, - } has_bluetooth_stack = HBS_UNKNOWN; - - if (has_bluetooth_stack == HBS_UNKNOWN) { - base::ThreadRestrictions::AssertIOAllowed(); - HRESULT hr = E_FAIL; - __try { - hr = __HrLoadAllImportsForDll("bthprops.cpl"); - } __except(FilterVisualCPPExceptions(::GetExceptionCode())) { - hr = E_FAIL; - } - has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO; - } - - return has_bluetooth_stack == HBS_YES; -} - -} // namespace +#include "base/sequenced_task_runner.h" +#include "base/single_thread_task_runner.h" +#include "base/thread_task_runner_handle.h" namespace device { -const int BluetoothAdapterWin::kPollIntervalMs = 500; - BluetoothAdapterWin::BluetoothAdapterWin() : BluetoothAdapter(), powered_(false), @@ -72,6 +21,10 @@ BluetoothAdapterWin::BluetoothAdapterWin() } BluetoothAdapterWin::~BluetoothAdapterWin() { + if (task_manager_) { + task_manager_->RemoveObserver(this); + task_manager_->Shutdown(); + } } void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { @@ -131,54 +84,19 @@ void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( NOTIMPLEMENTED(); } -void BluetoothAdapterWin::UpdateAdapterState() { - // TODO(youngki): Move this check to the earliest point reasonable so that no - // attempts are made to do any bluetooth processing if no BT stack is present. - if (!HasBluetoothStack()) - return; - - HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL; - BLUETOOTH_RADIO_INFO bluetooth_adapter_info = - { sizeof(BLUETOOTH_RADIO_INFO), 0 }; - HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio( - &bluetooth_adapter_param, &bluetooth_adapter_handle); - - if (bluetooth_adapter_handle) { - if (ERROR_SUCCESS == BluetoothGetRadioInfo(bluetooth_adapter_handle, - &bluetooth_adapter_info)) { - name_ = base::SysWideToUTF8(bluetooth_adapter_info.szName); - address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", - bluetooth_adapter_info.address.rgBytes[5], - bluetooth_adapter_info.address.rgBytes[4], - bluetooth_adapter_info.address.rgBytes[3], - bluetooth_adapter_info.address.rgBytes[2], - bluetooth_adapter_info.address.rgBytes[1], - bluetooth_adapter_info.address.rgBytes[0]); - powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) || - BluetoothIsDiscoverable(bluetooth_adapter_handle); - } else { - name_.clear(); - address_.clear(); - powered_ = false; - } - } - - if (bluetooth_handle) - BluetoothFindRadioClose(bluetooth_handle); +void BluetoothAdapterWin::AdapterStateChanged( + const BluetoothTaskManagerWin::AdapterState& state) { + DCHECK(thread_checker_.CalledOnValidThread()); + name_ = state.name; + address_ = state.address; + powered_ = state.powered; } void BluetoothAdapterWin::TrackDefaultAdapter() { - PollAdapterState(); -} - -void BluetoothAdapterWin::PollAdapterState() { - UpdateAdapterState(); - - MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&BluetoothAdapterWin::PollAdapterState, - weak_ptr_factory_.GetWeakPtr()), - base::TimeDelta::FromMilliseconds(kPollIntervalMs)); + task_manager_ = + new BluetoothTaskManagerWin(base::ThreadTaskRunnerHandle::Get()); + task_manager_->AddObserver(this); + task_manager_->Initialize(); } } // namespace device |