Index: components/proximity_auth/connection.h |
diff --git a/components/proximity_auth/connection.h b/components/proximity_auth/connection.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..24659b388b74b96b20126086d04138423337fdc1 |
--- /dev/null |
+++ b/components/proximity_auth/connection.h |
@@ -0,0 +1,116 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
+#define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
+ |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/observer_list.h" |
+#include "components/proximity_auth/remote_device.h" |
+#include "net/base/io_buffer.h" |
+ |
+namespace proximity_auth { |
+ |
+class ConnectionObserver; |
+class PermitMessage; |
+ |
+// Base class representing a connection with a remote device, which is a |
+// persistent bidirectional channel for sending and receiving permit messages. |
+class Connection { |
+ public: |
+ enum Status { |
+ DISCONNECTED, |
+ IN_PROGRESS, |
+ CONNECTED, |
+ }; |
+ |
+ // Constructs a connection to the given |remote_device|. |
+ explicit Connection(const RemoteDevice& remote_device); |
+ ~Connection(); |
+ |
+ // Returns true iff the connection's status is CONNECTED. |
+ bool IsConnected() const; |
+ |
+ // Sends a message to the remote device. |
Tim Song
2014/09/05 00:30:33
Also make a note that OnSendCompleted will be call
Ilya Sherman
2014/09/05 01:02:11
Done.
|
+ void SendMessage(scoped_ptr<PermitMessage> message); |
+ |
+ void AddObserver(ConnectionObserver* observer); |
+ void RemoveObserver(ConnectionObserver* observer); |
+ |
+ const RemoteDevice& remote_device() const { return remote_device_; } |
+ |
+ // Abstract methods that subclasses should implement: |
+ |
+ // Pauses or unpauses the handling of incoming messages. Pausing allows the |
+ // user of the connection to add or remove observers without missing messages. |
+ virtual void SetPaused(bool paused) = 0; |
+ |
+ // Attempts to connect to the remote device if not already connected. |
+ virtual void Connect() = 0; |
+ |
+ // Disconnects from the remote device. |
+ virtual void Disconnect() = 0; |
+ |
+ protected: |
+ // Sets the connection's status to |status|. If this is different from the |
+ // previous status, notifies observers of the change in status. |
+ void SetStatus(Status status); |
+ |
+ Status status() const { return status_; } |
+ |
+ // Called after attempting to send bytes over the connection, whether the |
+ // message was successfully sent or not. |
+ void OnDidSendMessage(const PermitMessage& message, bool success); |
+ |
+ // Called when bytes are read from the connection. There should not be a send |
+ // in progress when this function is called. |
+ void OnBytesReceived(scoped_refptr<net::IOBuffer> io_buffer, |
Tim Song
2014/09/05 00:30:34
Use vectors for the buffers so we don't have to th
Ilya Sherman
2014/09/05 01:02:11
Used strings (see also my response to your related
|
+ int bytes_received); |
+ |
+ // Cancels an in-progress connection. |
+ virtual void CancelConnectionAttempt() = 0; |
Tim Song
2014/09/05 00:30:34
This can be public or just delete it since we don'
Ilya Sherman
2014/09/05 01:02:11
Done.
|
+ |
+ // Sends bytes over the connection. The implementing class should call |
+ // OnSendCompleted() once the send succeeds or fails. At most one send will be |
+ // in progress. |
+ virtual void SendMessageImpl(scoped_ptr<PermitMessage> message) = 0; |
+ |
+ // Returns |true| iff the |received_bytes_| are long enough to contain a |
+ // complete permit message. Exposed for testing. |
+ virtual bool HasReceivedCompleteMessage(); |
+ |
+ // Deserializes the |recieved_bytes_| and returns the resulting PermitMessage, |
+ // or NULL if the message is malformed. Exposed for testing. |
+ virtual scoped_ptr<PermitMessage> DeserializePermitMessage(); |
Tim Song
2014/09/05 00:30:33
Are these functions temporarily exposed until the
Ilya Sherman
2014/09/05 01:02:11
I'd prefer to test the connection code independent
|
+ |
+ // Exposed for testing. |
+ scoped_refptr<net::GrowableIOBuffer> received_bytes() { |
+ return received_bytes_; |
+ } |
+ |
+ private: |
+ // The remote device corresponding to this connection. |
+ const RemoteDevice remote_device_; |
+ |
+ // The current status of the connection. |
+ Status status_; |
+ |
+ // The registered observers of the connection. |
+ ObserverList<ConnectionObserver> observers_; |
+ |
+ // A temporary buffer storing bytes received before a received message can be |
+ // fully constructed. |
+ scoped_refptr<net::GrowableIOBuffer> received_bytes_; |
+ |
+ // Whether a message is currently in the process of being sent. |
+ bool is_sending_message_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Connection); |
+}; |
+ |
+} // namespace proximity_auth |
+ |
+#endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |