1 // Copyright (c) 2012 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.
5 #include "net/quic/quic_reliable_client_stream.h"
7 #include "base/callback_helpers.h"
8 #include "base/location.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/quic/quic_spdy_session.h"
13 #include "net/quic/quic_write_blocked_list.h"
14 #include "net/quic/spdy_utils.h"
18 QuicReliableClientStream::QuicReliableClientStream(QuicStreamId id
,
19 QuicSpdySession
* session
,
20 const BoundNetLog
& net_log
)
21 : QuicDataStream(id
, session
),
24 headers_delivered_(false),
25 weak_factory_(this) {}
27 QuicReliableClientStream::~QuicReliableClientStream() {
29 delegate_
->OnClose(connection_error());
32 void QuicReliableClientStream::OnStreamHeadersComplete(bool fin
,
34 QuicDataStream::OnStreamHeadersComplete(fin
, frame_len
);
35 // The delegate will read the headers via a posted task.
36 NotifyDelegateOfHeadersCompleteLater();
39 void QuicReliableClientStream::OnDataAvailable() {
40 // TODO(rch): buffer data if we don't have a delegate.
42 DLOG(ERROR
) << "Missing delegate";
43 Reset(QUIC_STREAM_CANCELLED
);
47 if (!FinishedReadingHeaders() || !headers_delivered_
) {
48 // Buffer the data in the sequencer until the headers have been read.
52 // The delegate will read the data via a posted task, and
53 // will be able to, potentially, read all data which has queued up.
54 NotifyDelegateOfDataAvailableLater();
57 void QuicReliableClientStream::OnClose() {
59 delegate_
->OnClose(connection_error());
62 ReliableQuicStream::OnClose();
65 void QuicReliableClientStream::OnCanWrite() {
66 ReliableQuicStream::OnCanWrite();
68 if (!HasBufferedData() && !callback_
.is_null()) {
69 base::ResetAndReturn(&callback_
).Run(OK
);
73 QuicPriority
QuicReliableClientStream::EffectivePriority() const {
74 if (delegate_
&& delegate_
->HasSendHeadersComplete()) {
75 return QuicDataStream::EffectivePriority();
77 return QuicWriteBlockedList::kHighestPriority
;
80 int QuicReliableClientStream::WriteStreamData(
81 base::StringPiece data
,
83 const CompletionCallback
& callback
) {
84 // We should not have data buffered.
85 DCHECK(!HasBufferedData());
86 // Writes the data, or buffers it.
87 WriteOrBufferData(data
, fin
, nullptr);
88 if (!HasBufferedData()) {
93 return ERR_IO_PENDING
;
96 void QuicReliableClientStream::SetDelegate(
97 QuicReliableClientStream::Delegate
* delegate
) {
98 DCHECK(!(delegate_
&& delegate
));
100 if (delegate
== nullptr && sequencer()->IsClosed()) {
105 void QuicReliableClientStream::OnError(int error
) {
107 QuicReliableClientStream::Delegate
* delegate
= delegate_
;
109 delegate
->OnError(error
);
113 int QuicReliableClientStream::Read(IOBuffer
* buf
, int buf_len
) {
114 if (sequencer()->IsClosed())
117 if (!HasBytesToRead())
118 return ERR_IO_PENDING
;
121 iov
.iov_base
= buf
->data();
122 iov
.iov_len
= buf_len
;
123 return Readv(&iov
, 1);
126 bool QuicReliableClientStream::CanWrite(const CompletionCallback
& callback
) {
127 bool can_write
= session()->connection()->CanWrite(HAS_RETRANSMITTABLE_DATA
);
129 session()->MarkConnectionLevelWriteBlocked(id(), EffectivePriority());
130 DCHECK(callback_
.is_null());
131 callback_
= callback
;
136 void QuicReliableClientStream::NotifyDelegateOfHeadersCompleteLater() {
138 base::ThreadTaskRunnerHandle::Get()->PostTask(
140 base::Bind(&QuicReliableClientStream::NotifyDelegateOfHeadersComplete
,
141 weak_factory_
.GetWeakPtr()));
144 void QuicReliableClientStream::NotifyDelegateOfHeadersComplete() {
148 size_t headers_len
= decompressed_headers().length();
149 SpdyHeaderBlock headers
;
150 SpdyFramer
framer(SpdyUtils::GetSpdyVersionForQuicVersion(version()));
151 size_t len
= framer
.ParseHeaderBlockInBuffer(decompressed_headers().data(),
152 headers_len
, &headers
);
153 MarkHeadersConsumed(headers_len
);
154 headers_delivered_
= true;
155 if (len
== 0 || len
!= headers_len
) {
156 DLOG(WARNING
) << "Invalid headers";
157 Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
161 delegate_
->OnHeadersAvailable(headers
);
164 void QuicReliableClientStream::NotifyDelegateOfDataAvailableLater() {
166 base::ThreadTaskRunnerHandle::Get()->PostTask(
168 base::Bind(&QuicReliableClientStream::NotifyDelegateOfDataAvailable
,
169 weak_factory_
.GetWeakPtr()));
172 void QuicReliableClientStream::NotifyDelegateOfDataAvailable() {
174 delegate_
->OnDataAvailable();