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_
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_task_manager_win.h"
22 class SequencedTaskRunner;
28 class BluetoothAdapterFactory;
29 class BluetoothAdapterWinTest;
30 class BluetoothDevice;
32 class BluetoothAdapterWin : public BluetoothAdapter,
33 public BluetoothTaskManagerWin::Observer {
35 typedef base::Callback<void()> InitCallback;
37 // BluetoothAdapter override
38 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
39 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
40 virtual std::string GetAddress() const OVERRIDE;
41 virtual std::string GetName() const OVERRIDE;
42 virtual void SetName(const std::string& name,
43 const base::Closure& callback,
44 const ErrorCallback& error_callback) OVERRIDE;
45 virtual bool IsInitialized() const OVERRIDE;
46 virtual bool IsPresent() const OVERRIDE;
47 virtual bool IsPowered() const OVERRIDE;
48 virtual void SetPowered(
50 const base::Closure& callback,
51 const ErrorCallback& error_callback) OVERRIDE;
52 virtual bool IsDiscoverable() const OVERRIDE;
53 virtual void SetDiscoverable(
55 const base::Closure& callback,
56 const ErrorCallback& error_callback) OVERRIDE;
57 virtual bool IsDiscovering() const OVERRIDE;
59 virtual void StartDiscovering(
60 const base::Closure& callback,
61 const ErrorCallback& error_callback) OVERRIDE;
62 virtual void StopDiscovering(
63 const base::Closure& callback,
64 const ErrorCallback& error_callback) OVERRIDE;
65 virtual void ReadLocalOutOfBandPairingData(
66 const BluetoothOutOfBandPairingDataCallback& callback,
67 const ErrorCallback& error_callback) OVERRIDE;
69 // BluetoothTaskManagerWin::Observer override
70 virtual void AdapterStateChanged(
71 const BluetoothTaskManagerWin::AdapterState& state) OVERRIDE;
72 virtual void DiscoveryStarted(bool success) OVERRIDE;
73 virtual void DiscoveryStopped() OVERRIDE;
74 virtual void DevicesDiscovered(
75 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices)
78 virtual void DevicesUpdated(
79 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices)
83 friend class BluetoothAdapterFactory;
84 friend class BluetoothAdapterWinTest;
86 enum DiscoveryStatus {
93 explicit BluetoothAdapterWin(const InitCallback& init_callback);
94 virtual ~BluetoothAdapterWin();
98 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
99 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
101 void MaybePostStartDiscoveryTask();
102 void MaybePostStopDiscoveryTask();
104 InitCallback init_callback_;
105 std::string address_;
109 DiscoveryStatus discovery_status_;
110 base::hash_set<std::string> discovered_devices_;
112 std::vector<std::pair<base::Closure, ErrorCallback> >
113 on_start_discovery_callbacks_;
114 std::vector<base::Closure> on_stop_discovery_callbacks_;
115 size_t num_discovery_listeners_;
117 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
118 scoped_refptr<BluetoothTaskManagerWin> task_manager_;
120 base::ThreadChecker thread_checker_;
122 // List of observers interested in event notifications from us.
123 ObserverList<BluetoothAdapter::Observer> observers_;
125 // NOTE: This should remain the last member so it'll be destroyed and
126 // invalidate its weak pointers before any other members are destroyed.
127 base::WeakPtrFactory<BluetoothAdapterWin> weak_ptr_factory_;
129 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin);
132 } // namespace device
134 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_