Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_mac.h
blob064b6bf6919346373f1359f3035db02d2c05387d
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>
10 #include <string>
11 #include <vector>
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"
19 #ifdef __OBJC__
20 @class BluetoothAdapterMacDelegate;
21 @class IOBluetoothDevice;
22 @class IOBluetoothDeviceInquiry;
23 @class NSArray;
24 @class NSDate;
25 #else
26 class BluetoothAdapterMacDelegate;
27 class IOBluetoothDevice;
28 class IOBluetoothDeviceInquiry;
29 class NSArray;
30 class NSDate;
31 #endif
33 namespace base {
35 class SequencedTaskRunner;
37 } // namespace base
39 namespace device {
41 class BluetoothAdapterMacTest;
43 class BluetoothAdapterMac : public BluetoothAdapter {
44 public:
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(
57 bool powered,
58 const base::Closure& callback,
59 const ErrorCallback& error_callback) OVERRIDE;
60 virtual bool IsDiscoverable() const OVERRIDE;
61 virtual void SetDiscoverable(
62 bool discoverable,
63 const base::Closure& callback,
64 const ErrorCallback& error_callback) OVERRIDE;
65 virtual bool IsDiscovering() const OVERRIDE;
66 virtual void ReadLocalOutOfBandPairingData(
67 const BluetoothOutOfBandPairingDataCallback& callback,
68 const ErrorCallback& error_callback) OVERRIDE;
70 // called by BluetoothAdapterMacDelegate.
71 void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry);
72 void DeviceFound(IOBluetoothDeviceInquiry* inquiry,
73 IOBluetoothDevice* device);
74 void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry,
75 IOReturn error,
76 bool aborted);
78 protected:
79 // BluetoothAdapter override
80 virtual void RemovePairingDelegateInternal(
81 device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;
83 private:
84 friend class BluetoothAdapterFactory;
85 friend class BluetoothAdapterMacTest;
87 enum DiscoveryStatus {
88 NOT_DISCOVERING,
89 DISCOVERY_STARTING,
90 DISCOVERING,
91 DISCOVERY_STOPPING
94 BluetoothAdapterMac();
95 virtual ~BluetoothAdapterMac();
97 // BluetoothAdapter override.
98 virtual void AddDiscoverySession(
99 const base::Closure& callback,
100 const ErrorCallback& error_callback) OVERRIDE;
101 virtual void RemoveDiscoverySession(
102 const base::Closure& callback,
103 const ErrorCallback& error_callback) OVERRIDE;
105 void Init();
106 void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
107 void PollAdapter();
109 // Updates |devices_| to be consistent with |devices|.
110 void UpdateDevices(NSArray* devices);
112 void MaybeStartDeviceInquiry();
113 void MaybeStopDeviceInquiry();
115 typedef std::vector<std::pair<base::Closure, ErrorCallback> >
116 DiscoveryCallbackList;
117 void RunCallbacks(const DiscoveryCallbackList& callback_list,
118 bool success) const;
120 std::string address_;
121 std::string name_;
122 bool powered_;
123 DiscoveryStatus discovery_status_;
125 DiscoveryCallbackList on_start_discovery_callbacks_;
126 DiscoveryCallbackList on_stop_discovery_callbacks_;
127 size_t num_discovery_listeners_;
129 BluetoothAdapterMacDelegate* adapter_delegate_;
130 IOBluetoothDeviceInquiry* device_inquiry_;
132 // A list of discovered device addresses.
133 // This list is used to check if the same device is discovered twice during
134 // the discovery between consecutive inquiries.
135 base::hash_set<std::string> discovered_devices_;
137 // Timestamp for the recently accessed device.
138 // Used to determine if |devices_| needs an update.
139 NSDate* recently_accessed_device_timestamp_;
141 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
143 // List of observers interested in event notifications from us.
144 ObserverList<BluetoothAdapter::Observer> observers_;
146 base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_;
148 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac);
151 } // namespace device
153 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_