Index: net/spdy/bidirectional_stream_spdy_job.cc |
diff --git a/net/spdy/bidirectional_stream_spdy_job.cc b/net/spdy/bidirectional_stream_spdy_job.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8e1a59e2e31e75addc564ce12f48654113b6717 |
--- /dev/null |
+++ b/net/spdy/bidirectional_stream_spdy_job.cc |
@@ -0,0 +1,226 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/spdy/bidirectional_stream_spdy_job.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/time/time.h" |
+#include "net/base/request_priority.h" |
+#include "net/spdy/spdy_buffer.h" |
+#include "net/spdy/spdy_header_block.h" |
+#include "net/spdy/spdy_http_utils.h" |
+#include "net/spdy/spdy_stream.h" |
+ |
+namespace net { |
+ |
+const base::TimeDelta kBufferTime = base::TimeDelta::FromMilliseconds(1); |
+ |
+BidirectionalStreamSpdyJob::BidirectionalStreamSpdyJob( |
+ const base::WeakPtr<SpdySession>& spdy_session) |
+ : spdy_session_(spdy_session), |
+ stream_closed_(false), |
+ closed_stream_status_(ERR_FAILED), |
+ buffered_read_pending_(false), |
+ more_read_data_pending_(false), |
+ weak_factory_(this) {} |
+ |
+BidirectionalStreamSpdyJob::~BidirectionalStreamSpdyJob() { |
+ if (stream_.get()) { |
+ stream_->DetachDelegate(); |
+ DCHECK(!stream_.get()); |
+ } |
+} |
+ |
+void BidirectionalStreamSpdyJob::Start( |
+ const HttpRequestInfo& request_info, |
+ RequestPriority priority, |
+ const BoundNetLog& net_log, |
+ BidirectionalStreamJob::Delegate* delegate) { |
+ delegate_ = delegate; |
+ DCHECK(!stream_); |
+ if (!spdy_session_) { |
+ delegate_->OnClose(ERR_CONNECTION_CLOSED); |
+ return; |
+ } |
+ |
+ request_info_ = request_info; |
+ |
+ int rv = stream_request_.StartRequest( |
+ SPDY_REQUEST_RESPONSE_STREAM, spdy_session_, request_info_.url, priority, |
+ net_log, base::Bind(&BidirectionalStreamSpdyJob::OnStreamInitialized, |
+ weak_factory_.GetWeakPtr())); |
+ if (rv != ERR_IO_PENDING) |
+ OnStreamInitialized(rv); |
+} |
+ |
+int BidirectionalStreamSpdyJob::ReadData(IOBuffer* buf, int buf_len) { |
+ if (stream_.get()) |
+ CHECK(!stream_->IsIdle()); |
+ |
+ CHECK(buf); |
+ CHECK(buf_len); |
+ if (!stream_closed_) |
+ CHECK(stream_); |
+ |
+ // If there is data buffered, complete the IO immediately. |
+ if (!data_queue_.IsEmpty()) { |
+ return data_queue_.Dequeue(buf->data(), buf_len); |
+ } else if (stream_closed_) { |
+ return closed_stream_status_; |
+ } |
+ // Read will complete asynchronously and Delegate::OnReadCompleted will be |
+ // called upon completion. |
+ user_buffer_ = buf; |
+ user_buffer_len_ = buf_len; |
+ return ERR_IO_PENDING; |
+} |
+ |
+void BidirectionalStreamSpdyJob::SendData(IOBuffer* data, |
+ int length, |
+ bool end_stream) { |
+ stream_->SendData(data, length, |
+ end_stream ? NO_MORE_DATA_TO_SEND : MORE_DATA_TO_SEND); |
+} |
+ |
+void BidirectionalStreamSpdyJob::Cancel() { |
+ if (!stream_) |
+ return; |
+ // Cancels the stream and detaches the delegate so it doesn't get called back. |
+ stream_->DetachDelegate(); |
+ DCHECK(!stream_); |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnRequestHeadersSent() { |
+ delegate_->OnRequestHeadersSent(); |
+} |
+ |
+SpdyResponseHeadersStatus BidirectionalStreamSpdyJob::OnResponseHeadersUpdated( |
+ const SpdyHeaderBlock& response_headers) { |
+ delegate_->OnHeaders(response_headers); |
+ return RESPONSE_HEADERS_ARE_COMPLETE; |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { |
+ DCHECK(stream_); |
+ DCHECK(!stream_closed_); |
+ |
+ if (buffer) { |
+ data_queue_.Enqueue(buffer.Pass()); |
+ if (user_buffer_) { |
+ // Handing small chunks of data to the caller creates measurable overhead. |
+ // So buffer data in short time-spans and send a single read notification. |
+ ScheduleBufferedRead(); |
+ } |
+ } |
+ // If |buffer| is null, onClose will be invoked to indicate the end of stream. |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnDataSent() { |
+ DCHECK(stream_); |
+ DCHECK(!stream_closed_); |
+ |
+ delegate_->OnDataSent(); |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnTrailers(const SpdyHeaderBlock& trailers) { |
+ DCHECK(stream_); |
+ DCHECK(!stream_closed_); |
+ |
+ delegate_->OnTrailers(trailers); |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnClose(int status) { |
+ DCHECK(stream_); |
+ |
+ stream_closed_ = true; |
+ closed_stream_status_ = status; |
+ stream_.reset(); |
+ |
+ if (status == OK) { |
+ DoBufferedRead(); |
+ return; |
+ } |
+ |
+ delegate_->OnClose(status); |
+} |
+ |
+void BidirectionalStreamSpdyJob::SendRequestHeaders() { |
+ stream_->SetDelegate(this); |
+ scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
+ CreateSpdyHeadersFromHttpRequest(request_info_, request_info_.extra_headers, |
+ stream_->GetProtocolVersion(), true, |
+ headers.get()); |
+ bool end_stream = (request_info_.method == "GET"); |
+ stream_->SendRequestHeaders( |
+ headers.Pass(), end_stream ? NO_MORE_DATA_TO_SEND : MORE_DATA_TO_SEND); |
+} |
+ |
+void BidirectionalStreamSpdyJob::OnStreamInitialized(int rv) { |
+ DCHECK_NE(ERR_IO_PENDING, rv); |
+ if (rv == OK) { |
+ stream_ = stream_request_.ReleaseStream(); |
+ SendRequestHeaders(); |
+ return; |
+ } |
+ delegate_->OnClose(static_cast<Error>(rv)); |
+} |
+ |
+void BidirectionalStreamSpdyJob::ScheduleBufferedRead() { |
+ // If there is already a scheduled DoBufferedRead, don't issue |
+ // another one. Mark that we have received more data and return. |
+ if (buffered_read_pending_) { |
+ more_read_data_pending_ = true; |
+ return; |
+ } |
+ |
+ more_read_data_pending_ = false; |
+ buffered_read_pending_ = true; |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
+ FROM_HERE, base::Bind(base::IgnoreResult( |
+ &BidirectionalStreamSpdyJob::DoBufferedRead), |
+ weak_factory_.GetWeakPtr()), |
+ kBufferTime); |
+} |
+ |
+void BidirectionalStreamSpdyJob::DoBufferedRead() { |
+ buffered_read_pending_ = false; |
+ // If the stream errored out, do not complete the read. |
+ if (!stream_ && !stream_closed_) |
+ return; |
+ if (stream_closed_ && closed_stream_status_ != OK) |
+ return; |
+ |
+ // When |more_read_data_pending_| is true, it means that more data has arrived |
+ // since started waiting. Wait a little longer and continue to buffer. |
+ if (more_read_data_pending_ && ShouldWaitForMoreBufferedData()) { |
+ ScheduleBufferedRead(); |
+ return; |
+ } |
+ |
+ int rv = 0; |
+ if (user_buffer_) { |
+ rv = ReadData(user_buffer_.get(), user_buffer_len_); |
+ DCHECK_NE(ERR_IO_PENDING, rv); |
+ user_buffer_ = nullptr; |
+ user_buffer_len_ = 0; |
+ delegate_->OnReadCompleted(rv); |
+ |
+ // If all data is read, and BidirectionalStreamSpdyJob::onClose is invoked |
+ // previously, let the delegate know about the onClose event. |
+ if (data_queue_.IsEmpty() && stream_closed_) { |
+ DCHECK_EQ(OK, closed_stream_status_); |
+ delegate_->OnClose(OK); |
+ } |
+ } |
+} |
+ |
+bool BidirectionalStreamSpdyJob::ShouldWaitForMoreBufferedData() const { |
+ if (stream_closed_) |
+ return false; |
+ DCHECK_GT(user_buffer_len_, 0); |
+ return data_queue_.GetTotalSize() < static_cast<size_t>(user_buffer_len_); |
+} |
+ |
+} // namespace net |