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 94fb9e50388633ba5bfcff901338603efc5039b6..08ddc039a7b83e798bdd166bb7826b51f5c999ab 100644 |
| --- a/storage/browser/blob/blob_data_builder.cc |
| +++ b/storage/browser/blob/blob_data_builder.cc |
| @@ -15,6 +15,34 @@ BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { |
| BlobDataBuilder::~BlobDataBuilder() { |
| } |
| +void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
| + uint64 length = ipc_data.length(); |
| + switch (ipc_data.type()) { |
| + case DataElement::TYPE_BYTES: |
| + DCHECK(!ipc_data.offset()); |
| + AppendData(ipc_data.bytes(), length); |
|
palmer
2015/10/27 20:36:41
Implicit conversion from uint64 to size_t here (|A
dmurph
2015/10/28 00:56:21
Done.
|
| + break; |
| + case DataElement::TYPE_FILE: |
| + AppendFile(ipc_data.path(), ipc_data.offset(), length, |
| + ipc_data.expected_modification_time()); |
| + break; |
| + case DataElement::TYPE_FILE_FILESYSTEM: |
| + AppendFileSystemFile(ipc_data.filesystem_url(), ipc_data.offset(), length, |
| + ipc_data.expected_modification_time()); |
| + break; |
| + case DataElement::TYPE_BLOB: |
| + // This is a temporary item that will be deconstructed later in |
| + // BlobStorageContext. |
| + AppendBlob(ipc_data.blob_uuid(), ipc_data.offset(), ipc_data.length()); |
| + break; |
| + case DataElement::TYPE_BYTES_DESCRIPTION: |
| + case DataElement::TYPE_UNKNOWN: |
| + case DataElement::TYPE_DISK_CACHE_ENTRY: // This type can't be sent by IPC. |
| + NOTREACHED(); |
| + break; |
| + } |
| +} |
| + |
| void BlobDataBuilder::AppendData(const char* data, size_t length) { |
| if (!length) |
| return; |
| @@ -23,6 +51,44 @@ void BlobDataBuilder::AppendData(const char* data, size_t length) { |
| items_.push_back(new BlobDataItem(element.Pass())); |
| } |
| +size_t BlobDataBuilder::AppendFutureData(size_t length) { |
| + CHECK_NE(length, 0u); |
| + scoped_ptr<DataElement> element(new DataElement()); |
| + element->SetToBytesDescription(length); |
| + items_.push_back(new BlobDataItem(element.Pass())); |
| + return items_.size() - 1; |
| +} |
| + |
| +bool BlobDataBuilder::PopulateFutureData(size_t index, |
| + const char* data, |
| + size_t offset, |
| + size_t length) { |
| + DCHECK(data); |
| + DataElement* element = items_.at(index)->data_element_ptr(); |
| + |
| + // We lazily allocate our data buffer by waiting until the first |
| + // PopulateFutureData call. |
| + // Why? The reason we have the AppendFutureData method is to create our Blob |
| + // record when the Renderer tells us about the blob without actually |
| + // allocating the memory yet, as we might not have the quota yet. So we don't |
| + // want to allocate the memory until we're actually receiving the data (which |
| + // the browser process only does when it has quota). |
| + if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { |
| + element->SetToAllocatedBytes(element->length()); |
| + // The type of the element is now TYPE_BYTES. |
| + } |
| + if (element->type() != DataElement::TYPE_BYTES) { |
| + DVLOG(1) << "Invalid item type."; |
| + return false; |
| + } |
| + if (offset + length > element->length()) { |
|
palmer
2015/10/27 20:36:40
The expression offset + length could overflow. Con
dmurph
2015/10/28 00:56:21
Done.
|
| + DVLOG(1) << "Invalid offset or length."; |
| + return false; |
| + } |
| + std::memcpy(element->mutable_bytes() + offset, data, length); |
| + return true; |
| +} |
| + |
| void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
| uint64_t offset, |
| uint64_t length, |
| @@ -54,7 +120,7 @@ void BlobDataBuilder::AppendFileSystemFile( |
| uint64_t offset, |
| uint64_t length, |
| const base::Time& expected_modification_time) { |
| - DCHECK(length > 0); |
| + DCHECK_GT(length, 0ul); |
| scoped_ptr<DataElement> element(new DataElement()); |
| element->SetToFileSystemUrlRange(url, offset, length, |
| expected_modification_time); |
| @@ -73,4 +139,23 @@ void BlobDataBuilder::AppendDiskCacheEntry( |
| disk_cache_stream_index)); |
| } |
| +void BlobDataBuilder::Clear() { |
| + items_.clear(); |
| + content_disposition_.clear(); |
| + content_type_.clear(); |
| + uuid_.clear(); |
| +} |
| + |
| +void PrintTo(const BlobDataBuilder& x, std::ostream* os) { |
| + DCHECK(os); |
| + *os << "<BlobDataBuilder>{uuid: " << x.uuid() |
| + << ", content_type: " << x.content_type_ |
| + << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| + for (const auto& item : x.items_) { |
| + PrintTo(*item, os); |
| + *os << ", "; |
| + } |
| + *os << "]}"; |
| +} |
| + |
| } // namespace storage |