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 virtual void AddObserver(BluetoothAdapter::Observer
* observer
) override
;
45 virtual void RemoveObserver(BluetoothAdapter::Observer
* observer
) override
;
46 virtual std::string
GetAddress() const override
;
47 virtual std::string
GetName() const override
;
48 virtual void SetName(const std::string
& name
,
49 const base::Closure
& callback
,
50 const ErrorCallback
& error_callback
) override
;
51 virtual bool IsInitialized() const override
;
52 virtual bool IsPresent() const override
;
53 virtual bool IsPowered() const override
;
54 virtual void SetPowered(
56 const base::Closure
& callback
,
57 const ErrorCallback
& error_callback
) override
;
58 virtual bool IsDiscoverable() const override
;
59 virtual void SetDiscoverable(
61 const base::Closure
& callback
,
62 const ErrorCallback
& error_callback
) override
;
63 virtual bool IsDiscovering() const override
;
64 virtual void CreateRfcommService(
65 const BluetoothUUID
& uuid
,
66 const ServiceOptions
& options
,
67 const CreateServiceCallback
& callback
,
68 const CreateServiceErrorCallback
& error_callback
) override
;
69 virtual void CreateL2capService(
70 const BluetoothUUID
& uuid
,
71 const ServiceOptions
& options
,
72 const CreateServiceCallback
& callback
,
73 const CreateServiceErrorCallback
& error_callback
) override
;
74 void RegisterAudioSink(
75 const BluetoothAudioSink::Options
& options
,
76 const AcquiredCallback
& callback
,
77 const BluetoothAudioSink::ErrorCallback
& error_callback
) override
;
79 // BluetoothTaskManagerWin::Observer override
80 virtual void AdapterStateChanged(
81 const BluetoothTaskManagerWin::AdapterState
& state
) override
;
82 virtual void DiscoveryStarted(bool success
) override
;
83 virtual void DiscoveryStopped() override
;
84 virtual void DevicesPolled(const ScopedVector
<
85 BluetoothTaskManagerWin::DeviceState
>& devices
) override
;
87 const scoped_refptr
<base::SequencedTaskRunner
>& ui_task_runner() const {
88 return ui_task_runner_
;
90 const scoped_refptr
<BluetoothSocketThread
>& socket_thread() const {
91 return socket_thread_
;
96 virtual void RemovePairingDelegateInternal(
97 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
100 friend class base::DeleteHelper
<BluetoothAdapterWin
>;
101 friend class BluetoothAdapterWinTest
;
103 enum DiscoveryStatus
{
110 explicit BluetoothAdapterWin(const InitCallback
& init_callback
);
111 virtual ~BluetoothAdapterWin();
114 void DeleteOnCorrectThread() const override
;
115 virtual void AddDiscoverySession(
116 BluetoothDiscoveryFilter
* discovery_filter
,
117 const base::Closure
& callback
,
118 const ErrorCallback
& error_callback
) override
;
119 virtual void RemoveDiscoverySession(
120 BluetoothDiscoveryFilter
* discovery_filter
,
121 const base::Closure
& callback
,
122 const ErrorCallback
& error_callback
) override
;
123 virtual void SetDiscoveryFilter(
124 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
125 const base::Closure
& callback
,
126 const ErrorCallback
& error_callback
) override
;
130 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
131 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
133 void MaybePostStartDiscoveryTask();
134 void MaybePostStopDiscoveryTask();
136 InitCallback init_callback_
;
137 std::string address_
;
141 DiscoveryStatus discovery_status_
;
142 base::hash_set
<std::string
> discovered_devices_
;
144 std::vector
<std::pair
<base::Closure
, ErrorCallback
> >
145 on_start_discovery_callbacks_
;
146 std::vector
<base::Closure
> on_stop_discovery_callbacks_
;
147 size_t num_discovery_listeners_
;
149 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
150 scoped_refptr
<BluetoothSocketThread
> socket_thread_
;
151 scoped_refptr
<BluetoothTaskManagerWin
> task_manager_
;
153 base::ThreadChecker thread_checker_
;
155 // List of observers interested in event notifications from us.
156 ObserverList
<BluetoothAdapter::Observer
> observers_
;
158 // NOTE: This should remain the last member so it'll be destroyed and
159 // invalidate its weak pointers before any other members are destroyed.
160 base::WeakPtrFactory
<BluetoothAdapterWin
> weak_ptr_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin
);
165 } // namespace device
167 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_