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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
7 | |
8 #include <queue> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "extensions/common/api/cast_channel.h" | |
14 #include "net/base/completion_callback.h" | |
15 | |
16 namespace net { | |
17 class DrainableIOBuffer; | |
18 class IPEndPoint; | |
19 class IOBuffer; | |
20 class DrainableIOBuffer; | |
21 class GrowableIOBuffer; | |
22 } // namespace net | |
23 | |
24 namespace extensions { | |
25 namespace core_api { | |
26 namespace cast_channel { | |
27 class CastMessage; | |
28 struct LastErrors; | |
29 class Logger; | |
30 class MessageFramer; | |
31 | |
32 // TODO(kmarshall): Migrate CastSocket to new interface. | |
33 // Skeletal placeholder for a future implementation of CastSocket. | |
34 // New to this interface are read/write calls that operate on simple buffers. | |
35 // Methods that operate on CastMessage/MessageInfo objects will be removed. | |
36 class CastSocketPlaceholder { | |
37 public: | |
38 CastSocketPlaceholder() {} | |
39 virtual ~CastSocketPlaceholder() {} | |
40 | |
41 virtual int Write(net::IOBuffer* buffer, | |
42 size_t size, | |
43 const net::CompletionCallback& callback) = 0; | |
44 virtual int Read(net::IOBuffer* buf, | |
45 int buf_len, | |
46 const net::CompletionCallback& callback) = 0; | |
47 virtual const net::IPEndPoint& ip_endpoint() const = 0; | |
48 virtual ChannelAuthType channel_auth() const = 0; | |
49 virtual int id() const = 0; | |
50 }; | |
51 | |
52 // Manager class for reading and writing messages to/from a CastSocket. | |
53 class CastTransport { | |
mark a. foltz
2014/09/11 06:47:45
This is a clean API and a vast improvement. Thank
Kevin M
2014/09/11 18:07:58
:)
| |
54 public: | |
55 // Object to be informed of incoming messages and errors. | |
56 class Delegate { | |
57 public: | |
58 // An error occurred on the channel. |last_errors| contains the last errors | |
59 // logged for the channel from the implementation. | |
60 virtual void OnError(const CastSocketPlaceholder* socket, | |
61 ChannelError error_state, | |
62 const LastErrors& last_errors) = 0; | |
63 // A message was received on the channel. | |
64 virtual void OnMessage(const CastSocketPlaceholder* socket, | |
65 const CastMessage& message) = 0; | |
66 | |
67 protected: | |
68 virtual ~Delegate() {} | |
69 }; | |
70 | |
71 CastTransport(CastSocketPlaceholder* socket, | |
72 Delegate* read_delegate, | |
73 scoped_refptr<Logger> logger); | |
74 virtual ~CastTransport(); | |
75 | |
76 // Sends a CastMessage to |socket_|. | |
77 // |message|: The message to send. | |
78 // |callback|: Callback to be invoked when the write operation has finished. | |
79 void SendMessage(const CastMessage& message, | |
80 const net::CompletionCallback& callback); | |
81 | |
82 // Starts reading messages from |socket_|. | |
83 void StartReadLoop(); | |
84 | |
85 // Internal write states. | |
86 enum WriteState { | |
87 WRITE_STATE_NONE, | |
88 WRITE_STATE_WRITE, | |
89 WRITE_STATE_WRITE_COMPLETE, | |
90 WRITE_STATE_DO_CALLBACK, | |
91 WRITE_STATE_ERROR, | |
92 }; | |
93 | |
94 // Internal read states. | |
95 enum ReadState { | |
96 READ_STATE_NONE, | |
97 READ_STATE_READ, | |
98 READ_STATE_READ_COMPLETE, | |
99 READ_STATE_DO_CALLBACK, | |
100 READ_STATE_ERROR, | |
101 }; | |
102 | |
103 private: | |
104 // Holds a message to be written to the socket. |callback| is invoked when the | |
105 // message is fully written or an error occurrs. | |
106 struct WriteRequest { | |
107 explicit WriteRequest(const std::string& namespace_, | |
108 const std::string& payload, | |
109 const net::CompletionCallback& callback); | |
110 ~WriteRequest(); | |
111 | |
112 // Namespace of the serialized message. | |
113 std::string message_namespace; | |
114 // Write completion callback, invoked when the operation has completed or | |
115 // failed. | |
116 net::CompletionCallback callback; | |
117 // Buffer with outgoing data. | |
118 scoped_refptr<net::DrainableIOBuffer> io_buffer; | |
119 }; | |
120 // Terminates all in-flight write callbacks with error code ERR_FAILED. | |
121 void FlushPendingCallbacks(); | |
mark a. foltz
2014/09/11 06:47:45
Maybe FlushWriteQueue()?
Kevin M
2014/09/11 18:07:58
Done.
| |
122 | |
123 ///////////////////////////////////////////////////////////////////////////// | |
124 // Following methods work together to implement write flow. | |
125 // | |
126 // Main method that performs write flow state transitions. | |
127 void DoWriteLoop(int result); | |
128 // Each of the below Do* method is executed in the corresponding | |
129 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | |
130 // DowriteComplete is called, and so on. | |
131 int DoWrite(); | |
132 int DoWriteComplete(int result); | |
133 int DoWriteCallback(); | |
134 int DoWriteError(int result); | |
135 ///////////////////////////////////////////////////////////////////////////// | |
136 | |
137 ///////////////////////////////////////////////////////////////////////////// | |
138 // Following methods work together to implement read flow. | |
139 // | |
140 // Main method that performs write flow state transitions. | |
141 void DoReadLoop(int result); | |
142 // Each of the below Do* method is executed in the corresponding | |
143 // write state. For example when write state is READ_STATE_READ_COMPLETE | |
144 // DoReadComplete is called, and so on. | |
145 int DoRead(); | |
146 int DoReadComplete(int result); | |
147 int DoReadCallback(); | |
148 int DoReadError(int result); | |
149 | |
150 void SetReadState(ReadState read_state); | |
151 void SetWriteState(WriteState write_state); | |
152 void SetErrorState(ChannelError error_state); | |
153 | |
154 // Queue of pending writes. The message at the front of the queue is the one | |
155 // being written. | |
156 std::queue<WriteRequest> write_queue_; | |
157 // Buffer used for read operations. Reused for every read. | |
158 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
159 // Constructs and parses the wire representation of message frames. | |
160 scoped_ptr<MessageFramer> framer_; | |
161 // Last message received on the socket. | |
162 scoped_ptr<CastMessage> current_message_; | |
163 // Socket used for I/O operations. | |
164 CastSocketPlaceholder* socket_; | |
165 // Methods for communicating message receipt and error status to client code. | |
166 Delegate* read_delegate_; | |
167 // Write flow state machine state. | |
168 WriteState write_state_; | |
169 // Read flow state machine state. | |
170 ReadState read_state_; | |
171 // Most recent error that occurred during read or write operation, if any. | |
172 ChannelError error_state_; | |
173 | |
174 scoped_refptr<Logger> logger_; | |
175 base::ThreadChecker thread_checker_; | |
176 }; | |
177 } // namespace cast_channel | |
178 } // namespace core_api | |
179 } // namespace extensions | |
180 | |
181 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
OLD | NEW |