blob: 76581d9820dcfb28bd475b2dccb0f316b0054cc7 [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)2a99a7e2013-03-28 15:31:22 +000012#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_vector.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/memory/weak_ptr.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015#include "base/threading/thread_checker.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "device/bluetooth/bluetooth_adapter.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "device/bluetooth/bluetooth_task_manager_win.h"
18
19namespace base {
20
21class SequencedTaskRunner;
22
23} // namespace base
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024
25namespace device {
26
27class BluetoothAdapterFactory;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028class BluetoothAdapterWinTest;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029class BluetoothDevice;
30
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000031class BluetoothAdapterWin : public BluetoothAdapter,
32 public BluetoothTaskManagerWin::Observer {
33 public:
34 typedef base::Callback<void()> InitCallback;
35
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036 // BluetoothAdapter override
37 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
38 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000039 virtual bool IsInitialized() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040 virtual bool IsPresent() const OVERRIDE;
41 virtual bool IsPowered() const OVERRIDE;
42 virtual void SetPowered(
43 bool powered,
44 const base::Closure& callback,
45 const ErrorCallback& error_callback) OVERRIDE;
46 virtual bool IsDiscovering() const OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047 virtual bool IsScanning() const OVERRIDE;
48
49 virtual void StartDiscovering(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050 const base::Closure& callback,
51 const ErrorCallback& error_callback) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000052 virtual void StopDiscovering(
53 const base::Closure& callback,
54 const ErrorCallback& error_callback) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055 virtual void ReadLocalOutOfBandPairingData(
56 const BluetoothOutOfBandPairingDataCallback& callback,
57 const ErrorCallback& error_callback) OVERRIDE;
58
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000059 // BluetoothTaskManagerWin::Observer override
60 virtual void AdapterStateChanged(
61 const BluetoothTaskManagerWin::AdapterState& state) OVERRIDE;
62 virtual void DiscoveryStarted(bool success) OVERRIDE;
63 virtual void DiscoveryStopped() OVERRIDE;
64 virtual void ScanningChanged(bool scanning) OVERRIDE;
65 virtual void DevicesDiscovered(
66 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices)
67 OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 private:
70 friend class BluetoothAdapterFactory;
71 friend class BluetoothAdapterWinTest;
72
73 enum DiscoveryStatus {
74 NOT_DISCOVERING,
75 DISCOVERY_STARTING,
76 DISCOVERING,
77 DISCOVERY_STOPPING
78 };
79
80 explicit BluetoothAdapterWin(const InitCallback& init_callback);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000081 virtual ~BluetoothAdapterWin();
82
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083 void TrackDefaultAdapter();
84 void TrackTestAdapter(
85 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
86 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
87
88 void MaybePostStartDiscoveryTask();
89 void MaybePostStopDiscoveryTask();
90
91 InitCallback init_callback_;
92 bool initialized_;
93 bool powered_;
94 DiscoveryStatus discovery_status_;
95 bool scanning_;
96
97 std::vector<std::pair<base::Closure, ErrorCallback> >
98 on_start_discovery_callbacks_;
99 std::vector<base::Closure> on_stop_discovery_callbacks_;
100 size_t num_discovery_listeners_;
101
102 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
103 scoped_refptr<BluetoothTaskManagerWin> task_manager_;
104
105 base::ThreadChecker thread_checker_;
106
107 // List of observers interested in event notifications from us.
108 ObserverList<BluetoothAdapter::Observer> observers_;
109
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110 // NOTE: This should remain the last member so it'll be destroyed and
111 // invalidate its weak pointers before any other members are destroyed.
112 base::WeakPtrFactory<BluetoothAdapterWin> weak_ptr_factory_;
113
114 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin);
115};
116
117} // namespace device
118
119#endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_