OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 138 |
139 // Indicates whether the adapter is currently discovering new devices. | 139 // Indicates whether the adapter is currently discovering new devices. |
140 virtual bool IsDiscovering() const = 0; | 140 virtual bool IsDiscovering() const = 0; |
141 | 141 |
142 // Requests the adapter to start a new discovery session. On success, a new | 142 // Requests the adapter to start a new discovery session. On success, a new |
143 // instance of BluetoothDiscoverySession will be returned to the caller via | 143 // instance of BluetoothDiscoverySession will be returned to the caller via |
144 // |callback| and the adapter will be discovering nearby Bluetooth devices. | 144 // |callback| and the adapter will be discovering nearby Bluetooth devices. |
145 // The returned BluetoothDiscoverySession is owned by the caller and it's the | 145 // The returned BluetoothDiscoverySession is owned by the caller and it's the |
146 // owner's responsibility to properly clean it up and stop the session when | 146 // owner's responsibility to properly clean it up and stop the session when |
147 // device discovery is no longer needed. | 147 // device discovery is no longer needed. |
| 148 // |
| 149 // If clients desire device discovery to run, they should always call this |
| 150 // method and never make it conditional on the value of IsDiscovering(), as |
| 151 // another client might cause discovery to stop unexpectedly. Hence, clients |
| 152 // should always obtain a BluetoothDiscoverySession and call |
| 153 // BluetoothDiscoverySession::Stop when done. When this method gets called, |
| 154 // device discovery may actually be in progress. Clients can call GetDevices() |
| 155 // and check for those with IsPaired() as false to obtain the list of devices |
| 156 // that have been discovered so far. Otherwise, clients can be notified of all |
| 157 // new and lost devices can by implementing the Observer methods "DeviceAdded" |
| 158 // and "DeviceRemoved". |
148 typedef base::Callback<void(scoped_ptr<BluetoothDiscoverySession>)> | 159 typedef base::Callback<void(scoped_ptr<BluetoothDiscoverySession>)> |
149 DiscoverySessionCallback; | 160 DiscoverySessionCallback; |
150 virtual void StartDiscoverySession(const DiscoverySessionCallback& callback, | 161 virtual void StartDiscoverySession(const DiscoverySessionCallback& callback, |
151 const ErrorCallback& error_callback); | 162 const ErrorCallback& error_callback); |
152 | 163 |
153 // DEPRECATED: Use StartDiscoverySession instead. | |
154 // Requests that the adapter begin discovering new devices, code must | |
155 // always call this method if they require the adapter be in discovery | |
156 // and should not make it conditional on the value of IsDiscovering() | |
157 // as other adapter users may be making the same request. Code must also | |
158 // call StopDiscovering() when done. On success |callback| will be called, | |
159 // on failure |error_callback| will be called instead. | |
160 // | |
161 // Since discovery may already be in progress when this method is called, | |
162 // callers should retrieve the current set of discovered devices by calling | |
163 // GetDevices() and checking for those with IsPaired() as false. | |
164 virtual void StartDiscovering(const base::Closure& callback, | |
165 const ErrorCallback& error_callback); | |
166 | |
167 // DEPRECATED: Use BluetoothDiscoverySession::Stop instead. | |
168 // Requests that an earlier call to StartDiscovering() be cancelled; the | |
169 // adapter may not actually cease discovering devices if other callers | |
170 // have called StartDiscovering() and not yet called this method. On | |
171 // success |callback| will be called, on failure |error_callback| will be | |
172 // called instead. | |
173 virtual void StopDiscovering(const base::Closure& callback, | |
174 const ErrorCallback& error_callback); | |
175 | |
176 // Requests the list of devices from the adapter, all are returned | 164 // Requests the list of devices from the adapter, all are returned |
177 // including those currently connected and those paired. Use the | 165 // including those currently connected and those paired. Use the |
178 // returned device pointers to determine which they are. | 166 // returned device pointers to determine which they are. |
179 typedef std::vector<BluetoothDevice*> DeviceList; | 167 typedef std::vector<BluetoothDevice*> DeviceList; |
180 virtual DeviceList GetDevices(); | 168 virtual DeviceList GetDevices(); |
181 typedef std::vector<const BluetoothDevice*> ConstDeviceList; | 169 typedef std::vector<const BluetoothDevice*> ConstDeviceList; |
182 virtual ConstDeviceList GetDevices() const; | 170 virtual ConstDeviceList GetDevices() const; |
183 | 171 |
184 // Returns a pointer to the device with the given address |address| or | 172 // Returns a pointer to the device with the given address |address| or |
185 // NULL if no such device is known. | 173 // NULL if no such device is known. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 256 |
269 // Success callback passed to AddDiscoverySession by StartDiscoverySession. | 257 // Success callback passed to AddDiscoverySession by StartDiscoverySession. |
270 void OnStartDiscoverySession(const DiscoverySessionCallback& callback); | 258 void OnStartDiscoverySession(const DiscoverySessionCallback& callback); |
271 | 259 |
272 // Marks all known DiscoverySession instances as inactive. Called by | 260 // Marks all known DiscoverySession instances as inactive. Called by |
273 // BluetoothAdapter in the event that the adapter unexpectedly stops | 261 // BluetoothAdapter in the event that the adapter unexpectedly stops |
274 // discovering. This should be called by all platform implementations. | 262 // discovering. This should be called by all platform implementations. |
275 void MarkDiscoverySessionsAsInactive(); | 263 void MarkDiscoverySessionsAsInactive(); |
276 | 264 |
277 // Removes |discovery_session| from |discovery_sessions_|, if its in there. | 265 // Removes |discovery_session| from |discovery_sessions_|, if its in there. |
278 // Called by DiscoverySession when an instance is destroyed. | 266 // Called by DiscoverySession when an instance is destroyed or becomes |
279 void DiscoverySessionDestroyed(BluetoothDiscoverySession* discovery_session); | 267 // inactive. |
280 | 268 void DiscoverySessionBecameInactive( |
281 // List of all DiscoverySession objects. We keep raw pointers, with the | 269 BluetoothDiscoverySession* discovery_session); |
282 // assumption that a DiscoverySession will remove itself from this list when | |
283 // it gets destroyed. | |
284 std::set<BluetoothDiscoverySession*> discovery_sessions_; | |
285 | 270 |
286 // Devices paired with, connected to, discovered by, or visible to the | 271 // Devices paired with, connected to, discovered by, or visible to the |
287 // adapter. The key is the Bluetooth address of the device and the value | 272 // adapter. The key is the Bluetooth address of the device and the value |
288 // is the BluetoothDevice object whose lifetime is managed by the | 273 // is the BluetoothDevice object whose lifetime is managed by the |
289 // adapter instance. | 274 // adapter instance. |
290 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; | 275 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; |
291 DevicesMap devices_; | 276 DevicesMap devices_; |
292 | 277 |
293 // Default pairing delegates registered with the adapter. | 278 // Default pairing delegates registered with the adapter. |
294 typedef std::pair<BluetoothDevice::PairingDelegate*, | 279 typedef std::pair<BluetoothDevice::PairingDelegate*, |
295 PairingDelegatePriority> PairingDelegatePair; | 280 PairingDelegatePriority> PairingDelegatePair; |
296 std::list<PairingDelegatePair> pairing_delegates_; | 281 std::list<PairingDelegatePair> pairing_delegates_; |
297 | 282 |
298 private: | 283 private: |
| 284 // List of active DiscoverySession objects. This is used to notify sessions to |
| 285 // become inactive in case of an unexpected change to the adapter discovery |
| 286 // state. We keep raw pointers, with the invariant that a DiscoverySession |
| 287 // will remove itself from this list when it gets destroyed or becomes |
| 288 // inactive by calling DiscoverySessionBecameInactive(), hence no pointers to |
| 289 // deallocated sessions are kept. |
| 290 std::set<BluetoothDiscoverySession*> discovery_sessions_; |
| 291 |
299 // Note: This should remain the last member so it'll be destroyed and | 292 // Note: This should remain the last member so it'll be destroyed and |
300 // invalidate its weak pointers before any other members are destroyed. | 293 // invalidate its weak pointers before any other members are destroyed. |
301 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; | 294 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; |
302 }; | 295 }; |
303 | 296 |
304 } // namespace device | 297 } // namespace device |
305 | 298 |
306 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 299 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_H_ |
OLD | NEW |