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"
12 CronetUploadDataStreamAdapter::CronetUploadDataStreamAdapter(Delegate
* delegate
,
14 : UploadDataStream(size
< 0, 0),
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),
25 CronetUploadDataStreamAdapter::~CronetUploadDataStreamAdapter() {
26 delegate_
->OnAdapterDestroyed();
29 int CronetUploadDataStreamAdapter::InitInternal() {
30 // ResetInternal should have been called before init, if the adapter was in
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.
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
46 DCHECK(!read_in_progress_
);
47 DCHECK(!rewind_in_progress_
);
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_
)
57 return net::ERR_IO_PENDING
;
60 int CronetUploadDataStreamAdapter::ReadInternal(net::IOBuffer
* buf
,
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_
);
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
,
87 DCHECK(read_in_progress_
);
88 DCHECK(!rewind_in_progress_
);
89 DCHECK(bytes_read
> 0 || (final_chunk
&& bytes_read
== 0));
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.
102 // ResetInternal has been called, but still waiting on InitInternal.
103 if (!waiting_on_read_
)
106 waiting_on_read_
= false;
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_
)
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;
141 } // namespace cronet