blob: 773c6f32f190217d8daed57cf16a0d4a1fe8af2b [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
6#define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
7
8#include <string>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#include <utility>
10#include <vector>
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010012#include "base/containers/hash_tables.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_vector.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015#include "base/memory/weak_ptr.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "base/threading/thread_checker.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "device/bluetooth/bluetooth_adapter.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "device/bluetooth/bluetooth_task_manager_win.h"
19
20namespace base {
21
22class SequencedTaskRunner;
23
24} // namespace base
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025
26namespace device {
27
28class BluetoothAdapterFactory;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029class BluetoothAdapterWinTest;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030class BluetoothDevice;
31
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032class BluetoothAdapterWin : public BluetoothAdapter,
33 public BluetoothTaskManagerWin::Observer {
34 public:
35 typedef base::Callback<void()> InitCallback;
36
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037 // BluetoothAdapter override
38 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
39 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010040 virtual std::string GetAddress() const OVERRIDE;
41 virtual std::string GetName() const OVERRIDE;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000042 virtual void SetName(const std::string& name,
43 const base::Closure& callback,
44 const ErrorCallback& error_callback) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000045 virtual bool IsInitialized() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046 virtual bool IsPresent() const OVERRIDE;
47 virtual bool IsPowered() const OVERRIDE;
48 virtual void SetPowered(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000049 bool discoverable,
50 const base::Closure& callback,
51 const ErrorCallback& error_callback) OVERRIDE;
52 virtual bool IsDiscoverable() const OVERRIDE;
53 virtual void SetDiscoverable(
54 bool discoverable,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055 const base::Closure& callback,
56 const ErrorCallback& error_callback) OVERRIDE;
57 virtual bool IsDiscovering() const OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058
59 virtual void StartDiscovering(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000060 const base::Closure& callback,
61 const ErrorCallback& error_callback) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062 virtual void StopDiscovering(
63 const base::Closure& callback,
64 const ErrorCallback& error_callback) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065 virtual void ReadLocalOutOfBandPairingData(
66 const BluetoothOutOfBandPairingDataCallback& callback,
67 const ErrorCallback& error_callback) OVERRIDE;
68
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 // 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;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074 virtual void DevicesDiscovered(
75 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices)
76 OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010078 virtual void DevicesUpdated(
79 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices)
80 OVERRIDE;
81
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000082 private:
83 friend class BluetoothAdapterFactory;
84 friend class BluetoothAdapterWinTest;
85
86 enum DiscoveryStatus {
87 NOT_DISCOVERING,
88 DISCOVERY_STARTING,
89 DISCOVERING,
90 DISCOVERY_STOPPING
91 };
92
93 explicit BluetoothAdapterWin(const InitCallback& init_callback);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000094 virtual ~BluetoothAdapterWin();
95
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010096 void Init();
97 void InitForTest(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000098 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
99 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
100
101 void MaybePostStartDiscoveryTask();
102 void MaybePostStopDiscoveryTask();
103
104 InitCallback init_callback_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100105 std::string address_;
106 std::string name_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107 bool initialized_;
108 bool powered_;
109 DiscoveryStatus discovery_status_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100110 base::hash_set<std::string> discovered_devices_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000111
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_;
116
117 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
118 scoped_refptr<BluetoothTaskManagerWin> task_manager_;
119
120 base::ThreadChecker thread_checker_;
121
122 // List of observers interested in event notifications from us.
123 ObserverList<BluetoothAdapter::Observer> observers_;
124
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000125 // 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_;
128
129 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin);
130};
131
132} // namespace device
133
134#endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_