blob: dbc731d728aba7cfb61b5e68e9d455c11aa47d42 [file] [log] [blame]
isherman06c6a9a72014-09-10 05:48:211// 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
khorimotof7a6aff2017-01-19 22:50:475#ifndef COMPONENTS_CRYPTAUTH_CONNECTION_H_
6#define COMPONENTS_CRYPTAUTH_CONNECTION_H_
isherman06c6a9a72014-09-10 05:48:217
dcheng2f012692016-04-21 00:19:348#include <memory>
9
isherman06c6a9a72014-09-10 05:48:2110#include "base/macros.h"
11#include "base/memory/ref_counted.h"
isherman06c6a9a72014-09-10 05:48:2112#include "base/observer_list.h"
khorimotode98b9e2016-12-07 22:28:5513#include "components/cryptauth/remote_device.h"
isherman06c6a9a72014-09-10 05:48:2114
hansberrye7ad3892016-12-19 22:19:2115namespace cryptauth {
isherman06c6a9a72014-09-10 05:48:2116
17class ConnectionObserver;
18class WireMessage;
19
20// Base class representing a connection with a remote device, which is a
21// persistent bidirectional channel for sending and receiving wire messages.
22class Connection {
23 public:
24 enum Status {
25 DISCONNECTED,
26 IN_PROGRESS,
27 CONNECTED,
28 };
29
30 // Constructs a connection to the given |remote_device|.
khorimotof7a6aff2017-01-19 22:50:4731 explicit Connection(const RemoteDevice& remote_device);
isherman83d08a292014-09-26 03:32:4432 virtual ~Connection();
isherman06c6a9a72014-09-10 05:48:2133
34 // Returns true iff the connection's status is CONNECTED.
35 bool IsConnected() const;
36
isherman40e54e02014-10-15 02:20:0737 // Returns true iff the connection is currently sending a message.
38 bool is_sending_message() const { return is_sending_message_; }
39
isherman06c6a9a72014-09-10 05:48:2140 // Sends a message to the remote device.
41 // |OnSendCompleted()| will be called for all observers upon completion with
42 // either success or failure.
dcheng2f012692016-04-21 00:19:3443 void SendMessage(std::unique_ptr<WireMessage> message);
isherman06c6a9a72014-09-10 05:48:2144
khorimotof1ec4312017-01-31 01:04:4545 virtual void AddObserver(ConnectionObserver* observer);
46 virtual void RemoveObserver(ConnectionObserver* observer);
isherman06c6a9a72014-09-10 05:48:2147
khorimotof7a6aff2017-01-19 22:50:4748 const RemoteDevice& remote_device() const {
khorimotode98b9e2016-12-07 22:28:5549 return remote_device_;
50 }
isherman06c6a9a72014-09-10 05:48:2151
52 // Abstract methods that subclasses should implement:
53
isherman06c6a9a72014-09-10 05:48:2154 // Attempts to connect to the remote device if not already connected.
55 virtual void Connect() = 0;
56
57 // Disconnects from the remote device.
58 virtual void Disconnect() = 0;
59
sacomotoe4ca7e262015-09-30 14:27:5660 // The bluetooth address of the connected device.
61 virtual std::string GetDeviceAddress();
62
tengsda506c82015-08-03 22:49:0863 Status status() const { return status_; }
64
isherman06c6a9a72014-09-10 05:48:2165 protected:
66 // Sets the connection's status to |status|. If this is different from the
67 // previous status, notifies observers of the change in status.
isherman83d08a292014-09-26 03:32:4468 // Virtual for testing.
69 virtual void SetStatus(Status status);
isherman06c6a9a72014-09-10 05:48:2170
isherman06c6a9a72014-09-10 05:48:2171 // Called after attempting to send bytes over the connection, whether the
72 // message was successfully sent or not.
isherman83d08a292014-09-26 03:32:4473 // Virtual for testing.
74 virtual void OnDidSendMessage(const WireMessage& message, bool success);
isherman06c6a9a72014-09-10 05:48:2175
76 // Called when bytes are read from the connection. There should not be a send
77 // in progress when this function is called.
isherman83d08a292014-09-26 03:32:4478 // Virtual for testing.
79 virtual void OnBytesReceived(const std::string& bytes);
isherman06c6a9a72014-09-10 05:48:2180
81 // Sends bytes over the connection. The implementing class should call
tengs51902a32015-07-27 22:01:2982 // OnDidSendMessage() once the send succeeds or fails. At most one send will
83 // be
isherman06c6a9a72014-09-10 05:48:2184 // in progress.
dcheng2f012692016-04-21 00:19:3485 virtual void SendMessageImpl(std::unique_ptr<WireMessage> message) = 0;
isherman06c6a9a72014-09-10 05:48:2186
isherman06c6a9a72014-09-10 05:48:2187 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
isherman3ca088e12014-09-16 00:21:0388 // or NULL if the message is malformed. Sets |is_incomplete_message| to true
89 // if the |serialized_message| does not have enough data to parse the header,
90 // or if the message length encoded in the message header exceeds the size of
91 // the |serialized_message|. Exposed for testing.
dcheng2f012692016-04-21 00:19:3492 virtual std::unique_ptr<WireMessage> DeserializeWireMessage(
isherman3ca088e12014-09-16 00:21:0393 bool* is_incomplete_message);
isherman06c6a9a72014-09-10 05:48:2194
isherman06c6a9a72014-09-10 05:48:2195 private:
96 // The remote device corresponding to this connection.
khorimotof7a6aff2017-01-19 22:50:4797 const RemoteDevice remote_device_;
isherman06c6a9a72014-09-10 05:48:2198
99 // The current status of the connection.
100 Status status_;
101
102 // The registered observers of the connection.
brettw236d3172015-06-03 16:31:43103 base::ObserverList<ConnectionObserver> observers_;
isherman06c6a9a72014-09-10 05:48:21104
105 // A temporary buffer storing bytes received before a received message can be
106 // fully constructed.
107 std::string received_bytes_;
108
109 // Whether a message is currently in the process of being sent.
110 bool is_sending_message_;
111
112 DISALLOW_COPY_AND_ASSIGN(Connection);
113};
114
hansberrye7ad3892016-12-19 22:19:21115} // namespace cryptauth
isherman06c6a9a72014-09-10 05:48:21116
khorimotof7a6aff2017-01-19 22:50:47117#endif // COMPONENTS_CRYPTAUTH_CONNECTION_H_