| 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 | 14 |
| 15 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = |
| 16 "kFakeFilenameToBeChangedByPopulateFutureFile"; |
| 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: |
| 24 DCHECK(!ipc_data.offset()); | 27 DCHECK(!ipc_data.offset()); |
| (...skipping 33 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(base::FilePath::FromUTF8Unsafe(std::string( |
| 104 kAppendFutureFileTemporaryFileName)), |
| 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(kAppendFutureFileTemporaryFileName)) { |
| 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 scoped_ptr<DataElement> element(new DataElement()); |
| 128 element->SetToFilePathRange(file_reference->path(), offset, length, |
| 129 expected_modification_time); |
| 130 items_[index] = new BlobDataItem(element.Pass(), file_reference); |
| 131 return true; |
| 132 } |
| 133 |
| 96 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 134 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| 97 uint64_t offset, | 135 uint64_t offset, |
| 98 uint64_t length, | 136 uint64_t length, |
| 99 const base::Time& expected_modification_time) { | 137 const base::Time& expected_modification_time) { |
| 100 scoped_ptr<DataElement> element(new DataElement()); | 138 scoped_ptr<DataElement> element(new DataElement()); |
| 101 element->SetToFilePathRange(file_path, offset, length, | 139 element->SetToFilePathRange(file_path, offset, length, |
| 102 expected_modification_time); | 140 expected_modification_time); |
| 103 items_.push_back( | 141 items_.push_back( |
| 104 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); | 142 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); |
| 105 } | 143 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 << ", content_type: " << x.content_type_ | 194 << ", content_type: " << x.content_type_ |
| 157 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 195 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 158 for (const auto& item : x.items_) { | 196 for (const auto& item : x.items_) { |
| 159 PrintTo(*item, os); | 197 PrintTo(*item, os); |
| 160 *os << ", "; | 198 *os << ", "; |
| 161 } | 199 } |
| 162 *os << "]}"; | 200 *os << "]}"; |
| 163 } | 201 } |
| 164 | 202 |
| 165 } // namespace storage | 203 } // namespace storage |
| OLD | NEW |