Chromium Code Reviews| 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" | |
| 18 | 19 |
| 19 namespace storage { | 20 namespace storage { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const static int kInvalidDiskCacheSideStreamIndex = -1; | 24 const static int kInvalidDiskCacheSideStreamIndex = -1; |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = | 28 const char BlobDataBuilder::kFutureFileName[] = "_future_name_"; |
| 28 "kFakeFilenameToBeChangedByPopulateFutureFile"; | 29 |
| 30 /* static */ | |
| 31 bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) { | |
| 32 const std::string prefix(kFutureFileName); | |
| 33 const std::string path = element.path().MaybeAsASCII(); | |
| 34 return path.size() >= prefix.size() && | |
| 35 base::StartsWith(path, prefix, base::CompareCase::SENSITIVE); | |
| 36 } | |
| 37 | |
| 38 /* static */ | |
| 39 uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) { | |
| 40 DCHECK(IsFutureFileItem(element)); | |
| 41 uint64_t id = 0; | |
| 42 bool success = | |
| 43 base::StringToUint64(element.path().BaseName().MaybeAsASCII(), &id); | |
|
pwnall
2016/09/21 09:03:35
Won't BaseName be something like "_future_name.42"
dmurph
2016/09/21 23:45:52
Yeah, this was a suggestion that didn't end up wor
| |
| 44 DCHECK(success) << element.path().Extension(); | |
| 45 return id; | |
| 46 } | |
| 29 | 47 |
| 30 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 48 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| 31 } | 49 } |
| 32 BlobDataBuilder::~BlobDataBuilder() { | 50 BlobDataBuilder::~BlobDataBuilder() { |
| 33 } | 51 } |
| 34 | 52 |
| 35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | 53 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| 36 uint64_t length = ipc_data.length(); | 54 uint64_t length = ipc_data.length(); |
| 37 switch (ipc_data.type()) { | 55 switch (ipc_data.type()) { |
| 38 case DataElement::TYPE_BYTES: | 56 case DataElement::TYPE_BYTES: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 base::CheckedNumeric<size_t> checked_end = offset; | 121 base::CheckedNumeric<size_t> checked_end = offset; |
| 104 checked_end += length; | 122 checked_end += length; |
| 105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { | 123 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { |
| 106 DVLOG(1) << "Invalid offset or length."; | 124 DVLOG(1) << "Invalid offset or length."; |
| 107 return false; | 125 return false; |
| 108 } | 126 } |
| 109 std::memcpy(element->mutable_bytes() + offset, data, length); | 127 std::memcpy(element->mutable_bytes() + offset, data, length); |
| 110 return true; | 128 return true; |
| 111 } | 129 } |
| 112 | 130 |
| 113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | 131 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, |
| 132 uint64_t length, | |
| 133 uint64_t file_id) { | |
| 114 CHECK_NE(length, 0ull); | 134 CHECK_NE(length, 0ull); |
| 115 std::unique_ptr<DataElement> element(new DataElement()); | 135 std::unique_ptr<DataElement> element(new DataElement()); |
| 116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( | 136 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(kFutureFileName) |
|
pwnall
2016/09/21 09:03:35
How about pulling out base::FilePath::FromUTF8Unsa
dmurph
2016/09/21 23:45:52
Done.
| |
| 117 kAppendFutureFileTemporaryFileName)), | 137 .AddExtension(base::Uint64ToString(file_id)), |
| 118 offset, length, base::Time()); | 138 offset, length, base::Time()); |
| 119 items_.push_back(new BlobDataItem(std::move(element))); | 139 items_.push_back(new BlobDataItem(std::move(element))); |
| 120 return items_.size() - 1; | 140 return items_.size() - 1; |
| 121 } | 141 } |
| 122 | 142 |
| 123 bool BlobDataBuilder::PopulateFutureFile( | 143 bool BlobDataBuilder::PopulateFutureFile( |
| 124 size_t index, | 144 size_t index, |
| 125 const scoped_refptr<ShareableFileReference>& file_reference, | 145 const scoped_refptr<ShareableFileReference>& file_reference, |
| 126 const base::Time& expected_modification_time) { | 146 const base::Time& expected_modification_time) { |
| 127 DCHECK_LT(index, items_.size()); | 147 DCHECK_LT(index, items_.size()); |
| 128 DataElement* old_element = items_.at(index)->data_element_ptr(); | 148 DataElement* element = items_.at(index)->data_element_ptr(); |
| 129 | 149 |
| 130 if (old_element->type() != DataElement::TYPE_FILE) { | 150 if (element->type() != DataElement::TYPE_FILE) { |
| 131 DVLOG(1) << "Invalid item type."; | 151 DVLOG(1) << "Invalid item type."; |
| 132 return false; | 152 return false; |
| 133 } else if (old_element->path().AsUTF8Unsafe() != | 153 } else if (!IsFutureFileItem(*element)) { |
| 134 std::string(kAppendFutureFileTemporaryFileName)) { | |
| 135 DVLOG(1) << "Item not created by AppendFutureFile"; | 154 DVLOG(1) << "Item not created by AppendFutureFile"; |
| 136 return false; | 155 return false; |
| 137 } | 156 } |
| 138 uint64_t length = old_element->length(); | 157 uint64_t length = element->length(); |
| 139 uint64_t offset = old_element->offset(); | 158 uint64_t offset = element->offset(); |
| 140 std::unique_ptr<DataElement> element(new DataElement()); | 159 items_.at(index)->data_handle_ = std::move(file_reference); |
|
pwnall
2016/09/21 09:03:35
I think you can use [] instead of at(), because yo
dmurph
2016/09/21 23:45:52
Done.
| |
| 141 element->SetToFilePathRange(file_reference->path(), offset, length, | 160 element->SetToFilePathRange(file_reference->path(), offset, length, |
| 142 expected_modification_time); | 161 expected_modification_time); |
| 143 items_[index] = new BlobDataItem(std::move(element), file_reference); | |
| 144 return true; | 162 return true; |
| 145 } | 163 } |
| 146 | 164 |
| 147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 165 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| 148 uint64_t offset, | 166 uint64_t offset, |
| 149 uint64_t length, | 167 uint64_t length, |
| 150 const base::Time& expected_modification_time) { | 168 const base::Time& expected_modification_time) { |
| 151 std::unique_ptr<DataElement> element(new DataElement()); | 169 std::unique_ptr<DataElement> element(new DataElement()); |
| 152 element->SetToFilePathRange(file_path, offset, length, | 170 element->SetToFilePathRange(file_path, offset, length, |
| 153 expected_modification_time); | 171 expected_modification_time); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 << ", content_type: " << x.content_type_ | 238 << ", content_type: " << x.content_type_ |
| 221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 239 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 222 for (const auto& item : x.items_) { | 240 for (const auto& item : x.items_) { |
| 223 PrintTo(*item, os); | 241 PrintTo(*item, os); |
| 224 *os << ", "; | 242 *os << ", "; |
| 225 } | 243 } |
| 226 *os << "]}"; | 244 *os << "]}"; |
| 227 } | 245 } |
| 228 | 246 |
| 229 } // namespace storage | 247 } // namespace storage |
| OLD | NEW |