Index: device/bluetooth/bluetooth_polling_thread_win.cc |
diff --git a/device/bluetooth/bluetooth_polling_thread_win.cc b/device/bluetooth/bluetooth_polling_thread_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0e2f486c7dbdfd0ebd6754d465959c04ef3170e5 |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_polling_thread_win.cc |
@@ -0,0 +1,108 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "device/bluetooth/bluetooth_polling_thread_win.h" |
+ |
+#include <BluetoothAPIs.h> |
+#include <string> |
+#include "base/message_loop.h" |
+#include "base/stringprintf.h" |
+#include "base/sys_string_conversions.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "device/bluetooth/bluetooth_task_manager_win.h" |
+ |
+# pragma comment(lib, "Bthprops.lib") |
+ |
+namespace { |
+ |
+const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin"; |
+const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = |
+ { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
+ |
+// Populates bluetooth adapter state using adapter_handle. |
+void GetAdapterState(const HANDLE adapter_handle, |
+ device::BluetoothTaskManagerWin::AdapterState* state) { |
+ std::string name; |
+ std::string address; |
+ bool powered = false; |
+ BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; |
+ if (adapter_handle && |
+ ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, |
+ &adapter_info)) { |
+ name = base::SysWideToUTF8(adapter_info.szName); |
+ address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
+ adapter_info.address.rgBytes[5], |
+ adapter_info.address.rgBytes[4], |
+ adapter_info.address.rgBytes[3], |
+ adapter_info.address.rgBytes[2], |
+ adapter_info.address.rgBytes[1], |
+ adapter_info.address.rgBytes[0]); |
+ powered = !!BluetoothIsConnectable(adapter_handle); |
+ } |
+ state->name = name; |
+ state->address = address; |
+ state->powered = powered; |
+} |
+ |
+} // namespace |
+ |
+namespace device { |
+ |
+// static |
+const int BluetoothPollingThreadWin::kPollIntervalMs = 500; |
+ |
+BluetoothPollingThreadWin::BluetoothPollingThreadWin( |
+ BluetoothTaskManagerWin* task_manager) |
+ : Thread(kBluetoothManagerThreadName), |
+ task_manager_(task_manager) { |
+ DCHECK_EQ(task_manager_->message_loop(), MessageLoop::current()); |
bryeung
2012/12/10 19:51:28
don't use task_manager_ here...you've passed it in
youngki
2012/12/17 17:17:23
Done.
|
+} |
+ |
+// Stop() waits until all the pending tasks in BluetoothPollingThread finish. |
bryeung
2012/12/10 19:51:28
this comment doesn't make sense here: maybe you me
youngki
2012/12/17 17:17:23
I moved the comment to Cancel() and modified it.
|
+// Also this class will be destroyed after Stop() returns, having all the |
+// pending tasks completed. Setting |cancellation_flag_| will effectively |
+// cancel all the pending tasks. |
+BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { |
+ DCHECK_EQ(task_manager_->message_loop(), MessageLoop::current()); |
+ Cancel(); |
bryeung
2012/12/10 19:51:28
is there a way to CHECK that the message loop has
youngki
2012/12/17 17:17:23
I don't think that's necessary.. That must be guar
|
+} |
+ |
+void BluetoothPollingThreadWin::Cancel() { |
+ DCHECK_EQ(task_manager_->message_loop(), MessageLoop::current()); |
+ cancellation_flag_.Set(); |
+ Stop(); |
+} |
+ |
+void BluetoothPollingThreadWin::PollAdapter() { |
+ if (cancellation_flag_.IsSet()) |
+ return; |
+ |
+ DCHECK_EQ(message_loop(), MessageLoop::current()); |
bryeung
2012/12/10 19:51:28
does this have to go below the cancellation_flag_
youngki
2012/12/17 17:17:23
Done.
|
+ |
+ HBLUETOOTH_RADIO_FIND adapter_handle = NULL; |
+ HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, |
+ &adapter_handle); |
+ if (handle) |
+ BluetoothFindRadioClose(handle); |
+ |
+ BluetoothTaskManagerWin::AdapterState* state = |
+ new BluetoothTaskManagerWin::AdapterState(); |
+ GetAdapterState(adapter_handle, state); |
+ |
+ // Notify the UI thread of the Bluetooth Adapter state. |
+ task_manager_->message_loop()->PostTask( |
bryeung
2012/12/10 19:51:28
if adapter_handle was NULL, we probably shouldn't
youngki
2012/12/17 17:17:23
That's not true. If adapter_handle is NULL we stil
|
+ FROM_HERE, |
+ base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, |
+ make_scoped_refptr(task_manager_), |
+ base::Owned(state))); |
+ |
+ // Re-poll. |
+ message_loop()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::PollAdapter, |
+ base::Unretained(this)), |
+ base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
+} |
+ |
+} // namespace device |