Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / proximity_auth / connection.h
bloba79ce1888c84018ca2f9bf61b8eddfcc67dcd7dc
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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H
6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H
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"
14 namespace proximity_auth {
16 class ConnectionObserver;
17 class WireMessage;
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,
29 // Constructs a connection to the given |remote_device|.
30 explicit Connection(const RemoteDevice& remote_device);
31 virtual ~Connection();
33 // Returns true iff the connection's status is CONNECTED.
34 bool IsConnected() const;
36 // Returns true iff the connection is currently sending a message.
37 bool is_sending_message() const { return is_sending_message_; }
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);
44 void AddObserver(ConnectionObserver* observer);
45 void RemoveObserver(ConnectionObserver* observer);
47 const RemoteDevice& remote_device() const { return remote_device_; }
49 // Abstract methods that subclasses should implement:
51 // Attempts to connect to the remote device if not already connected.
52 virtual void Connect() = 0;
54 // Disconnects from the remote device.
55 virtual void Disconnect() = 0;
57 Status status() const { return status_; }
59 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.
62 // Virtual for testing.
63 virtual void SetStatus(Status status);
65 // Called after attempting to send bytes over the connection, whether the
66 // message was successfully sent or not.
67 // Virtual for testing.
68 virtual void OnDidSendMessage(const WireMessage& message, bool success);
70 // Called when bytes are read from the connection. There should not be a send
71 // in progress when this function is called.
72 // Virtual for testing.
73 virtual void OnBytesReceived(const std::string& bytes);
75 // Sends bytes over the connection. The implementing class should call
76 // OnDidSendMessage() once the send succeeds or fails. At most one send will
77 // be
78 // in progress.
79 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0;
81 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage,
82 // 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);
89 private:
90 // The remote device corresponding to this connection.
91 const RemoteDevice remote_device_;
93 // The current status of the connection.
94 Status status_;
96 // The registered observers of the connection.
97 base::ObserverList<ConnectionObserver> observers_;
99 // A temporary buffer storing bytes received before a received message can be
100 // fully constructed.
101 std::string received_bytes_;
103 // Whether a message is currently in the process of being sent.
104 bool is_sending_message_;
106 DISALLOW_COPY_AND_ASSIGN(Connection);
109 } // namespace proximity_auth
111 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H