Roll WebRTC 8837:8852, Libjingle 8836:8851
[chromium-blink-merge.git] / device / bluetooth / bluetooth_discovery_session.h
blob2dae0de2e2af4c7042fed90a74adf7267972a6f8
1 // Copyright 2014 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_DISCOVERY_SESSION_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_SESSION_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "device/bluetooth/bluetooth_export.h"
13 namespace device {
15 class BluetoothAdapter;
17 // BluetoothDiscoverySession represents a current active or inactive device
18 // discovery session. Instances of this class are obtained by calling
19 // BluetoothAdapter::StartDiscoverySession. The Bluetooth adapter will be
20 // constantly searching for nearby devices, as long as at least one instance
21 // of an active BluetoothDiscoverySession exists. A BluetoothDiscoverySession is
22 // considered active, as long as the adapter is discovering AND the owner of the
23 // instance has not called BluetoothDiscoverySession::Stop. A
24 // BluetoothDiscoverySession might unexpectedly become inactive, if the adapter
25 // unexpectedly stops discovery. Users can implement the
26 // AdapterDiscoveringChanged method of the BluetoothAdapter::Observer interface
27 // to be notified of such a change and promptly request a new
28 // BluetoothDiscoverySession if their existing sessions have become inactive.
29 class DEVICE_BLUETOOTH_EXPORT BluetoothDiscoverySession {
30 public:
31 // The ErrorCallback is used by methods to asynchronously report errors.
32 typedef base::Closure ErrorCallback;
34 // Destructor automatically terminates the discovery session. If this
35 // results in a call to the underlying system to stop device discovery
36 // (i.e. this instance represents the last active discovery session),
37 // the call may not always succeed. To be notified of such failures,
38 // users are highly encouraged to call BluetoothDiscoverySession::Stop,
39 // instead of relying on the destructor.
40 virtual ~BluetoothDiscoverySession();
42 // Returns true if the session is active, false otherwise. If false, the
43 // adapter might still be discovering as there might still be other active
44 // sessions; this just means that this instance no longer has a say in
45 // whether or not discovery should continue. In this case, the application
46 // should request a new BluetoothDiscoverySession to make sure that device
47 // discovery continues.
48 virtual bool IsActive() const;
50 // Requests this discovery session instance to stop. If this instance is
51 // active, the session will stop. On success, |callback| is called and
52 // on error |error_callback| is called. After a successful invocation, the
53 // adapter may or may not stop device discovery, depending on whether or not
54 // other active discovery sessions are present. Users are highly encouraged
55 // to call this method to end a discovery session, instead of relying on the
56 // destructor, so that they can be notified of the result via the callback
57 // arguments.
58 virtual void Stop(const base::Closure& callback,
59 const ErrorCallback& error_callback);
61 protected:
62 explicit BluetoothDiscoverySession(scoped_refptr<BluetoothAdapter> adapter);
64 private:
65 friend class BluetoothAdapter;
67 // Internal callback invoked when a call to Stop has succeeded.
68 void OnStop(const base::Closure& callback);
70 // Marks this instance as inactive. Called by BluetoothAdapter to mark a
71 // session as inactive in the case of an unexpected change to the adapter
72 // discovery state.
73 void MarkAsInactive();
75 // Whether or not this instance represents an active discovery session.
76 bool active_;
78 // The adapter that created this instance.
79 scoped_refptr<BluetoothAdapter> adapter_;
81 // Note: This should remain the last member so it'll be destroyed and
82 // invalidate its weak pointers before any other members are destroyed.
83 base::WeakPtrFactory<BluetoothDiscoverySession> weak_ptr_factory_;
85 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoverySession);
88 } // namespace device
90 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_SESSION_H_