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..b87ccb134ab141dfb844b0727136c0cb9bc266d9 |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_polling_thread_win.cc |
@@ -0,0 +1,130 @@ |
+// 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 "base/message_loop.h" |
+#include "base/stringprintf.h" |
+#include "base/synchronization/cancellation_flag.h" |
+#include "base/sys_string_conversions.h" |
+#include "base/threading/thread_checker.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "device/bluetooth/bluetooth_adapter_win.h" |
+ |
+# pragma comment(lib, "Bthprops.lib") |
+ |
+namespace { |
+ |
+const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin"; |
+const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = |
+ { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; |
+ |
+} // namespace |
+ |
+namespace device { |
+ |
+// static |
+const int BluetoothPollingThreadWin::kPollIntervalMs = 500; |
+ |
+BluetoothPollingThreadWin::BluetoothPollingThreadWin() |
+ : Thread(kBluetoothManagerThreadName), |
+ cancellation_flag_(new base::CancellationFlag()), |
+ thread_checker_(NULL), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
+} |
+ |
+BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { |
+ cancellation_flag_->Set(); |
+ Stop(); |
+} |
+ |
+void BluetoothPollingThreadWin::AddObserver(Observer* observer) { |
+ DCHECK(observer); |
+ observers_.AddObserver(observer); |
+} |
+ |
+void BluetoothPollingThreadWin::RemoveObserver(Observer* observer) { |
+ DCHECK(observer); |
+ observers_.RemoveObserver(observer); |
+} |
+ |
+void BluetoothPollingThreadWin::Init() { |
+ if (!message_loop()) { |
+ Start(); |
+ message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::PollAdapter, |
+ base::Unretained(this), |
+ weak_ptr_factory_.GetWeakPtr())); |
+ } |
+} |
+ |
+// static |
+void BluetoothPollingThreadWin::GetAdapterState( |
+ const HANDLE adapter_handle, 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; |
+} |
+ |
+void BluetoothPollingThreadWin::PollAdapter( |
+ base::WeakPtr<BluetoothPollingThreadWin> weak_ptr) { |
+ if (cancellation_flag_->IsSet()) |
+ return; |
+ |
+ if (!thread_checker_) { |
+ thread_checker_.reset(new base::ThreadChecker()); |
bryeung
2012/11/27 20:37:04
I think the right pattern here would be for the cl
youngki
2012/11/27 21:43:34
I made this pointer because I thought forward-decl
youngki
2012/11/28 15:04:39
I think we should use pointer for this because if
|
+ } |
+ DCHECK(thread_checker_->CalledOnValidThread()); |
+ |
+ HBLUETOOTH_RADIO_FIND adapter_handle = NULL; |
+ HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, |
+ &adapter_handle); |
+ if (handle) |
+ BluetoothFindRadioClose(handle); |
+ |
+ AdapterState* state = new AdapterState(); |
+ GetAdapterState(adapter_handle, state); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::OnAdapterStateChanged, |
+ weak_ptr, |
+ base::Owned(state))); |
+ |
+ // Re-poll. |
+ MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothPollingThreadWin::PollAdapter, |
+ base::Unretained(this), |
+ weak_ptr), |
+ base::TimeDelta::FromMilliseconds(kPollIntervalMs)); |
+} |
+ |
+void BluetoothPollingThreadWin::OnAdapterStateChanged( |
+ const AdapterState* state) { |
+ FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_, |
+ AdapterStateChanged(*state)); |
+} |
+ |
+} // namespace device |