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_TASK_MANAGER_WIN_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/observer_list.h" |
| 12 |
| 13 class MessageLoop; |
| 14 |
| 15 namespace base { |
| 16 |
| 17 class SequencedTaskRunner; |
| 18 class SequencedWorkerPool; |
| 19 |
| 20 } // namespace base |
| 21 |
| 22 namespace device { |
| 23 |
| 24 // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs |
| 25 // bluetooth tasks using |SequencedWorkerPool| and informs its observers of |
| 26 // bluetooth adapter state changes and any other bluetooth device inquiry |
| 27 // result. |
| 28 // |
| 29 // It delegates the blocking Windows API calls to |bluetooth_task_runner_|'s |
| 30 // message loop, and receives responses via methods like OnAdapterStateChanged |
| 31 // posted to UI thread. |
| 32 class BluetoothTaskManagerWin |
| 33 : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> { |
| 34 public: |
| 35 struct AdapterState { |
| 36 std::string name; |
| 37 std::string address; |
| 38 bool powered; |
| 39 }; |
| 40 |
| 41 class Observer { |
| 42 public: |
| 43 virtual ~Observer() {} |
| 44 |
| 45 virtual void AdapterStateChanged(const AdapterState& state) {} |
| 46 }; |
| 47 |
| 48 BluetoothTaskManagerWin( |
| 49 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); |
| 50 |
| 51 void AddObserver(Observer* observer); |
| 52 void RemoveObserver(Observer* observer); |
| 53 |
| 54 void Initialize(); |
| 55 void Shutdown(); |
| 56 |
| 57 private: |
| 58 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; |
| 59 friend class BluetoothTaskManagerWinTest; |
| 60 |
| 61 // Returns true if the machine has a bluetooth stack available. The first call |
| 62 // to this function will involve file IO, so it should be done on an |
| 63 // appropriate thread. This function is not threadsafe. |
| 64 static bool HasBluetoothStack(); |
| 65 |
| 66 static const int kPollIntervalMs; |
| 67 |
| 68 // Constructor to pass |ui_task_runner_| and |bluetooth_task_runner_| for |
| 69 // testing. |
| 70 BluetoothTaskManagerWin( |
| 71 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 72 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); |
| 73 |
| 74 virtual ~BluetoothTaskManagerWin(); |
| 75 |
| 76 // Notify all Observers of updated AdapterState. Should only be called on the |
| 77 // UI thread. |
| 78 void OnAdapterStateChanged(const AdapterState* state); |
| 79 |
| 80 // Called on BluetoothTaskRunner. |
| 81 void StartPolling(); |
| 82 void PollAdapter(); |
| 83 |
| 84 // UI task runner reference. |
| 85 const scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 86 |
| 87 const scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
| 88 const scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_; |
| 89 |
| 90 // List of observers interested in event notifications. |
| 91 ObserverList<Observer> observers_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); |
| 94 }; |
| 95 |
| 96 } // namespace device |
| 97 |
| 98 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
OLD | NEW |