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" | |
10 #include "net/base/file_stream.h" | |
11 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/upload_element_reader.h" |
13 | 11 |
14 namespace net { | 12 namespace net { |
15 | 13 |
16 bool UploadDataStream::merge_chunks_ = true; | 14 bool UploadDataStream::merge_chunks_ = true; |
17 | 15 |
18 // static | 16 // static |
19 void UploadDataStream::ResetMergeChunks() { | 17 void UploadDataStream::ResetMergeChunks() { |
20 // WARNING: merge_chunks_ must match the above initializer. | 18 // WARNING: merge_chunks_ must match the above initializer. |
21 merge_chunks_ = true; | 19 merge_chunks_ = true; |
22 } | 20 } |
23 | 21 |
24 UploadDataStream::UploadDataStream(UploadData* upload_data) | 22 UploadDataStream::UploadDataStream(UploadData* upload_data) |
25 : upload_data_(upload_data), | 23 : upload_data_(upload_data), |
26 element_index_(0), | 24 element_index_(0), |
27 total_size_(0), | 25 total_size_(0), |
28 current_position_(0), | 26 current_position_(0), |
29 initialized_successfully_(false) { | 27 initialized_successfully_(false) { |
| 28 const std::vector<UploadElement>& elements = *upload_data_->elements(); |
| 29 for (size_t i = 0; i < elements.size(); ++i) |
| 30 element_readers_.push_back(UploadElementReader::Create(elements[i])); |
30 } | 31 } |
31 | 32 |
32 UploadDataStream::~UploadDataStream() { | 33 UploadDataStream::~UploadDataStream() { |
33 } | 34 } |
34 | 35 |
35 int UploadDataStream::Init() { | 36 int UploadDataStream::Init() { |
36 DCHECK(!initialized_successfully_); | 37 DCHECK(!initialized_successfully_); |
37 std::vector<UploadElement>* elements = upload_data_->elements_mutable(); | 38 |
38 { | 39 uint64 total_size = 0; |
39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 40 for (size_t i = 0; i < element_readers_.size(); ++i) { |
40 total_size_ = 0; | 41 UploadElementReader* reader = element_readers_[i]; |
41 if (!is_chunked()) { | 42 const int result = reader->InitSync(); |
42 for (size_t i = 0; i < elements->size(); ++i) | 43 if (result != OK) |
43 total_size_ += (*elements)[i].GetContentLength(); | 44 return result; |
44 } | 45 if (!is_chunked()) |
| 46 total_size += reader->GetContentLength(); |
45 } | 47 } |
46 | 48 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 | 49 |
72 initialized_successfully_ = true; | 50 initialized_successfully_ = true; |
73 return OK; | 51 return OK; |
74 } | 52 } |
75 | 53 |
76 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { | 54 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { |
77 std::vector<UploadElement>& elements = | 55 DCHECK(initialized_successfully_); |
78 *upload_data_->elements_mutable(); | 56 |
| 57 // Initialize readers for newly appended chunks. |
| 58 if (is_chunked()) { |
| 59 const std::vector<UploadElement>& elements = *upload_data_->elements(); |
| 60 DCHECK_LE(element_readers_.size(), elements.size()); |
| 61 |
| 62 for (size_t i = element_readers_.size(); i < elements.size(); ++i) { |
| 63 const UploadElement& element = elements[i]; |
| 64 DCHECK_EQ(UploadElement::TYPE_BYTES, element.type()); |
| 65 UploadElementReader* reader = UploadElementReader::Create(element); |
| 66 |
| 67 const int rv = reader->InitSync(); |
| 68 DCHECK_EQ(rv, OK); |
| 69 element_readers_.push_back(reader); |
| 70 } |
| 71 } |
79 | 72 |
80 int bytes_copied = 0; | 73 int bytes_copied = 0; |
81 while (bytes_copied < buf_len && element_index_ < elements.size()) { | 74 while (bytes_copied < buf_len && element_index_ < element_readers_.size()) { |
82 UploadElement& element = elements[element_index_]; | 75 UploadElementReader* reader = element_readers_[element_index_]; |
83 | 76 bytes_copied += reader->ReadSync(buf->data() + bytes_copied, |
84 bytes_copied += element.ReadSync(buf->data() + bytes_copied, | |
85 buf_len - bytes_copied); | 77 buf_len - bytes_copied); |
86 | 78 if (reader->BytesRemaining() == 0) |
87 if (element.BytesRemaining() == 0) | |
88 ++element_index_; | 79 ++element_index_; |
89 | 80 |
90 if (is_chunked() && !merge_chunks_) | 81 if (is_chunked() && !merge_chunks_) |
91 break; | 82 break; |
92 } | 83 } |
93 | 84 |
94 current_position_ += bytes_copied; | 85 current_position_ += bytes_copied; |
95 if (is_chunked() && !IsEOF() && bytes_copied == 0) | 86 if (is_chunked() && !IsEOF() && bytes_copied == 0) |
96 return ERR_IO_PENDING; | 87 return ERR_IO_PENDING; |
97 | 88 |
98 return bytes_copied; | 89 return bytes_copied; |
99 } | 90 } |
100 | 91 |
101 bool UploadDataStream::IsEOF() const { | 92 bool UploadDataStream::IsEOF() const { |
| 93 DCHECK(initialized_successfully_); |
102 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 94 const std::vector<UploadElement>& elements = *upload_data_->elements(); |
103 | 95 |
104 // Check if all elements are consumed. | 96 // Check if all elements are consumed. |
105 if (element_index_ == elements.size()) { | 97 if (element_index_ == elements.size()) { |
106 // If the upload data is chunked, check if the last chunk is appended. | 98 // If the upload data is chunked, check if the last chunk is appended. |
107 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) | 99 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) |
108 return true; | 100 return true; |
109 } | 101 } |
110 return false; | 102 return false; |
111 } | 103 } |
112 | 104 |
113 bool UploadDataStream::IsInMemory() const { | 105 bool UploadDataStream::IsInMemory() const { |
114 // Chunks are in memory, but UploadData does not have all the chunks at | 106 // Chunks are in memory, but UploadData does not have all the chunks at |
115 // once. Chunks are provided progressively with AppendChunk() as chunks | 107 // once. Chunks are provided progressively with AppendChunk() as chunks |
116 // are ready. Check is_chunked_ here, rather than relying on the loop | 108 // 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 | 109 // below, as there is a case that is_chunked_ is set to true, but the |
118 // first chunk is not yet delivered. | 110 // first chunk is not yet delivered. |
119 if (is_chunked()) | 111 if (is_chunked()) |
120 return false; | 112 return false; |
121 | 113 |
122 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 114 for (size_t i = 0; i < element_readers_.size(); ++i) { |
123 for (size_t i = 0; i < elements.size(); ++i) { | 115 if (!element_readers_[i]->IsInMemory()) |
124 if (elements[i].type() != UploadElement::TYPE_BYTES) | |
125 return false; | 116 return false; |
126 } | 117 } |
127 return true; | 118 return true; |
128 } | 119 } |
129 | 120 |
130 } // namespace net | 121 } // namespace net |
OLD | NEW |