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

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: Implemented Unittests Created 8 years 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..e2ef32f7cffcf91fb4239eb6cdb804d9a773c451
--- /dev/null
+++ b/device/bluetooth/bluetooth_polling_thread_win.cc
@@ -0,0 +1,157 @@
+// 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>
+#if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700
+// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h.
+#undef FACILITY_VISUALCPP
+#endif
+#include <delayimp.h>
+#include <string>
+#include "base/message_loop.h"
+#include "base/stringprintf.h"
+#include "base/sys_string_conversions.h"
+#include "base/threading/thread_restrictions.h"
+#include "content/public/browser/browser_thread.h"
+#include "device/bluetooth/bluetooth_task_manager_win.h"
+
+#pragma comment(lib, "Bthprops.lib")
+#pragma comment(lib, "delayimp.lib")
+
+namespace {
+
+const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin";
+const BLUETOOTH_FIND_RADIO_PARAMS adapter_param =
+ { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
+
+// A frame-based exception handler filter function for a handler for exceptions
+// generated by the Visual C++ delay loader helper function.
+int FilterVisualCPPExceptions(DWORD exception_code) {
+ return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ?
+ EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
+}
+
+// Returns true if the machine has a bluetooth stack available. The first call
+// to this function will involve file IO, so it should be done on an
+// appropriate thread. This function is not threadsafe.
+//
+// TODO(youngki): Call this function when BluetoothAdapter is initialized. We
+// should disable Bluetooth support entirely if this function returns false.
+bool HasBluetoothStack() {
brettw 2013/01/04 22:05:34 It looks like this is never called?
youngki 2013/01/07 23:56:34 Added the function call. I also added TODO that th
+ static enum {
+ HBS_UNKNOWN,
+ HBS_YES,
+ HBS_NO,
+ } has_bluetooth_stack = HBS_UNKNOWN;
+
+ if (has_bluetooth_stack == HBS_UNKNOWN) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ HRESULT hr = E_FAIL;
+ __try {
+ hr = __HrLoadAllImportsForDll("bthprops.cpl");
+ } __except(FilterVisualCPPExceptions(::GetExceptionCode())) {
+ hr = E_FAIL;
+ }
+ has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO;
+ }
+
+ return has_bluetooth_stack == HBS_YES;
+}
+
+// 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) {
+}
+
+BluetoothPollingThreadWin::~BluetoothPollingThreadWin() {
+ DCHECK(task_manager_->ui_task_runner()->RunsTasksOnCurrentThread());
+ Cancel();
+}
+
+void BluetoothPollingThreadWin::StartPolling() {
+ if (!message_loop()) {
+ DCHECK(task_manager_->ui_task_runner()->RunsTasksOnCurrentThread());
+ Start();
+ message_loop()->PostTask(
+ FROM_HERE,
+ base::Bind(&BluetoothPollingThreadWin::PollAdapter,
+ base::Unretained(this)));
+ }
+}
+
+// Cancels the pending tasks and waits until all the pending tasks in
+// BluetoothPollingThread finish.
+void BluetoothPollingThreadWin::Cancel() {
+ DCHECK(task_manager_->ui_task_runner()->RunsTasksOnCurrentThread());
+ cancellation_flag_.Set();
+ Stop();
+}
+
+void BluetoothPollingThreadWin::PollAdapter() {
+ DCHECK_EQ(message_loop(), MessageLoop::current());
+
+ if (cancellation_flag_.IsSet())
+ return;
+
+ 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_->ui_task_runner()->PostTask(
+ 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
« no previous file with comments | « device/bluetooth/bluetooth_polling_thread_win.h ('k') | device/bluetooth/bluetooth_polling_thread_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698