| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "storage/browser/blob/blob_data_builder.h" | 5 #include "storage/browser/blob/blob_data_builder.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 16 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
| 17 #include "storage/browser/blob/shareable_file_reference.h" | 19 |
| 20 using base::FilePath; |
| 18 | 21 |
| 19 namespace storage { | 22 namespace storage { |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 const static int kInvalidDiskCacheSideStreamIndex = -1; | 26 const static int kInvalidDiskCacheSideStreamIndex = -1; |
| 24 | 27 |
| 25 } // namespace | 28 } // namespace |
| 26 | 29 |
| 27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = | 30 const char kFutureFileName[] = "_future_name_"; |
| 28 "kFakeFilenameToBeChangedByPopulateFutureFile"; | |
| 29 | 31 |
| 30 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 32 /* static */ |
| 33 base::FilePath BlobDataBuilder::GetFutureFileItemPath(uint64_t file_id) { |
| 34 std::string file_id_str = base::Uint64ToString(file_id); |
| 35 return base::FilePath::FromUTF8Unsafe(kFutureFileName) |
| 36 .AddExtension( |
| 37 base::FilePath::StringType(file_id_str.begin(), file_id_str.end())); |
| 31 } | 38 } |
| 32 BlobDataBuilder::~BlobDataBuilder() { | 39 |
| 40 /* static */ |
| 41 bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) { |
| 42 const std::string prefix(kFutureFileName); |
| 43 const std::string path = element.path().MaybeAsASCII(); |
| 44 // The prefix shouldn't occur unless the user used "AppendFutureFile". We |
| 45 // DCHECK on AppendFile to make sure no one appends a future file. |
| 46 return base::StartsWith(path, prefix, base::CompareCase::SENSITIVE); |
| 33 } | 47 } |
| 34 | 48 |
| 49 /* static */ |
| 50 uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) { |
| 51 DCHECK(IsFutureFileItem(element)); |
| 52 uint64_t id = 0; |
| 53 bool success = |
| 54 base::StringToUint64(element.path().Extension().substr(1), &id); |
| 55 DCHECK(success) << element.path().Extension(); |
| 56 return id; |
| 57 } |
| 58 |
| 59 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) {} |
| 60 BlobDataBuilder::~BlobDataBuilder() {} |
| 61 |
| 35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | 62 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| 36 uint64_t length = ipc_data.length(); | 63 uint64_t length = ipc_data.length(); |
| 37 switch (ipc_data.type()) { | 64 switch (ipc_data.type()) { |
| 38 case DataElement::TYPE_BYTES: | 65 case DataElement::TYPE_BYTES: |
| 39 DCHECK(!ipc_data.offset()); | 66 DCHECK(!ipc_data.offset()); |
| 40 AppendData(ipc_data.bytes(), | 67 AppendData(ipc_data.bytes(), |
| 41 base::checked_cast<size_t, uint64_t>(length)); | 68 base::checked_cast<size_t, uint64_t>(length)); |
| 42 break; | 69 break; |
| 43 case DataElement::TYPE_FILE: | 70 case DataElement::TYPE_FILE: |
| 44 AppendFile(ipc_data.path(), ipc_data.offset(), length, | 71 AppendFile(ipc_data.path(), ipc_data.offset(), length, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 items_.push_back(new BlobDataItem(std::move(element))); | 103 items_.push_back(new BlobDataItem(std::move(element))); |
| 77 return items_.size() - 1; | 104 return items_.size() - 1; |
| 78 } | 105 } |
| 79 | 106 |
| 80 bool BlobDataBuilder::PopulateFutureData(size_t index, | 107 bool BlobDataBuilder::PopulateFutureData(size_t index, |
| 81 const char* data, | 108 const char* data, |
| 82 size_t offset, | 109 size_t offset, |
| 83 size_t length) { | 110 size_t length) { |
| 84 DCHECK_LT(index, items_.size()); | 111 DCHECK_LT(index, items_.size()); |
| 85 DCHECK(data); | 112 DCHECK(data); |
| 86 DataElement* element = items_.at(index)->data_element_ptr(); | 113 DataElement* element = items_[index]->data_element_ptr(); |
| 87 | 114 |
| 88 // We lazily allocate our data buffer by waiting until the first | 115 // We lazily allocate our data buffer by waiting until the first |
| 89 // PopulateFutureData call. | 116 // PopulateFutureData call. |
| 90 // Why? The reason we have the AppendFutureData method is to create our Blob | 117 // Why? The reason we have the AppendFutureData method is to create our Blob |
| 91 // record when the Renderer tells us about the blob without actually | 118 // record when the Renderer tells us about the blob without actually |
| 92 // allocating the memory yet, as we might not have the quota yet. So we don't | 119 // allocating the memory yet, as we might not have the quota yet. So we don't |
| 93 // want to allocate the memory until we're actually receiving the data (which | 120 // want to allocate the memory until we're actually receiving the data (which |
| 94 // the browser process only does when it has quota). | 121 // the browser process only does when it has quota). |
| 95 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { | 122 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { |
| 96 element->SetToAllocatedBytes(element->length()); | 123 element->SetToAllocatedBytes(element->length()); |
| 97 // The type of the element is now TYPE_BYTES. | 124 // The type of the element is now TYPE_BYTES. |
| 98 } | 125 } |
| 99 if (element->type() != DataElement::TYPE_BYTES) { | 126 if (element->type() != DataElement::TYPE_BYTES) { |
| 100 DVLOG(1) << "Invalid item type."; | 127 DVLOG(1) << "Invalid item type."; |
| 101 return false; | 128 return false; |
| 102 } | 129 } |
| 103 base::CheckedNumeric<size_t> checked_end = offset; | 130 base::CheckedNumeric<size_t> checked_end = offset; |
| 104 checked_end += length; | 131 checked_end += length; |
| 105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { | 132 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { |
| 106 DVLOG(1) << "Invalid offset or length."; | 133 DVLOG(1) << "Invalid offset or length."; |
| 107 return false; | 134 return false; |
| 108 } | 135 } |
| 109 std::memcpy(element->mutable_bytes() + offset, data, length); | 136 std::memcpy(element->mutable_bytes() + offset, data, length); |
| 110 return true; | 137 return true; |
| 111 } | 138 } |
| 112 | 139 |
| 113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | 140 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, |
| 141 uint64_t length, |
| 142 uint64_t file_id) { |
| 114 CHECK_NE(length, 0ull); | 143 CHECK_NE(length, 0ull); |
| 115 std::unique_ptr<DataElement> element(new DataElement()); | 144 std::unique_ptr<DataElement> element(new DataElement()); |
| 116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( | 145 element->SetToFilePathRange(GetFutureFileItemPath(file_id), offset, length, |
| 117 kAppendFutureFileTemporaryFileName)), | 146 base::Time()); |
| 118 offset, length, base::Time()); | |
| 119 items_.push_back(new BlobDataItem(std::move(element))); | 147 items_.push_back(new BlobDataItem(std::move(element))); |
| 120 return items_.size() - 1; | 148 return items_.size() - 1; |
| 121 } | 149 } |
| 122 | 150 |
| 123 bool BlobDataBuilder::PopulateFutureFile( | 151 bool BlobDataBuilder::PopulateFutureFile( |
| 124 size_t index, | 152 size_t index, |
| 125 const scoped_refptr<ShareableFileReference>& file_reference, | 153 const scoped_refptr<ShareableFileReference>& file_reference, |
| 126 const base::Time& expected_modification_time) { | 154 const base::Time& expected_modification_time) { |
| 127 DCHECK_LT(index, items_.size()); | 155 DCHECK_LT(index, items_.size()); |
| 128 DataElement* old_element = items_.at(index)->data_element_ptr(); | 156 DataElement* element = items_[index]->data_element_ptr(); |
| 129 | 157 |
| 130 if (old_element->type() != DataElement::TYPE_FILE) { | 158 if (element->type() != DataElement::TYPE_FILE) { |
| 131 DVLOG(1) << "Invalid item type."; | 159 DVLOG(1) << "Invalid item type."; |
| 132 return false; | 160 return false; |
| 133 } else if (old_element->path().AsUTF8Unsafe() != | 161 } else if (!IsFutureFileItem(*element)) { |
| 134 std::string(kAppendFutureFileTemporaryFileName)) { | |
| 135 DVLOG(1) << "Item not created by AppendFutureFile"; | 162 DVLOG(1) << "Item not created by AppendFutureFile"; |
| 136 return false; | 163 return false; |
| 137 } | 164 } |
| 138 uint64_t length = old_element->length(); | 165 uint64_t length = element->length(); |
| 139 uint64_t offset = old_element->offset(); | 166 uint64_t offset = element->offset(); |
| 140 std::unique_ptr<DataElement> element(new DataElement()); | 167 items_[index]->data_handle_ = std::move(file_reference); |
| 141 element->SetToFilePathRange(file_reference->path(), offset, length, | 168 element->SetToFilePathRange(file_reference->path(), offset, length, |
| 142 expected_modification_time); | 169 expected_modification_time); |
| 143 items_[index] = new BlobDataItem(std::move(element), file_reference); | |
| 144 return true; | 170 return true; |
| 145 } | 171 } |
| 146 | 172 |
| 147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 173 void BlobDataBuilder::AppendFile(const FilePath& file_path, |
| 148 uint64_t offset, | 174 uint64_t offset, |
| 149 uint64_t length, | 175 uint64_t length, |
| 150 const base::Time& expected_modification_time) { | 176 const base::Time& expected_modification_time) { |
| 151 std::unique_ptr<DataElement> element(new DataElement()); | 177 std::unique_ptr<DataElement> element(new DataElement()); |
| 152 element->SetToFilePathRange(file_path, offset, length, | 178 element->SetToFilePathRange(file_path, offset, length, |
| 153 expected_modification_time); | 179 expected_modification_time); |
| 180 DCHECK(!IsFutureFileItem(*element)) << file_path.value(); |
| 154 items_.push_back(new BlobDataItem(std::move(element), | 181 items_.push_back(new BlobDataItem(std::move(element), |
| 155 ShareableFileReference::Get(file_path))); | 182 ShareableFileReference::Get(file_path))); |
| 156 } | 183 } |
| 157 | 184 |
| 158 void BlobDataBuilder::AppendBlob(const std::string& uuid, | 185 void BlobDataBuilder::AppendBlob(const std::string& uuid, |
| 159 uint64_t offset, | 186 uint64_t offset, |
| 160 uint64_t length) { | 187 uint64_t length) { |
| 161 DCHECK_GT(length, 0ul); | 188 DCHECK_GT(length, 0ul); |
| 162 std::unique_ptr<DataElement> element(new DataElement()); | 189 std::unique_ptr<DataElement> element(new DataElement()); |
| 163 element->SetToBlobRange(uuid, offset, length); | 190 element->SetToBlobRange(uuid, offset, length); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 << ", content_type: " << x.content_type_ | 247 << ", content_type: " << x.content_type_ |
| 221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 248 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 222 for (const auto& item : x.items_) { | 249 for (const auto& item : x.items_) { |
| 223 PrintTo(*item, os); | 250 PrintTo(*item, os); |
| 224 *os << ", "; | 251 *os << ", "; |
| 225 } | 252 } |
| 226 *os << "]}"; | 253 *os << "]}"; |
| 227 } | 254 } |
| 228 | 255 |
| 229 } // namespace storage | 256 } // namespace storage |
| OLD | NEW |