OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ |
7 | 7 |
8 #include <IOKit/IOReturn.h> | 8 #include <IOKit/IOReturn.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
18 #include "device/bluetooth/bluetooth_adapter.h" | 18 #include "device/bluetooth/bluetooth_adapter.h" |
19 | 19 |
20 @class BluetoothAdapterMacDelegate; | 20 @class BluetoothDeviceInquiryDelegate; |
21 @class IOBluetoothDevice; | 21 @class IOBluetoothDevice; |
22 @class IOBluetoothDeviceInquiry; | 22 @class IOBluetoothDeviceInquiry; |
23 @class NSArray; | 23 @class NSArray; |
24 @class NSDate; | 24 @class NSDate; |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 | 27 |
28 class SequencedTaskRunner; | 28 class SequencedTaskRunner; |
29 | 29 |
30 } // namespace base | 30 } // namespace base |
31 | 31 |
32 namespace device { | 32 namespace device { |
33 | 33 |
34 class BluetoothAdapterMacTest; | 34 class BluetoothAdapterMacTest; |
35 class BluetoothMacClassicDiscoveryManager; | |
35 | 36 |
36 class BluetoothAdapterMac : public BluetoothAdapter { | 37 class BluetoothAdapterMac : public BluetoothAdapter { |
37 public: | 38 public: |
38 static base::WeakPtr<BluetoothAdapter> CreateAdapter(); | 39 static base::WeakPtr<BluetoothAdapter> CreateAdapter(); |
39 | 40 |
40 // BluetoothAdapter: | 41 // BluetoothAdapter: |
41 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE; | 42 virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE; |
42 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; | 43 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; |
43 virtual std::string GetAddress() const OVERRIDE; | 44 virtual std::string GetAddress() const OVERRIDE; |
44 virtual std::string GetName() const OVERRIDE; | 45 virtual std::string GetName() const OVERRIDE; |
(...skipping 18 matching lines...) Expand all Loading... | |
63 int channel, | 64 int channel, |
64 bool insecure, | 65 bool insecure, |
65 const CreateServiceCallback& callback, | 66 const CreateServiceCallback& callback, |
66 const CreateServiceErrorCallback& error_callback) OVERRIDE; | 67 const CreateServiceErrorCallback& error_callback) OVERRIDE; |
67 virtual void CreateL2capService( | 68 virtual void CreateL2capService( |
68 const BluetoothUUID& uuid, | 69 const BluetoothUUID& uuid, |
69 int psm, | 70 int psm, |
70 const CreateServiceCallback& callback, | 71 const CreateServiceCallback& callback, |
71 const CreateServiceErrorCallback& error_callback) OVERRIDE; | 72 const CreateServiceErrorCallback& error_callback) OVERRIDE; |
72 | 73 |
73 // called by BluetoothAdapterMacDelegate. | 74 // Called by BluetoothMacClassicDiscoveryManager. |
74 void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry); | 75 void DeviceFound(IOBluetoothDevice* device); |
75 void DeviceFound(IOBluetoothDeviceInquiry* inquiry, | 76 void ClassicDiscoveryStopped(bool unexpected); |
76 IOBluetoothDevice* device); | |
77 void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry, | |
78 IOReturn error, | |
79 bool aborted); | |
80 | 77 |
81 protected: | 78 protected: |
82 // BluetoothAdapter: | 79 // BluetoothAdapter: |
83 virtual void RemovePairingDelegateInternal( | 80 virtual void RemovePairingDelegateInternal( |
84 device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE; | 81 device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE; |
85 | 82 |
86 private: | 83 private: |
87 friend class BluetoothAdapterMacTest; | 84 friend class BluetoothAdapterMacTest; |
88 | 85 |
89 enum DiscoveryStatus { | |
90 NOT_DISCOVERING, | |
91 DISCOVERY_STARTING, | |
92 DISCOVERING, | |
93 DISCOVERY_STOPPING | |
94 }; | |
95 | |
96 BluetoothAdapterMac(); | 86 BluetoothAdapterMac(); |
97 virtual ~BluetoothAdapterMac(); | 87 virtual ~BluetoothAdapterMac(); |
98 | 88 |
99 // BluetoothAdapter: | 89 // BluetoothAdapter: |
100 virtual void AddDiscoverySession( | 90 virtual void AddDiscoverySession( |
101 const base::Closure& callback, | 91 const base::Closure& callback, |
102 const ErrorCallback& error_callback) OVERRIDE; | 92 const ErrorCallback& error_callback) OVERRIDE; |
103 virtual void RemoveDiscoverySession( | 93 virtual void RemoveDiscoverySession( |
104 const base::Closure& callback, | 94 const base::Closure& callback, |
105 const ErrorCallback& error_callback) OVERRIDE; | 95 const ErrorCallback& error_callback) OVERRIDE; |
106 | 96 |
107 void Init(); | 97 void Init(); |
108 void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | 98 void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner); |
109 void PollAdapter(); | 99 void PollAdapter(); |
110 | 100 |
111 // Updates |devices_| to be consistent with |devices|. | 101 // Updates |devices_| to be consistent with |devices|. |
112 void UpdateDevices(NSArray* devices); | 102 void UpdateDevices(NSArray* devices); |
113 | 103 |
114 void MaybeStartDeviceInquiry(); | |
115 void MaybeStopDeviceInquiry(); | |
116 | |
117 typedef std::vector<std::pair<base::Closure, ErrorCallback> > | |
118 DiscoveryCallbackList; | |
119 void RunCallbacks(const DiscoveryCallbackList& callback_list, | |
120 bool success) const; | |
121 | |
122 std::string address_; | 104 std::string address_; |
123 std::string name_; | 105 std::string name_; |
124 bool powered_; | 106 bool powered_; |
125 DiscoveryStatus discovery_status_; | |
126 | 107 |
127 DiscoveryCallbackList on_start_discovery_callbacks_; | 108 int num_discovery_sessions_; |
128 DiscoveryCallbackList on_stop_discovery_callbacks_; | |
129 size_t num_discovery_listeners_; | |
130 | 109 |
131 base::scoped_nsobject<BluetoothAdapterMacDelegate> adapter_delegate_; | 110 scoped_ptr<BluetoothMacClassicDiscoveryManager> |
132 base::scoped_nsobject<IOBluetoothDeviceInquiry> device_inquiry_; | 111 classic_discovery_session_manager_; |
133 | 112 |
134 // A list of discovered device addresses. | 113 // A list of discovered device addresses. |
135 // This list is used to check if the same device is discovered twice during | 114 // This list is used to check if the same device is discovered twice during |
136 // the discovery between consecutive inquiries. | 115 // the discovery between consecutive inquiries. |
137 base::hash_set<std::string> discovered_devices_; | 116 base::hash_set<std::string> discovered_devices_; |
138 | 117 |
139 // Timestamp for the recently accessed device. | 118 // Timestamp for the recently accessed device. |
140 // Used to determine if |devices_| needs an update. | 119 // Used to determine if |devices_| needs an update. |
141 base::scoped_nsobject<NSDate> recently_accessed_device_timestamp_; | 120 base::scoped_nsobject<NSDate> recently_accessed_device_timestamp_; |
142 | 121 |
143 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | 122 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
144 | 123 |
145 // List of observers interested in event notifications from us. | 124 // List of observers interested in event notifications from us. |
146 ObserverList<BluetoothAdapter::Observer> observers_; | 125 ObserverList<BluetoothAdapter::Observer> observers_; |
147 | 126 |
148 base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_; | 127 base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_; |
149 | 128 |
150 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac); | 129 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac); |
151 }; | 130 }; |
152 | 131 |
132 // Class used by BluetoothAdapterMac to manage a classic discovery session. | |
133 // This class makes sure that a device inquiry is being performed as long as | |
134 // discovery has been requested by the adapter. Instances are created and owned | |
135 // by BluetoothAdapterMac and should not be created by others. | |
136 class BluetoothMacClassicDiscoveryManager { | |
keybuk
2014/06/09 17:57:39
I dislike having multiple classes in a header like
| |
137 public: | |
138 BluetoothMacClassicDiscoveryManager(BluetoothAdapterMac* adapter); | |
139 ~BluetoothMacClassicDiscoveryManager(); | |
140 | |
141 // Returns true, if discovery is currently being performed. | |
142 bool IsDiscovering() const; | |
143 | |
144 // Initiates a discovery session. Returns true on success, or if discovery | |
145 // is already running. | |
146 bool StartDiscovery(); | |
147 bool StopDiscovery(); | |
148 | |
149 // Called by BluetoothDeviceInquiryDelegate. | |
150 void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry); | |
151 void DeviceFound(IOBluetoothDeviceInquiry* inquiry, | |
152 IOBluetoothDevice* device); | |
153 void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry, | |
154 IOReturn error, | |
155 bool aborted); | |
156 | |
157 private: | |
158 // The requested discovery state. | |
159 bool should_do_discovery_; | |
160 | |
161 // The current inquiry state. | |
162 bool inquiry_running_; | |
163 | |
164 // The BluetoothAdapterMac that this instance belongs to. | |
165 BluetoothAdapterMac* adapter_; | |
166 | |
167 // Objective-C objects for running and tracking device inquiry. | |
168 base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_; | |
169 base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_; | |
170 | |
171 DISALLOW_COPY_AND_ASSIGN(BluetoothMacClassicDiscoveryManager); | |
172 }; | |
173 | |
153 } // namespace device | 174 } // namespace device |
154 | 175 |
155 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ | 176 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_ |
OLD | NEW |