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 "net/base/net_errors.h"
9 #include "net/quic/quic_spdy_session.h"
10 #include "net/quic/quic_write_blocked_list.h"
14 QuicReliableClientStream::QuicReliableClientStream(QuicStreamId id
,
15 QuicSpdySession
* session
,
16 const BoundNetLog
& net_log
)
17 : QuicDataStream(id
, session
), net_log_(net_log
), delegate_(nullptr) {
20 QuicReliableClientStream::~QuicReliableClientStream() {
22 delegate_
->OnClose(connection_error());
25 uint32
QuicReliableClientStream::ProcessData(const char* data
,
27 // TODO(rch): buffer data if we don't have a delegate.
29 DLOG(ERROR
) << "Missing delegate";
30 Reset(QUIC_STREAM_CANCELLED
);
34 int rv
= delegate_
->OnDataReceived(data
, data_len
);
36 DLOG(ERROR
) << "Delegate refused data, rv: " << rv
;
37 Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
43 void QuicReliableClientStream::OnClose() {
45 delegate_
->OnClose(connection_error());
48 ReliableQuicStream::OnClose();
51 void QuicReliableClientStream::OnCanWrite() {
52 ReliableQuicStream::OnCanWrite();
54 if (!HasBufferedData() && !callback_
.is_null()) {
55 base::ResetAndReturn(&callback_
).Run(OK
);
59 QuicPriority
QuicReliableClientStream::EffectivePriority() const {
60 if (delegate_
&& delegate_
->HasSendHeadersComplete()) {
61 return QuicDataStream::EffectivePriority();
63 return QuicWriteBlockedList::kHighestPriority
;
66 int QuicReliableClientStream::WriteStreamData(
67 base::StringPiece data
,
69 const CompletionCallback
& callback
) {
70 // We should not have data buffered.
71 DCHECK(!HasBufferedData());
72 // Writes the data, or buffers it.
73 WriteOrBufferData(data
, fin
, nullptr);
74 if (!HasBufferedData()) {
79 return ERR_IO_PENDING
;
82 void QuicReliableClientStream::SetDelegate(
83 QuicReliableClientStream::Delegate
* delegate
) {
84 DCHECK(!(delegate_
&& delegate
));
88 void QuicReliableClientStream::OnError(int error
) {
90 QuicReliableClientStream::Delegate
* delegate
= delegate_
;
92 delegate
->OnError(error
);
96 bool QuicReliableClientStream::CanWrite(const CompletionCallback
& callback
) {
97 bool can_write
= session()->connection()->CanWrite(HAS_RETRANSMITTABLE_DATA
);
99 session()->MarkWriteBlocked(id(), EffectivePriority());
100 DCHECK(callback_
.is_null());
101 callback_
= callback
;