OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
| 7 |
| 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" |
| 13 |
| 14 namespace proximity_auth { |
| 15 |
| 16 class ConnectionObserver; |
| 17 class WireMessage; |
| 18 |
| 19 // Base class representing a connection with a remote device, which is a |
| 20 // persistent bidirectional channel for sending and receiving wire messages. |
| 21 class Connection { |
| 22 public: |
| 23 enum Status { |
| 24 DISCONNECTED, |
| 25 IN_PROGRESS, |
| 26 CONNECTED, |
| 27 }; |
| 28 |
| 29 // Constructs a connection to the given |remote_device|. |
| 30 explicit Connection(const RemoteDevice& remote_device); |
| 31 ~Connection(); |
| 32 |
| 33 // Returns true iff the connection's status is CONNECTED. |
| 34 bool IsConnected() const; |
| 35 |
| 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); |
| 40 |
| 41 void AddObserver(ConnectionObserver* observer); |
| 42 void RemoveObserver(ConnectionObserver* observer); |
| 43 |
| 44 const RemoteDevice& remote_device() const { return remote_device_; } |
| 45 |
| 46 // Abstract methods that subclasses should implement: |
| 47 |
| 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; |
| 51 |
| 52 // Attempts to connect to the remote device if not already connected. |
| 53 virtual void Connect() = 0; |
| 54 |
| 55 // Disconnects from the remote device. |
| 56 virtual void Disconnect() = 0; |
| 57 |
| 58 protected: |
| 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); |
| 62 |
| 63 Status status() const { return status_; } |
| 64 |
| 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); |
| 68 |
| 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); |
| 72 |
| 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 |
| 75 // in progress. |
| 76 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0; |
| 77 |
| 78 // Returns |true| iff the |received_bytes_| are long enough to contain a |
| 79 // complete wire message. Exposed for testing. |
| 80 virtual bool HasReceivedCompleteMessage(); |
| 81 |
| 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(); |
| 85 |
| 86 private: |
| 87 // The remote device corresponding to this connection. |
| 88 const RemoteDevice remote_device_; |
| 89 |
| 90 // The current status of the connection. |
| 91 Status status_; |
| 92 |
| 93 // The registered observers of the connection. |
| 94 ObserverList<ConnectionObserver> observers_; |
| 95 |
| 96 // A temporary buffer storing bytes received before a received message can be |
| 97 // fully constructed. |
| 98 std::string received_bytes_; |
| 99 |
| 100 // Whether a message is currently in the process of being sent. |
| 101 bool is_sending_message_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 104 }; |
| 105 |
| 106 } // namespace proximity_auth |
| 107 |
| 108 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
OLD | NEW |