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 "base/message_loop.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/sys_string_conversions.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "device/bluetooth/bluetooth_adapter_win.h" | |
13 | |
14 # pragma comment(lib, "Bthprops.lib") | |
15 | |
16 namespace { | |
17 | |
18 const char* kBluetoothManagerThreadName = "BluetoothPollingThreadWin"; | |
19 const BLUETOOTH_FIND_RADIO_PARAMS adapter_param = | |
20 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | |
21 | |
22 // Populates bluetooth adapter state using adapter_handle. | |
23 void GetAdapterState(const HANDLE adapter_handle, | |
24 device::BluetoothPollingThreadWin::AdapterState* state) { | |
25 std::string name; | |
26 std::string address; | |
27 bool powered = false; | |
28 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | |
29 if (adapter_handle && | |
30 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, | |
31 &adapter_info)) { | |
32 name = base::SysWideToUTF8(adapter_info.szName); | |
33 address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | |
34 adapter_info.address.rgBytes[5], | |
35 adapter_info.address.rgBytes[4], | |
36 adapter_info.address.rgBytes[3], | |
37 adapter_info.address.rgBytes[2], | |
38 adapter_info.address.rgBytes[1], | |
39 adapter_info.address.rgBytes[0]); | |
40 powered = !!BluetoothIsConnectable(adapter_handle); | |
41 } | |
42 state->name = name; | |
43 state->address = address; | |
44 state->powered = powered; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 namespace device { | |
50 | |
51 // static | |
52 const int BluetoothPollingThreadWin::kPollIntervalMs = 500; | |
53 | |
54 BluetoothPollingThreadWin::BluetoothPollingThreadWin() | |
55 : Thread(kBluetoothManagerThreadName), | |
56 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
57 thread_checker_.DetachFromThread(); | |
58 } | |
59 | |
60 // Stop() waits until all the pending tasks in BluetoothPollingThread finish. | |
61 // Also this class will be destroyed after Stop() returns, having all the | |
62 // pending tasks completed. Setting |cancellation_flag_| will effectively | |
63 // cancel all the pending tasks. | |
64 BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { | |
65 cancellation_flag_.Set(); | |
66 Stop(); | |
67 } | |
68 | |
69 void BluetoothPollingThreadWin::AddObserver(Observer* observer) { | |
70 CHECK(observer); | |
71 observers_.AddObserver(observer); | |
72 } | |
73 | |
74 void BluetoothPollingThreadWin::RemoveObserver(Observer* observer) { | |
75 CHECK(observer); | |
76 observers_.RemoveObserver(observer); | |
77 } | |
78 | |
79 void BluetoothPollingThreadWin::Init() { | |
bryeung
2012/12/10 19:15:35
CHECK that you're on the UI thread here
| |
80 if (!message_loop()) { | |
bryeung
2012/12/10 19:15:35
// WeakPtrs are bound to the thread that creates t
| |
81 Start(); | |
82 message_loop()->PostTask( | |
83 FROM_HERE, | |
84 base::Bind(&BluetoothPollingThreadWin::PollAdapter, | |
85 base::Unretained(this), | |
86 weak_ptr_factory_.GetWeakPtr())); | |
87 } | |
88 } | |
89 | |
90 void BluetoothPollingThreadWin::PollAdapter( | |
91 base::WeakPtr<BluetoothPollingThreadWin> weak_ptr) { | |
bryeung
2012/12/10 19:15:35
rename this to weak_ptr_for_ui_thread
| |
92 if (cancellation_flag_.IsSet()) | |
93 return; | |
94 | |
95 CHECK(thread_checker_.CalledOnValidThread()); | |
96 | |
97 HBLUETOOTH_RADIO_FIND adapter_handle = NULL; | |
98 HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, | |
99 &adapter_handle); | |
100 if (handle) | |
101 BluetoothFindRadioClose(handle); | |
102 | |
103 AdapterState* state = new AdapterState(); | |
104 GetAdapterState(adapter_handle, state); | |
105 | |
106 content::BrowserThread::PostTask( | |
107 content::BrowserThread::UI, | |
108 FROM_HERE, | |
109 base::Bind(&BluetoothPollingThreadWin::OnAdapterStateChanged, | |
110 weak_ptr, | |
111 base::Owned(state))); | |
112 | |
113 // Re-poll. | |
114 MessageLoop::current()->PostDelayedTask( | |
115 FROM_HERE, | |
116 base::Bind(&BluetoothPollingThreadWin::PollAdapter, | |
117 base::Unretained(this), | |
118 weak_ptr), | |
119 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | |
120 } | |
121 | |
122 void BluetoothPollingThreadWin::OnAdapterStateChanged( | |
123 const AdapterState* state) { | |
124 CHECK(thread_checker_.CalledOnValidThread()); | |
bryeung
2012/12/10 19:15:35
use a separate, ui_thread_checker_ here
| |
125 FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_, | |
126 AdapterStateChanged(*state)); | |
127 } | |
128 | |
129 } // namespace device | |
OLD | NEW |