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

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

Issue 10868064: net: Move data reading functionalities from UploadElement to UploadElementReader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 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/file_util.h"
8 #include "base/logging.h" 7 #include "base/logging.h"
9 #include "base/threading/thread_restrictions.h" 8 #include "base/threading/thread_restrictions.h"
10 #include "net/base/file_stream.h"
11 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "net/base/upload_element_reader.h"
13 12
14 namespace net { 13 namespace net {
15 14
16 bool UploadDataStream::merge_chunks_ = true; 15 bool UploadDataStream::merge_chunks_ = true;
17 16
18 // static 17 // static
19 void UploadDataStream::ResetMergeChunks() { 18 void UploadDataStream::ResetMergeChunks() {
20 // WARNING: merge_chunks_ must match the above initializer. 19 // WARNING: merge_chunks_ must match the above initializer.
21 merge_chunks_ = true; 20 merge_chunks_ = true;
22 } 21 }
23 22
24 UploadDataStream::UploadDataStream(UploadData* upload_data) 23 UploadDataStream::UploadDataStream(UploadData* upload_data)
25 : upload_data_(upload_data), 24 : upload_data_(upload_data),
26 element_index_(0), 25 element_index_(0),
27 total_size_(0), 26 total_size_(0),
28 current_position_(0), 27 current_position_(0),
29 initialized_successfully_(false) { 28 initialized_successfully_(false) {
29 const std::vector<UploadElement>& elements = *upload_data_->elements();
30 for (size_t i = 0; i < elements.size(); ++i)
31 element_readers_.push_back(UploadElementReader::Create(elements[i]));
30 } 32 }
31 33
32 UploadDataStream::~UploadDataStream() { 34 UploadDataStream::~UploadDataStream() {
35 // Temporarily allow until fix: http://crbug.com/72001.
36 base::ThreadRestrictions::ScopedAllowIO allow_io;
willchan no longer on Chromium 2012/09/05 02:55:59 Shouldn't this be moved to the specific UploadElem
hashimoto 2012/09/05 22:00:55 I thought if we placed ScopedAllowIO in UploadFile
willchan no longer on Chromium 2012/09/06 01:06:01 OIC. Yes, you're right then.
37 element_readers_.clear();
33 } 38 }
34 39
35 int UploadDataStream::Init() { 40 int UploadDataStream::Init() {
36 DCHECK(!initialized_successfully_); 41 DCHECK(!initialized_successfully_);
37 std::vector<UploadElement>* elements = upload_data_->elements_mutable(); 42
38 { 43 uint64 total_size = 0;
44 for (size_t i = 0; i < element_readers_.size(); ++i) {
45 // Temporarily allow until fix: http://crbug.com/72001.
39 base::ThreadRestrictions::ScopedAllowIO allow_io; 46 base::ThreadRestrictions::ScopedAllowIO allow_io;
40 total_size_ = 0; 47
41 if (!is_chunked()) { 48 UploadElementReader* reader = element_readers_[i];
42 for (size_t i = 0; i < elements->size(); ++i) 49 const int result = reader->InitSync();
43 total_size_ += (*elements)[i].GetContentLength(); 50 if (result != OK)
44 } 51 return result;
52 if (!is_chunked())
53 total_size += reader->GetContentLength();
45 } 54 }
46 55 total_size_ = total_size;
47 // If the underlying file has been changed and the expected file
48 // modification time is set, treat it as error. Note that the expected
49 // modification time from WebKit is based on time_t precision. So we
50 // have to convert both to time_t to compare. This check is used for
51 // sliced files.
52 for (size_t i = 0; i < elements->size(); ++i) {
53 const UploadElement& element = (*elements)[i];
54 if (element.type() == UploadElement::TYPE_FILE &&
55 !element.expected_file_modification_time().is_null()) {
56 // Temporarily allow until fix: http://crbug.com/72001.
57 base::ThreadRestrictions::ScopedAllowIO allow_io;
58 base::PlatformFileInfo info;
59 if (file_util::GetFileInfo(element.file_path(), &info) &&
60 element.expected_file_modification_time().ToTimeT() !=
61 info.last_modified.ToTimeT()) {
62 return ERR_UPLOAD_FILE_CHANGED;
63 }
64 }
65 }
66
67 // Reset the offset, as upload_data_ may already be read (i.e. UploadData
68 // can be reused for a new UploadDataStream).
69 for (size_t i = 0; i < elements->size(); ++i)
70 (*elements)[i].ResetOffset();
71 56
72 initialized_successfully_ = true; 57 initialized_successfully_ = true;
73 return OK; 58 return OK;
74 } 59 }
75 60
76 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { 61 int UploadDataStream::Read(IOBuffer* buf, int buf_len) {
77 std::vector<UploadElement>& elements = 62 DCHECK(initialized_successfully_);
78 *upload_data_->elements_mutable(); 63
64 // Initialize readers for newly appended chunks.
65 if (is_chunked()) {
66 const std::vector<UploadElement>& elements = *upload_data_->elements();
67 DCHECK_LE(element_readers_.size(), elements.size());
68
69 for (size_t i = element_readers_.size(); i < elements.size(); ++i) {
70 const UploadElement& element = elements[i];
71 DCHECK(element.type() == UploadElement::TYPE_BYTES);
72 UploadElementReader* reader =
73 new UploadBytesElementReader(element.bytes(), element.bytes_length());
74
75 const int rv = reader->InitSync();
76 DCHECK_EQ(rv, OK);
77 element_readers_.push_back(reader);
78 }
79 }
79 80
80 int bytes_copied = 0; 81 int bytes_copied = 0;
81 while (bytes_copied < buf_len && element_index_ < elements.size()) { 82 while (bytes_copied < buf_len && element_index_ < element_readers_.size()) {
82 UploadElement& element = elements[element_index_]; 83 // Temporarily allow until fix: http://crbug.com/72001.
84 base::ThreadRestrictions::ScopedAllowIO allow_io;
willchan no longer on Chromium 2012/09/05 02:55:59 Can't this also be moved to the specific UploadEle
hashimoto 2012/09/05 22:00:55 Done.
83 85
84 bytes_copied += element.ReadSync(buf->data() + bytes_copied, 86 UploadElementReader* reader = element_readers_[element_index_];
87 bytes_copied += reader->ReadSync(buf->data() + bytes_copied,
85 buf_len - bytes_copied); 88 buf_len - bytes_copied);
86 89 if (reader->BytesRemaining() == 0)
87 if (element.BytesRemaining() == 0)
88 ++element_index_; 90 ++element_index_;
89 91
90 if (is_chunked() && !merge_chunks_) 92 if (is_chunked() && !merge_chunks_)
91 break; 93 break;
92 } 94 }
93 95
94 current_position_ += bytes_copied; 96 current_position_ += bytes_copied;
95 if (is_chunked() && !IsEOF() && bytes_copied == 0) 97 if (is_chunked() && !IsEOF() && bytes_copied == 0)
96 return ERR_IO_PENDING; 98 return ERR_IO_PENDING;
97 99
98 return bytes_copied; 100 return bytes_copied;
99 } 101 }
100 102
101 bool UploadDataStream::IsEOF() const { 103 bool UploadDataStream::IsEOF() const {
104 DCHECK(initialized_successfully_);
102 const std::vector<UploadElement>& elements = *upload_data_->elements(); 105 const std::vector<UploadElement>& elements = *upload_data_->elements();
103 106
104 // Check if all elements are consumed. 107 // Check if all elements are consumed.
105 if (element_index_ == elements.size()) { 108 if (element_index_ == elements.size()) {
106 // If the upload data is chunked, check if the last chunk is appended. 109 // If the upload data is chunked, check if the last chunk is appended.
107 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) 110 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended())
108 return true; 111 return true;
109 } 112 }
110 return false; 113 return false;
111 } 114 }
112 115
113 bool UploadDataStream::IsInMemory() const { 116 bool UploadDataStream::IsInMemory() const {
114 // Chunks are in memory, but UploadData does not have all the chunks at 117 // Chunks are in memory, but UploadData does not have all the chunks at
115 // once. Chunks are provided progressively with AppendChunk() as chunks 118 // once. Chunks are provided progressively with AppendChunk() as chunks
116 // are ready. Check is_chunked_ here, rather than relying on the loop 119 // are ready. Check is_chunked_ here, rather than relying on the loop
117 // below, as there is a case that is_chunked_ is set to true, but the 120 // below, as there is a case that is_chunked_ is set to true, but the
118 // first chunk is not yet delivered. 121 // first chunk is not yet delivered.
119 if (is_chunked()) 122 if (is_chunked())
120 return false; 123 return false;
121 124
122 const std::vector<UploadElement>& elements = *upload_data_->elements(); 125 for (size_t i = 0; i < element_readers_.size(); ++i) {
123 for (size_t i = 0; i < elements.size(); ++i) { 126 if (!element_readers_[i]->IsInMemory())
124 if (elements[i].type() != UploadElement::TYPE_BYTES)
125 return false; 127 return false;
126 } 128 }
127 return true; 129 return true;
128 } 130 }
129 131
130 } // namespace net 132 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698