OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/cronet/android/cronet_upload_data_stream_adapter.h" | |
6 | |
7 #include "net/base/io_buffer.h" | |
8 #include "net/base/net_errors.h" | |
9 | |
10 namespace cronet { | |
11 | |
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) { | |
23 } | |
24 | |
25 CronetUploadDataStreamAdapter::~CronetUploadDataStreamAdapter() { | |
26 delegate_->OnAdapterDestroyed(); | |
27 } | |
28 | |
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_); | |
34 | |
35 if (!weak_factory_.HasWeakPtrs()) | |
36 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr()); | |
37 | |
38 // Set size of non-chunked uploads. | |
39 if (size_ >= 0) | |
40 SetSize(static_cast<uint64>(size_)); | |
41 | |
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; | |
49 } | |
50 | |
51 // Otherwise, the request is now waiting for the stream to be rewound. | |
52 waiting_on_rewind_ = true; | |
53 | |
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; | |
58 } | |
59 | |
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_); | |
67 | |
68 DCHECK(buf); | |
69 DCHECK_GT(buf_len, 0); | |
70 | |
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; | |
76 } | |
77 | |
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; | |
83 } | |
84 | |
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); | |
92 } | |
93 | |
94 read_in_progress_ = false; | |
95 | |
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; | |
101 } | |
102 // ResetInternal has been called, but still waiting on InitInternal. | |
103 if (!waiting_on_read_) | |
104 return; | |
105 | |
106 waiting_on_read_ = false; | |
107 if (final_chunk) | |
108 SetIsFinalChunk(); | |
109 OnReadCompleted(bytes_read); | |
110 } | |
111 | |
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_); | |
117 | |
118 rewind_in_progress_ = false; | |
119 at_front_of_stream_ = true; | |
120 | |
121 // Possible that ResetInternal was called since the rewind was started, but | |
122 // InitInternal has not been. | |
123 if (!waiting_on_rewind_) | |
124 return; | |
125 | |
126 waiting_on_rewind_ = false; | |
127 OnInitCompleted(net::OK); | |
128 } | |
129 | |
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_); | |
136 | |
137 rewind_in_progress_ = true; | |
138 delegate_->Rewind(); | |
139 } | |
140 | |
141 } // namespace cronet | |
OLD | NEW |