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> |
| 9 |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
12 #include "components/proximity_auth/remote_device.h" | 13 #include "components/proximity_auth/remote_device.h" |
13 | 14 |
14 namespace proximity_auth { | 15 namespace proximity_auth { |
15 | 16 |
16 class ConnectionObserver; | 17 class ConnectionObserver; |
17 class WireMessage; | 18 class WireMessage; |
18 | 19 |
19 // 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 |
20 // persistent bidirectional channel for sending and receiving wire messages. | 21 // persistent bidirectional channel for sending and receiving wire messages. |
(...skipping 11 matching lines...) Expand all Loading... |
32 | 33 |
33 // Returns true iff the connection's status is CONNECTED. | 34 // Returns true iff the connection's status is CONNECTED. |
34 bool IsConnected() const; | 35 bool IsConnected() const; |
35 | 36 |
36 // Returns true iff the connection is currently sending a message. | 37 // Returns true iff the connection is currently sending a message. |
37 bool is_sending_message() const { return is_sending_message_; } | 38 bool is_sending_message() const { return is_sending_message_; } |
38 | 39 |
39 // Sends a message to the remote device. | 40 // Sends a message to the remote device. |
40 // |OnSendCompleted()| will be called for all observers upon completion with | 41 // |OnSendCompleted()| will be called for all observers upon completion with |
41 // either success or failure. | 42 // either success or failure. |
42 void SendMessage(scoped_ptr<WireMessage> message); | 43 void SendMessage(std::unique_ptr<WireMessage> message); |
43 | 44 |
44 void AddObserver(ConnectionObserver* observer); | 45 void AddObserver(ConnectionObserver* observer); |
45 void RemoveObserver(ConnectionObserver* observer); | 46 void RemoveObserver(ConnectionObserver* observer); |
46 | 47 |
47 const RemoteDevice& remote_device() const { return remote_device_; } | 48 const RemoteDevice& remote_device() const { return remote_device_; } |
48 | 49 |
49 // Abstract methods that subclasses should implement: | 50 // Abstract methods that subclasses should implement: |
50 | 51 |
51 // Attempts to connect to the remote device if not already connected. | 52 // Attempts to connect to the remote device if not already connected. |
52 virtual void Connect() = 0; | 53 virtual void Connect() = 0; |
(...skipping 19 matching lines...) Expand all Loading... |
72 | 73 |
73 // Called when bytes are read from the connection. There should not be a send | 74 // Called when bytes are read from the connection. There should not be a send |
74 // in progress when this function is called. | 75 // in progress when this function is called. |
75 // Virtual for testing. | 76 // Virtual for testing. |
76 virtual void OnBytesReceived(const std::string& bytes); | 77 virtual void OnBytesReceived(const std::string& bytes); |
77 | 78 |
78 // Sends bytes over the connection. The implementing class should call | 79 // Sends bytes over the connection. The implementing class should call |
79 // OnDidSendMessage() once the send succeeds or fails. At most one send will | 80 // OnDidSendMessage() once the send succeeds or fails. At most one send will |
80 // be | 81 // be |
81 // in progress. | 82 // in progress. |
82 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) = 0; | 83 virtual void SendMessageImpl(std::unique_ptr<WireMessage> message) = 0; |
83 | 84 |
84 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, | 85 // Deserializes the |recieved_bytes_| and returns the resulting WireMessage, |
85 // or NULL if the message is malformed. Sets |is_incomplete_message| to true | 86 // 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 // 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 // or if the message length encoded in the message header exceeds the size of |
88 // the |serialized_message|. Exposed for testing. | 89 // the |serialized_message|. Exposed for testing. |
89 virtual scoped_ptr<WireMessage> DeserializeWireMessage( | 90 virtual std::unique_ptr<WireMessage> DeserializeWireMessage( |
90 bool* is_incomplete_message); | 91 bool* is_incomplete_message); |
91 | 92 |
92 private: | 93 private: |
93 // The remote device corresponding to this connection. | 94 // The remote device corresponding to this connection. |
94 const RemoteDevice remote_device_; | 95 const RemoteDevice remote_device_; |
95 | 96 |
96 // The current status of the connection. | 97 // The current status of the connection. |
97 Status status_; | 98 Status status_; |
98 | 99 |
99 // The registered observers of the connection. | 100 // The registered observers of the connection. |
100 base::ObserverList<ConnectionObserver> observers_; | 101 base::ObserverList<ConnectionObserver> observers_; |
101 | 102 |
102 // A temporary buffer storing bytes received before a received message can be | 103 // A temporary buffer storing bytes received before a received message can be |
103 // fully constructed. | 104 // fully constructed. |
104 std::string received_bytes_; | 105 std::string received_bytes_; |
105 | 106 |
106 // Whether a message is currently in the process of being sent. | 107 // Whether a message is currently in the process of being sent. |
107 bool is_sending_message_; | 108 bool is_sending_message_; |
108 | 109 |
109 DISALLOW_COPY_AND_ASSIGN(Connection); | 110 DISALLOW_COPY_AND_ASSIGN(Connection); |
110 }; | 111 }; |
111 | 112 |
112 } // namespace proximity_auth | 113 } // namespace proximity_auth |
113 | 114 |
114 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H | 115 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H |
OLD | NEW |