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 "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 #include "net/base/io_buffer.h" | |
14 | |
15 namespace proximity_auth { | |
16 | |
17 class ConnectionObserver; | |
18 class PermitMessage; | |
19 | |
20 // Base class representing a connection with a remote device, which is a | |
21 // persistent bidirectional channel for sending and receiving permit 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 RemoteDevice& remote_device); | |
32 ~Connection(); | |
33 | |
34 // Returns true iff the connection's status is CONNECTED. | |
35 bool IsConnected() const; | |
36 | |
37 // Sends a message to the remote device. | |
Tim Song
2014/09/05 00:30:33
Also make a note that OnSendCompleted will be call
Ilya Sherman
2014/09/05 01:02:11
Done.
| |
38 void SendMessage(scoped_ptr<PermitMessage> message); | |
39 | |
40 void AddObserver(ConnectionObserver* observer); | |
41 void RemoveObserver(ConnectionObserver* observer); | |
42 | |
43 const RemoteDevice& remote_device() const { return remote_device_; } | |
44 | |
45 // Abstract methods that subclasses should implement: | |
46 | |
47 // Pauses or unpauses the handling of incoming messages. Pausing allows the | |
48 // user of the connection to add or remove observers without missing messages. | |
49 virtual void SetPaused(bool paused) = 0; | |
50 | |
51 // 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 | |
57 protected: | |
58 // Sets the connection's status to |status|. If this is different from the | |
59 // previous status, notifies observers of the change in status. | |
60 void SetStatus(Status status); | |
61 | |
62 Status status() const { return status_; } | |
63 | |
64 // Called after attempting to send bytes over the connection, whether the | |
65 // message was successfully sent or not. | |
66 void OnDidSendMessage(const PermitMessage& message, bool success); | |
67 | |
68 // Called when bytes are read from the connection. There should not be a send | |
69 // in progress when this function is called. | |
70 void OnBytesReceived(scoped_refptr<net::IOBuffer> io_buffer, | |
Tim Song
2014/09/05 00:30:34
Use vectors for the buffers so we don't have to th
Ilya Sherman
2014/09/05 01:02:11
Used strings (see also my response to your related
| |
71 int bytes_received); | |
72 | |
73 // Cancels an in-progress connection. | |
74 virtual void CancelConnectionAttempt() = 0; | |
Tim Song
2014/09/05 00:30:34
This can be public or just delete it since we don'
Ilya Sherman
2014/09/05 01:02:11
Done.
| |
75 | |
76 // Sends bytes over the connection. The implementing class should call | |
77 // OnSendCompleted() once the send succeeds or fails. At most one send will be | |
78 // in progress. | |
79 virtual void SendMessageImpl(scoped_ptr<PermitMessage> message) = 0; | |
80 | |
81 // Returns |true| iff the |received_bytes_| are long enough to contain a | |
82 // complete permit message. Exposed for testing. | |
83 virtual bool HasReceivedCompleteMessage(); | |
84 | |
85 // Deserializes the |recieved_bytes_| and returns the resulting PermitMessage, | |
86 // or NULL if the message is malformed. Exposed for testing. | |
87 virtual scoped_ptr<PermitMessage> DeserializePermitMessage(); | |
Tim Song
2014/09/05 00:30:33
Are these functions temporarily exposed until the
Ilya Sherman
2014/09/05 01:02:11
I'd prefer to test the connection code independent
| |
88 | |
89 // Exposed for testing. | |
90 scoped_refptr<net::GrowableIOBuffer> received_bytes() { | |
91 return received_bytes_; | |
92 } | |
93 | |
94 private: | |
95 // The remote device corresponding to this connection. | |
96 const RemoteDevice remote_device_; | |
97 | |
98 // The current status of the connection. | |
99 Status status_; | |
100 | |
101 // The registered observers of the connection. | |
102 ObserverList<ConnectionObserver> observers_; | |
103 | |
104 // A temporary buffer storing bytes received before a received message can be | |
105 // fully constructed. | |
106 scoped_refptr<net::GrowableIOBuffer> received_bytes_; | |
107 | |
108 // Whether a message is currently in the process of being sent. | |
109 bool is_sending_message_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(Connection); | |
112 }; | |
113 | |
114 } // namespace proximity_auth | |
115 | |
116 #endif // COMPONENTS_PROXIMITY_AUTH_CONNECTION_H | |
OLD | NEW |