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"
12 CronetUploadDataStream::CronetUploadDataStream(Delegate
* delegate
, int64 size
)
13 : UploadDataStream(size
< 0, 0),
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),
24 CronetUploadDataStream::~CronetUploadDataStream() {
25 delegate_
->OnUploadDataStreamDestroyed();
28 int CronetUploadDataStream::InitInternal() {
29 // ResetInternal should have been called before init, if the stream was in
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.
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
45 DCHECK(!read_in_progress_
);
46 DCHECK(!rewind_in_progress_
);
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_
)
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_
);
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));
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.
99 // ResetInternal has been called, but still waiting on InitInternal.
100 if (!waiting_on_read_
)
103 waiting_on_read_
= false;
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_
)
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;
138 } // namespace cronet