isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 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); |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 31 | virtual ~Connection(); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 32 | |
| 33 | // Returns true iff the connection's status is CONNECTED. |
| 34 | bool IsConnected() const; |
| 35 | |
isherman | 40e54e0 | 2014-10-15 02:20:07 | [diff] [blame] | 36 | // Returns true iff the connection is currently sending a message. |
| 37 | bool is_sending_message() const { return is_sending_message_; } |
| 38 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 39 | // Sends a message to the remote device. |
| 40 | // |OnSendCompleted()| will be called for all observers upon completion with |
| 41 | // either success or failure. |
| 42 | void SendMessage(scoped_ptr<WireMessage> message); |
| 43 | |
| 44 | void AddObserver(ConnectionObserver* observer); |
| 45 | void RemoveObserver(ConnectionObserver* observer); |
| 46 | |
| 47 | const RemoteDevice& remote_device() const { return remote_device_; } |
| 48 | |
| 49 | // Abstract methods that subclasses should implement: |
| 50 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 51 | // Attempts to connect to the remote device if not already connected. |
| 52 | virtual void Connect() = 0; |
| 53 | |
| 54 | // Disconnects from the remote device. |
| 55 | virtual void Disconnect() = 0; |
| 56 | |
sacomoto | e4ca7e26 | 2015-09-30 14:27:56 | [diff] [blame^] | 57 | // The bluetooth address of the connected device. |
| 58 | virtual std::string GetDeviceAddress(); |
| 59 | |
tengs | da506c8 | 2015-08-03 22:49:08 | [diff] [blame] | 60 | Status status() const { return status_; } |
| 61 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 62 | protected: |
| 63 | // Sets the connection's status to |status|. If this is different from the |
| 64 | // previous status, notifies observers of the change in status. |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 65 | // Virtual for testing. |
| 66 | virtual void SetStatus(Status status); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 67 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 68 | // Called after attempting to send bytes over the connection, whether the |
| 69 | // message was successfully sent or not. |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 70 | // Virtual for testing. |
| 71 | virtual void OnDidSendMessage(const WireMessage& message, bool success); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 72 | |
| 73 | // Called when bytes are read from the connection. There should not be a send |
| 74 | // in progress when this function is called. |
isherman | 83d08a29 | 2014-09-26 03:32:44 | [diff] [blame] | 75 | // Virtual for testing. |
| 76 | virtual void OnBytesReceived(const std::string& bytes); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 77 | |
| 78 | // Sends bytes over the connection. The implementing class should call |
tengs | 51902a3 | 2015-07-27 22:01:29 | [diff] [blame] | 79 | // OnDidSendMessage() once the send succeeds or fails. At most one send will |
| 80 | // be |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 81 | // in progress. |
| 82 | virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0; |
| 83 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 84 | // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, |
isherman | 3ca088e1 | 2014-09-16 00:21:03 | [diff] [blame] | 85 | // or NULL if the message is malformed. Sets |is_incomplete_message| to true |
| 86 | // if the |serialized_message| does not have enough data to parse the header, |
| 87 | // or if the message length encoded in the message header exceeds the size of |
| 88 | // the |serialized_message|. Exposed for testing. |
| 89 | virtual scoped_ptr<WireMessage> DeserializeWireMessage( |
| 90 | bool* is_incomplete_message); |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 91 | |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 92 | private: |
| 93 | // The remote device corresponding to this connection. |
| 94 | const RemoteDevice remote_device_; |
| 95 | |
| 96 | // The current status of the connection. |
| 97 | Status status_; |
| 98 | |
| 99 | // The registered observers of the connection. |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 100 | base::ObserverList<ConnectionObserver> observers_; |
isherman | 06c6a9a7 | 2014-09-10 05:48:21 | [diff] [blame] | 101 | |
| 102 | // A temporary buffer storing bytes received before a received message can be |
| 103 | // fully constructed. |
| 104 | std::string received_bytes_; |
| 105 | |
| 106 | // Whether a message is currently in the process of being sent. |
| 107 | bool is_sending_message_; |
| 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 110 | }; |
| 111 | |
| 112 | } // namespace proximity_auth |
| 113 | |
| 114 | #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |