blob: 532e03d93769592d6e38ea56cf8b492feaa7ffce [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
sacomotoe4ca7e262015-09-30 14:27:5657 // The bluetooth address of the connected device.
58 virtual std::string GetDeviceAddress();
59
tengsda506c82015-08-03 22:49:0860 Status status() const { return status_; }
61
isherman06c6a9a72014-09-10 05:48:2162 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.
isherman83d08a292014-09-26 03:32:4465 // Virtual for testing.
66 virtual void SetStatus(Status status);
isherman06c6a9a72014-09-10 05:48:2167
isherman06c6a9a72014-09-10 05:48:2168 // Called after attempting to send bytes over the connection, whether the
69 // message was successfully sent or not.
isherman83d08a292014-09-26 03:32:4470 // Virtual for testing.
71 virtual void OnDidSendMessage(const WireMessage& message, bool success);
isherman06c6a9a72014-09-10 05:48:2172
73 // Called when bytes are read from the connection. There should not be a send
74 // in progress when this function is called.
isherman83d08a292014-09-26 03:32:4475 // Virtual for testing.
76 virtual void OnBytesReceived(const std::string& bytes);
isherman06c6a9a72014-09-10 05:48:2177
78 // Sends bytes over the connection. The implementing class should call
tengs51902a32015-07-27 22:01:2979 // OnDidSendMessage() once the send succeeds or fails. At most one send will
80 // be
isherman06c6a9a72014-09-10 05:48:2181 // in progress.
82 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0;
83
isherman06c6a9a72014-09-10 05:48:2184 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
isherman3ca088e12014-09-16 00:21:0385 // 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);
isherman06c6a9a72014-09-10 05:48:2191
isherman06c6a9a72014-09-10 05:48:2192 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.
brettw236d3172015-06-03 16:31:43100 base::ObserverList<ConnectionObserver> observers_;
isherman06c6a9a72014-09-10 05:48:21101
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