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 "base/numerics/safe_conversions.h" | 7 #include "base/numerics/safe_conversions.h" |
| 8 #include "base/numerics/safe_math.h" | 8 #include "base/numerics/safe_math.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
| 11 #include "storage/browser/blob/shareable_file_reference.h" | 11 #include "storage/browser/blob/shareable_file_reference.h" |
| 12 | 12 |
| 13 namespace storage { | 13 namespace storage { |
| 14 namespace { | |
| 15 const char* kFuturePopulatingFilePath = "kFakeFilenameToBeChanged"; | |
| 16 } | |
| 14 | 17 |
| 15 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 18 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| 16 } | 19 } |
| 17 BlobDataBuilder::~BlobDataBuilder() { | 20 BlobDataBuilder::~BlobDataBuilder() { |
| 18 } | 21 } |
| 19 | 22 |
| 20 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | 23 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| 21 uint64 length = ipc_data.length(); | 24 uint64 length = ipc_data.length(); |
| 22 switch (ipc_data.type()) { | 25 switch (ipc_data.type()) { |
| 23 case DataElement::TYPE_BYTES: | 26 case DataElement::TYPE_BYTES: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 scoped_ptr<DataElement> element(new DataElement()); | 61 scoped_ptr<DataElement> element(new DataElement()); |
| 59 element->SetToBytesDescription(length); | 62 element->SetToBytesDescription(length); |
| 60 items_.push_back(new BlobDataItem(element.Pass())); | 63 items_.push_back(new BlobDataItem(element.Pass())); |
| 61 return items_.size() - 1; | 64 return items_.size() - 1; |
| 62 } | 65 } |
| 63 | 66 |
| 64 bool BlobDataBuilder::PopulateFutureData(size_t index, | 67 bool BlobDataBuilder::PopulateFutureData(size_t index, |
| 65 const char* data, | 68 const char* data, |
| 66 size_t offset, | 69 size_t offset, |
| 67 size_t length) { | 70 size_t length) { |
| 71 DCHECK_LT(index, items_.size()); | |
| 68 DCHECK(data); | 72 DCHECK(data); |
| 69 DataElement* element = items_.at(index)->data_element_ptr(); | 73 DataElement* element = items_.at(index)->data_element_ptr(); |
| 70 | 74 |
| 71 // We lazily allocate our data buffer by waiting until the first | 75 // We lazily allocate our data buffer by waiting until the first |
| 72 // PopulateFutureData call. | 76 // PopulateFutureData call. |
| 73 // Why? The reason we have the AppendFutureData method is to create our Blob | 77 // Why? The reason we have the AppendFutureData method is to create our Blob |
| 74 // record when the Renderer tells us about the blob without actually | 78 // record when the Renderer tells us about the blob without actually |
| 75 // allocating the memory yet, as we might not have the quota yet. So we don't | 79 // allocating the memory yet, as we might not have the quota yet. So we don't |
| 76 // want to allocate the memory until we're actually receiving the data (which | 80 // want to allocate the memory until we're actually receiving the data (which |
| 77 // the browser process only does when it has quota). | 81 // the browser process only does when it has quota). |
| 78 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { | 82 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { |
| 79 element->SetToAllocatedBytes(element->length()); | 83 element->SetToAllocatedBytes(element->length()); |
| 80 // The type of the element is now TYPE_BYTES. | 84 // The type of the element is now TYPE_BYTES. |
| 81 } | 85 } |
| 82 if (element->type() != DataElement::TYPE_BYTES) { | 86 if (element->type() != DataElement::TYPE_BYTES) { |
| 83 DVLOG(1) << "Invalid item type."; | 87 DVLOG(1) << "Invalid item type."; |
| 84 return false; | 88 return false; |
| 85 } | 89 } |
| 86 base::CheckedNumeric<size_t> checked_end = offset; | 90 base::CheckedNumeric<size_t> checked_end = offset; |
| 87 checked_end += length; | 91 checked_end += length; |
| 88 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { | 92 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { |
| 89 DVLOG(1) << "Invalid offset or length."; | 93 DVLOG(1) << "Invalid offset or length."; |
| 90 return false; | 94 return false; |
| 91 } | 95 } |
| 92 std::memcpy(element->mutable_bytes() + offset, data, length); | 96 std::memcpy(element->mutable_bytes() + offset, data, length); |
| 93 return true; | 97 return true; |
| 94 } | 98 } |
| 95 | 99 |
| 100 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | |
| 101 CHECK_NE(length, 0ull); | |
| 102 scoped_ptr<DataElement> element(new DataElement()); | |
| 103 element->SetToFilePathRange( | |
| 104 base::FilePath::FromUTF8Unsafe(std::string(kFuturePopulatingFilePath)), | |
| 105 offset, length, base::Time()); | |
| 106 items_.push_back(new BlobDataItem(element.Pass())); | |
| 107 return items_.size() - 1; | |
| 108 } | |
| 109 | |
| 110 bool BlobDataBuilder::PopulateFutureFile( | |
| 111 size_t index, | |
| 112 const scoped_refptr<ShareableFileReference>& file_reference, | |
| 113 const base::Time& expected_modification_time) { | |
| 114 DCHECK_LT(index, items_.size()); | |
| 115 DataElement* old_element = items_.at(index)->data_element_ptr(); | |
| 116 | |
| 117 if (old_element->type() != DataElement::TYPE_FILE) { | |
| 118 DVLOG(1) << "Invalid item type."; | |
| 119 return false; | |
| 120 } else if (old_element->path().AsUTF8Unsafe() != | |
| 121 std::string(kFuturePopulatingFilePath)) { | |
| 122 DVLOG(1) << "Item not created by AppendFutureFile"; | |
| 123 return false; | |
| 124 } | |
| 125 uint64_t length = old_element->length(); | |
| 126 uint64_t offset = old_element->offset(); | |
| 127 // We null this out as it is about to be invalid after we set the vector item | |
| 128 // to a different BlobDataItem | |
| 129 old_element = nullptr; | |
|
michaeln
2015/11/21 00:59:46
Not sure you need to do this or comment about it,
dmurph
2015/11/23 20:07:02
Done.
| |
| 130 scoped_ptr<DataElement> element(new DataElement()); | |
| 131 element->SetToFilePathRange(file_reference->path(), offset, length, | |
| 132 expected_modification_time); | |
| 133 items_[index] = new BlobDataItem(element.Pass(), file_reference); | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 96 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 137 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| 97 uint64_t offset, | 138 uint64_t offset, |
| 98 uint64_t length, | 139 uint64_t length, |
| 99 const base::Time& expected_modification_time) { | 140 const base::Time& expected_modification_time) { |
| 100 scoped_ptr<DataElement> element(new DataElement()); | 141 scoped_ptr<DataElement> element(new DataElement()); |
| 101 element->SetToFilePathRange(file_path, offset, length, | 142 element->SetToFilePathRange(file_path, offset, length, |
| 102 expected_modification_time); | 143 expected_modification_time); |
| 103 items_.push_back( | 144 items_.push_back( |
| 104 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); | 145 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); |
| 105 } | 146 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 << ", content_type: " << x.content_type_ | 197 << ", content_type: " << x.content_type_ |
| 157 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 198 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 158 for (const auto& item : x.items_) { | 199 for (const auto& item : x.items_) { |
| 159 PrintTo(*item, os); | 200 PrintTo(*item, os); |
| 160 *os << ", "; | 201 *os << ", "; |
| 161 } | 202 } |
| 162 *os << "]}"; | 203 *os << "]}"; |
| 163 } | 204 } |
| 164 | 205 |
| 165 } // namespace storage | 206 } // namespace storage |
| OLD | NEW |