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

Side by Side 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: Use Observer pattern 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "device/bluetooth/bluetooth_polling_thread_win.h"
6
7 #include <BluetoothAPIs.h>
8 #include "base/stringprintf.h"
9 #include "base/sys_string_conversions.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "device/bluetooth/bluetooth_adapter_win.h"
12
13 # pragma comment(lib, "Bthprops.lib")
14
15 namespace {
16
17 const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin";
18 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param =
19 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
20
21 } // namespace
22
23 namespace device {
24
25 const int BluetoothPollingThreadWin::kPollIntervalMs = 500;
26
27 BluetoothPollingThreadWin::BluetoothPollingThreadWin()
28 : Thread(kBluetoothManagerThreadName),
29 adapter_handle_(NULL),
30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
31 }
32
33 BluetoothPollingThreadWin::~BluetoothPollingThreadWin() {
34 Stop();
35 }
36
37 void BluetoothPollingThreadWin::AddObserver(Observer* observer) {
38 DCHECK(observer);
39 observers_.AddObserver(observer);
40 }
41
42 void BluetoothPollingThreadWin::RemoveObserver(Observer* observer) {
43 DCHECK(observer);
44 observers_.RemoveObserver(observer);
45 }
46
47 void BluetoothPollingThreadWin::Init() {
48 if (!message_loop()) {
49 Start();
50 message_loop()->PostTask(
51 FROM_HERE,
52 base::Bind(&BluetoothPollingThreadWin::FindAdapterHandleAndReply,
53 base::Unretained(this),
54 weak_ptr_factory_.GetWeakPtr()));
55 }
56 }
57
58 void BluetoothPollingThreadWin::FindAdapterHandleAndReply(
bryeung 2012/11/23 13:32:22 This method name no longer makes sense. PollAdapa
youngki 2012/11/23 18:26:38 Done.
59 base::WeakPtr<BluetoothPollingThreadWin> weak_ptr) const {
bryeung 2012/11/23 13:32:22 CHECK for thread would be nice here
youngki 2012/11/23 18:26:38 Done.
60 HBLUETOOTH_RADIO_FIND adapter_handle = NULL;
61 HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param,
62 &adapter_handle);
63 if (handle)
64 BluetoothFindRadioClose(handle);
65
66 content::BrowserThread::PostTask(
67 content::BrowserThread::UI,
68 FROM_HERE,
69 base::Bind(&BluetoothPollingThreadWin::OnAdapterHandleAvailable,
70 weak_ptr,
71 adapter_handle));
72 }
73
74 void BluetoothPollingThreadWin::OnAdapterHandleAvailable(
75 HANDLE adapter_handle) {
76 adapter_handle_ = adapter_handle;
bryeung 2012/11/23 13:32:22 I'd prefer if the only thing that happened on the
youngki 2012/11/23 18:26:38 Done.
77 Properties properties;
78 GetAdapterProperties(&properties);
79 FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_,
80 AdapterPropertyChanged(properties));
81
82 message_loop()->PostDelayedTask(
bryeung 2012/11/23 13:32:22 This would probably be better up in FindAdapterHan
youngki 2012/11/23 18:26:38 Done.
83 FROM_HERE,
84 base::Bind(&BluetoothPollingThreadWin::FindAdapterHandleAndReply,
85 base::Unretained(this),
86 weak_ptr_factory_.GetWeakPtr()),
87 base::TimeDelta::FromMilliseconds(kPollIntervalMs));
88 }
89
90 void BluetoothPollingThreadWin::GetAdapterProperties(
91 Properties* properties) const {
92 std::string name;
93 std::string address;
94 bool powered = false;
95 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 };
96 if (adapter_handle_ &&
97 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle_,
98 &adapter_info)) {
99 name = base::SysWideToUTF8(adapter_info.szName);
100 address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
101 adapter_info.address.rgBytes[5],
102 adapter_info.address.rgBytes[4],
103 adapter_info.address.rgBytes[3],
104 adapter_info.address.rgBytes[2],
105 adapter_info.address.rgBytes[1],
106 adapter_info.address.rgBytes[0]);
107 powered = !!BluetoothIsConnectable(adapter_handle_);
108 }
109 properties->name = name;
110 properties->address = address;
111 properties->powered = powered;
112 }
113
114 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698