Chromium Code Reviews| Index: storage/browser/blob/blob_data_builder.cc |
| diff --git a/storage/browser/blob/blob_data_builder.cc b/storage/browser/blob/blob_data_builder.cc |
| index 1397cede297e47dbb88f2f52f3fa5f24172c39b9..3ec6b65e78e5b8d067ee4595a446b43b40f17be8 100644 |
| --- a/storage/browser/blob/blob_data_builder.cc |
| +++ b/storage/browser/blob/blob_data_builder.cc |
| @@ -12,9 +12,10 @@ |
| #include "base/numerics/safe_conversions.h" |
| #include "base/numerics/safe_math.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| #include "base/time/time.h" |
| #include "net/disk_cache/disk_cache.h" |
| -#include "storage/browser/blob/shareable_file_reference.h" |
| namespace storage { |
| @@ -24,14 +25,35 @@ const static int kInvalidDiskCacheSideStreamIndex = -1; |
| } // namespace |
| -const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = |
| - "kFakeFilenameToBeChangedByPopulateFutureFile"; |
| +const char kFutureFileName[] = "_future_name_"; |
| -BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| +/* static */ |
| +base::FilePath BlobDataBuilder::GetFutureFileItemPath(uint64_t file_id) { |
| + return base::FilePath::FromUTF8Unsafe(kFutureFileName) |
| + .AddExtension(base::Uint64ToString(file_id)); |
| } |
| -BlobDataBuilder::~BlobDataBuilder() { |
| + |
| +/* static */ |
| +bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) { |
| + const std::string prefix(kFutureFileName); |
| + const std::string path = element.path().MaybeAsASCII(); |
| + return path.size() >= prefix.size() && |
|
michaeln
2016/09/27 00:09:29
not sure the size comparison condition needed, Sta
dmurph
2016/09/29 00:44:20
Ah, right.Done.
|
| + base::StartsWith(path, prefix, base::CompareCase::SENSITIVE); |
|
michaeln
2016/09/27 00:09:29
is there any chance that "_future_name_" is valid
dmurph
2016/09/29 00:44:20
I mean, it's technically valid. I can DCHECK in Ap
michaeln
2016/10/04 00:11:54
This seems like something we have to resolve? I do
|
| +} |
| + |
| +/* static */ |
| +uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) { |
| + DCHECK(IsFutureFileItem(element)); |
| + uint64_t id = 0; |
| + bool success = |
| + base::StringToUint64(element.path().Extension().substr(1), &id); |
| + DCHECK(success) << element.path().Extension(); |
| + return id; |
| } |
| +BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) {} |
| +BlobDataBuilder::~BlobDataBuilder() {} |
| + |
| void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| uint64_t length = ipc_data.length(); |
| switch (ipc_data.type()) { |
| @@ -83,7 +105,7 @@ bool BlobDataBuilder::PopulateFutureData(size_t index, |
| size_t length) { |
| DCHECK_LT(index, items_.size()); |
| DCHECK(data); |
| - DataElement* element = items_.at(index)->data_element_ptr(); |
| + DataElement* element = items_[index]->data_element_ptr(); |
| // We lazily allocate our data buffer by waiting until the first |
| // PopulateFutureData call. |
| @@ -110,12 +132,13 @@ bool BlobDataBuilder::PopulateFutureData(size_t index, |
| return true; |
| } |
| -size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { |
| +size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, |
| + uint64_t length, |
| + uint64_t file_id) { |
| CHECK_NE(length, 0ull); |
| std::unique_ptr<DataElement> element(new DataElement()); |
| - element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( |
| - kAppendFutureFileTemporaryFileName)), |
| - offset, length, base::Time()); |
| + element->SetToFilePathRange(GetFutureFileItemPath(file_id), offset, length, |
| + base::Time()); |
| items_.push_back(new BlobDataItem(std::move(element))); |
| return items_.size() - 1; |
| } |
| @@ -125,22 +148,20 @@ bool BlobDataBuilder::PopulateFutureFile( |
| const scoped_refptr<ShareableFileReference>& file_reference, |
| const base::Time& expected_modification_time) { |
| DCHECK_LT(index, items_.size()); |
| - DataElement* old_element = items_.at(index)->data_element_ptr(); |
| + DataElement* element = items_[index]->data_element_ptr(); |
| - if (old_element->type() != DataElement::TYPE_FILE) { |
| + if (element->type() != DataElement::TYPE_FILE) { |
| DVLOG(1) << "Invalid item type."; |
| return false; |
| - } else if (old_element->path().AsUTF8Unsafe() != |
| - std::string(kAppendFutureFileTemporaryFileName)) { |
| + } else if (!IsFutureFileItem(*element)) { |
| DVLOG(1) << "Item not created by AppendFutureFile"; |
| return false; |
| } |
| - uint64_t length = old_element->length(); |
| - uint64_t offset = old_element->offset(); |
| - std::unique_ptr<DataElement> element(new DataElement()); |
| + uint64_t length = element->length(); |
| + uint64_t offset = element->offset(); |
| + items_[index]->data_handle_ = std::move(file_reference); |
| element->SetToFilePathRange(file_reference->path(), offset, length, |
| expected_modification_time); |
| - items_[index] = new BlobDataItem(std::move(element), file_reference); |
| return true; |
| } |