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 COMPONENTS_COPRESENCE_SOCKETS_COPRESENCE_PEER_H_
6 #define COMPONENTS_COPRESENCE_SOCKETS_COPRESENCE_PEER_H_
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "device/bluetooth/bluetooth_device.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
19 class BluetoothAdapter
;
20 class BluetoothDevice
;
21 class BluetoothSocket
;
24 namespace copresence_sockets
{
26 class CopresenceSocket
;
28 // A CopresencePeer is an object that can be connected to. Creating a peer
29 // will create a server that will listen on a medium (currently only Bluetooth)
30 // and accept connections.
31 class CopresencePeer
{
33 // Callback with the locator data for the created peer. On a failed create,
34 // the locator string will be empty.
35 typedef base::Callback
<void(const std::string
&)> CreatePeerCallback
;
37 // Callback that accepts connections. The callback takes ownership of the
38 // Copresence socket passed in the parameter.
39 typedef base::Callback
<void(scoped_ptr
<CopresenceSocket
> socket
)>
42 // Create a CopresencePeer and start listening for connections. Once the peer
43 // object is created, the locator data for the object is returned via
44 // create_callback. This locator data can be used to connect to this peer by
45 // remote CopresenceSockets. Any connections accepted by this peer are
46 // delivered as CopresenceSocket objects to accept_callback.
47 CopresencePeer(const CreatePeerCallback
& create_callback
,
48 const AcceptCallback
& accept_callback
);
50 virtual ~CopresencePeer();
52 // This will return a string containing the data needed for a remote
53 // CopresenceSocket to connect to this peer.
54 std::string
GetLocatorData();
57 void OnGetAdapter(scoped_refptr
<device::BluetoothAdapter
> adapter
);
58 void OnCreateService(scoped_refptr
<device::BluetoothSocket
> socket
);
59 void OnCreateServiceError(const std::string
& message
);
61 void OnAccept(const device::BluetoothDevice
* device
,
62 scoped_refptr
<device::BluetoothSocket
> socket
);
63 void OnAcceptError(const std::string
& message
);
65 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
66 scoped_refptr
<device::BluetoothSocket
> server_socket_
;
68 CreatePeerCallback create_callback_
;
69 AcceptCallback accept_callback_
;
71 scoped_ptr
<device::BluetoothDevice::PairingDelegate
> delegate_
;
73 base::WeakPtrFactory
<CopresencePeer
> weak_ptr_factory_
;
75 DISALLOW_COPY_AND_ASSIGN(CopresencePeer
);
78 } // namespace copresence_sockets
80 #endif // COMPONENTS_COPRESENCE_SOCKETS_COPRESENCE_PEER_H_