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_
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"
19 class SequencedTaskRunner
;
20 class SequencedWorkerPool
;
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
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
> {
43 struct ServiceRecordState
{
46 std::vector
<uint8
> sdp_bytes
;
52 uint32 bluetooth_class
;
56 ScopedVector
<ServiceRecordState
> service_record_states
;
61 virtual ~Observer() {}
63 virtual void AdapterStateChanged(const AdapterState
& state
) {}
64 virtual void DiscoveryStarted(bool success
) {}
65 virtual void DiscoveryStopped() {}
66 virtual void DevicesUpdated(const ScopedVector
<DeviceState
>& devices
) {}
67 virtual void DevicesDiscovered(const ScopedVector
<DeviceState
>& devices
) {}
70 explicit BluetoothTaskManagerWin(
71 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
73 void AddObserver(Observer
* observer
);
74 void RemoveObserver(Observer
* observer
);
77 void InitializeWithBluetoothTaskRunner(
78 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
81 void PostSetPoweredBluetoothTask(
83 const base::Closure
& callback
,
84 const BluetoothAdapter::ErrorCallback
& error_callback
);
85 void PostStartDiscoveryTask();
86 void PostStopDiscoveryTask();
89 friend class base::RefCountedThreadSafe
<BluetoothTaskManagerWin
>;
90 friend class BluetoothTaskManagerWinTest
;
92 static const int kPollIntervalMs
;
94 virtual ~BluetoothTaskManagerWin();
96 // Notify all Observers of updated AdapterState. Should only be called on the
98 void OnAdapterStateChanged(const AdapterState
* state
);
99 void OnDiscoveryStarted(bool success
);
100 void OnDiscoveryStopped();
101 void OnDevicesUpdated(const ScopedVector
<DeviceState
>* devices
);
102 void OnDevicesDiscovered(const ScopedVector
<DeviceState
>* devices
);
104 // Called on BluetoothTaskRunner.
107 void PostAdapterStateToUi();
108 void SetPowered(bool powered
,
109 const base::Closure
& callback
,
110 const BluetoothAdapter::ErrorCallback
& error_callback
);
112 // Starts discovery. Once the discovery starts, it issues a discovery inquiry
113 // with a short timeout, then issues more inquiries with greater timeout
114 // values. The discovery finishes when StopDiscovery() is called or timeout
115 // has reached its maximum value.
116 void StartDiscovery();
117 void StopDiscovery();
119 // Issues a device inquiry that runs for |timeout| * 1.28 seconds.
120 // This posts itself again with |timeout| + 1 until |timeout| reaches the
121 // maximum value or stop discovery call is received.
122 void DiscoverDevices(int timeout
);
124 // Fetch already known device information. Similar to |StartDiscovery|, except
125 // this function does not issue a discovery inquiry. Instead it gets the
126 // device info cached in the adapter.
127 void GetKnownDevices();
129 // Sends a device search API call to the adapter.
130 void SearchDevices(int timeout
,
131 bool search_cached_devices_only
,
132 ScopedVector
<DeviceState
>* device_list
);
134 // Discover services for the devices in |device_list|.
135 void DiscoverServices(ScopedVector
<DeviceState
>* device_list
);
137 // UI task runner reference.
138 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
140 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
141 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner_
;
143 // List of observers interested in event notifications.
144 ObserverList
<Observer
> observers_
;
146 // Adapter handle owned by bluetooth task runner.
147 base::win::ScopedHandle adapter_handle_
;
149 // indicates whether the adapter is in discovery mode or not.
152 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin
);
155 } // namespace device
157 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_