OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/cronet_upload_data_stream_adapter.h" | 5 #include "components/cronet/android/cronet_upload_data_stream.h" |
6 | 6 |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 | 9 |
10 namespace cronet { | 10 namespace cronet { |
11 | 11 |
12 CronetUploadDataStreamAdapter::CronetUploadDataStreamAdapter(Delegate* delegate, | 12 CronetUploadDataStream::CronetUploadDataStream(Delegate* delegate, int64 size) |
13 int64 size) | |
14 : UploadDataStream(size < 0, 0), | 13 : UploadDataStream(size < 0, 0), |
15 size_(size), | 14 size_(size), |
16 waiting_on_read_(false), | 15 waiting_on_read_(false), |
17 read_in_progress_(false), | 16 read_in_progress_(false), |
18 waiting_on_rewind_(false), | 17 waiting_on_rewind_(false), |
19 rewind_in_progress_(false), | 18 rewind_in_progress_(false), |
20 at_front_of_stream_(true), | 19 at_front_of_stream_(true), |
21 delegate_(delegate), | 20 delegate_(delegate), |
22 weak_factory_(this) { | 21 weak_factory_(this) { |
23 } | 22 } |
24 | 23 |
25 CronetUploadDataStreamAdapter::~CronetUploadDataStreamAdapter() { | 24 CronetUploadDataStream::~CronetUploadDataStream() { |
26 delegate_->OnAdapterDestroyed(); | 25 delegate_->OnUploadDataStreamDestroyed(); |
27 } | 26 } |
28 | 27 |
29 int CronetUploadDataStreamAdapter::InitInternal() { | 28 int CronetUploadDataStream::InitInternal() { |
30 // ResetInternal should have been called before init, if the adapter was in | 29 // ResetInternal should have been called before init, if the stream was in |
31 // use. | 30 // use. |
32 DCHECK(!waiting_on_read_); | 31 DCHECK(!waiting_on_read_); |
33 DCHECK(!waiting_on_rewind_); | 32 DCHECK(!waiting_on_rewind_); |
34 | 33 |
35 if (!weak_factory_.HasWeakPtrs()) | 34 if (!weak_factory_.HasWeakPtrs()) |
36 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr()); | 35 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr()); |
37 | 36 |
38 // Set size of non-chunked uploads. | 37 // Set size of non-chunked uploads. |
39 if (size_ >= 0) | 38 if (size_ >= 0) |
40 SetSize(static_cast<uint64>(size_)); | 39 SetSize(static_cast<uint64>(size_)); |
41 | 40 |
42 // If already at the front of the stream, nothing to do. | 41 // If already at the front of the stream, nothing to do. |
43 if (at_front_of_stream_) { | 42 if (at_front_of_stream_) { |
44 // Being at the front of the stream implies there's no read or rewind in | 43 // Being at the front of the stream implies there's no read or rewind in |
45 // progress. | 44 // progress. |
46 DCHECK(!read_in_progress_); | 45 DCHECK(!read_in_progress_); |
47 DCHECK(!rewind_in_progress_); | 46 DCHECK(!rewind_in_progress_); |
48 return net::OK; | 47 return net::OK; |
49 } | 48 } |
50 | 49 |
51 // Otherwise, the request is now waiting for the stream to be rewound. | 50 // Otherwise, the request is now waiting for the stream to be rewound. |
52 waiting_on_rewind_ = true; | 51 waiting_on_rewind_ = true; |
53 | 52 |
54 // Start rewinding the stream if no operation is in progress. | 53 // Start rewinding the stream if no operation is in progress. |
55 if (!read_in_progress_ && !rewind_in_progress_) | 54 if (!read_in_progress_ && !rewind_in_progress_) |
56 StartRewind(); | 55 StartRewind(); |
57 return net::ERR_IO_PENDING; | 56 return net::ERR_IO_PENDING; |
58 } | 57 } |
59 | 58 |
60 int CronetUploadDataStreamAdapter::ReadInternal(net::IOBuffer* buf, | 59 int CronetUploadDataStream::ReadInternal(net::IOBuffer* buf, int buf_len) { |
61 int buf_len) { | |
62 // All pending operations should have completed before a read can start. | 60 // All pending operations should have completed before a read can start. |
63 DCHECK(!waiting_on_read_); | 61 DCHECK(!waiting_on_read_); |
64 DCHECK(!read_in_progress_); | 62 DCHECK(!read_in_progress_); |
65 DCHECK(!waiting_on_rewind_); | 63 DCHECK(!waiting_on_rewind_); |
66 DCHECK(!rewind_in_progress_); | 64 DCHECK(!rewind_in_progress_); |
67 | 65 |
68 DCHECK(buf); | 66 DCHECK(buf); |
69 DCHECK_GT(buf_len, 0); | 67 DCHECK_GT(buf_len, 0); |
70 | 68 |
71 read_in_progress_ = true; | 69 read_in_progress_ = true; |
72 waiting_on_read_ = true; | 70 waiting_on_read_ = true; |
73 at_front_of_stream_ = false; | 71 at_front_of_stream_ = false; |
74 delegate_->Read(buf, buf_len); | 72 delegate_->Read(buf, buf_len); |
75 return net::ERR_IO_PENDING; | 73 return net::ERR_IO_PENDING; |
76 } | 74 } |
77 | 75 |
78 void CronetUploadDataStreamAdapter::ResetInternal() { | 76 void CronetUploadDataStream::ResetInternal() { |
79 // Consumer is not waiting on any operation. Note that the active operation, | 77 // Consumer is not waiting on any operation. Note that the active operation, |
80 // if any, will continue. | 78 // if any, will continue. |
81 waiting_on_read_ = false; | 79 waiting_on_read_ = false; |
82 waiting_on_rewind_ = false; | 80 waiting_on_rewind_ = false; |
83 } | 81 } |
84 | 82 |
85 void CronetUploadDataStreamAdapter::OnReadSuccess(int bytes_read, | 83 void CronetUploadDataStream::OnReadSuccess(int bytes_read, bool final_chunk) { |
86 bool final_chunk) { | |
87 DCHECK(read_in_progress_); | 84 DCHECK(read_in_progress_); |
88 DCHECK(!rewind_in_progress_); | 85 DCHECK(!rewind_in_progress_); |
89 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0)); | 86 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0)); |
90 if (!is_chunked()) { | 87 if (!is_chunked()) { |
91 DCHECK(!final_chunk); | 88 DCHECK(!final_chunk); |
92 } | 89 } |
93 | 90 |
94 read_in_progress_ = false; | 91 read_in_progress_ = false; |
95 | 92 |
96 if (waiting_on_rewind_) { | 93 if (waiting_on_rewind_) { |
97 DCHECK(!waiting_on_read_); | 94 DCHECK(!waiting_on_read_); |
98 // Since a read just completed, can't be at the front of the stream. | 95 // Since a read just completed, can't be at the front of the stream. |
99 StartRewind(); | 96 StartRewind(); |
100 return; | 97 return; |
101 } | 98 } |
102 // ResetInternal has been called, but still waiting on InitInternal. | 99 // ResetInternal has been called, but still waiting on InitInternal. |
103 if (!waiting_on_read_) | 100 if (!waiting_on_read_) |
104 return; | 101 return; |
105 | 102 |
106 waiting_on_read_ = false; | 103 waiting_on_read_ = false; |
107 if (final_chunk) | 104 if (final_chunk) |
108 SetIsFinalChunk(); | 105 SetIsFinalChunk(); |
109 OnReadCompleted(bytes_read); | 106 OnReadCompleted(bytes_read); |
110 } | 107 } |
111 | 108 |
112 void CronetUploadDataStreamAdapter::OnRewindSuccess() { | 109 void CronetUploadDataStream::OnRewindSuccess() { |
113 DCHECK(!waiting_on_read_); | 110 DCHECK(!waiting_on_read_); |
114 DCHECK(!read_in_progress_); | 111 DCHECK(!read_in_progress_); |
115 DCHECK(rewind_in_progress_); | 112 DCHECK(rewind_in_progress_); |
116 DCHECK(!at_front_of_stream_); | 113 DCHECK(!at_front_of_stream_); |
117 | 114 |
118 rewind_in_progress_ = false; | 115 rewind_in_progress_ = false; |
119 at_front_of_stream_ = true; | 116 at_front_of_stream_ = true; |
120 | 117 |
121 // Possible that ResetInternal was called since the rewind was started, but | 118 // Possible that ResetInternal was called since the rewind was started, but |
122 // InitInternal has not been. | 119 // InitInternal has not been. |
123 if (!waiting_on_rewind_) | 120 if (!waiting_on_rewind_) |
124 return; | 121 return; |
125 | 122 |
126 waiting_on_rewind_ = false; | 123 waiting_on_rewind_ = false; |
127 OnInitCompleted(net::OK); | 124 OnInitCompleted(net::OK); |
128 } | 125 } |
129 | 126 |
130 void CronetUploadDataStreamAdapter::StartRewind() { | 127 void CronetUploadDataStream::StartRewind() { |
131 DCHECK(!waiting_on_read_); | 128 DCHECK(!waiting_on_read_); |
132 DCHECK(!read_in_progress_); | 129 DCHECK(!read_in_progress_); |
133 DCHECK(waiting_on_rewind_); | 130 DCHECK(waiting_on_rewind_); |
134 DCHECK(!rewind_in_progress_); | 131 DCHECK(!rewind_in_progress_); |
135 DCHECK(!at_front_of_stream_); | 132 DCHECK(!at_front_of_stream_); |
136 | 133 |
137 rewind_in_progress_ = true; | 134 rewind_in_progress_ = true; |
138 delegate_->Rewind(); | 135 delegate_->Rewind(); |
139 } | 136 } |
140 | 137 |
141 } // namespace cronet | 138 } // namespace cronet |
OLD | NEW |