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 DevicesDiscovered(const ScopedVector
<DeviceState
>& devices
) {}
69 explicit BluetoothTaskManagerWin(
70 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
72 void AddObserver(Observer
* observer
);
73 void RemoveObserver(Observer
* observer
);
76 void InitializeWithBluetoothTaskRunner(
77 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
80 void PostSetPoweredBluetoothTask(
82 const base::Closure
& callback
,
83 const BluetoothAdapter::ErrorCallback
& error_callback
);
84 void PostStartDiscoveryTask();
85 void PostStopDiscoveryTask();
88 friend class base::RefCountedThreadSafe
<BluetoothTaskManagerWin
>;
89 friend class BluetoothTaskManagerWinTest
;
91 static const int kPollIntervalMs
;
93 virtual ~BluetoothTaskManagerWin();
95 // Notify all Observers of updated AdapterState. Should only be called on the
97 void OnAdapterStateChanged(const AdapterState
* state
);
98 void OnDiscoveryStarted(bool success
);
99 void OnDiscoveryStopped();
100 void OnDevicesDiscovered(const ScopedVector
<DeviceState
>* devices
);
102 // Called on BluetoothTaskRunner.
105 void PostAdapterStateToUi();
106 void SetPowered(bool powered
,
107 const base::Closure
& callback
,
108 const BluetoothAdapter::ErrorCallback
& error_callback
);
110 // Starts discovery. Once the discovery starts, it issues a discovery inquiry
111 // with a short timeout, then issues more inquiries with greater timeout
112 // values. The discovery finishes when StopDiscovery() is called or timeout
113 // has reached its maximum value.
114 void StartDiscovery();
115 void StopDiscovery();
117 // Issues a device inquiry that runs for |timeout| * 1.28 seconds.
118 // This posts itself again with |timeout| + 1 until |timeout| reaches the
119 // maximum value or stop discovery call is received.
120 void DiscoverDevices(int timeout
);
122 // Fetch already known device information. Similar to |StartDiscovery|, except
123 // this function does not issue a discovery inquiry. Instead it gets the
124 // device info cached in the adapter.
125 void GetKnownDevices();
127 // Sends a device search API call to the adapter.
128 void SearchDevices(int timeout
,
129 bool search_cached_devices_only
,
130 ScopedVector
<DeviceState
>* device_list
);
132 // Discover services for the devices in |device_list|.
133 void DiscoverServices(ScopedVector
<DeviceState
>* device_list
);
135 // UI task runner reference.
136 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
138 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
139 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner_
;
141 // List of observers interested in event notifications.
142 ObserverList
<Observer
> observers_
;
144 // Adapter handle owned by bluetooth task runner.
145 base::win::ScopedHandle adapter_handle_
;
147 // indicates whether the adapter is in discovery mode or not.
150 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin
);
153 } // namespace device
155 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_