Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_ |
| 7 | |
| 8 | #include <string> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include <vector> |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 12 | #include "base/hash_tables.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/memory/scoped_vector.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "base/threading/thread_checker.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | #include "device/bluetooth/bluetooth_adapter.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "device/bluetooth/bluetooth_task_manager_win.h" |
| 19 | |
| 20 | namespace base { |
| 21 | |
| 22 | class SequencedTaskRunner; |
| 23 | |
| 24 | } // namespace base |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | |
| 26 | namespace device { |
| 27 | |
| 28 | class BluetoothAdapterFactory; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 29 | class BluetoothAdapterWinTest; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | class BluetoothDevice; |
| 31 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | class BluetoothAdapterWin : public BluetoothAdapter, |
| 33 | public BluetoothTaskManagerWin::Observer { |
| 34 | public: |
| 35 | typedef base::Callback<void()> InitCallback; |
| 36 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 37 | // BluetoothAdapter override |
| 38 | virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE; |
| 39 | virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 40 | virtual std::string GetAddress() const OVERRIDE; |
| 41 | virtual std::string GetName() const OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | virtual bool IsInitialized() const OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | virtual bool IsPresent() const OVERRIDE; |
| 44 | virtual bool IsPowered() const OVERRIDE; |
| 45 | virtual void SetPowered( |
| 46 | bool powered, |
| 47 | const base::Closure& callback, |
| 48 | const ErrorCallback& error_callback) OVERRIDE; |
| 49 | virtual bool IsDiscovering() const OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | |
| 51 | virtual void StartDiscovering( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | const base::Closure& callback, |
| 53 | const ErrorCallback& error_callback) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 54 | virtual void StopDiscovering( |
| 55 | const base::Closure& callback, |
| 56 | const ErrorCallback& error_callback) OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | virtual void ReadLocalOutOfBandPairingData( |
| 58 | const BluetoothOutOfBandPairingDataCallback& callback, |
| 59 | const ErrorCallback& error_callback) OVERRIDE; |
| 60 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 61 | // BluetoothTaskManagerWin::Observer override |
| 62 | virtual void AdapterStateChanged( |
| 63 | const BluetoothTaskManagerWin::AdapterState& state) OVERRIDE; |
| 64 | virtual void DiscoveryStarted(bool success) OVERRIDE; |
| 65 | virtual void DiscoveryStopped() OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 66 | virtual void DevicesDiscovered( |
| 67 | const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) |
| 68 | OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 70 | virtual void DevicesUpdated( |
| 71 | const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) |
| 72 | OVERRIDE; |
| 73 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 74 | private: |
| 75 | friend class BluetoothAdapterFactory; |
| 76 | friend class BluetoothAdapterWinTest; |
| 77 | |
| 78 | enum DiscoveryStatus { |
| 79 | NOT_DISCOVERING, |
| 80 | DISCOVERY_STARTING, |
| 81 | DISCOVERING, |
| 82 | DISCOVERY_STOPPING |
| 83 | }; |
| 84 | |
| 85 | explicit BluetoothAdapterWin(const InitCallback& init_callback); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 86 | virtual ~BluetoothAdapterWin(); |
| 87 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 88 | void Init(); |
| 89 | void InitForTest( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 90 | scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 91 | scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); |
| 92 | |
| 93 | void MaybePostStartDiscoveryTask(); |
| 94 | void MaybePostStopDiscoveryTask(); |
| 95 | |
| 96 | InitCallback init_callback_; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 97 | std::string address_; |
| 98 | std::string name_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 99 | bool initialized_; |
| 100 | bool powered_; |
| 101 | DiscoveryStatus discovery_status_; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 102 | base::hash_set<std::string> discovered_devices_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 103 | |
| 104 | std::vector<std::pair<base::Closure, ErrorCallback> > |
| 105 | on_start_discovery_callbacks_; |
| 106 | std::vector<base::Closure> on_stop_discovery_callbacks_; |
| 107 | size_t num_discovery_listeners_; |
| 108 | |
| 109 | scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 110 | scoped_refptr<BluetoothTaskManagerWin> task_manager_; |
| 111 | |
| 112 | base::ThreadChecker thread_checker_; |
| 113 | |
| 114 | // List of observers interested in event notifications from us. |
| 115 | ObserverList<BluetoothAdapter::Observer> observers_; |
| 116 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 117 | // NOTE: This should remain the last member so it'll be destroyed and |
| 118 | // invalidate its weak pointers before any other members are destroyed. |
| 119 | base::WeakPtrFactory<BluetoothAdapterWin> weak_ptr_factory_; |
| 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin); |
| 122 | }; |
| 123 | |
| 124 | } // namespace device |
| 125 | |
| 126 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_ |