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
;
74 void RegisterAdvertisement(
75 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
76 const CreateAdvertisementCallback
& callback
,
77 const CreateAdvertisementErrorCallback
& error_callback
) override
;
79 // BluetoothTaskManagerWin::Observer override
80 void AdapterStateChanged(
81 const BluetoothTaskManagerWin::AdapterState
& state
) override
;
82 void DiscoveryStarted(bool success
) override
;
83 void DiscoveryStopped() override
;
84 void DevicesPolled(const ScopedVector
<BluetoothTaskManagerWin::DeviceState
>&
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 void RemovePairingDelegateInternal(
97 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) override
;
100 friend class BluetoothAdapterWinTest
;
102 enum DiscoveryStatus
{
109 explicit BluetoothAdapterWin(const InitCallback
& init_callback
);
110 ~BluetoothAdapterWin() override
;
113 void AddDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
114 const base::Closure
& callback
,
115 const ErrorCallback
& error_callback
) override
;
116 void RemoveDiscoverySession(BluetoothDiscoveryFilter
* discovery_filter
,
117 const base::Closure
& callback
,
118 const ErrorCallback
& error_callback
) override
;
119 void SetDiscoveryFilter(scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
120 const base::Closure
& callback
,
121 const ErrorCallback
& error_callback
) override
;
125 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
126 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
128 void MaybePostStartDiscoveryTask();
129 void MaybePostStopDiscoveryTask();
131 InitCallback init_callback_
;
132 std::string address_
;
136 DiscoveryStatus discovery_status_
;
137 base::hash_set
<std::string
> discovered_devices_
;
139 std::vector
<std::pair
<base::Closure
, ErrorCallback
> >
140 on_start_discovery_callbacks_
;
141 std::vector
<base::Closure
> on_stop_discovery_callbacks_
;
142 size_t num_discovery_listeners_
;
144 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
145 scoped_refptr
<BluetoothSocketThread
> socket_thread_
;
146 scoped_refptr
<BluetoothTaskManagerWin
> task_manager_
;
148 base::ThreadChecker thread_checker_
;
150 // NOTE: This should remain the last member so it'll be destroyed and
151 // invalidate its weak pointers before any other members are destroyed.
152 base::WeakPtrFactory
<BluetoothAdapterWin
> weak_ptr_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin
);
157 } // namespace device
159 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_