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_PROXIMITY_AUTH_CONNECTION_H
6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "components/proximity_auth/remote_device.h"
14 namespace proximity_auth
{
16 class ConnectionObserver
;
19 // Base class representing a connection with a remote device, which is a
20 // persistent bidirectional channel for sending and receiving wire messages.
29 // Constructs a connection to the given |remote_device|.
30 explicit Connection(const RemoteDevice
& remote_device
);
33 // Returns true iff the connection's status is CONNECTED.
34 bool IsConnected() const;
36 // Sends a message to the remote device.
37 // |OnSendCompleted()| will be called for all observers upon completion with
38 // either success or failure.
39 void SendMessage(scoped_ptr
<WireMessage
> message
);
41 void AddObserver(ConnectionObserver
* observer
);
42 void RemoveObserver(ConnectionObserver
* observer
);
44 const RemoteDevice
& remote_device() const { return remote_device_
; }
46 // Abstract methods that subclasses should implement:
48 // Pauses or unpauses the handling of incoming messages. Pausing allows the
49 // user of the connection to add or remove observers without missing messages.
50 virtual void SetPaused(bool paused
) = 0;
52 // Attempts to connect to the remote device if not already connected.
53 virtual void Connect() = 0;
55 // Disconnects from the remote device.
56 virtual void Disconnect() = 0;
59 // Sets the connection's status to |status|. If this is different from the
60 // previous status, notifies observers of the change in status.
61 void SetStatus(Status status
);
63 Status
status() const { return status_
; }
65 // Called after attempting to send bytes over the connection, whether the
66 // message was successfully sent or not.
67 void OnDidSendMessage(const WireMessage
& message
, bool success
);
69 // Called when bytes are read from the connection. There should not be a send
70 // in progress when this function is called.
71 void OnBytesReceived(const std::string
& bytes
);
73 // Sends bytes over the connection. The implementing class should call
74 // OnSendCompleted() once the send succeeds or fails. At most one send will be
76 virtual void SendMessageImpl(scoped_ptr
<WireMessage
> message
) = 0;
78 // Returns |true| iff the |received_bytes_| are long enough to contain a
79 // complete wire message. Exposed for testing.
80 virtual bool HasReceivedCompleteMessage();
82 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
83 // or NULL if the message is malformed. Exposed for testing.
84 virtual scoped_ptr
<WireMessage
> DeserializeWireMessage();
87 // The remote device corresponding to this connection.
88 const RemoteDevice remote_device_
;
90 // The current status of the connection.
93 // The registered observers of the connection.
94 ObserverList
<ConnectionObserver
> observers_
;
96 // A temporary buffer storing bytes received before a received message can be
98 std::string received_bytes_
;
100 // Whether a message is currently in the process of being sent.
101 bool is_sending_message_
;
103 DISALLOW_COPY_AND_ASSIGN(Connection
);
106 } // namespace proximity_auth
108 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H