OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H | 6 #define COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
13 #include "components/proximity_auth/remote_device.h" | 13 #include "components/cryptauth/remote_device.h" |
14 | 14 |
15 namespace proximity_auth { | 15 namespace proximity_auth { |
16 | 16 |
17 class ConnectionObserver; | 17 class ConnectionObserver; |
18 class WireMessage; | 18 class WireMessage; |
19 | 19 |
20 // Base class representing a connection with a remote device, which is a | 20 // Base class representing a connection with a remote device, which is a |
21 // persistent bidirectional channel for sending and receiving wire messages. | 21 // persistent bidirectional channel for sending and receiving wire messages. |
22 class Connection { | 22 class Connection { |
23 public: | 23 public: |
24 enum Status { | 24 enum Status { |
25 DISCONNECTED, | 25 DISCONNECTED, |
26 IN_PROGRESS, | 26 IN_PROGRESS, |
27 CONNECTED, | 27 CONNECTED, |
28 }; | 28 }; |
29 | 29 |
30 // Constructs a connection to the given |remote_device|. | 30 // Constructs a connection to the given |remote_device|. |
31 explicit Connection(const RemoteDevice& remote_device); | 31 explicit Connection(const cryptauth::RemoteDevice& remote_device); |
32 virtual ~Connection(); | 32 virtual ~Connection(); |
33 | 33 |
34 // Returns true iff the connection's status is CONNECTED. | 34 // Returns true iff the connection's status is CONNECTED. |
35 bool IsConnected() const; | 35 bool IsConnected() const; |
36 | 36 |
37 // Returns true iff the connection is currently sending a message. | 37 // Returns true iff the connection is currently sending a message. |
38 bool is_sending_message() const { return is_sending_message_; } | 38 bool is_sending_message() const { return is_sending_message_; } |
39 | 39 |
40 // Sends a message to the remote device. | 40 // Sends a message to the remote device. |
41 // |OnSendCompleted()| will be called for all observers upon completion with | 41 // |OnSendCompleted()| will be called for all observers upon completion with |
42 // either success or failure. | 42 // either success or failure. |
43 void SendMessage(std::unique_ptr<WireMessage> message); | 43 void SendMessage(std::unique_ptr<WireMessage> message); |
44 | 44 |
45 void AddObserver(ConnectionObserver* observer); | 45 void AddObserver(ConnectionObserver* observer); |
46 void RemoveObserver(ConnectionObserver* observer); | 46 void RemoveObserver(ConnectionObserver* observer); |
47 | 47 |
48 const RemoteDevice& remote_device() const { return remote_device_; } | 48 const cryptauth::RemoteDevice& remote_device() const { |
| 49 return remote_device_; |
| 50 } |
49 | 51 |
50 // Abstract methods that subclasses should implement: | 52 // Abstract methods that subclasses should implement: |
51 | 53 |
52 // Attempts to connect to the remote device if not already connected. | 54 // Attempts to connect to the remote device if not already connected. |
53 virtual void Connect() = 0; | 55 virtual void Connect() = 0; |
54 | 56 |
55 // Disconnects from the remote device. | 57 // Disconnects from the remote device. |
56 virtual void Disconnect() = 0; | 58 virtual void Disconnect() = 0; |
57 | 59 |
58 // The bluetooth address of the connected device. | 60 // The bluetooth address of the connected device. |
(...skipping 26 matching lines...) Expand all Loading... |
85 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, | 87 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, |
86 // or NULL if the message is malformed. Sets |is_incomplete_message| to true | 88 // or NULL if the message is malformed. Sets |is_incomplete_message| to true |
87 // if the |serialized_message| does not have enough data to parse the header, | 89 // if the |serialized_message| does not have enough data to parse the header, |
88 // or if the message length encoded in the message header exceeds the size of | 90 // or if the message length encoded in the message header exceeds the size of |
89 // the |serialized_message|. Exposed for testing. | 91 // the |serialized_message|. Exposed for testing. |
90 virtual std::unique_ptr<WireMessage> DeserializeWireMessage( | 92 virtual std::unique_ptr<WireMessage> DeserializeWireMessage( |
91 bool* is_incomplete_message); | 93 bool* is_incomplete_message); |
92 | 94 |
93 private: | 95 private: |
94 // The remote device corresponding to this connection. | 96 // The remote device corresponding to this connection. |
95 const RemoteDevice remote_device_; | 97 const cryptauth::RemoteDevice remote_device_; |
96 | 98 |
97 // The current status of the connection. | 99 // The current status of the connection. |
98 Status status_; | 100 Status status_; |
99 | 101 |
100 // The registered observers of the connection. | 102 // The registered observers of the connection. |
101 base::ObserverList<ConnectionObserver> observers_; | 103 base::ObserverList<ConnectionObserver> observers_; |
102 | 104 |
103 // A temporary buffer storing bytes received before a received message can be | 105 // A temporary buffer storing bytes received before a received message can be |
104 // fully constructed. | 106 // fully constructed. |
105 std::string received_bytes_; | 107 std::string received_bytes_; |
106 | 108 |
107 // Whether a message is currently in the process of being sent. | 109 // Whether a message is currently in the process of being sent. |
108 bool is_sending_message_; | 110 bool is_sending_message_; |
109 | 111 |
110 DISALLOW_COPY_AND_ASSIGN(Connection); | 112 DISALLOW_COPY_AND_ASSIGN(Connection); |
111 }; | 113 }; |
112 | 114 |
113 } // namespace proximity_auth | 115 } // namespace proximity_auth |
114 | 116 |
115 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H | 117 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
OLD | NEW |