blob: cbaf3a51f5329b18c19fe619f3370f2e1dd7156a [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
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
14namespace proximity_auth {
15
16class ConnectionObserver;
17class 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.
21class 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);
isherman83d08a292014-09-26 03:32:4431 virtual ~Connection();
isherman06c6a9a72014-09-10 05:48:2132
33 // Returns true iff the connection's status is CONNECTED.
34 bool IsConnected() const;
35
isherman40e54e02014-10-15 02:20:0736 // Returns true iff the connection is currently sending a message.
37 bool is_sending_message() const { return is_sending_message_; }
38
isherman06c6a9a72014-09-10 05:48:2139 // 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
isherman06c6a9a72014-09-10 05:48:2151 // 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
tengsda506c82015-08-03 22:49:0857 Status status() const { return status_; }
58
isherman06c6a9a72014-09-10 05:48:2159 protected:
60 // Sets the connection's status to |status|. If this is different from the
61 // previous status, notifies observers of the change in status.
isherman83d08a292014-09-26 03:32:4462 // Virtual for testing.
63 virtual void SetStatus(Status status);
isherman06c6a9a72014-09-10 05:48:2164
isherman06c6a9a72014-09-10 05:48:2165 // Called after attempting to send bytes over the connection, whether the
66 // message was successfully sent or not.
isherman83d08a292014-09-26 03:32:4467 // Virtual for testing.
68 virtual void OnDidSendMessage(const WireMessage& message, bool success);
isherman06c6a9a72014-09-10 05:48:2169
70 // Called when bytes are read from the connection. There should not be a send
71 // in progress when this function is called.
isherman83d08a292014-09-26 03:32:4472 // Virtual for testing.
73 virtual void OnBytesReceived(const std::string& bytes);
isherman06c6a9a72014-09-10 05:48:2174
75 // Sends bytes over the connection. The implementing class should call
tengs51902a32015-07-27 22:01:2976 // OnDidSendMessage() once the send succeeds or fails. At most one send will
77 // be
isherman06c6a9a72014-09-10 05:48:2178 // in progress.
79 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0;
80
isherman06c6a9a72014-09-10 05:48:2181 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
isherman3ca088e12014-09-16 00:21:0382 // or NULL if the message is malformed. Sets |is_incomplete_message| to true
83 // if the |serialized_message| does not have enough data to parse the header,
84 // or if the message length encoded in the message header exceeds the size of
85 // the |serialized_message|. Exposed for testing.
86 virtual scoped_ptr<WireMessage> DeserializeWireMessage(
87 bool* is_incomplete_message);
isherman06c6a9a72014-09-10 05:48:2188
sacomoto574c16372015-05-14 12:41:2789 // Exposed so it is possible to override DeserializeWireMessage in
90 // BluetoothLowEnergyConnection class, while mantaining the same
91 // functionality.
92 // TODO(sacomoto): remove this when FakeWireMessage is not needed anymore.
93 const std::string& received_bytes() { return received_bytes_; }
94
isherman06c6a9a72014-09-10 05:48:2195 private:
96 // The remote device corresponding to this connection.
97 const RemoteDevice remote_device_;
98
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
115} // namespace proximity_auth
116
117#endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H