Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_discovery_session.h
blob8e9b30268862aa0de8ca0545bcec35144beda11e
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_adapter.h"
12 #include "device/bluetooth/bluetooth_discovery_filter.h"
13 #include "device/bluetooth/bluetooth_export.h"
15 namespace device {
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 virtual void SetDiscoveryFilter(
62 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
63 const base::Closure& callback,
64 const ErrorCallback& error_callback);
66 virtual const BluetoothDiscoveryFilter* GetDiscoveryFilter() const;
68 protected:
69 explicit BluetoothDiscoverySession(
70 scoped_refptr<BluetoothAdapter> adapter,
71 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter);
73 private:
74 friend class BluetoothAdapter;
76 // Internal callback invoked when a call to
77 // BluetoothAdapter::RemoveDiscoverySession has succeeded. Invokes
78 // |deactivate_discovery_session| if the session object still
79 // exists when this callback executes. Always invokes |success_callback|.
80 static void OnDiscoverySessionRemoved(
81 const base::Closure& deactivate_discovery_session,
82 const base::Closure& success_callback);
84 // Deactivate discovery session object after
85 // BluetoothAdapter::RemoveDiscoverySession completes.
86 void DeactivateDiscoverySession();
88 // Marks this instance as inactive. Called by BluetoothAdapter to mark a
89 // session as inactive in the case of an unexpected change to the adapter
90 // discovery state.
91 void MarkAsInactive();
93 // Whether or not this instance represents an active discovery session.
94 bool active_;
96 // The adapter that created this instance.
97 scoped_refptr<BluetoothAdapter> adapter_;
99 // Filter assigned to this session, if any
100 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter_;
102 // Note: This should remain the last member so it'll be destroyed and
103 // invalidate its weak pointers before any other members are destroyed.
104 base::WeakPtrFactory<BluetoothDiscoverySession> weak_ptr_factory_;
106 DISALLOW_COPY_AND_ASSIGN(BluetoothDiscoverySession);
109 } // namespace device
111 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_SESSION_H_