Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Unified Diff: device/bluetooth/bluetooth_polling_thread_win.cc

Issue 11411130: Implemented BluetoothTaskManagerWin class. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Turned thread_checker_ to value in polling thread. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..e0ca499e180ea63224ee0f4452106d63f8a46da8
--- /dev/null
+++ b/device/bluetooth/bluetooth_polling_thread_win.cc
@@ -0,0 +1,124 @@
+// 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/sys_string_conversions.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),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
+ thread_checker_.DetachFromThread();
+}
+
+BluetoothPollingThreadWin::~BluetoothPollingThreadWin() {
+ cancellation_flag_.Set();
bryeung 2012/11/28 16:26:08 Are you sure this is the right pattern? To me, th
youngki 2012/11/28 19:19:13 Thread::Stop() on the next line waits until all th
+ Stop();
+}
+
+void BluetoothPollingThreadWin::AddObserver(Observer* observer) {
+ DCHECK(observer);
bryeung 2012/11/28 16:26:08 CHECK is probably better here (and below)
youngki 2012/11/28 19:19:13 Done.
+ 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) {
bryeung 2012/11/28 16:26:08 why is this taking a weak_ptr? When do you intend
youngki 2012/11/28 19:19:13 All the instances that we intend to post tasks to
+ if (cancellation_flag_.IsSet())
+ return;
+
+ 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) {
bryeung 2012/11/28 16:26:08 CHECK the thread here
youngki 2012/11/28 19:19:13 Done. I also changed all DCHECK in this file to CH
+ FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_,
+ AdapterStateChanged(*state));
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698