| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 #include "net/spdy/bidirectional_stream_spdy_job.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/time/time.h" |
| 10 #include "base/timer/timer.h" |
| 11 #include "net/base/request_priority.h" |
| 12 #include "net/spdy/spdy_buffer.h" |
| 13 #include "net/spdy/spdy_header_block.h" |
| 14 #include "net/spdy/spdy_http_utils.h" |
| 15 #include "net/spdy/spdy_stream.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 const base::TimeDelta kBufferTime = base::TimeDelta::FromMilliseconds(1); |
| 20 |
| 21 BidirectionalStreamSpdyJob::BidirectionalStreamSpdyJob( |
| 22 const base::WeakPtr<SpdySession>& spdy_session, |
| 23 scoped_ptr<base::Timer> timer) |
| 24 : spdy_session_(spdy_session), |
| 25 timer_(timer.release()), |
| 26 stream_closed_(false), |
| 27 closed_stream_status_(ERR_FAILED), |
| 28 more_read_data_pending_(false), |
| 29 negotiated_protocol_(kProtoUnknown), |
| 30 closed_stream_received_bytes_(0), |
| 31 closed_stream_sent_bytes_(0), |
| 32 weak_factory_(this) {} |
| 33 |
| 34 BidirectionalStreamSpdyJob::BidirectionalStreamSpdyJob( |
| 35 const base::WeakPtr<SpdySession>& spdy_session) |
| 36 : BidirectionalStreamSpdyJob( |
| 37 spdy_session, |
| 38 make_scoped_ptr(new base::Timer(false, false))) {} |
| 39 |
| 40 BidirectionalStreamSpdyJob::~BidirectionalStreamSpdyJob() { |
| 41 if (stream_.get()) { |
| 42 stream_->DetachDelegate(); |
| 43 DCHECK(!stream_.get()); |
| 44 } |
| 45 } |
| 46 |
| 47 void BidirectionalStreamSpdyJob::Start( |
| 48 const HttpRequestInfo& request_info, |
| 49 RequestPriority priority, |
| 50 const BoundNetLog& net_log, |
| 51 BidirectionalStreamJob::Delegate* delegate) { |
| 52 delegate_ = delegate; |
| 53 DCHECK(!stream_); |
| 54 if (!spdy_session_) { |
| 55 delegate_->OnFailed(ERR_CONNECTION_CLOSED); |
| 56 return; |
| 57 } |
| 58 |
| 59 request_info_ = request_info; |
| 60 |
| 61 int rv = stream_request_.StartRequest( |
| 62 SPDY_BIDIRECTIONAL_STREAM, spdy_session_, request_info_.url, priority, |
| 63 net_log, base::Bind(&BidirectionalStreamSpdyJob::OnStreamInitialized, |
| 64 weak_factory_.GetWeakPtr())); |
| 65 if (rv != ERR_IO_PENDING) |
| 66 OnStreamInitialized(rv); |
| 67 } |
| 68 |
| 69 int BidirectionalStreamSpdyJob::ReadData(IOBuffer* buf, int buf_len) { |
| 70 if (stream_.get()) |
| 71 CHECK(!stream_->IsIdle()); |
| 72 |
| 73 CHECK(buf); |
| 74 CHECK(buf_len); |
| 75 CHECK(!timer_->IsRunning()) << "There should be only one ReadData in flight"; |
| 76 |
| 77 if (!stream_closed_) |
| 78 CHECK(stream_); |
| 79 |
| 80 // If there is data buffered, complete the IO immediately. |
| 81 if (!data_queue_.IsEmpty()) { |
| 82 return data_queue_.Dequeue(buf->data(), buf_len); |
| 83 } else if (stream_closed_) { |
| 84 return closed_stream_status_; |
| 85 } |
| 86 // Read will complete asynchronously and Delegate::OnReadCompleted will be |
| 87 // called upon completion. |
| 88 user_buffer_ = buf; |
| 89 user_buffer_len_ = buf_len; |
| 90 return ERR_IO_PENDING; |
| 91 } |
| 92 |
| 93 void BidirectionalStreamSpdyJob::SendData(IOBuffer* data, |
| 94 int length, |
| 95 bool end_stream) { |
| 96 CHECK(!stream_closed_); |
| 97 CHECK(stream_); |
| 98 |
| 99 stream_->SendData(data, length, |
| 100 end_stream ? NO_MORE_DATA_TO_SEND : MORE_DATA_TO_SEND); |
| 101 } |
| 102 |
| 103 void BidirectionalStreamSpdyJob::Cancel() { |
| 104 if (!stream_) |
| 105 return; |
| 106 // Cancels the stream and detaches the delegate so it doesn't get called back. |
| 107 stream_->DetachDelegate(); |
| 108 DCHECK(!stream_); |
| 109 } |
| 110 |
| 111 NextProto BidirectionalStreamSpdyJob::GetProtocol() const { |
| 112 return negotiated_protocol_; |
| 113 } |
| 114 |
| 115 int64_t BidirectionalStreamSpdyJob::GetTotalReceivedBytes() const { |
| 116 if (stream_closed_) |
| 117 return closed_stream_received_bytes_; |
| 118 |
| 119 if (!stream_) |
| 120 return 0; |
| 121 |
| 122 return stream_->raw_received_bytes(); |
| 123 } |
| 124 |
| 125 int64_t BidirectionalStreamSpdyJob::GetTotalSentBytes() const { |
| 126 if (stream_closed_) |
| 127 return closed_stream_sent_bytes_; |
| 128 |
| 129 if (!stream_) |
| 130 return 0; |
| 131 |
| 132 return stream_->raw_sent_bytes(); |
| 133 } |
| 134 |
| 135 void BidirectionalStreamSpdyJob::OnRequestHeadersSent() { |
| 136 DCHECK(stream_); |
| 137 |
| 138 delegate_->OnRequestHeadersSent(); |
| 139 } |
| 140 |
| 141 SpdyResponseHeadersStatus BidirectionalStreamSpdyJob::OnResponseHeadersUpdated( |
| 142 const SpdyHeaderBlock& response_headers) { |
| 143 DCHECK(stream_); |
| 144 |
| 145 negotiated_protocol_ = stream_->GetProtocol(); |
| 146 delegate_->OnHeaders(response_headers); |
| 147 return RESPONSE_HEADERS_ARE_COMPLETE; |
| 148 } |
| 149 |
| 150 void BidirectionalStreamSpdyJob::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { |
| 151 DCHECK(stream_); |
| 152 DCHECK(!stream_closed_); |
| 153 |
| 154 if (buffer) { |
| 155 data_queue_.Enqueue(buffer.Pass()); |
| 156 if (user_buffer_) { |
| 157 // Handing small chunks of data to the caller creates measurable overhead. |
| 158 // So buffer data in short time-spans and send a single read notification. |
| 159 ScheduleBufferedRead(); |
| 160 } |
| 161 } |
| 162 // If |buffer| is null, BidirectionalStreamSpdyJob::OnClose will be invoked by |
| 163 // SpdyStream to indicate the end of stream. |
| 164 } |
| 165 |
| 166 void BidirectionalStreamSpdyJob::OnDataSent() { |
| 167 DCHECK(stream_); |
| 168 DCHECK(!stream_closed_); |
| 169 |
| 170 delegate_->OnDataSent(); |
| 171 } |
| 172 |
| 173 void BidirectionalStreamSpdyJob::OnTrailers(const SpdyHeaderBlock& trailers) { |
| 174 DCHECK(stream_); |
| 175 DCHECK(!stream_closed_); |
| 176 |
| 177 delegate_->OnTrailers(trailers); |
| 178 } |
| 179 |
| 180 void BidirectionalStreamSpdyJob::OnClose(int status) { |
| 181 DCHECK(stream_); |
| 182 |
| 183 stream_closed_ = true; |
| 184 closed_stream_status_ = status; |
| 185 closed_stream_received_bytes_ = stream_->raw_received_bytes(); |
| 186 closed_stream_sent_bytes_ = stream_->raw_sent_bytes(); |
| 187 stream_.reset(); |
| 188 |
| 189 if (status != OK) { |
| 190 delegate_->OnFailed(status); |
| 191 return; |
| 192 } |
| 193 // Complete any remaining read, as all data has been buffered. |
| 194 // If user has not called ReadData (i.e |user_buffer_| is nullptr), this will |
| 195 // do nothing. |
| 196 DCHECK_EQ(OK, status); |
| 197 timer_->Stop(); |
| 198 DoBufferedRead(); |
| 199 } |
| 200 |
| 201 void BidirectionalStreamSpdyJob::SendRequestHeaders() { |
| 202 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
| 203 CreateSpdyHeadersFromHttpRequest(request_info_, request_info_.extra_headers, |
| 204 stream_->GetProtocolVersion(), true, |
| 205 headers.get()); |
| 206 stream_->SendRequestHeaders(headers.Pass(), MORE_DATA_TO_SEND); |
| 207 } |
| 208 |
| 209 void BidirectionalStreamSpdyJob::OnStreamInitialized(int rv) { |
| 210 DCHECK_NE(ERR_IO_PENDING, rv); |
| 211 if (rv == OK) { |
| 212 stream_ = stream_request_.ReleaseStream(); |
| 213 stream_->SetDelegate(this); |
| 214 SendRequestHeaders(); |
| 215 return; |
| 216 } |
| 217 delegate_->OnFailed(static_cast<Error>(rv)); |
| 218 } |
| 219 |
| 220 void BidirectionalStreamSpdyJob::ScheduleBufferedRead() { |
| 221 // If there is already a scheduled DoBufferedRead, don't issue |
| 222 // another one. Mark that we have received more data and return. |
| 223 if (timer_->IsRunning()) { |
| 224 more_read_data_pending_ = true; |
| 225 return; |
| 226 } |
| 227 |
| 228 more_read_data_pending_ = false; |
| 229 timer_->Start(FROM_HERE, kBufferTime, |
| 230 base::Bind(&BidirectionalStreamSpdyJob::DoBufferedRead, |
| 231 weak_factory_.GetWeakPtr())); |
| 232 } |
| 233 |
| 234 void BidirectionalStreamSpdyJob::DoBufferedRead() { |
| 235 DCHECK(!timer_->IsRunning()); |
| 236 // If the stream errored out, do not complete the read. |
| 237 if (!stream_ && !stream_closed_) |
| 238 return; |
| 239 if (stream_closed_ && closed_stream_status_ != OK) |
| 240 return; |
| 241 |
| 242 // When |more_read_data_pending_| is true, it means that more data has arrived |
| 243 // since started waiting. Wait a little longer and continue to buffer. |
| 244 if (more_read_data_pending_ && ShouldWaitForMoreBufferedData()) { |
| 245 ScheduleBufferedRead(); |
| 246 return; |
| 247 } |
| 248 |
| 249 int rv = 0; |
| 250 if (user_buffer_) { |
| 251 rv = ReadData(user_buffer_.get(), user_buffer_len_); |
| 252 DCHECK_NE(ERR_IO_PENDING, rv); |
| 253 user_buffer_ = nullptr; |
| 254 user_buffer_len_ = 0; |
| 255 delegate_->OnReadCompleted(rv); |
| 256 } |
| 257 } |
| 258 |
| 259 bool BidirectionalStreamSpdyJob::ShouldWaitForMoreBufferedData() const { |
| 260 if (stream_closed_) |
| 261 return false; |
| 262 DCHECK_GT(user_buffer_len_, 0); |
| 263 return data_queue_.GetTotalSize() < static_cast<size_t>(user_buffer_len_); |
| 264 } |
| 265 |
| 266 } // namespace net |
| OLD | NEW |