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 } // namespace | |
23 | |
24 namespace device { | |
25 | |
26 // static | |
27 const int BluetoothPollingThreadWin::kPollIntervalMs = 500; | |
28 | |
29 BluetoothPollingThreadWin::BluetoothPollingThreadWin() | |
30 : Thread(kBluetoothManagerThreadName), | |
31 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
32 thread_checker_.DetachFromThread(); | |
33 } | |
34 | |
35 BluetoothPollingThreadWin::~BluetoothPollingThreadWin() { | |
36 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
| |
37 Stop(); | |
38 } | |
39 | |
40 void BluetoothPollingThreadWin::AddObserver(Observer* observer) { | |
41 DCHECK(observer); | |
bryeung
2012/11/28 16:26:08
CHECK is probably better here (and below)
youngki
2012/11/28 19:19:13
Done.
| |
42 observers_.AddObserver(observer); | |
43 } | |
44 | |
45 void BluetoothPollingThreadWin::RemoveObserver(Observer* observer) { | |
46 DCHECK(observer); | |
47 observers_.RemoveObserver(observer); | |
48 } | |
49 | |
50 void BluetoothPollingThreadWin::Init() { | |
51 if (!message_loop()) { | |
52 Start(); | |
53 message_loop()->PostTask( | |
54 FROM_HERE, | |
55 base::Bind(&BluetoothPollingThreadWin::PollAdapter, | |
56 base::Unretained(this), | |
57 weak_ptr_factory_.GetWeakPtr())); | |
58 } | |
59 } | |
60 | |
61 // static | |
62 void BluetoothPollingThreadWin::GetAdapterState( | |
63 const HANDLE adapter_handle, AdapterState* state) { | |
64 std::string name; | |
65 std::string address; | |
66 bool powered = false; | |
67 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | |
68 if (adapter_handle && | |
69 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, | |
70 &adapter_info)) { | |
71 name = base::SysWideToUTF8(adapter_info.szName); | |
72 address = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | |
73 adapter_info.address.rgBytes[5], | |
74 adapter_info.address.rgBytes[4], | |
75 adapter_info.address.rgBytes[3], | |
76 adapter_info.address.rgBytes[2], | |
77 adapter_info.address.rgBytes[1], | |
78 adapter_info.address.rgBytes[0]); | |
79 powered = !!BluetoothIsConnectable(adapter_handle); | |
80 } | |
81 state->name = name; | |
82 state->address = address; | |
83 state->powered = powered; | |
84 } | |
85 | |
86 void BluetoothPollingThreadWin::PollAdapter( | |
87 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
| |
88 if (cancellation_flag_.IsSet()) | |
89 return; | |
90 | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 | |
93 HBLUETOOTH_RADIO_FIND adapter_handle = NULL; | |
94 HBLUETOOTH_RADIO_FIND handle = BluetoothFindFirstRadio(&adapter_param, | |
95 &adapter_handle); | |
96 if (handle) | |
97 BluetoothFindRadioClose(handle); | |
98 | |
99 AdapterState* state = new AdapterState(); | |
100 GetAdapterState(adapter_handle, state); | |
101 | |
102 content::BrowserThread::PostTask( | |
103 content::BrowserThread::UI, | |
104 FROM_HERE, | |
105 base::Bind(&BluetoothPollingThreadWin::OnAdapterStateChanged, | |
106 weak_ptr, | |
107 base::Owned(state))); | |
108 | |
109 // Re-poll. | |
110 MessageLoop::current()->PostDelayedTask( | |
111 FROM_HERE, | |
112 base::Bind(&BluetoothPollingThreadWin::PollAdapter, | |
113 base::Unretained(this), | |
114 weak_ptr), | |
115 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | |
116 } | |
117 | |
118 void BluetoothPollingThreadWin::OnAdapterStateChanged( | |
119 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
| |
120 FOR_EACH_OBSERVER(BluetoothPollingThreadWin::Observer, observers_, | |
121 AdapterStateChanged(*state)); | |
122 } | |
123 | |
124 } // namespace device | |
OLD | NEW |