OLD | NEW |
---|---|
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 const UploadElement& element = elements[i]; | |
32 UploadElementReader* reader = NULL; | |
satorux1
2012/08/28 20:42:35
maybe
UploadElementReader* reader = UploadElement
hashimoto
2012/08/28 21:41:20
Done.
| |
33 switch (element.type()) { | |
34 case UploadElement::TYPE_BYTES: | |
35 reader = new UploadBytesElementReader(element.bytes(), | |
36 element.bytes_length()); | |
37 break; | |
38 case UploadElement::TYPE_FILE: | |
39 reader = new UploadFileElementReader( | |
40 element.file_path(), | |
41 element.file_range_offset(), | |
42 element.file_range_length(), | |
43 element.expected_file_modification_time()); | |
44 break; | |
45 } | |
46 DCHECK(reader); | |
47 element_readers_.push_back(reader); | |
48 } | |
30 } | 49 } |
31 | 50 |
32 UploadDataStream::~UploadDataStream() { | 51 UploadDataStream::~UploadDataStream() { |
52 // Temporarily allow until fix: http://crbug.com/72001. | |
53 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
54 element_readers_.clear(); | |
33 } | 55 } |
34 | 56 |
35 int UploadDataStream::Init() { | 57 int UploadDataStream::Init() { |
36 DCHECK(!initialized_successfully_); | 58 DCHECK(!initialized_successfully_); |
37 std::vector<UploadElement>* elements = upload_data_->elements_mutable(); | 59 |
38 { | 60 uint64 total_size = 0; |
61 for (size_t i = 0; i < element_readers_.size(); ++i) { | |
62 // Temporarily allow until fix: http://crbug.com/72001. | |
39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 63 base::ThreadRestrictions::ScopedAllowIO allow_io; |
40 total_size_ = 0; | 64 |
41 if (!is_chunked()) { | 65 UploadElementReader* reader = element_readers_[i]; |
42 for (size_t i = 0; i < elements->size(); ++i) | 66 const int result = reader->InitSync(); |
43 total_size_ += (*elements)[i].GetContentLength(); | 67 if (result != OK) |
44 } | 68 return result; |
69 if (!is_chunked()) | |
70 total_size += reader->GetContentLength(); | |
45 } | 71 } |
46 | 72 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 | 73 |
72 initialized_successfully_ = true; | 74 initialized_successfully_ = true; |
73 return OK; | 75 return OK; |
74 } | 76 } |
75 | 77 |
76 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { | 78 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { |
77 std::vector<UploadElement>& elements = | 79 DCHECK(initialized_successfully_); |
78 *upload_data_->elements_mutable(); | 80 |
81 // Initialize readers for newly appended chunks. | |
82 if (is_chunked()) { | |
83 const std::vector<UploadElement>& elements = *upload_data_->elements(); | |
84 DCHECK_LE(element_readers_.size(), elements.size()); | |
85 | |
86 for (size_t i = element_readers_.size(); i < elements.size(); ++i) { | |
87 const UploadElement& element = elements[i]; | |
88 DCHECK(element.type() == UploadElement::TYPE_BYTES); | |
89 UploadElementReader* reader = | |
90 new UploadBytesElementReader(element.bytes(), element.bytes_length()); | |
91 | |
92 const int rv = reader->InitSync(); | |
93 DCHECK_EQ(rv, OK); | |
94 element_readers_.push_back(reader); | |
95 } | |
96 } | |
79 | 97 |
80 int bytes_copied = 0; | 98 int bytes_copied = 0; |
81 while (bytes_copied < buf_len && element_index_ < elements.size()) { | 99 while (bytes_copied < buf_len && element_index_ < element_readers_.size()) { |
82 UploadElement& element = elements[element_index_]; | 100 // Temporarily allow until fix: http://crbug.com/72001. |
101 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
83 | 102 |
84 bytes_copied += element.ReadSync(buf->data() + bytes_copied, | 103 UploadElementReader* reader = element_readers_[element_index_]; |
104 bytes_copied += reader->ReadSync(buf->data() + bytes_copied, | |
85 buf_len - bytes_copied); | 105 buf_len - bytes_copied); |
86 | 106 if (reader->BytesRemaining() == 0) |
87 if (element.BytesRemaining() == 0) | |
88 ++element_index_; | 107 ++element_index_; |
89 | 108 |
90 if (is_chunked() && !merge_chunks_) | 109 if (is_chunked() && !merge_chunks_) |
91 break; | 110 break; |
92 } | 111 } |
93 | 112 |
94 current_position_ += bytes_copied; | 113 current_position_ += bytes_copied; |
95 if (is_chunked() && !IsEOF() && bytes_copied == 0) | 114 if (is_chunked() && !IsEOF() && bytes_copied == 0) |
96 return ERR_IO_PENDING; | 115 return ERR_IO_PENDING; |
97 | 116 |
98 return bytes_copied; | 117 return bytes_copied; |
99 } | 118 } |
100 | 119 |
101 bool UploadDataStream::IsEOF() const { | 120 bool UploadDataStream::IsEOF() const { |
121 DCHECK(initialized_successfully_); | |
102 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 122 const std::vector<UploadElement>& elements = *upload_data_->elements(); |
103 | 123 |
104 // Check if all elements are consumed. | 124 // Check if all elements are consumed. |
105 if (element_index_ == elements.size()) { | 125 if (element_index_ == elements.size()) { |
106 // If the upload data is chunked, check if the last chunk is appended. | 126 // If the upload data is chunked, check if the last chunk is appended. |
107 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) | 127 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) |
108 return true; | 128 return true; |
109 } | 129 } |
110 return false; | 130 return false; |
111 } | 131 } |
112 | 132 |
113 bool UploadDataStream::IsInMemory() const { | 133 bool UploadDataStream::IsInMemory() const { |
114 // Chunks are in memory, but UploadData does not have all the chunks at | 134 // Chunks are in memory, but UploadData does not have all the chunks at |
115 // once. Chunks are provided progressively with AppendChunk() as chunks | 135 // once. Chunks are provided progressively with AppendChunk() as chunks |
116 // are ready. Check is_chunked_ here, rather than relying on the loop | 136 // 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 | 137 // below, as there is a case that is_chunked_ is set to true, but the |
118 // first chunk is not yet delivered. | 138 // first chunk is not yet delivered. |
119 if (is_chunked()) | 139 if (is_chunked()) |
120 return false; | 140 return false; |
121 | 141 |
122 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 142 for (size_t i = 0; i < element_readers_.size(); ++i) { |
123 for (size_t i = 0; i < elements.size(); ++i) { | 143 if (!element_readers_[i]->IsInMemory()) |
124 if (elements[i].type() != UploadElement::TYPE_BYTES) | |
125 return false; | 144 return false; |
126 } | 145 } |
127 return true; | 146 return true; |
128 } | 147 } |
129 | 148 |
130 } // namespace net | 149 } // namespace net |
OLD | NEW |