OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 6 |
| 7 #include <BluetoothAPIs.h> |
| 8 #if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 |
| 9 // The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h. |
| 10 #undef FACILITY_VISUALCPP |
| 11 #endif |
| 12 #include <delayimp.h> |
| 13 #include <string> |
| 14 |
| 15 #include "base/bind.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/message_loop.h" |
| 18 #include "base/sequenced_task_runner.h" |
| 19 #include "base/stringprintf.h" |
| 20 #include "base/sys_string_conversions.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/threading/thread_restrictions.h" |
| 23 #include "base/win/scoped_handle.h" |
| 24 |
| 25 #pragma comment(lib, "Bthprops.lib") |
| 26 |
| 27 namespace { |
| 28 |
| 29 const int kNumThreadsInWorkerPool = 3; |
| 30 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; |
| 31 |
| 32 // A frame-based exception handler filter function for a handler for exceptions |
| 33 // generated by the Visual C++ delay loader helper function. |
| 34 int FilterVisualCPPExceptions(DWORD exception_code) { |
| 35 return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ? |
| 36 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; |
| 37 } // namespace |
| 38 |
| 39 // Populates bluetooth adapter state using adapter_handle. |
| 40 void GetAdapterState(HANDLE adapter_handle, |
| 41 device::BluetoothTaskManagerWin::AdapterState* state) { |
| 42 std::string name; |
| 43 std::string address; |
| 44 bool powered = false; |
| 45 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
| 46 if (adapter_handle && |
| 47 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, |
| 48 &adapter_info)) { |
| 49 name = base::SysWideToUTF8(adapter_info.szName); |
| 50 address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
| 51 adapter_info.address.rgBytes[5], |
| 52 adapter_info.address.rgBytes[4], |
| 53 adapter_info.address.rgBytes[3], |
| 54 adapter_info.address.rgBytes[2], |
| 55 adapter_info.address.rgBytes[1], |
| 56 adapter_info.address.rgBytes[0]); |
| 57 powered = !!BluetoothIsConnectable(adapter_handle); |
| 58 } |
| 59 state->name = name; |
| 60 state->address = address; |
| 61 state->powered = powered; |
| 62 } |
| 63 |
| 64 } |
| 65 |
| 66 namespace device { |
| 67 |
| 68 // static |
| 69 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; |
| 70 |
| 71 // static |
| 72 bool BluetoothTaskManagerWin::HasBluetoothStack() { |
| 73 static enum { |
| 74 HBS_UNKNOWN, |
| 75 HBS_YES, |
| 76 HBS_NO, |
| 77 } has_bluetooth_stack = HBS_UNKNOWN; |
| 78 |
| 79 if (has_bluetooth_stack == HBS_UNKNOWN) { |
| 80 base::ThreadRestrictions::AssertIOAllowed(); |
| 81 HRESULT hr = E_FAIL; |
| 82 __try { |
| 83 hr = __HrLoadAllImportsForDll("bthprops.cpl"); |
| 84 } __except(FilterVisualCPPExceptions(::GetExceptionCode())) { |
| 85 hr = E_FAIL; |
| 86 } |
| 87 has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO; |
| 88 } |
| 89 |
| 90 return has_bluetooth_stack == HBS_YES; |
| 91 } |
| 92 |
| 93 BluetoothTaskManagerWin::BluetoothTaskManagerWin( |
| 94 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 95 : ui_task_runner_(ui_task_runner), |
| 96 worker_pool_(new base::SequencedWorkerPool(kNumThreadsInWorkerPool, |
| 97 kBluetoothThreadName)), |
| 98 bluetooth_task_runner_( |
| 99 worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( |
| 100 worker_pool_->GetSequenceToken(), |
| 101 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { |
| 102 } |
| 103 |
| 104 BluetoothTaskManagerWin::BluetoothTaskManagerWin( |
| 105 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 106 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) |
| 107 : ui_task_runner_(ui_task_runner), |
| 108 bluetooth_task_runner_(bluetooth_task_runner) { |
| 109 } |
| 110 |
| 111 BluetoothTaskManagerWin::~BluetoothTaskManagerWin() { |
| 112 } |
| 113 |
| 114 void BluetoothTaskManagerWin::AddObserver(Observer* observer) { |
| 115 DCHECK(observer); |
| 116 observers_.AddObserver(observer); |
| 117 } |
| 118 |
| 119 void BluetoothTaskManagerWin::RemoveObserver(Observer* observer) { |
| 120 DCHECK(observer); |
| 121 observers_.RemoveObserver(observer); |
| 122 } |
| 123 |
| 124 void BluetoothTaskManagerWin::Initialize() { |
| 125 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 126 bluetooth_task_runner_->PostTask( |
| 127 FROM_HERE, |
| 128 base::Bind(&BluetoothTaskManagerWin::StartPolling, |
| 129 this)); |
| 130 } |
| 131 |
| 132 void BluetoothTaskManagerWin::StartPolling() { |
| 133 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 134 |
| 135 // TODO(youngki): Handle this check where BluetoothAdapter is initialized. |
| 136 if (HasBluetoothStack()) { |
| 137 PollAdapter(); |
| 138 } |
| 139 } |
| 140 |
| 141 void BluetoothTaskManagerWin::Shutdown() { |
| 142 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 143 worker_pool_->Shutdown(); |
| 144 } |
| 145 |
| 146 void BluetoothTaskManagerWin::OnAdapterStateChanged(const AdapterState* state) { |
| 147 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 148 FOR_EACH_OBSERVER(BluetoothTaskManagerWin::Observer, observers_, |
| 149 AdapterStateChanged(*state)); |
| 150 } |
| 151 |
| 152 void BluetoothTaskManagerWin::PollAdapter() { |
| 153 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 154 |
| 155 base::win::ScopedHandle adapter_handle; |
| 156 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = |
| 157 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
| 158 HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio( |
| 159 &adapter_param, adapter_handle.Receive()); |
| 160 |
| 161 if (handle) |
| 162 BluetoothFindRadioClose(handle); |
| 163 |
| 164 AdapterState* state = new AdapterState(); |
| 165 GetAdapterState(adapter_handle, state); |
| 166 |
| 167 // Notify the UI thread of the Bluetooth Adapter state. |
| 168 ui_task_runner_->PostTask( |
| 169 FROM_HERE, |
| 170 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, |
| 171 this, |
| 172 base::Owned(state))); |
| 173 |
| 174 // Re-poll. |
| 175 bluetooth_task_runner_->PostDelayedTask( |
| 176 FROM_HERE, |
| 177 base::Bind(&BluetoothTaskManagerWin::PollAdapter, |
| 178 this), |
| 179 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
| 180 } |
| 181 |
| 182 } // namespace device |
OLD | NEW |