Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: components/cronet/android/cronet_upload_data_stream_adapter.cc

Issue 849903002: [Cronet] Upload support for async APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Combined with Matt's CL Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 }
27
28 int CronetUploadDataStreamAdapter::InitInternal() {
29 // ResetInternal should have been called before init, if the adapter was in
30 // use.
31 DCHECK(!waiting_on_read_);
32 DCHECK(!waiting_on_rewind_);
33
34 if (!weak_factory_.HasWeakPtrs())
35 delegate_->InitializeOnNetworkThread(weak_factory_.GetWeakPtr());
36
37 // Set size of non-chunked uploads.
38 if (size_ >= 0)
39 SetSize(static_cast<uint64>(size_));
40
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
44 // progress.
45 DCHECK(!read_in_progress_);
46 DCHECK(!rewind_in_progress_);
47 return net::OK;
48 }
49
50 // Otherwise, the request is now waiting for the stream to be rewound.
51 waiting_on_rewind_ = true;
52
53 // Start rewinding the stream if no operation is in progress.
54 if (!read_in_progress_ && !rewind_in_progress_)
55 StartRewind();
56 return net::ERR_IO_PENDING;
57 }
58
59 int CronetUploadDataStreamAdapter::ReadInternal(net::IOBuffer* buf,
60 int buf_len) {
61 // All pending operations should have completed before a read can start.
62 DCHECK(!waiting_on_read_);
63 DCHECK(!read_in_progress_);
64 DCHECK(!waiting_on_rewind_);
65 DCHECK(!rewind_in_progress_);
66
67 DCHECK(buf);
68 DCHECK_GT(buf_len, 0);
69
70 read_in_progress_ = true;
71 waiting_on_read_ = true;
72 at_front_of_stream_ = false;
73 delegate_->Read(buf, buf_len);
74 return net::ERR_IO_PENDING;
75 }
76
77 void CronetUploadDataStreamAdapter::ResetInternal() {
78 // Emedder is not waiting on any operation. Note that the active operation,
79 // if any, will continue.
80 waiting_on_read_ = false;
81 waiting_on_rewind_ = false;
82 }
83
84 void CronetUploadDataStreamAdapter::OnReadSuccess(int bytes_read,
85 bool final_chunk) {
86 DCHECK(read_in_progress_);
87 DCHECK(!rewind_in_progress_);
88 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0));
89 DCHECK(!is_chunked() || !final_chunk);
90
91 read_in_progress_ = false;
92
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.
96 StartRewind();
97 return;
98 }
99 // ResetInternal has been called, but still waiting on InitInternal.
100 if (!waiting_on_read_)
101 return;
102
103 waiting_on_read_ = false;
104 if (final_chunk)
105 SetIsFinalChunk();
106 OnReadCompleted(bytes_read);
107 }
108
109 void CronetUploadDataStreamAdapter::OnRewindSuccess() {
110 DCHECK(!waiting_on_read_);
111 DCHECK(!read_in_progress_);
112 DCHECK(rewind_in_progress_);
113 DCHECK(!at_front_of_stream_);
114
115 rewind_in_progress_ = false;
116 at_front_of_stream_ = true;
117
118 // Possible that ResetInternal was called since the rewind was started, but
119 // InitInternal has not been.
120 if (!waiting_on_rewind_)
121 return;
122
123 waiting_on_rewind_ = false;
124 OnInitCompleted(net::OK);
125 }
126
127 void CronetUploadDataStreamAdapter::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_);
133
134 rewind_in_progress_ = true;
135 delegate_->Rewind();
136 }
137
138 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698