OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | 5 #ifndef NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ |
6 #define NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | 6 #define NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <queue> | 10 #include <queue> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/location.h" |
14 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
15 #include "base/task.h" | 16 #include "base/task.h" |
16 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
18 #include "net/base/net_log.h" | 19 #include "net/base/net_log.h" |
19 #include "net/base/ssl_config_service.h" | 20 #include "net/base/ssl_config_service.h" |
20 #include "net/base/upload_data_stream.h" | 21 #include "net/base/upload_data_stream.h" |
21 #include "net/http/http_pipelined_connection.h" | 22 #include "net/http/http_pipelined_connection.h" |
22 #include "net/http/http_request_info.h" | 23 #include "net/http/http_request_info.h" |
23 #include "net/http/http_stream_parser.h" | 24 #include "net/http/http_stream_parser.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 119 |
119 private: | 120 private: |
120 enum StreamState { | 121 enum StreamState { |
121 STREAM_CREATED, | 122 STREAM_CREATED, |
122 STREAM_BOUND, | 123 STREAM_BOUND, |
123 STREAM_SENDING, | 124 STREAM_SENDING, |
124 STREAM_SENT, | 125 STREAM_SENT, |
125 STREAM_READ_PENDING, | 126 STREAM_READ_PENDING, |
126 STREAM_ACTIVE, | 127 STREAM_ACTIVE, |
127 STREAM_CLOSED, | 128 STREAM_CLOSED, |
| 129 STREAM_READ_EVICTED, |
128 STREAM_UNUSED, | 130 STREAM_UNUSED, |
129 }; | 131 }; |
130 enum SendRequestState { | 132 enum SendRequestState { |
131 SEND_STATE_NEXT_REQUEST, | 133 SEND_STATE_START_IMMEDIATELY, |
| 134 SEND_STATE_START_NEXT_DEFERRED_REQUEST, |
| 135 SEND_STATE_SEND_ACTIVE_REQUEST, |
132 SEND_STATE_COMPLETE, | 136 SEND_STATE_COMPLETE, |
| 137 SEND_STATE_EVICT_PENDING_REQUESTS, |
133 SEND_STATE_NONE, | 138 SEND_STATE_NONE, |
134 SEND_STATE_UNUSABLE, | |
135 }; | 139 }; |
136 enum ReadHeadersState { | 140 enum ReadHeadersState { |
137 READ_STATE_NEXT_HEADERS, | 141 READ_STATE_START_IMMEDIATELY, |
138 READ_STATE_COMPLETE, | 142 READ_STATE_START_NEXT_DEFERRED_READ, |
| 143 READ_STATE_READ_HEADERS, |
| 144 READ_STATE_READ_HEADERS_COMPLETE, |
139 READ_STATE_WAITING_FOR_CLOSE, | 145 READ_STATE_WAITING_FOR_CLOSE, |
140 READ_STATE_STREAM_CLOSED, | 146 READ_STATE_STREAM_CLOSED, |
141 READ_STATE_NONE, | 147 READ_STATE_NONE, |
142 READ_STATE_UNUSABLE, | 148 READ_STATE_EVICT_PENDING_READS, |
143 }; | 149 }; |
144 | 150 |
145 struct DeferredSendRequest { | 151 struct PendingSendRequest { |
146 DeferredSendRequest(); | 152 PendingSendRequest(); |
147 ~DeferredSendRequest(); | 153 ~PendingSendRequest(); |
148 | 154 |
149 int pipeline_id; | 155 int pipeline_id; |
150 std::string request_line; | 156 std::string request_line; |
151 HttpRequestHeaders headers; | 157 HttpRequestHeaders headers; |
152 UploadDataStream* request_body; | 158 UploadDataStream* request_body; |
153 HttpResponseInfo* response; | 159 HttpResponseInfo* response; |
154 OldCompletionCallback* callback; | 160 OldCompletionCallback* callback; |
155 }; | 161 }; |
156 | 162 |
157 struct StreamInfo { | 163 struct StreamInfo { |
(...skipping 15 matching lines...) Expand all Loading... |
173 // requests. | 179 // requests. |
174 void ActivatePipeline(); | 180 void ActivatePipeline(); |
175 | 181 |
176 // Responsible for sending one request at a time and waiting until each | 182 // Responsible for sending one request at a time and waiting until each |
177 // comepletes. | 183 // comepletes. |
178 int DoSendRequestLoop(int result); | 184 int DoSendRequestLoop(int result); |
179 | 185 |
180 // Called when an asynchronous Send() completes. | 186 // Called when an asynchronous Send() completes. |
181 void OnSendIOCallback(int result); | 187 void OnSendIOCallback(int result); |
182 | 188 |
183 // Sends the next deferred request. This may be called immediately after | 189 // Activates the only request in |pending_send_request_queue_|. This should |
184 // SendRequest(), or it may be in a new task after a prior send completes in | 190 // only be called via SendRequest() when the send loop is idle. |
185 // DoSendComplete(). | 191 int DoStartRequestImmediately(int result); |
186 int DoSendNextRequest(int result); | 192 |
| 193 // Activates the first request in |pending_send_request_queue_| that hasn't |
| 194 // been closed, if any. This is called via DoSendComplete() after a prior |
| 195 // request complets. |
| 196 int DoStartNextDeferredRequest(int result); |
| 197 |
| 198 // Sends the active request. |
| 199 int DoSendActiveRequest(int result); |
187 | 200 |
188 // Notifies the user that the send has completed. This may be called directly | 201 // Notifies the user that the send has completed. This may be called directly |
189 // after SendRequest() for a synchronous request, or it may be called in | 202 // after SendRequest() for a synchronous request, or it may be called in |
190 // response to OnSendIOCallback for an asynchronous request. | 203 // response to OnSendIOCallback for an asynchronous request. |
191 int DoSendComplete(int result); | 204 int DoSendComplete(int result); |
192 | 205 |
193 // Evicts all unsent deferred requests. This is called if there is a Send() | 206 // Evicts all unsent deferred requests. This is called if there is a Send() |
194 // error or one of our streams informs us the connection is no longer | 207 // error or one of our streams informs us the connection is no longer |
195 // reusable. | 208 // reusable. |
196 int DoEvictPendingSendRequests(int result); | 209 int DoEvictPendingSendRequests(int result); |
197 | 210 |
198 // Ensures that only the active request's HttpPipelinedSocket can read from | 211 // Ensures that only the active request's HttpPipelinedSocket can read from |
199 // the underlying socket until it completes. A HttpPipelinedSocket informs us | 212 // the underlying socket until it completes. A HttpPipelinedSocket informs us |
200 // that it's done by calling Close(). | 213 // that it's done by calling Close(). |
201 int DoReadHeadersLoop(int result); | 214 int DoReadHeadersLoop(int result); |
202 | 215 |
203 // Called when the pending asynchronous ReadResponseHeaders() completes. | 216 // Called when the pending asynchronous ReadResponseHeaders() completes. |
204 void OnReadIOCallback(int result); | 217 void OnReadIOCallback(int result); |
205 | 218 |
206 // Determines if the next response in the pipeline is ready to be read. | 219 // Invokes DoStartNextDeferredRead() if the read loop is idle. This is called |
207 // If it's ready, then we call ReadResponseHeaders() on the underlying parser. | 220 // via a task queued when the previous |active_read_id_| closes its stream |
208 // HttpPipelinedSocket indicates its readiness by calling | 221 // after a succesful response. |
209 // ReadResponseHeaders(). This function may be called immediately after | 222 void StartNextDeferredRead(); |
210 // ReadResponseHeaders(), or it may be called in a new task after a previous | 223 |
211 // HttpPipelinedSocket finishes its work. | 224 // Activates the next read request immediately. This is called via |
212 int DoReadNextHeaders(int result); | 225 // ReadResponseHeaders() if that stream is at the front of |request_order_| |
| 226 // and the read loop is idle. |
| 227 int DoStartReadImmediately(int result); |
| 228 |
| 229 // Activates the next read request in |request_order_| if it's ready to go. |
| 230 // This is called via StartNextDeferredRead(). |
| 231 int DoStartNextDeferredRead(int result); |
| 232 |
| 233 // Calls ReadResponseHeaders() on the active request's parser. |
| 234 int DoReadHeaders(int result); |
213 | 235 |
214 // Notifies the user that reading the headers has completed. This may happen | 236 // Notifies the user that reading the headers has completed. This may happen |
215 // directly after DoReadNextHeaders() if the response is already available. | 237 // directly after DoReadNextHeaders() if the response is already available. |
216 // Otherwise, it is called in response to OnReadIOCallback(). | 238 // Otherwise, it is called in response to OnReadIOCallback(). |
217 int DoReadHeadersComplete(int result); | 239 int DoReadHeadersComplete(int result); |
218 | 240 |
219 // This is a holding state. It does not do anything, except exit the | 241 // Halts the read loop until Close() is called by the active stream. |
220 // DoReadHeadersLoop(). It is called after DoReadHeadersComplete(). | 242 int DoReadWaitForClose(int result); |
221 int DoReadWaitingForClose(int result); | |
222 | 243 |
223 // Cleans up the state associated with the active request. Invokes | 244 // Cleans up the state associated with the active request. Invokes |
224 // DoReadNextHeaders() in a new task to start the next response. This is | 245 // DoReadNextHeaders() in a new task to start the next response. This is |
225 // called after the active request's HttpPipelinedSocket calls Close(). | 246 // called after the active request's HttpPipelinedSocket calls Close(). |
226 int DoReadStreamClosed(); | 247 int DoReadStreamClosed(); |
227 | 248 |
228 // Removes all pending ReadResponseHeaders() requests from the queue. This may | 249 // Removes all pending ReadResponseHeaders() requests from the queue. This may |
229 // happen if there is an error with the pipeline or one of our | 250 // happen if there is an error with the pipeline or one of our |
230 // HttpPipelinedSockets indicates the connection was suddenly closed. | 251 // HttpPipelinedSockets indicates the connection was suddenly closed. |
231 int DoEvictPendingReadHeaders(int result); | 252 int DoEvictPendingReadHeaders(int result); |
232 | 253 |
233 // Invokes the user's callback in response to SendRequest() or | 254 // Posts a task to fire the user's callback in response to SendRequest() or |
234 // ReadResponseHeaders() completing on an underlying parser. This might be | 255 // ReadResponseHeaders() completing on an underlying parser. This might be |
235 // invoked in response to our own IO callbacks, or it may be invoked if the | 256 // invoked in response to our own IO callbacks, or it may be invoked if the |
236 // underlying parser completes SendRequest() or ReadResponseHeaders() | 257 // underlying parser completes SendRequest() or ReadResponseHeaders() |
237 // synchronously, but we've already returned ERR_IO_PENDING to the user's | 258 // synchronously, but we've already returned ERR_IO_PENDING to the user's |
238 // SendRequest() or ReadResponseHeaders() call into us. | 259 // SendRequest() or ReadResponseHeaders() call into us. |
| 260 void QueueUserCallback(int pipeline_id, |
| 261 OldCompletionCallback* callback, |
| 262 int rv, |
| 263 const tracked_objects::Location& from_here); |
| 264 |
| 265 // Invokes the callback queued in QueueUserCallback(). |
239 void FireUserCallback(int pipeline_id, int result); | 266 void FireUserCallback(int pipeline_id, int result); |
240 | 267 |
241 Delegate* delegate_; | 268 Delegate* delegate_; |
242 scoped_ptr<ClientSocketHandle> connection_; | 269 scoped_ptr<ClientSocketHandle> connection_; |
243 SSLConfig used_ssl_config_; | 270 SSLConfig used_ssl_config_; |
244 ProxyInfo used_proxy_info_; | 271 ProxyInfo used_proxy_info_; |
245 BoundNetLog net_log_; | 272 BoundNetLog net_log_; |
246 bool was_npn_negotiated_; | 273 bool was_npn_negotiated_; |
247 scoped_refptr<GrowableIOBuffer> read_buf_; | 274 scoped_refptr<GrowableIOBuffer> read_buf_; |
248 int next_pipeline_id_; | 275 int next_pipeline_id_; |
249 bool active_; | 276 bool active_; |
250 bool usable_; | 277 bool usable_; |
251 bool completed_one_request_; | 278 bool completed_one_request_; |
252 ScopedRunnableMethodFactory<HttpPipelinedConnectionImpl> method_factory_; | 279 ScopedRunnableMethodFactory<HttpPipelinedConnectionImpl> method_factory_; |
253 | 280 |
254 StreamInfoMap stream_info_map_; | 281 StreamInfoMap stream_info_map_; |
255 | 282 |
256 std::queue<int> request_order_; | 283 std::queue<int> request_order_; |
257 | 284 |
258 std::queue<DeferredSendRequest> deferred_request_queue_; | 285 std::queue<PendingSendRequest*> pending_send_request_queue_; |
| 286 scoped_ptr<PendingSendRequest> active_send_request_; |
259 SendRequestState send_next_state_; | 287 SendRequestState send_next_state_; |
| 288 bool send_still_on_call_stack_; |
260 OldCompletionCallbackImpl<HttpPipelinedConnectionImpl> send_io_callback_; | 289 OldCompletionCallbackImpl<HttpPipelinedConnectionImpl> send_io_callback_; |
261 OldCompletionCallback* send_user_callback_; | |
262 | 290 |
263 ReadHeadersState read_next_state_; | 291 ReadHeadersState read_next_state_; |
| 292 int active_read_id_; |
| 293 bool read_still_on_call_stack_; |
264 OldCompletionCallbackImpl<HttpPipelinedConnectionImpl> read_io_callback_; | 294 OldCompletionCallbackImpl<HttpPipelinedConnectionImpl> read_io_callback_; |
265 OldCompletionCallback* read_user_callback_; | |
266 | 295 |
267 DISALLOW_COPY_AND_ASSIGN(HttpPipelinedConnectionImpl); | 296 DISALLOW_COPY_AND_ASSIGN(HttpPipelinedConnectionImpl); |
268 }; | 297 }; |
269 | 298 |
270 } // namespace net | 299 } // namespace net |
271 | 300 |
272 #endif // NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | 301 #endif // NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ |
OLD | NEW |