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" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "net/base/file_stream.h" | 10 #include "net/base/file_stream.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 total_size_(0), | 27 total_size_(0), |
28 current_position_(0), | 28 current_position_(0), |
29 initialized_successfully_(false) { | 29 initialized_successfully_(false) { |
30 } | 30 } |
31 | 31 |
32 UploadDataStream::~UploadDataStream() { | 32 UploadDataStream::~UploadDataStream() { |
33 } | 33 } |
34 | 34 |
35 int UploadDataStream::Init() { | 35 int UploadDataStream::Init() { |
36 DCHECK(!initialized_successfully_); | 36 DCHECK(!initialized_successfully_); |
37 | 37 std::vector<UploadElement>* elements = upload_data_->elements_mutable(); |
38 { | 38 { |
39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 39 base::ThreadRestrictions::ScopedAllowIO allow_io; |
40 total_size_ = upload_data_->GetContentLengthSync(); | 40 total_size_ = 0; |
| 41 if (!is_chunked()) { |
| 42 for (size_t i = 0; i < elements->size(); ++i) |
| 43 total_size_ += (*elements)[i].GetContentLength(); |
| 44 } |
41 } | 45 } |
42 | 46 |
43 // If the underlying file has been changed and the expected file | 47 // If the underlying file has been changed and the expected file |
44 // modification time is set, treat it as error. Note that the expected | 48 // modification time is set, treat it as error. Note that the expected |
45 // modification time from WebKit is based on time_t precision. So we | 49 // modification time from WebKit is based on time_t precision. So we |
46 // have to convert both to time_t to compare. This check is used for | 50 // have to convert both to time_t to compare. This check is used for |
47 // sliced files. | 51 // sliced files. |
48 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 52 for (size_t i = 0; i < elements->size(); ++i) { |
49 for (size_t i = 0; i < elements.size(); ++i) { | 53 const UploadElement& element = (*elements)[i]; |
50 const UploadElement& element = elements[i]; | |
51 if (element.type() == UploadElement::TYPE_FILE && | 54 if (element.type() == UploadElement::TYPE_FILE && |
52 !element.expected_file_modification_time().is_null()) { | 55 !element.expected_file_modification_time().is_null()) { |
53 // Temporarily allow until fix: http://crbug.com/72001. | 56 // Temporarily allow until fix: http://crbug.com/72001. |
54 base::ThreadRestrictions::ScopedAllowIO allow_io; | 57 base::ThreadRestrictions::ScopedAllowIO allow_io; |
55 base::PlatformFileInfo info; | 58 base::PlatformFileInfo info; |
56 if (file_util::GetFileInfo(element.file_path(), &info) && | 59 if (file_util::GetFileInfo(element.file_path(), &info) && |
57 element.expected_file_modification_time().ToTimeT() != | 60 element.expected_file_modification_time().ToTimeT() != |
58 info.last_modified.ToTimeT()) { | 61 info.last_modified.ToTimeT()) { |
59 return ERR_UPLOAD_FILE_CHANGED; | 62 return ERR_UPLOAD_FILE_CHANGED; |
60 } | 63 } |
61 } | 64 } |
62 } | 65 } |
63 | 66 |
64 // Reset the offset, as upload_data_ may already be read (i.e. UploadData | 67 // Reset the offset, as upload_data_ may already be read (i.e. UploadData |
65 // can be reused for a new UploadDataStream). | 68 // can be reused for a new UploadDataStream). |
66 upload_data_->ResetOffset(); | 69 for (size_t i = 0; i < elements->size(); ++i) |
| 70 (*elements)[i].ResetOffset(); |
67 | 71 |
68 initialized_successfully_ = true; | 72 initialized_successfully_ = true; |
69 return OK; | 73 return OK; |
70 } | 74 } |
71 | 75 |
72 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { | 76 int UploadDataStream::Read(IOBuffer* buf, int buf_len) { |
73 std::vector<UploadElement>& elements = | 77 std::vector<UploadElement>& elements = |
74 *upload_data_->elements_mutable(); | 78 *upload_data_->elements_mutable(); |
75 | 79 |
76 int bytes_copied = 0; | 80 int bytes_copied = 0; |
(...skipping 23 matching lines...) Expand all Loading... |
100 // Check if all elements are consumed. | 104 // Check if all elements are consumed. |
101 if (element_index_ == elements.size()) { | 105 if (element_index_ == elements.size()) { |
102 // If the upload data is chunked, check if the last chunk is appended. | 106 // If the upload data is chunked, check if the last chunk is appended. |
103 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) | 107 if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended()) |
104 return true; | 108 return true; |
105 } | 109 } |
106 return false; | 110 return false; |
107 } | 111 } |
108 | 112 |
109 bool UploadDataStream::IsInMemory() const { | 113 bool UploadDataStream::IsInMemory() const { |
110 DCHECK(initialized_successfully_); | 114 // Chunks are in memory, but UploadData does not have all the chunks at |
| 115 // once. Chunks are provided progressively with AppendChunk() as chunks |
| 116 // 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 |
| 118 // first chunk is not yet delivered. |
| 119 if (is_chunked()) |
| 120 return false; |
111 | 121 |
112 return upload_data_->IsInMemory(); | 122 const std::vector<UploadElement>& elements = *upload_data_->elements(); |
| 123 for (size_t i = 0; i < elements.size(); ++i) { |
| 124 if (elements[i].type() != UploadElement::TYPE_BYTES) |
| 125 return false; |
| 126 } |
| 127 return true; |
113 } | 128 } |
114 | 129 |
115 } // namespace net | 130 } // namespace net |
OLD | NEW |