OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "net/quic/bidirectional_stream_quic_impl.h" | 5 #include "net/quic/bidirectional_stream_quic_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { | 41 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { |
42 Cancel(); | 42 Cancel(); |
43 if (session_) | 43 if (session_) |
44 session_->RemoveObserver(this); | 44 session_->RemoveObserver(this); |
45 } | 45 } |
46 | 46 |
47 void BidirectionalStreamQuicImpl::Start( | 47 void BidirectionalStreamQuicImpl::Start( |
48 const BidirectionalStreamRequestInfo* request_info, | 48 const BidirectionalStreamRequestInfo* request_info, |
49 const BoundNetLog& net_log, | 49 const BoundNetLog& net_log, |
50 bool send_request_headers_automatically, | 50 bool send_request_headers_automatically, |
51 BidirectionalStreamImpl::Delegate* delegate, | 51 BidirectionalStreamImpl::Delegate* delegate, |
kapishnikov
2016/06/03 20:10:08
Is delegate optional here, i.e. is it allowed to p
xunjieli
2016/06/03 20:28:36
Great catch! We need a delegate. I added a check.
| |
52 std::unique_ptr<base::Timer> /* timer */) { | 52 std::unique_ptr<base::Timer> /* timer */) { |
53 DCHECK(!stream_); | 53 DCHECK(!stream_); |
54 | 54 |
55 send_request_headers_automatically_ = send_request_headers_automatically; | 55 send_request_headers_automatically_ = send_request_headers_automatically; |
56 if (!session_) { | 56 if (!session_) { |
57 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR | 57 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
58 : ERR_QUIC_HANDSHAKE_FAILED); | 58 : ERR_QUIC_HANDSHAKE_FAILED); |
59 return; | 59 return; |
60 } | 60 } |
61 | 61 |
62 delegate_ = delegate; | 62 delegate_ = delegate; |
63 request_info_ = request_info; | 63 request_info_ = request_info; |
64 | 64 |
65 int rv = stream_request_.StartRequest( | 65 int rv = stream_request_.StartRequest( |
66 session_, &stream_, | 66 session_, &stream_, |
67 base::Bind(&BidirectionalStreamQuicImpl::OnStreamReady, | 67 base::Bind(&BidirectionalStreamQuicImpl::OnStreamReady, |
68 weak_factory_.GetWeakPtr())); | 68 weak_factory_.GetWeakPtr())); |
69 if (rv == OK) { | 69 if (rv == OK) { |
70 OnStreamReady(rv); | 70 OnStreamReady(rv); |
71 } else if (!was_handshake_confirmed_) { | 71 } else if (!was_handshake_confirmed_) { |
72 NotifyError(ERR_QUIC_HANDSHAKE_FAILED); | 72 NotifyError(ERR_QUIC_HANDSHAKE_FAILED); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 void BidirectionalStreamQuicImpl::SendRequestHeaders() { | 76 void BidirectionalStreamQuicImpl::SendRequestHeaders() { |
77 DCHECK(!has_sent_headers_); | 77 DCHECK(!has_sent_headers_); |
78 DCHECK(stream_); | 78 |
79 if (!stream_) { | |
80 LOG(ERROR) | |
81 << "Trying to send request headers after stream has been destroyed."; | |
82 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
83 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::NotifyError, | |
84 weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); | |
85 return; | |
86 } | |
79 | 87 |
80 SpdyHeaderBlock headers; | 88 SpdyHeaderBlock headers; |
81 HttpRequestInfo http_request_info; | 89 HttpRequestInfo http_request_info; |
82 http_request_info.url = request_info_->url; | 90 http_request_info.url = request_info_->url; |
83 http_request_info.method = request_info_->method; | 91 http_request_info.method = request_info_->method; |
84 http_request_info.extra_headers = request_info_->extra_headers; | 92 http_request_info.extra_headers = request_info_->extra_headers; |
85 | 93 |
86 CreateSpdyHeadersFromHttpRequest(http_request_info, | 94 CreateSpdyHeadersFromHttpRequest(http_request_info, |
87 http_request_info.extra_headers, HTTP2, true, | 95 http_request_info.extra_headers, HTTP2, true, |
88 &headers); | 96 &headers); |
(...skipping 23 matching lines...) Expand all Loading... | |
112 // Read will complete asynchronously and Delegate::OnReadCompleted will be | 120 // Read will complete asynchronously and Delegate::OnReadCompleted will be |
113 // called upon completion. | 121 // called upon completion. |
114 read_buffer_ = buffer; | 122 read_buffer_ = buffer; |
115 read_buffer_len_ = buffer_len; | 123 read_buffer_len_ = buffer_len; |
116 return ERR_IO_PENDING; | 124 return ERR_IO_PENDING; |
117 } | 125 } |
118 | 126 |
119 void BidirectionalStreamQuicImpl::SendData(const scoped_refptr<IOBuffer>& data, | 127 void BidirectionalStreamQuicImpl::SendData(const scoped_refptr<IOBuffer>& data, |
120 int length, | 128 int length, |
121 bool end_stream) { | 129 bool end_stream) { |
122 DCHECK(stream_); | |
123 DCHECK(length > 0 || (length == 0 && end_stream)); | 130 DCHECK(length > 0 || (length == 0 && end_stream)); |
124 | 131 |
132 if (!stream_) { | |
133 LOG(ERROR) << "Trying to send data after stream has been destroyed."; | |
134 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
135 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::NotifyError, | |
136 weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); | |
137 return; | |
138 } | |
139 | |
125 std::unique_ptr<QuicConnection::ScopedPacketBundler> bundler; | 140 std::unique_ptr<QuicConnection::ScopedPacketBundler> bundler; |
126 if (!has_sent_headers_) { | 141 if (!has_sent_headers_) { |
127 DCHECK(!send_request_headers_automatically_); | 142 DCHECK(!send_request_headers_automatically_); |
128 // Creates a bundler only if there are headers to be sent along with the | 143 // Creates a bundler only if there are headers to be sent along with the |
129 // single data buffer. | 144 // single data buffer. |
130 bundler.reset(new QuicConnection::ScopedPacketBundler( | 145 bundler.reset(new QuicConnection::ScopedPacketBundler( |
131 session_->connection(), QuicConnection::SEND_ACK_IF_PENDING)); | 146 session_->connection(), QuicConnection::SEND_ACK_IF_PENDING)); |
132 SendRequestHeaders(); | 147 SendRequestHeaders(); |
133 } | 148 } |
134 | 149 |
135 base::StringPiece string_data(data->data(), length); | 150 base::StringPiece string_data(data->data(), length); |
136 int rv = stream_->WriteStreamData( | 151 int rv = stream_->WriteStreamData( |
137 string_data, end_stream, | 152 string_data, end_stream, |
138 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 153 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
139 weak_factory_.GetWeakPtr())); | 154 weak_factory_.GetWeakPtr())); |
140 DCHECK(rv == OK || rv == ERR_IO_PENDING); | 155 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
141 if (rv == OK) { | 156 if (rv == OK) { |
142 base::ThreadTaskRunnerHandle::Get()->PostTask( | 157 base::ThreadTaskRunnerHandle::Get()->PostTask( |
143 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 158 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
144 weak_factory_.GetWeakPtr(), OK)); | 159 weak_factory_.GetWeakPtr(), OK)); |
145 } | 160 } |
146 } | 161 } |
147 | 162 |
148 void BidirectionalStreamQuicImpl::SendvData( | 163 void BidirectionalStreamQuicImpl::SendvData( |
149 const std::vector<scoped_refptr<IOBuffer>>& buffers, | 164 const std::vector<scoped_refptr<IOBuffer>>& buffers, |
150 const std::vector<int>& lengths, | 165 const std::vector<int>& lengths, |
151 bool end_stream) { | 166 bool end_stream) { |
152 DCHECK(stream_); | |
153 DCHECK_EQ(buffers.size(), lengths.size()); | 167 DCHECK_EQ(buffers.size(), lengths.size()); |
154 | 168 |
169 if (!stream_) { | |
170 LOG(ERROR) << "Trying to send data after stream has been destroyed."; | |
171 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
172 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::NotifyError, | |
173 weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); | |
174 return; | |
175 } | |
176 | |
155 QuicConnection::ScopedPacketBundler bundler( | 177 QuicConnection::ScopedPacketBundler bundler( |
156 session_->connection(), QuicConnection::SEND_ACK_IF_PENDING); | 178 session_->connection(), QuicConnection::SEND_ACK_IF_PENDING); |
157 if (!has_sent_headers_) { | 179 if (!has_sent_headers_) { |
158 DCHECK(!send_request_headers_automatically_); | 180 DCHECK(!send_request_headers_automatically_); |
159 SendRequestHeaders(); | 181 SendRequestHeaders(); |
160 } | 182 } |
161 | 183 |
162 int rv = stream_->WritevStreamData( | 184 int rv = stream_->WritevStreamData( |
163 buffers, lengths, end_stream, | 185 buffers, lengths, end_stream, |
164 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 186 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
165 weak_factory_.GetWeakPtr())); | 187 weak_factory_.GetWeakPtr())); |
166 | 188 |
167 DCHECK(rv == OK || rv == ERR_IO_PENDING); | 189 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
168 if (rv == OK) { | 190 if (rv == OK) { |
169 base::ThreadTaskRunnerHandle::Get()->PostTask( | 191 base::ThreadTaskRunnerHandle::Get()->PostTask( |
170 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 192 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
171 weak_factory_.GetWeakPtr(), OK)); | 193 weak_factory_.GetWeakPtr(), OK)); |
172 } | 194 } |
173 } | 195 } |
174 | 196 |
175 void BidirectionalStreamQuicImpl::Cancel() { | 197 void BidirectionalStreamQuicImpl::Cancel() { |
176 if (stream_) { | 198 if (!stream_) |
177 stream_->SetDelegate(nullptr); | 199 return; |
178 stream_->Reset(QUIC_STREAM_CANCELLED); | 200 stream_->SetDelegate(nullptr); |
kapishnikov
2016/06/03 20:10:08
ResetStream() calls "stream_->SetDelegate(nullptr)
xunjieli
2016/06/03 20:28:36
Great catch!
| |
179 ResetStream(); | 201 stream_->Reset(QUIC_STREAM_CANCELLED); |
180 } | 202 delegate_ = nullptr; |
kapishnikov
2016/06/03 20:10:08
Should we unconditionally set "delegate_ = nullptr
xunjieli
2016/06/03 20:28:36
Great catch! I've addressed it a newer patch.
| |
203 // Cancel any pending callbacks. | |
204 weak_factory_.InvalidateWeakPtrs(); | |
205 ResetStream(); | |
181 } | 206 } |
182 | 207 |
183 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { | 208 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { |
184 return negotiated_protocol_; | 209 return negotiated_protocol_; |
185 } | 210 } |
186 | 211 |
187 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { | 212 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { |
188 if (stream_) | 213 if (stream_) |
189 return headers_bytes_received_ + stream_->stream_bytes_read(); | 214 return headers_bytes_received_ + stream_->stream_bytes_read(); |
190 return headers_bytes_received_ + closed_stream_received_bytes_; | 215 return headers_bytes_received_ + closed_stream_received_bytes_; |
191 } | 216 } |
192 | 217 |
193 int64_t BidirectionalStreamQuicImpl::GetTotalSentBytes() const { | 218 int64_t BidirectionalStreamQuicImpl::GetTotalSentBytes() const { |
194 if (stream_) | 219 if (stream_) |
195 return headers_bytes_sent_ + stream_->stream_bytes_written(); | 220 return headers_bytes_sent_ + stream_->stream_bytes_written(); |
196 return headers_bytes_sent_ + closed_stream_sent_bytes_; | 221 return headers_bytes_sent_ + closed_stream_sent_bytes_; |
197 } | 222 } |
198 | 223 |
199 void BidirectionalStreamQuicImpl::OnHeadersAvailable( | 224 void BidirectionalStreamQuicImpl::OnHeadersAvailable( |
200 const SpdyHeaderBlock& headers, | 225 const SpdyHeaderBlock& headers, |
201 size_t frame_len) { | 226 size_t frame_len) { |
202 headers_bytes_received_ += frame_len; | 227 headers_bytes_received_ += frame_len; |
203 negotiated_protocol_ = kProtoQUIC1SPDY3; | 228 negotiated_protocol_ = kProtoQUIC1SPDY3; |
204 if (!has_received_headers_) { | 229 if (!has_received_headers_) { |
205 has_received_headers_ = true; | 230 has_received_headers_ = true; |
206 delegate_->OnHeadersReceived(headers); | 231 if (delegate_) |
232 delegate_->OnHeadersReceived(headers); | |
207 } else { | 233 } else { |
208 if (stream_->IsDoneReading()) { | 234 if (stream_->IsDoneReading()) { |
209 // If the write side is closed, OnFinRead() will call | 235 // If the write side is closed, OnFinRead() will call |
210 // BidirectionalStreamQuicImpl::OnClose(). | 236 // BidirectionalStreamQuicImpl::OnClose(). |
211 stream_->OnFinRead(); | 237 stream_->OnFinRead(); |
212 } | 238 } |
213 delegate_->OnTrailersReceived(headers); | 239 if (delegate_) |
240 delegate_->OnTrailersReceived(headers); | |
214 } | 241 } |
215 } | 242 } |
216 | 243 |
217 void BidirectionalStreamQuicImpl::OnDataAvailable() { | 244 void BidirectionalStreamQuicImpl::OnDataAvailable() { |
218 // Return early if ReadData has not been called. | 245 // Return early if ReadData has not been called. |
219 if (!read_buffer_) | 246 if (!read_buffer_) |
220 return; | 247 return; |
221 | 248 |
222 CHECK(read_buffer_); | 249 CHECK(read_buffer_); |
223 CHECK_NE(0, read_buffer_len_); | 250 CHECK_NE(0, read_buffer_len_); |
224 int rv = ReadData(read_buffer_.get(), read_buffer_len_); | 251 int rv = ReadData(read_buffer_.get(), read_buffer_len_); |
225 if (rv == ERR_IO_PENDING) { | 252 if (rv == ERR_IO_PENDING) { |
226 // Spurrious notification. Wait for the next one. | 253 // Spurrious notification. Wait for the next one. |
227 return; | 254 return; |
228 } | 255 } |
229 read_buffer_ = nullptr; | 256 read_buffer_ = nullptr; |
230 read_buffer_len_ = 0; | 257 read_buffer_len_ = 0; |
231 delegate_->OnDataRead(rv); | 258 if (delegate_) |
259 delegate_->OnDataRead(rv); | |
232 } | 260 } |
233 | 261 |
234 void BidirectionalStreamQuicImpl::OnClose(QuicErrorCode error) { | 262 void BidirectionalStreamQuicImpl::OnClose(QuicErrorCode error) { |
235 DCHECK(stream_); | 263 DCHECK(stream_); |
264 | |
236 if (error == QUIC_NO_ERROR && | 265 if (error == QUIC_NO_ERROR && |
237 stream_->stream_error() == QUIC_STREAM_NO_ERROR) { | 266 stream_->stream_error() == QUIC_STREAM_NO_ERROR) { |
238 ResetStream(); | 267 ResetStream(); |
239 return; | 268 return; |
240 } | 269 } |
241 ResetStream(); | |
242 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR | 270 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
243 : ERR_QUIC_HANDSHAKE_FAILED); | 271 : ERR_QUIC_HANDSHAKE_FAILED); |
244 } | 272 } |
245 | 273 |
246 void BidirectionalStreamQuicImpl::OnError(int error) { | 274 void BidirectionalStreamQuicImpl::OnError(int error) { |
247 NotifyError(error); | 275 NotifyError(error); |
248 } | 276 } |
249 | 277 |
250 bool BidirectionalStreamQuicImpl::HasSendHeadersComplete() { | 278 bool BidirectionalStreamQuicImpl::HasSendHeadersComplete() { |
251 return has_sent_headers_; | 279 return has_sent_headers_; |
(...skipping 12 matching lines...) Expand all Loading... | |
264 } | 292 } |
265 | 293 |
266 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { | 294 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { |
267 DCHECK_NE(ERR_IO_PENDING, rv); | 295 DCHECK_NE(ERR_IO_PENDING, rv); |
268 DCHECK(rv == OK || !stream_); | 296 DCHECK(rv == OK || !stream_); |
269 if (rv == OK) { | 297 if (rv == OK) { |
270 stream_->SetDelegate(this); | 298 stream_->SetDelegate(this); |
271 if (send_request_headers_automatically_) { | 299 if (send_request_headers_automatically_) { |
272 SendRequestHeaders(); | 300 SendRequestHeaders(); |
273 } | 301 } |
274 delegate_->OnStreamReady(has_sent_headers_); | 302 if (delegate_) |
303 delegate_->OnStreamReady(has_sent_headers_); | |
275 } else { | 304 } else { |
276 NotifyError(rv); | 305 NotifyError(rv); |
277 } | 306 } |
278 } | 307 } |
279 | 308 |
280 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { | 309 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { |
281 DCHECK(rv == OK || !stream_); | 310 DCHECK(rv == OK || !stream_); |
282 if (rv == OK) { | 311 if (rv == OK) { |
283 delegate_->OnDataSent(); | 312 if (delegate_) |
313 delegate_->OnDataSent(); | |
284 } else { | 314 } else { |
285 NotifyError(rv); | 315 NotifyError(rv); |
286 } | 316 } |
287 } | 317 } |
288 | 318 |
289 void BidirectionalStreamQuicImpl::NotifyError(int error) { | 319 void BidirectionalStreamQuicImpl::NotifyError(int error) { |
290 DCHECK_NE(OK, error); | 320 DCHECK_NE(OK, error); |
291 DCHECK_NE(ERR_IO_PENDING, error); | 321 DCHECK_NE(ERR_IO_PENDING, error); |
292 | 322 |
323 if (!delegate_) | |
324 return; | |
293 response_status_ = error; | 325 response_status_ = error; |
294 ResetStream(); | 326 ResetStream(); |
kapishnikov
2016/06/03 20:10:07
Should we call ResetStream() before "if (!delegate
xunjieli
2016/06/03 20:28:36
Great catch. I've also realized this and addressed
| |
295 delegate_->OnFailed(error); | 327 BidirectionalStreamImpl::Delegate* delegate = delegate_; |
328 delegate_ = nullptr; | |
329 // Cancel any pending callback. | |
330 weak_factory_.InvalidateWeakPtrs(); | |
331 delegate->OnFailed(error); | |
296 } | 332 } |
297 | 333 |
298 void BidirectionalStreamQuicImpl::ResetStream() { | 334 void BidirectionalStreamQuicImpl::ResetStream() { |
299 if (!stream_) | 335 if (!stream_) |
300 return; | 336 return; |
301 closed_stream_received_bytes_ = stream_->stream_bytes_read(); | 337 closed_stream_received_bytes_ = stream_->stream_bytes_read(); |
302 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); | 338 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); |
303 stream_->SetDelegate(nullptr); | 339 stream_->SetDelegate(nullptr); |
304 stream_ = nullptr; | 340 stream_ = nullptr; |
305 } | 341 } |
306 | 342 |
307 } // namespace net | 343 } // namespace net |
OLD | NEW |