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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_POLLING_THREAD_WIN_H_ | |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_POLLING_THREAD_WIN_H_ | |
7 | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/synchronization/cancellation_flag.h" | |
10 #include "base/threading/thread.h" | |
11 #include "base/threading/thread_checker.h" | |
12 | |
13 namespace device { | |
14 | |
15 // A thread dedicated to perform blocking bluetooth APIs on Windows. | |
16 class BluetoothPollingThreadWin : public base::Thread { | |
17 public: | |
18 struct AdapterState { | |
19 std::string name; | |
20 std::string address; | |
21 bool powered; | |
22 }; | |
23 | |
24 class Observer { | |
25 public: | |
26 virtual ~Observer() {} | |
27 | |
28 // This will always be called on the UI thread. | |
29 virtual void AdapterStateChanged(const AdapterState& state) {} | |
30 }; | |
31 | |
32 BluetoothPollingThreadWin(); | |
33 virtual ~BluetoothPollingThreadWin(); | |
34 | |
35 void AddObserver(Observer* observer); | |
36 void RemoveObserver(Observer* observer); | |
37 | |
38 void Init(); | |
39 | |
40 private: | |
41 // Called periodically to get the latest state of the Bluetooth Adapter from | |
42 // the Windows APIs. Posts another call to PollAdapter unless | |
43 // |cancellation_flag_| is set. | |
bryeung
2012/12/10 19:15:35
Include a comment here about why the weak_ptr is n
| |
44 void PollAdapter(base::WeakPtr<BluetoothPollingThreadWin> weak_ptr); | |
45 | |
46 // Notify all Observers of updated AdapterState. Should only be called on the | |
47 // UI thread. | |
48 void OnAdapterStateChanged(const AdapterState* state); | |
49 | |
50 static const int kPollIntervalMs; | |
51 | |
52 // Polling will be canceled when BluetoothPollingThread terminates. | |
53 base::CancellationFlag cancellation_flag_; | |
54 | |
55 // Checks that PollAdapter runs on BluetoothPollingThread. | |
56 base::ThreadChecker thread_checker_; | |
57 | |
58 // List of observers interested in event notifications. | |
59 ObserverList<Observer> observers_; | |
60 | |
61 // NOTE: This should remain the last member so it'll be destroyed and | |
62 // invalidate its weak pointers before any other members are destroyed. | |
63 base::WeakPtrFactory<BluetoothPollingThreadWin> weak_ptr_factory_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(BluetoothPollingThreadWin); | |
66 }; | |
67 | |
68 } // namespace device | |
69 | |
70 #endif // DEVICE_BLUETOOTH_BLUETOOTH_POLLING_THREAD_WIN_H_ | |
OLD | NEW |