Files.app: Provide detailed change information on onDirectoryChanged event
[chromium-blink-merge.git] / device / bluetooth / bluetooth_task_manager_win.h
blob991e95c34a94b9ec7bc7c884881ab2a3a819416b
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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/observer_list.h"
14 #include "base/win/scoped_handle.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
17 namespace base {
19 class SequencedTaskRunner;
20 class SequencedWorkerPool;
22 } // namespace base
24 namespace device {
26 // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs
27 // bluetooth tasks using |SequencedWorkerPool| and informs its observers of
28 // bluetooth adapter state changes and any other bluetooth device inquiry
29 // result.
31 // It delegates the blocking Windows API calls to |bluetooth_task_runner_|'s
32 // message loop, and receives responses via methods like OnAdapterStateChanged
33 // posted to UI thread.
34 class BluetoothTaskManagerWin
35 : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> {
36 public:
37 struct AdapterState {
38 AdapterState();
39 ~AdapterState();
40 std::string name;
41 std::string address;
42 bool powered;
45 struct ServiceRecordState {
46 ServiceRecordState();
47 ~ServiceRecordState();
48 std::string name;
49 std::string address;
50 std::vector<uint8> sdp_bytes;
53 struct DeviceState {
54 DeviceState();
55 ~DeviceState();
56 std::string name;
57 std::string address;
58 uint32 bluetooth_class;
59 bool visible;
60 bool connected;
61 bool authenticated;
62 ScopedVector<ServiceRecordState> service_record_states;
65 class Observer {
66 public:
67 virtual ~Observer() {}
69 virtual void AdapterStateChanged(const AdapterState& state) {}
70 virtual void DiscoveryStarted(bool success) {}
71 virtual void DiscoveryStopped() {}
72 virtual void DevicesUpdated(const ScopedVector<DeviceState>& devices) {}
73 virtual void DevicesDiscovered(const ScopedVector<DeviceState>& devices) {}
76 explicit BluetoothTaskManagerWin(
77 scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
79 void AddObserver(Observer* observer);
80 void RemoveObserver(Observer* observer);
82 void Initialize();
83 void InitializeWithBluetoothTaskRunner(
84 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
85 void Shutdown();
87 void PostSetPoweredBluetoothTask(
88 bool powered,
89 const base::Closure& callback,
90 const BluetoothAdapter::ErrorCallback& error_callback);
91 void PostStartDiscoveryTask();
92 void PostStopDiscoveryTask();
94 private:
95 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>;
96 friend class BluetoothTaskManagerWinTest;
98 static const int kPollIntervalMs;
100 virtual ~BluetoothTaskManagerWin();
102 // Notify all Observers of updated AdapterState. Should only be called on the
103 // UI thread.
104 void OnAdapterStateChanged(const AdapterState* state);
105 void OnDiscoveryStarted(bool success);
106 void OnDiscoveryStopped();
107 void OnDevicesUpdated(const ScopedVector<DeviceState>* devices);
108 void OnDevicesDiscovered(const ScopedVector<DeviceState>* devices);
110 // Called on BluetoothTaskRunner.
111 void StartPolling();
112 void PollAdapter();
113 void PostAdapterStateToUi();
114 void SetPowered(bool powered,
115 const base::Closure& callback,
116 const BluetoothAdapter::ErrorCallback& error_callback);
118 // Starts discovery. Once the discovery starts, it issues a discovery inquiry
119 // with a short timeout, then issues more inquiries with greater timeout
120 // values. The discovery finishes when StopDiscovery() is called or timeout
121 // has reached its maximum value.
122 void StartDiscovery();
123 void StopDiscovery();
125 // Issues a device inquiry that runs for |timeout| * 1.28 seconds.
126 // This posts itself again with |timeout| + 1 until |timeout| reaches the
127 // maximum value or stop discovery call is received.
128 void DiscoverDevices(int timeout);
130 // Fetch already known device information. Similar to |StartDiscovery|, except
131 // this function does not issue a discovery inquiry. Instead it gets the
132 // device info cached in the adapter.
133 void GetKnownDevices();
135 // Sends a device search API call to the adapter.
136 void SearchDevices(int timeout,
137 bool search_cached_devices_only,
138 ScopedVector<DeviceState>* device_list);
140 // Discover services for the devices in |device_list|.
141 void DiscoverServices(ScopedVector<DeviceState>* device_list);
143 // UI task runner reference.
144 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
146 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
147 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_;
149 // List of observers interested in event notifications.
150 ObserverList<Observer> observers_;
152 // Adapter handle owned by bluetooth task runner.
153 base::win::ScopedHandle adapter_handle_;
155 // indicates whether the adapter is in discovery mode or not.
156 bool discovering_;
158 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin);
161 } // namespace device
163 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_