Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / cronet / android / cronet_upload_data_stream_adapter.cc
blob4ac38b9467237da8e3227a7e909ddd2d8fb5ff35
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_adapter.h"
7 #include "net/base/io_buffer.h"
8 #include "net/base/net_errors.h"
10 namespace cronet {
12 CronetUploadDataStreamAdapter::CronetUploadDataStreamAdapter(Delegate* delegate,
13 int64 size)
14 : UploadDataStream(size < 0, 0),
15 size_(size),
16 waiting_on_read_(false),
17 read_in_progress_(false),
18 waiting_on_rewind_(false),
19 rewind_in_progress_(false),
20 at_front_of_stream_(true),
21 delegate_(delegate),
22 weak_factory_(this) {
25 CronetUploadDataStreamAdapter::~CronetUploadDataStreamAdapter() {
26 delegate_->OnAdapterDestroyed();
29 int CronetUploadDataStreamAdapter::InitInternal() {
30 // ResetInternal should have been called before init, if the adapter was in
31 // use.
32 DCHECK(!waiting_on_read_);
33 DCHECK(!waiting_on_rewind_);
35 if (!weak_factory_.HasWeakPtrs())
36 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr());
38 // Set size of non-chunked uploads.
39 if (size_ >= 0)
40 SetSize(static_cast<uint64>(size_));
42 // If already at the front of the stream, nothing to do.
43 if (at_front_of_stream_) {
44 // Being at the front of the stream implies there's no read or rewind in
45 // progress.
46 DCHECK(!read_in_progress_);
47 DCHECK(!rewind_in_progress_);
48 return net::OK;
51 // Otherwise, the request is now waiting for the stream to be rewound.
52 waiting_on_rewind_ = true;
54 // Start rewinding the stream if no operation is in progress.
55 if (!read_in_progress_ && !rewind_in_progress_)
56 StartRewind();
57 return net::ERR_IO_PENDING;
60 int CronetUploadDataStreamAdapter::ReadInternal(net::IOBuffer* buf,
61 int buf_len) {
62 // All pending operations should have completed before a read can start.
63 DCHECK(!waiting_on_read_);
64 DCHECK(!read_in_progress_);
65 DCHECK(!waiting_on_rewind_);
66 DCHECK(!rewind_in_progress_);
68 DCHECK(buf);
69 DCHECK_GT(buf_len, 0);
71 read_in_progress_ = true;
72 waiting_on_read_ = true;
73 at_front_of_stream_ = false;
74 delegate_->Read(buf, buf_len);
75 return net::ERR_IO_PENDING;
78 void CronetUploadDataStreamAdapter::ResetInternal() {
79 // Consumer is not waiting on any operation. Note that the active operation,
80 // if any, will continue.
81 waiting_on_read_ = false;
82 waiting_on_rewind_ = false;
85 void CronetUploadDataStreamAdapter::OnReadSuccess(int bytes_read,
86 bool final_chunk) {
87 DCHECK(read_in_progress_);
88 DCHECK(!rewind_in_progress_);
89 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0));
90 if (!is_chunked()) {
91 DCHECK(!final_chunk);
94 read_in_progress_ = false;
96 if (waiting_on_rewind_) {
97 DCHECK(!waiting_on_read_);
98 // Since a read just completed, can't be at the front of the stream.
99 StartRewind();
100 return;
102 // ResetInternal has been called, but still waiting on InitInternal.
103 if (!waiting_on_read_)
104 return;
106 waiting_on_read_ = false;
107 if (final_chunk)
108 SetIsFinalChunk();
109 OnReadCompleted(bytes_read);
112 void CronetUploadDataStreamAdapter::OnRewindSuccess() {
113 DCHECK(!waiting_on_read_);
114 DCHECK(!read_in_progress_);
115 DCHECK(rewind_in_progress_);
116 DCHECK(!at_front_of_stream_);
118 rewind_in_progress_ = false;
119 at_front_of_stream_ = true;
121 // Possible that ResetInternal was called since the rewind was started, but
122 // InitInternal has not been.
123 if (!waiting_on_rewind_)
124 return;
126 waiting_on_rewind_ = false;
127 OnInitCompleted(net::OK);
130 void CronetUploadDataStreamAdapter::StartRewind() {
131 DCHECK(!waiting_on_read_);
132 DCHECK(!read_in_progress_);
133 DCHECK(waiting_on_rewind_);
134 DCHECK(!rewind_in_progress_);
135 DCHECK(!at_front_of_stream_);
137 rewind_in_progress_ = true;
138 delegate_->Rewind();
141 } // namespace cronet