OLD | NEW |
---|---|
(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 <string> | |
9 #include "base/message_loop.h" | |
10 #include "base/stringprintf.h" | |
11 #include "base/sys_string_conversions.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "device/bluetooth/bluetooth_task_manager_win.h" | |
14 | |
15 # pragma comment(lib, "Bthprops.lib") | |
16 | |
17 namespace { | |
18 | |
19 const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin"; | |
20 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = | |
21 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | |
22 | |
23 // Populates bluetooth adapter state using adapter_handle. | |
24 void GetAdapterState(const HANDLE adapter_handle, | |
25 device::BluetoothTaskManagerWin::AdapterState* state) { | |
26 std::string name; | |
27 std::string address; | |
28 bool powered = false; | |
29 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | |
30 if (adapter_handle && | |
31 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, | |
32 &adapter_info)) { | |
33 name = base::SysWideToUTF8(adapter_info.szName); | |
34 address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | |
35 adapter_info.address.rgBytes[5], | |
36 adapter_info.address.rgBytes[4], | |
37 adapter_info.address.rgBytes[3], | |
38 adapter_info.address.rgBytes[2], | |
39 adapter_info.address.rgBytes[1], | |
40 adapter_info.address.rgBytes[0]); | |
41 powered = !!BluetoothIsConnectable(adapter_handle); | |
42 } | |
43 state->name = name; | |
44 state->address = address; | |
45 state->powered = powered; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 namespace device { | |
51 | |
52 // static | |
53 const int BluetoothPollingThreadWin::kPollIntervalMs = 500; | |
54 | |
55 BluetoothPollingThreadWin::BluetoothPollingThreadWin( | |
56 BluetoothTaskManagerWin* task_manager) | |
57 : Thread(kBluetoothManagerThreadName), | |
58 task_manager_(task_manager) { | |
59 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.
| |
60 } | |
61 | |
62 // 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.
| |
63 // Also this class will be destroyed after Stop() returns, having all the | |
64 // pending tasks completed. Setting |cancellation_flag_| will effectively | |
65 // cancel all the pending tasks. | |
66 BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { | |
67 DCHECK_EQ(task_manager_->message_loop(), MessageLoop::current()); | |
68 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
| |
69 } | |
70 | |
71 void BluetoothPollingThreadWin::Cancel() { | |
72 DCHECK_EQ(task_manager_->message_loop(), MessageLoop::current()); | |
73 cancellation_flag_.Set(); | |
74 Stop(); | |
75 } | |
76 | |
77 void BluetoothPollingThreadWin::PollAdapter() { | |
78 if (cancellation_flag_.IsSet()) | |
79 return; | |
80 | |
81 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.
| |
82 | |
83 HBLUETOOTH_RADIO_FIND adapter_handle = NULL; | |
84 HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, | |
85 &adapter_handle); | |
86 if (handle) | |
87 BluetoothFindRadioClose(handle); | |
88 | |
89 BluetoothTaskManagerWin::AdapterState* state = | |
90 new BluetoothTaskManagerWin::AdapterState(); | |
91 GetAdapterState(adapter_handle, state); | |
92 | |
93 // Notify the UI thread of the Bluetooth Adapter state. | |
94 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
| |
95 FROM_HERE, | |
96 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, | |
97 make_scoped_refptr(task_manager_), | |
98 base::Owned(state))); | |
99 | |
100 // Re-poll. | |
101 message_loop()->PostDelayedTask( | |
102 FROM_HERE, | |
103 base::Bind(&BluetoothPollingThreadWin::PollAdapter, | |
104 base::Unretained(this)), | |
105 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | |
106 } | |
107 | |
108 } // namespace device | |
OLD | NEW |