Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / components / cronet / android / cronet_upload_data_stream.cc
blobc933f403765bb8bb2e74a6fe71f1b01c1dca9ef9
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.
5 #include "components/cronet/android/cronet_upload_data_stream.h"
7 #include "net/base/io_buffer.h"
8 #include "net/base/net_errors.h"
10 namespace cronet {
12 CronetUploadDataStream::CronetUploadDataStream(Delegate* delegate, int64 size)
13 : UploadDataStream(size < 0, 0),
14 size_(size),
15 waiting_on_read_(false),
16 read_in_progress_(false),
17 waiting_on_rewind_(false),
18 rewind_in_progress_(false),
19 at_front_of_stream_(true),
20 delegate_(delegate),
21 weak_factory_(this) {
24 CronetUploadDataStream::~CronetUploadDataStream() {
25 delegate_->OnUploadDataStreamDestroyed();
28 int CronetUploadDataStream::InitInternal() {
29 // ResetInternal should have been called before init, if the stream was in
30 // use.
31 DCHECK(!waiting_on_read_);
32 DCHECK(!waiting_on_rewind_);
34 if (!weak_factory_.HasWeakPtrs())
35 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr());
37 // Set size of non-chunked uploads.
38 if (size_ >= 0)
39 SetSize(static_cast<uint64>(size_));
41 // If already at the front of the stream, nothing to do.
42 if (at_front_of_stream_) {
43 // Being at the front of the stream implies there's no read or rewind in
44 // progress.
45 DCHECK(!read_in_progress_);
46 DCHECK(!rewind_in_progress_);
47 return net::OK;
50 // Otherwise, the request is now waiting for the stream to be rewound.
51 waiting_on_rewind_ = true;
53 // Start rewinding the stream if no operation is in progress.
54 if (!read_in_progress_ && !rewind_in_progress_)
55 StartRewind();
56 return net::ERR_IO_PENDING;
59 int CronetUploadDataStream::ReadInternal(net::IOBuffer* buf, int buf_len) {
60 // All pending operations should have completed before a read can start.
61 DCHECK(!waiting_on_read_);
62 DCHECK(!read_in_progress_);
63 DCHECK(!waiting_on_rewind_);
64 DCHECK(!rewind_in_progress_);
66 DCHECK(buf);
67 DCHECK_GT(buf_len, 0);
69 read_in_progress_ = true;
70 waiting_on_read_ = true;
71 at_front_of_stream_ = false;
72 delegate_->Read(buf, buf_len);
73 return net::ERR_IO_PENDING;
76 void CronetUploadDataStream::ResetInternal() {
77 // Consumer is not waiting on any operation. Note that the active operation,
78 // if any, will continue.
79 waiting_on_read_ = false;
80 waiting_on_rewind_ = false;
83 void CronetUploadDataStream::OnReadSuccess(int bytes_read, bool final_chunk) {
84 DCHECK(read_in_progress_);
85 DCHECK(!rewind_in_progress_);
86 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0));
87 if (!is_chunked()) {
88 DCHECK(!final_chunk);
91 read_in_progress_ = false;
93 if (waiting_on_rewind_) {
94 DCHECK(!waiting_on_read_);
95 // Since a read just completed, can't be at the front of the stream.
96 StartRewind();
97 return;
99 // ResetInternal has been called, but still waiting on InitInternal.
100 if (!waiting_on_read_)
101 return;
103 waiting_on_read_ = false;
104 if (final_chunk)
105 SetIsFinalChunk();
106 OnReadCompleted(bytes_read);
109 void CronetUploadDataStream::OnRewindSuccess() {
110 DCHECK(!waiting_on_read_);
111 DCHECK(!read_in_progress_);
112 DCHECK(rewind_in_progress_);
113 DCHECK(!at_front_of_stream_);
115 rewind_in_progress_ = false;
116 at_front_of_stream_ = true;
118 // Possible that ResetInternal was called since the rewind was started, but
119 // InitInternal has not been.
120 if (!waiting_on_rewind_)
121 return;
123 waiting_on_rewind_ = false;
124 OnInitCompleted(net::OK);
127 void CronetUploadDataStream::StartRewind() {
128 DCHECK(!waiting_on_read_);
129 DCHECK(!read_in_progress_);
130 DCHECK(waiting_on_rewind_);
131 DCHECK(!rewind_in_progress_);
132 DCHECK(!at_front_of_stream_);
134 rewind_in_progress_ = true;
135 delegate_->Rewind();
138 } // namespace cronet