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_ADAPTER_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "device/bluetooth/bluetooth_adapter.h"
20 #include "device/bluetooth/bluetooth_audio_sink.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_export.h"
23 #include "device/bluetooth/bluetooth_task_manager_win.h"
26 class SequencedTaskRunner
;
32 class BluetoothAdapterWinTest
;
33 class BluetoothDevice
;
34 class BluetoothSocketThread
;
36 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
37 : public BluetoothAdapter
,
38 public BluetoothTaskManagerWin::Observer
{
40 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter(
41 const InitCallback
& init_callback
);
44 std::string
GetAddress() const override
;
45 std::string
GetName() const override
;
46 void SetName(const std::string
& name
,
47 const base::Closure
& callback
,
48 const ErrorCallback
& error_callback
) override
;
49 bool IsInitialized() const override
;
50 bool IsPresent() const override
;
51 bool IsPowered() const override
;
52 void SetPowered(bool discoverable
,
53 const base::Closure
& callback
,
54 const ErrorCallback
& error_callback
) override
;
55 bool IsDiscoverable() const override
;
56 void SetDiscoverable(bool discoverable
,
57 const base::Closure
& callback
,
58 const ErrorCallback
& error_callback
) override
;
59 bool IsDiscovering() const override
;
60 void CreateRfcommService(
61 const BluetoothUUID
& uuid
,
62 const ServiceOptions
& options
,
63 const CreateServiceCallback
& callback
,
64 const CreateServiceErrorCallback
& error_callback
) override
;
65 void CreateL2capService(
66 const BluetoothUUID
& uuid
,
67 const ServiceOptions
& options
,
68 const CreateServiceCallback
& callback
,
69 const CreateServiceErrorCallback
& error_callback
) override
;
70 void RegisterAudioSink(
71 const BluetoothAudioSink::Options
& options
,
72 const AcquiredCallback
& callback
,
73 const BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
75 // BluetoothTaskManagerWin::Observer override
76 void AdapterStateChanged(
77 const BluetoothTaskManagerWin::AdapterState
& state
) override
;
78 void DiscoveryStarted(bool success
) override
;
79 void DiscoveryStopped() override
;
80 void DevicesPolled(const ScopedVector
<BluetoothTaskManagerWin::DeviceState
>&
83 const scoped_refptr
<base::SequencedTaskRunner
>& ui_task_runner() const {
84 return ui_task_runner_
;
86 const scoped_refptr
<BluetoothSocketThread
>& socket_thread() const {
87 return socket_thread_
;
92 void RemovePairingDelegateInternal(
93 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
96 friend class BluetoothAdapterWinTest
;
98 enum DiscoveryStatus
{
105 explicit BluetoothAdapterWin(const InitCallback
& init_callback
);
106 ~BluetoothAdapterWin() override
;
109 void AddDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
110 const base::Closure
& callback
,
111 const ErrorCallback
& error_callback
) override
;
112 void RemoveDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
113 const base::Closure
& callback
,
114 const ErrorCallback
& error_callback
) override
;
115 void SetDiscoveryFilter(scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
116 const base::Closure
& callback
,
117 const ErrorCallback
& error_callback
) override
;
121 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
122 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
124 void MaybePostStartDiscoveryTask();
125 void MaybePostStopDiscoveryTask();
127 InitCallback init_callback_
;
128 std::string address_
;
132 DiscoveryStatus discovery_status_
;
133 base::hash_set
<std::string
> discovered_devices_
;
135 std::vector
<std::pair
<base::Closure
, ErrorCallback
> >
136 on_start_discovery_callbacks_
;
137 std::vector
<base::Closure
> on_stop_discovery_callbacks_
;
138 size_t num_discovery_listeners_
;
140 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
141 scoped_refptr
<BluetoothSocketThread
> socket_thread_
;
142 scoped_refptr
<BluetoothTaskManagerWin
> task_manager_
;
144 base::ThreadChecker thread_checker_
;
146 // NOTE: This should remain the last member so it'll be destroyed and
147 // invalidate its weak pointers before any other members are destroyed.
148 base::WeakPtrFactory
<BluetoothAdapterWin
> weak_ptr_factory_
;
150 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin
);
153 } // namespace device
155 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_