| 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 <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> | 7 #include <string> |
| 14 | 8 |
| 15 #include "base/bind.h" | 9 #include "base/bind.h" |
| 16 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 17 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 18 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 19 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 20 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/threading/thread_restrictions.h" | |
| 23 #include "base/win/scoped_handle.h" | 16 #include "base/win/scoped_handle.h" |
| 24 | 17 #include "device/bluetooth/bluetooth_init_win.h" |
| 25 #pragma comment(lib, "Bthprops.lib") | |
| 26 | 18 |
| 27 namespace { | 19 namespace { |
| 28 | 20 |
| 29 const int kNumThreadsInWorkerPool = 3; | 21 const int kNumThreadsInWorkerPool = 3; |
| 30 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; | 22 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; |
| 31 | 23 |
| 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. | 24 // Populates bluetooth adapter state using adapter_handle. |
| 40 void GetAdapterState(HANDLE adapter_handle, | 25 void GetAdapterState(HANDLE adapter_handle, |
| 41 device::BluetoothTaskManagerWin::AdapterState* state) { | 26 device::BluetoothTaskManagerWin::AdapterState* state) { |
| 42 std::string name; | 27 std::string name; |
| 43 std::string address; | 28 std::string address; |
| 44 bool powered = false; | 29 bool powered = false; |
| 45 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | 30 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
| 46 if (adapter_handle && | 31 if (adapter_handle && |
| 47 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, | 32 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, |
| 48 &adapter_info)) { | 33 &adapter_info)) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 61 state->powered = powered; | 46 state->powered = powered; |
| 62 } | 47 } |
| 63 | 48 |
| 64 } | 49 } |
| 65 | 50 |
| 66 namespace device { | 51 namespace device { |
| 67 | 52 |
| 68 // static | 53 // static |
| 69 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; | 54 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; |
| 70 | 55 |
| 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( | 56 BluetoothTaskManagerWin::BluetoothTaskManagerWin( |
| 94 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | 57 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 95 : ui_task_runner_(ui_task_runner), | 58 : ui_task_runner_(ui_task_runner), |
| 96 worker_pool_(new base::SequencedWorkerPool(kNumThreadsInWorkerPool, | 59 worker_pool_(new base::SequencedWorkerPool(kNumThreadsInWorkerPool, |
| 97 kBluetoothThreadName)), | 60 kBluetoothThreadName)), |
| 98 bluetooth_task_runner_( | 61 bluetooth_task_runner_( |
| 99 worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | 62 worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( |
| 100 worker_pool_->GetSequenceToken(), | 63 worker_pool_->GetSequenceToken(), |
| 101 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { | 64 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { |
| 102 } | 65 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 bluetooth_task_runner_->PostTask( | 91 bluetooth_task_runner_->PostTask( |
| 129 FROM_HERE, | 92 FROM_HERE, |
| 130 base::Bind(&BluetoothTaskManagerWin::StartPolling, | 93 base::Bind(&BluetoothTaskManagerWin::StartPolling, |
| 131 this)); | 94 this)); |
| 132 } | 95 } |
| 133 | 96 |
| 134 void BluetoothTaskManagerWin::StartPolling() { | 97 void BluetoothTaskManagerWin::StartPolling() { |
| 135 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | 98 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); |
| 136 | 99 |
| 137 // TODO(youngki): Handle this check where BluetoothAdapter is initialized. | 100 // TODO(youngki): Handle this check where BluetoothAdapter is initialized. |
| 138 if (HasBluetoothStack()) { | 101 if (device::bluetooth_init_win::HasBluetoothStack()) { |
| 139 PollAdapter(); | 102 PollAdapter(); |
| 103 } else { |
| 104 // IF the bluetooth stack is not available, we still send an empty state |
| 105 // to BluetoothAdapter so that it is marked initialized, but the adapter |
| 106 // will not be present. |
| 107 AdapterState* state = new AdapterState(); |
| 108 ui_task_runner_->PostTask( |
| 109 FROM_HERE, |
| 110 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, |
| 111 this, |
| 112 base::Owned(state))); |
| 140 } | 113 } |
| 141 } | 114 } |
| 142 | 115 |
| 143 void BluetoothTaskManagerWin::Shutdown() { | 116 void BluetoothTaskManagerWin::Shutdown() { |
| 144 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 117 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 145 worker_pool_->Shutdown(); | 118 worker_pool_->Shutdown(); |
| 146 } | 119 } |
| 147 | 120 |
| 148 void BluetoothTaskManagerWin::PostSetPoweredBluetoothTask( | 121 void BluetoothTaskManagerWin::PostSetPoweredBluetoothTask( |
| 149 bool powered, | 122 bool powered, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, | 189 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, |
| 217 this, | 190 this, |
| 218 base::Owned(state))); | 191 base::Owned(state))); |
| 219 ui_task_runner_->PostTask(FROM_HERE, callback); | 192 ui_task_runner_->PostTask(FROM_HERE, callback); |
| 220 } else { | 193 } else { |
| 221 ui_task_runner_->PostTask(FROM_HERE, error_callback); | 194 ui_task_runner_->PostTask(FROM_HERE, error_callback); |
| 222 } | 195 } |
| 223 } | 196 } |
| 224 | 197 |
| 225 } // namespace device | 198 } // namespace device |
| OLD | NEW |