| 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 // Emedder 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 DCHECK(!is_chunked() || !final_chunk); |
| 91 |
| 92 read_in_progress_ = false; |
| 93 |
| 94 if (waiting_on_rewind_) { |
| 95 DCHECK(!waiting_on_read_); |
| 96 // Since a read just completed, can't be at the front of the stream. |
| 97 StartRewind(); |
| 98 return; |
| 99 } |
| 100 // ResetInternal has been called, but still waiting on InitInternal. |
| 101 if (!waiting_on_read_) |
| 102 return; |
| 103 |
| 104 waiting_on_read_ = false; |
| 105 if (final_chunk) |
| 106 SetIsFinalChunk(); |
| 107 OnReadCompleted(bytes_read); |
| 108 } |
| 109 |
| 110 void CronetUploadDataStreamAdapter::OnRewindSuccess() { |
| 111 DCHECK(!waiting_on_read_); |
| 112 DCHECK(!read_in_progress_); |
| 113 DCHECK(rewind_in_progress_); |
| 114 DCHECK(!at_front_of_stream_); |
| 115 |
| 116 rewind_in_progress_ = false; |
| 117 at_front_of_stream_ = true; |
| 118 |
| 119 // Possible that ResetInternal was called since the rewind was started, but |
| 120 // InitInternal has not been. |
| 121 if (!waiting_on_rewind_) |
| 122 return; |
| 123 |
| 124 waiting_on_rewind_ = false; |
| 125 OnInitCompleted(net::OK); |
| 126 } |
| 127 |
| 128 void CronetUploadDataStreamAdapter::StartRewind() { |
| 129 DCHECK(!waiting_on_read_); |
| 130 DCHECK(!read_in_progress_); |
| 131 DCHECK(waiting_on_rewind_); |
| 132 DCHECK(!rewind_in_progress_); |
| 133 DCHECK(!at_front_of_stream_); |
| 134 |
| 135 rewind_in_progress_ = true; |
| 136 delegate_->Rewind(); |
| 137 } |
| 138 |
| 139 } // namespace cronet |
| OLD | NEW |