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" | |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
| 17 #include "storage/browser/blob/shareable_file_reference.h" | |
| 18 | 18 |
| 19 namespace storage { | 19 namespace storage { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const static int kInvalidDiskCacheSideStreamIndex = -1; | 23 const static int kInvalidDiskCacheSideStreamIndex = -1; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = | 27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = |
| 28 "kFakeFilenameToBeChangedByPopulateFutureFile"; | 28 "kFakeFilenameToBeChangedByPopulateFutureFile"; |
|
michaeln
2016/08/15 22:44:43
would be safer to use a shorter filename to avoid
dmurph
2016/08/19 00:18:32
Done.
| |
| 29 | 29 |
| 30 bool BlobDataBuilder::IsTemporaryFileItem(const DataElement& element) { | |
|
michaeln
2016/08/15 22:44:43
i think this can be simplified, element.path().Bas
dmurph
2016/08/19 00:18:32
Done.
| |
| 31 const std::string prefix(kAppendFutureFileTemporaryFileName); | |
| 32 const std::string path = element.path().MaybeAsASCII(); | |
| 33 return path.size() >= prefix.size() && | |
| 34 element.path().MaybeAsASCII().substr(0, prefix.size()) == prefix; | |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Why call element.path().MaybeAsASCII() if you alre
dmurph
2016/08/19 00:18:32
Done.
| |
| 35 } | |
| 36 | |
| 37 std::string BlobDataBuilder::GetTemporaryFileID(const DataElement& element) { | |
|
michaeln
2016/08/15 22:44:43
maybe DCHECK(IsFutureFileItem(element))
dmurph
2016/08/19 00:18:32
Done.
| |
| 38 return element.path().Extension(); | |
| 39 } | |
| 40 | |
| 30 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 41 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| 31 } | 42 } |
| 32 BlobDataBuilder::~BlobDataBuilder() { | 43 BlobDataBuilder::~BlobDataBuilder() { |
| 33 } | 44 } |
| 34 | 45 |
| 35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | 46 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| 36 uint64_t length = ipc_data.length(); | 47 uint64_t length = ipc_data.length(); |
| 37 switch (ipc_data.type()) { | 48 switch (ipc_data.type()) { |
| 38 case DataElement::TYPE_BYTES: | 49 case DataElement::TYPE_BYTES: |
| 39 DCHECK(!ipc_data.offset()); | 50 DCHECK(!ipc_data.offset()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 base::CheckedNumeric<size_t> checked_end = offset; | 114 base::CheckedNumeric<size_t> checked_end = offset; |
| 104 checked_end += length; | 115 checked_end += length; |
| 105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { | 116 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { |
| 106 DVLOG(1) << "Invalid offset or length."; | 117 DVLOG(1) << "Invalid offset or length."; |
| 107 return false; | 118 return false; |
| 108 } | 119 } |
| 109 std::memcpy(element->mutable_bytes() + offset, data, length); | 120 std::memcpy(element->mutable_bytes() + offset, data, length); |
| 110 return true; | 121 return true; |
| 111 } | 122 } |
| 112 | 123 |
| 113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | 124 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, |
| 125 uint64_t length, | |
| 126 const std::string& file_id) { | |
| 114 CHECK_NE(length, 0ull); | 127 CHECK_NE(length, 0ull); |
| 115 std::unique_ptr<DataElement> element(new DataElement()); | 128 std::unique_ptr<DataElement> element(new DataElement()); |
| 116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( | 129 element->SetToFilePathRange( |
| 117 kAppendFutureFileTemporaryFileName)), | 130 base::FilePath::FromUTF8Unsafe( |
| 118 offset, length, base::Time()); | 131 std::string(kAppendFutureFileTemporaryFileName)) |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Why the conversion to a std::string? FromUTF8Unsaf
dmurph
2016/08/19 00:18:32
Done.
| |
| 132 .AddExtension(file_id), | |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Does this work on windows where FilePath::StringTy
| |
| 133 offset, length, base::Time()); | |
| 119 items_.push_back(new BlobDataItem(std::move(element))); | 134 items_.push_back(new BlobDataItem(std::move(element))); |
| 120 return items_.size() - 1; | 135 return items_.size() - 1; |
|
michaeln
2016/08/15 22:44:43
Why does file_id need to be passed in? the class c
dmurph
2016/08/19 00:18:32
The id can be reused between elements. I'm putting
| |
| 121 } | 136 } |
| 122 | 137 |
| 123 bool BlobDataBuilder::PopulateFutureFile( | 138 bool BlobDataBuilder::PopulateFutureFile( |
| 124 size_t index, | 139 size_t index, |
| 125 const scoped_refptr<ShareableFileReference>& file_reference, | 140 const scoped_refptr<ShareableFileReference>& file_reference, |
| 126 const base::Time& expected_modification_time) { | 141 const base::Time& expected_modification_time) { |
| 127 DCHECK_LT(index, items_.size()); | 142 DCHECK_LT(index, items_.size()); |
| 128 DataElement* old_element = items_.at(index)->data_element_ptr(); | 143 DataElement* element = items_.at(index)->data_element_ptr(); |
| 129 | 144 |
| 130 if (old_element->type() != DataElement::TYPE_FILE) { | 145 if (element->type() != DataElement::TYPE_FILE) { |
| 131 DVLOG(1) << "Invalid item type."; | 146 DVLOG(1) << "Invalid item type."; |
| 132 return false; | 147 return false; |
| 133 } else if (old_element->path().AsUTF8Unsafe() != | 148 } else if (!IsTemporaryFileItem(*element)) { |
| 134 std::string(kAppendFutureFileTemporaryFileName)) { | |
| 135 DVLOG(1) << "Item not created by AppendFutureFile"; | 149 DVLOG(1) << "Item not created by AppendFutureFile"; |
| 136 return false; | 150 return false; |
| 137 } | 151 } |
| 138 uint64_t length = old_element->length(); | 152 uint64_t length = element->length(); |
| 139 uint64_t offset = old_element->offset(); | 153 uint64_t offset = element->offset(); |
| 140 std::unique_ptr<DataElement> element(new DataElement()); | 154 items_.at(index)->data_handle_ = std::move(file_reference); |
| 141 element->SetToFilePathRange(file_reference->path(), offset, length, | 155 element->SetToFilePathRange(file_reference->path(), offset, length, |
| 142 expected_modification_time); | 156 expected_modification_time); |
| 143 items_[index] = new BlobDataItem(std::move(element), file_reference); | |
| 144 return true; | 157 return true; |
| 145 } | 158 } |
| 146 | 159 |
| 147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 160 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| 148 uint64_t offset, | 161 uint64_t offset, |
| 149 uint64_t length, | 162 uint64_t length, |
| 150 const base::Time& expected_modification_time) { | 163 const base::Time& expected_modification_time) { |
| 151 std::unique_ptr<DataElement> element(new DataElement()); | 164 std::unique_ptr<DataElement> element(new DataElement()); |
| 152 element->SetToFilePathRange(file_path, offset, length, | 165 element->SetToFilePathRange(file_path, offset, length, |
| 153 expected_modification_time); | 166 expected_modification_time); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 << ", content_type: " << x.content_type_ | 233 << ", content_type: " << x.content_type_ |
| 221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 234 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 222 for (const auto& item : x.items_) { | 235 for (const auto& item : x.items_) { |
| 223 PrintTo(*item, os); | 236 PrintTo(*item, os); |
| 224 *os << ", "; | 237 *os << ", "; |
| 225 } | 238 } |
| 226 *os << "]}"; | 239 *os << "]}"; |
| 227 } | 240 } |
| 228 | 241 |
| 229 } // namespace storage | 242 } // namespace storage |
| OLD | NEW |