Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / quic / quic_data_stream.cc
blobaf5c5de95b48c10e5970b989e44be63f10f1fae6
1 // Copyright 2013 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_data_stream.h"
7 #include "base/logging.h"
8 #include "net/quic/quic_spdy_session.h"
9 #include "net/quic/quic_utils.h"
10 #include "net/quic/quic_write_blocked_list.h"
12 using base::StringPiece;
13 using std::min;
15 namespace net {
17 #define ENDPOINT \
18 (session()->perspective() == Perspective::IS_SERVER ? "Server: " : "Client:" \
19 " ")
21 namespace {
23 // This is somewhat arbitrary. It's possible, but unlikely, we will either fail
24 // to set a priority client-side, or cancel a stream before stripping the
25 // priority from the wire server-side. In either case, start out with a
26 // priority in the middle.
27 QuicPriority kDefaultPriority = 3;
29 } // namespace
31 QuicDataStream::QuicDataStream(QuicStreamId id, QuicSpdySession* spdy_session)
32 : ReliableQuicStream(id, spdy_session),
33 spdy_session_(spdy_session),
34 visitor_(nullptr),
35 headers_decompressed_(false),
36 priority_(kDefaultPriority) {
37 DCHECK_NE(kCryptoStreamId, id);
38 // Don't receive any callbacks from the sequencer until headers
39 // are complete.
40 sequencer()->SetBlockedUntilFlush();
43 QuicDataStream::~QuicDataStream() {
46 size_t QuicDataStream::WriteHeaders(
47 const SpdyHeaderBlock& header_block,
48 bool fin,
49 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
50 size_t bytes_written = spdy_session_->WriteHeaders(
51 id(), header_block, fin, priority_, ack_notifier_delegate);
52 if (fin) {
53 // TODO(rch): Add test to ensure fin_sent_ is set whenever a fin is sent.
54 set_fin_sent(true);
55 CloseWriteSide();
57 return bytes_written;
60 size_t QuicDataStream::Readv(const struct iovec* iov, size_t iov_len) {
61 DCHECK(FinishedReadingHeaders());
62 return sequencer()->Readv(iov, iov_len);
65 int QuicDataStream::GetReadableRegions(iovec* iov, size_t iov_len) const {
66 DCHECK(FinishedReadingHeaders());
67 return sequencer()->GetReadableRegions(iov, iov_len);
70 void QuicDataStream::MarkConsumed(size_t num_bytes) {
71 DCHECK(FinishedReadingHeaders());
72 return sequencer()->MarkConsumed(num_bytes);
75 bool QuicDataStream::IsDoneReading() const {
76 if (!headers_decompressed_ || !decompressed_headers_.empty()) {
77 return false;
79 return sequencer()->IsClosed();
82 bool QuicDataStream::HasBytesToRead() const {
83 return !decompressed_headers_.empty() || sequencer()->HasBytesToRead();
86 void QuicDataStream::MarkHeadersConsumed(size_t bytes_consumed) {
87 decompressed_headers_.erase(0, bytes_consumed);
88 if (FinishedReadingHeaders()) {
89 sequencer()->SetUnblocked();
93 void QuicDataStream::set_priority(QuicPriority priority) {
94 DCHECK_EQ(0u, stream_bytes_written());
95 priority_ = priority;
98 QuicPriority QuicDataStream::EffectivePriority() const {
99 return priority();
102 void QuicDataStream::OnStreamHeaders(StringPiece headers_data) {
103 headers_data.AppendToString(&decompressed_headers_);
106 void QuicDataStream::OnStreamHeadersPriority(QuicPriority priority) {
107 DCHECK_EQ(Perspective::IS_SERVER, session()->connection()->perspective());
108 set_priority(priority);
111 void QuicDataStream::OnStreamHeadersComplete(bool fin, size_t frame_len) {
112 headers_decompressed_ = true;
113 if (fin) {
114 OnStreamFrame(QuicStreamFrame(id(), fin, 0, StringPiece()));
116 if (FinishedReadingHeaders()) {
117 sequencer()->SetUnblocked();
121 void QuicDataStream::OnClose() {
122 ReliableQuicStream::OnClose();
124 if (visitor_) {
125 Visitor* visitor = visitor_;
126 // Calling Visitor::OnClose() may result the destruction of the visitor,
127 // so we need to ensure we don't call it again.
128 visitor_ = nullptr;
129 visitor->OnClose(this);
133 bool QuicDataStream::FinishedReadingHeaders() const {
134 return headers_decompressed_ && decompressed_headers_.empty();
137 } // namespace net