1 // Copyright 2013 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_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
8 #include <IOKit/IOReturn.h>
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
20 @class BluetoothAdapterMacDelegate;
21 @class IOBluetoothDevice;
22 @class IOBluetoothDeviceInquiry;
26 class BluetoothAdapterMacDelegate;
27 class IOBluetoothDevice;
28 class IOBluetoothDeviceInquiry;
35 class SequencedTaskRunner;
41 class BluetoothAdapterMacTest;
43 class BluetoothAdapterMac : public BluetoothAdapter {
45 // BluetoothAdapter override
46 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
47 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
48 virtual std::string GetAddress() const OVERRIDE;
49 virtual std::string GetName() const OVERRIDE;
50 virtual void SetName(const std::string& name,
51 const base::Closure& callback,
52 const ErrorCallback& error_callback) OVERRIDE;
53 virtual bool IsInitialized() const OVERRIDE;
54 virtual bool IsPresent() const OVERRIDE;
55 virtual bool IsPowered() const OVERRIDE;
56 virtual void SetPowered(
58 const base::Closure& callback,
59 const ErrorCallback& error_callback) OVERRIDE;
60 virtual bool IsDiscoverable() const OVERRIDE;
61 virtual void SetDiscoverable(
63 const base::Closure& callback,
64 const ErrorCallback& error_callback) OVERRIDE;
65 virtual bool IsDiscovering() const OVERRIDE;
67 virtual void StartDiscovering(
68 const base::Closure& callback,
69 const ErrorCallback& error_callback) OVERRIDE;
70 virtual void StopDiscovering(
71 const base::Closure& callback,
72 const ErrorCallback& error_callback) OVERRIDE;
73 virtual void ReadLocalOutOfBandPairingData(
74 const BluetoothOutOfBandPairingDataCallback& callback,
75 const ErrorCallback& error_callback) OVERRIDE;
77 // called by BluetoothAdapterMacDelegate.
78 void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry);
79 void DeviceFound(IOBluetoothDeviceInquiry* inquiry,
80 IOBluetoothDevice* device);
81 void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry,
86 friend class BluetoothAdapterFactory;
87 friend class BluetoothAdapterMacTest;
89 enum DiscoveryStatus {
96 BluetoothAdapterMac();
97 virtual ~BluetoothAdapterMac();
100 void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
103 // Updates |devices_| to be consistent with |devices|.
104 void UpdateDevices(NSArray* devices);
106 void MaybeStartDeviceInquiry();
107 void MaybeStopDeviceInquiry();
109 typedef std::vector<std::pair<base::Closure, ErrorCallback> >
110 DiscoveryCallbackList;
111 void RunCallbacks(const DiscoveryCallbackList& callback_list,
114 std::string address_;
117 DiscoveryStatus discovery_status_;
119 DiscoveryCallbackList on_start_discovery_callbacks_;
120 DiscoveryCallbackList on_stop_discovery_callbacks_;
121 size_t num_discovery_listeners_;
123 BluetoothAdapterMacDelegate* adapter_delegate_;
124 IOBluetoothDeviceInquiry* device_inquiry_;
126 // A list of discovered device addresses.
127 // This list is used to check if the same device is discovered twice during
128 // the discovery between consecutive inquiries.
129 base::hash_set<std::string> discovered_devices_;
131 // Timestamp for the recently accessed device.
132 // Used to determine if |devices_| needs an update.
133 NSDate* recently_accessed_device_timestamp_;
135 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
137 // List of observers interested in event notifications from us.
138 ObserverList<BluetoothAdapter::Observer> observers_;
140 base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_;
142 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac);
145 } // namespace device
147 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_