OLD | NEW |
| (Empty) |
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 <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/observer_list.h" | |
13 #include "components/cryptauth/remote_device.h" | |
14 | |
15 namespace proximity_auth { | |
16 | |
17 class ConnectionObserver; | |
18 class 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. | |
22 class Connection { | |
23 public: | |
24 enum Status { | |
25 DISCONNECTED, | |
26 IN_PROGRESS, | |
27 CONNECTED, | |
28 }; | |
29 | |
30 // Constructs a connection to the given |remote_device|. | |
31 explicit Connection(const cryptauth::RemoteDevice& remote_device); | |
32 virtual ~Connection(); | |
33 | |
34 // Returns true iff the connection's status is CONNECTED. | |
35 bool IsConnected() const; | |
36 | |
37 // Returns true iff the connection is currently sending a message. | |
38 bool is_sending_message() const { return is_sending_message_; } | |
39 | |
40 // Sends a message to the remote device. | |
41 // |OnSendCompleted()| will be called for all observers upon completion with | |
42 // either success or failure. | |
43 void SendMessage(std::unique_ptr<WireMessage> message); | |
44 | |
45 void AddObserver(ConnectionObserver* observer); | |
46 void RemoveObserver(ConnectionObserver* observer); | |
47 | |
48 const cryptauth::RemoteDevice& remote_device() const { | |
49 return remote_device_; | |
50 } | |
51 | |
52 // Abstract methods that subclasses should implement: | |
53 | |
54 // 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 | |
60 // The bluetooth address of the connected device. | |
61 virtual std::string GetDeviceAddress(); | |
62 | |
63 Status status() const { return status_; } | |
64 | |
65 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. | |
68 // Virtual for testing. | |
69 virtual void SetStatus(Status status); | |
70 | |
71 // Called after attempting to send bytes over the connection, whether the | |
72 // message was successfully sent or not. | |
73 // Virtual for testing. | |
74 virtual void OnDidSendMessage(const WireMessage& message, bool success); | |
75 | |
76 // Called when bytes are read from the connection. There should not be a send | |
77 // in progress when this function is called. | |
78 // Virtual for testing. | |
79 virtual void OnBytesReceived(const std::string& bytes); | |
80 | |
81 // Sends bytes over the connection. The implementing class should call | |
82 // OnDidSendMessage() once the send succeeds or fails. At most one send will | |
83 // be | |
84 // in progress. | |
85 virtual void SendMessageImpl(std::unique_ptr<WireMessage> message) = 0; | |
86 | |
87 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, | |
88 // 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. | |
92 virtual std::unique_ptr<WireMessage> DeserializeWireMessage( | |
93 bool* is_incomplete_message); | |
94 | |
95 private: | |
96 // The remote device corresponding to this connection. | |
97 const cryptauth::RemoteDevice remote_device_; | |
98 | |
99 // The current status of the connection. | |
100 Status status_; | |
101 | |
102 // The registered observers of the connection. | |
103 base::ObserverList<ConnectionObserver> observers_; | |
104 | |
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 | |
OLD | NEW |