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

Side by Side Diff: net/base/upload_data_stream.cc

Issue 10913179: net: Make UploadDataStream::Init() asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: _ Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/base/upload_data_stream.h" 5 #include "net/base/upload_data_stream.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/base/upload_element_reader.h" 10 #include "net/base/upload_element_reader.h"
11 11
12 namespace net { 12 namespace net {
13 13
14 bool UploadDataStream::merge_chunks_ = true; 14 bool UploadDataStream::merge_chunks_ = true;
15 15
16 // static 16 // static
17 void UploadDataStream::ResetMergeChunks() { 17 void UploadDataStream::ResetMergeChunks() {
18 // WARNING: merge_chunks_ must match the above initializer. 18 // WARNING: merge_chunks_ must match the above initializer.
19 merge_chunks_ = true; 19 merge_chunks_ = true;
20 } 20 }
21 21
22 UploadDataStream::UploadDataStream(UploadData* upload_data) 22 UploadDataStream::UploadDataStream(UploadData* upload_data)
23 : upload_data_(upload_data), 23 : upload_data_(upload_data),
24 element_index_(0), 24 element_index_(0),
25 total_size_(0), 25 total_size_(0),
26 current_position_(0), 26 current_position_(0),
27 initialized_successfully_(false) { 27 initialized_successfully_(false),
28 weak_ptr_factory_(this) {
28 const std::vector<UploadElement>& elements = *upload_data_->elements(); 29 const std::vector<UploadElement>& elements = *upload_data_->elements();
29 for (size_t i = 0; i < elements.size(); ++i) 30 for (size_t i = 0; i < elements.size(); ++i)
30 element_readers_.push_back(UploadElementReader::Create(elements[i])); 31 element_readers_.push_back(UploadElementReader::Create(elements[i]));
31 } 32 }
32 33
33 UploadDataStream::~UploadDataStream() { 34 UploadDataStream::~UploadDataStream() {
34 } 35 }
35 36
36 int UploadDataStream::Init() { 37 int UploadDataStream::Init(const CompletionCallback& callback) {
37 DCHECK(!initialized_successfully_); 38 DCHECK(!initialized_successfully_);
38 39
39 uint64 total_size = 0; 40 // Use fast path when initialization can be done synchronously.
41 if (IsInMemory())
42 return InitSync();
43
44 InitInternal(callback, 0, OK);
45 return ERR_IO_PENDING;
46 }
47
48 int UploadDataStream::InitSync() {
49 DCHECK(!initialized_successfully_);
50
51 // Initialize all readers synchronously.
40 for (size_t i = 0; i < element_readers_.size(); ++i) { 52 for (size_t i = 0; i < element_readers_.size(); ++i) {
41 UploadElementReader* reader = element_readers_[i]; 53 UploadElementReader* reader = element_readers_[i];
42 const int result = reader->InitSync(); 54 const int result = reader->InitSync();
43 if (result != OK) 55 if (result != OK)
44 return result; 56 return result;
45 if (!is_chunked())
46 total_size += reader->GetContentLength();
47 } 57 }
48 total_size_ = total_size;
49 58
50 initialized_successfully_ = true; 59 FinalizeInitialization();
51 return OK; 60 return OK;
52 } 61 }
53 62
54 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { 63 int UploadDataStream::Read(IOBuffer* buf, int buf_len) {
55 DCHECK(initialized_successfully_); 64 DCHECK(initialized_successfully_);
56 65
57 // Initialize readers for newly appended chunks. 66 // Initialize readers for newly appended chunks.
58 if (is_chunked()) { 67 if (is_chunked()) {
59 const std::vector<UploadElement>& elements = *upload_data_->elements(); 68 const std::vector<UploadElement>& elements = *upload_data_->elements();
60 DCHECK_LE(element_readers_.size(), elements.size()); 69 DCHECK_LE(element_readers_.size(), elements.size());
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 if (is_chunked()) 120 if (is_chunked())
112 return false; 121 return false;
113 122
114 for (size_t i = 0; i < element_readers_.size(); ++i) { 123 for (size_t i = 0; i < element_readers_.size(); ++i) {
115 if (!element_readers_[i]->IsInMemory()) 124 if (!element_readers_[i]->IsInMemory())
116 return false; 125 return false;
117 } 126 }
118 return true; 127 return true;
119 } 128 }
120 129
130 void UploadDataStream::InitInternal(const CompletionCallback& callback,
131 int index,
willchan no longer on Chromium 2012/09/11 19:32:40 indentation is off
hashimoto 2012/09/11 22:51:31 Done.
132 int last_result) {
willchan no longer on Chromium 2012/09/11 19:32:40 I suggest renaming: s/index/start_index/ - hopeful
hashimoto 2012/09/11 22:51:31 Done.
133 DCHECK(!initialized_successfully_);
134
willchan no longer on Chromium 2012/09/11 19:32:40 I recommend adding a DCHECK_NE(ERR_IO_PENDING, las
hashimoto 2012/09/11 22:51:31 Done.
135 // Check the last result.
136 if (last_result != OK) {
willchan no longer on Chromium 2012/09/11 19:32:40 I wonder if we should do element_readers_.clear();
hashimoto 2012/09/11 22:51:31 Done.
137 callback.Run(last_result);
138 return;
139 }
140
141 // Call Init() for all elements.
142 for (size_t i = index; i < element_readers_.size(); ++i) {
143 UploadElementReader* reader = element_readers_[i];
144 const int new_result = reader->Init(
145 base::Bind(&UploadDataStream::InitInternal,
146 weak_ptr_factory_.GetWeakPtr(),
147 callback,
148 index + 1));
willchan no longer on Chromium 2012/09/11 19:32:40 Is this a bug? Should it be i + 1? Can you write a
hashimoto 2012/09/11 22:51:31 You are absolutely right. Thank you for finding th
149 if (new_result != OK) {
150 if (new_result != ERR_IO_PENDING)
151 callback.Run(new_result);
152 return;
153 }
154 }
155
156 // Finalize initialization.
157 FinalizeInitialization();
158 callback.Run(OK);
159 }
160
161 void UploadDataStream::FinalizeInitialization() {
162 DCHECK(!initialized_successfully_);
163 if (!is_chunked()) {
164 uint64 total_size = 0;
165 for (size_t i = 0; i < element_readers_.size(); ++i) {
166 UploadElementReader* reader = element_readers_[i];
167 total_size += reader->GetContentLength();
willchan no longer on Chromium 2012/09/11 19:32:40 Just double checking...could this block? I'm guess
hashimoto 2012/09/11 22:51:31 For Google Drive, I'm intended to fetch the conten
168 }
169 total_size_ = total_size;
170 }
171 initialized_successfully_ = true;
172 }
173
121 } // namespace net 174 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698