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/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "net/disk_cache/disk_cache.h" | 8 #include "net/disk_cache/disk_cache.h" |
| 9 #include "storage/browser/blob/shareable_file_reference.h" | 9 #include "storage/browser/blob/shareable_file_reference.h" |
| 10 | 10 |
| 11 namespace storage { | 11 namespace storage { |
| 12 | 12 |
| 13 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 13 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| 14 } | 14 } |
| 15 BlobDataBuilder::~BlobDataBuilder() { | 15 BlobDataBuilder::~BlobDataBuilder() { |
| 16 } | 16 } |
| 17 | 17 |
| 18 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | |
| 19 uint64 length = ipc_data.length(); | |
| 20 switch (ipc_data.type()) { | |
| 21 case DataElement::TYPE_BYTES: | |
| 22 DCHECK(!ipc_data.offset()); | |
| 23 AppendData(ipc_data.bytes(), length); | |
| 24 break; | |
| 25 case DataElement::TYPE_FILE: | |
| 26 AppendFile(ipc_data.path(), ipc_data.offset(), length, | |
| 27 ipc_data.expected_modification_time()); | |
| 28 break; | |
| 29 case DataElement::TYPE_FILE_FILESYSTEM: | |
| 30 AppendFileSystemFile(ipc_data.filesystem_url(), ipc_data.offset(), length, | |
| 31 ipc_data.expected_modification_time()); | |
| 32 break; | |
| 33 case DataElement::TYPE_BLOB: | |
| 34 // This is a temporary item that will be deconstructed later in | |
| 35 // BlobStorageContext. | |
| 36 AppendBlob(ipc_data.blob_uuid(), ipc_data.offset(), ipc_data.length()); | |
| 37 break; | |
| 38 case DataElement::TYPE_BYTES_DESCRIPTION: | |
| 39 case DataElement::TYPE_UNKNOWN: | |
| 40 case DataElement::TYPE_DISK_CACHE_ENTRY: // This type can't be sent by IPC. | |
| 41 NOTREACHED(); | |
| 42 break; | |
| 43 } | |
| 44 } | |
| 45 | |
| 18 void BlobDataBuilder::AppendData(const char* data, size_t length) { | 46 void BlobDataBuilder::AppendData(const char* data, size_t length) { |
| 19 if (!length) | 47 if (!length) |
| 20 return; | 48 return; |
| 21 scoped_ptr<DataElement> element(new DataElement()); | 49 scoped_ptr<DataElement> element(new DataElement()); |
| 22 element->SetToBytes(data, length); | 50 element->SetToBytes(data, length); |
| 23 items_.push_back(new BlobDataItem(element.Pass())); | 51 items_.push_back(new BlobDataItem(element.Pass())); |
| 24 } | 52 } |
| 25 | 53 |
| 54 void BlobDataBuilder::AppendFutureData(size_t length) { | |
| 55 if (!length) | |
| 56 return; | |
|
michaeln
2015/10/20 20:50:02
does this mess up index addressability at populate
dmurph
2015/10/20 22:49:35
Done. I also now return the index of the item, so
| |
| 57 scoped_ptr<DataElement> element(new DataElement()); | |
| 58 element->SetToBytesDescription(length); | |
| 59 items_.push_back(new BlobDataItem(element.Pass())); | |
| 60 } | |
| 61 | |
| 62 bool BlobDataBuilder::PopulateFutureData(size_t index, | |
| 63 const char* data, | |
| 64 size_t offset, | |
| 65 size_t length) { | |
| 66 DCHECK(data); | |
| 67 DataElement* element = items_.at(index)->data_element_ptr(); | |
| 68 | |
| 69 // We lazily allocate our data buffer by waiting until the first | |
| 70 // PopulateFutureData call. | |
| 71 // Why? The reason we have the AppendFutureData method is to create our Blob | |
| 72 // record when the Renderer tells us about the blob without actually | |
| 73 // allocating the memory yet, as we might not have the quota yet. So we don't | |
| 74 // want to allocate the memory until we're actually receiving the data (which | |
| 75 // the browser process only does when it has quota). | |
| 76 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { | |
| 77 element->SetToAllocatedBytes(element->length()); | |
| 78 // The type of the element is now TYPE_BYTES. | |
| 79 } | |
| 80 if (element->type() != DataElement::TYPE_BYTES) { | |
| 81 DVLOG(1) << "Invalid item type."; | |
| 82 return false; | |
| 83 } | |
| 84 if (offset + length > element->length()) { | |
| 85 DVLOG(1) << "Invalid offset or length."; | |
| 86 return false; | |
| 87 } | |
| 88 std::memcpy(element->mutable_bytes() + offset, data, length); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 26 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 92 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| 27 uint64_t offset, | 93 uint64_t offset, |
| 28 uint64_t length, | 94 uint64_t length, |
| 29 const base::Time& expected_modification_time) { | 95 const base::Time& expected_modification_time) { |
| 30 scoped_ptr<DataElement> element(new DataElement()); | 96 scoped_ptr<DataElement> element(new DataElement()); |
| 31 element->SetToFilePathRange(file_path, offset, length, | 97 element->SetToFilePathRange(file_path, offset, length, |
| 32 expected_modification_time); | 98 expected_modification_time); |
| 33 items_.push_back( | 99 items_.push_back( |
| 34 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); | 100 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); |
| 35 } | 101 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 47 scoped_ptr<DataElement> element(new DataElement()); | 113 scoped_ptr<DataElement> element(new DataElement()); |
| 48 element->SetToBlob(uuid); | 114 element->SetToBlob(uuid); |
| 49 items_.push_back(new BlobDataItem(element.Pass())); | 115 items_.push_back(new BlobDataItem(element.Pass())); |
| 50 } | 116 } |
| 51 | 117 |
| 52 void BlobDataBuilder::AppendFileSystemFile( | 118 void BlobDataBuilder::AppendFileSystemFile( |
| 53 const GURL& url, | 119 const GURL& url, |
| 54 uint64_t offset, | 120 uint64_t offset, |
| 55 uint64_t length, | 121 uint64_t length, |
| 56 const base::Time& expected_modification_time) { | 122 const base::Time& expected_modification_time) { |
| 57 DCHECK(length > 0); | 123 DCHECK_GT(length, 0ul); |
| 58 scoped_ptr<DataElement> element(new DataElement()); | 124 scoped_ptr<DataElement> element(new DataElement()); |
| 59 element->SetToFileSystemUrlRange(url, offset, length, | 125 element->SetToFileSystemUrlRange(url, offset, length, |
| 60 expected_modification_time); | 126 expected_modification_time); |
| 61 items_.push_back(new BlobDataItem(element.Pass())); | 127 items_.push_back(new BlobDataItem(element.Pass())); |
| 62 } | 128 } |
| 63 | 129 |
| 64 void BlobDataBuilder::AppendDiskCacheEntry( | 130 void BlobDataBuilder::AppendDiskCacheEntry( |
| 65 const scoped_refptr<DataHandle>& data_handle, | 131 const scoped_refptr<DataHandle>& data_handle, |
| 66 disk_cache::Entry* disk_cache_entry, | 132 disk_cache::Entry* disk_cache_entry, |
| 67 int disk_cache_stream_index) { | 133 int disk_cache_stream_index) { |
| 68 scoped_ptr<DataElement> element(new DataElement()); | 134 scoped_ptr<DataElement> element(new DataElement()); |
| 69 element->SetToDiskCacheEntryRange( | 135 element->SetToDiskCacheEntryRange( |
| 70 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); | 136 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); |
| 71 items_.push_back( | 137 items_.push_back( |
| 72 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, | 138 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, |
| 73 disk_cache_stream_index)); | 139 disk_cache_stream_index)); |
| 74 } | 140 } |
| 75 | 141 |
| 142 void BlobDataBuilder::Clear() { | |
| 143 items_.clear(); | |
| 144 content_disposition_.clear(); | |
| 145 content_type_.clear(); | |
| 146 uuid_.clear(); | |
| 147 } | |
| 148 | |
| 149 void PrintTo(const BlobDataBuilder& x, std::ostream* os) { | |
| 150 DCHECK(os); | |
| 151 *os << "<BlobDataBuilder>{uuid: " << x.uuid() | |
| 152 << ", content_type: " << x.content_type_ | |
| 153 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | |
| 154 for (const auto& item : x.items_) { | |
| 155 PrintTo(*item, os); | |
| 156 *os << ", "; | |
| 157 } | |
| 158 *os << "]}"; | |
| 159 } | |
| 160 | |
| 76 } // namespace storage | 161 } // namespace storage |
| OLD | NEW |