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::AppendDataElement(const DataElement& ipc_data) { |
| 19 scoped_refptr<BlobDataItem> blob_item; |
| 20 |
| 21 uint64 length = ipc_data.length(); |
| 22 scoped_ptr<DataElement> element(new DataElement()); |
| 23 switch (ipc_data.type()) { |
| 24 case DataElement::TYPE_BYTES: |
| 25 DCHECK(!ipc_data.offset()); |
| 26 element->SetToBytes(ipc_data.bytes(), length); |
| 27 blob_item = new BlobDataItem(element.Pass()); |
| 28 break; |
| 29 case DataElement::TYPE_FILE: |
| 30 element->SetToFilePathRange(ipc_data.path(), ipc_data.offset(), length, |
| 31 ipc_data.expected_modification_time()); |
| 32 blob_item = new BlobDataItem( |
| 33 element.Pass(), ShareableFileReference::Get(ipc_data.path())); |
| 34 break; |
| 35 case DataElement::TYPE_FILE_FILESYSTEM: |
| 36 element->SetToFileSystemUrlRange(ipc_data.filesystem_url(), |
| 37 ipc_data.offset(), length, |
| 38 ipc_data.expected_modification_time()); |
| 39 blob_item = new BlobDataItem(element.Pass()); |
| 40 break; |
| 41 case DataElement::TYPE_BLOB: |
| 42 // This is a temporary item that will be deconstructed later. |
| 43 element->SetToBlobRange(ipc_data.blob_uuid(), ipc_data.offset(), |
| 44 ipc_data.length()); |
| 45 blob_item = new BlobDataItem(element.Pass()); |
| 46 break; |
| 47 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 48 case DataElement::TYPE_UNKNOWN: |
| 49 case DataElement::TYPE_DISK_CACHE_ENTRY: // This type can't be sent by IPC. |
| 50 NOTREACHED(); |
| 51 break; |
| 52 } |
| 53 items_.push_back(blob_item.Pass()); |
| 54 } |
| 55 |
18 void BlobDataBuilder::AppendData(const char* data, size_t length) { | 56 void BlobDataBuilder::AppendData(const char* data, size_t length) { |
19 if (!length) | 57 if (!length) |
20 return; | 58 return; |
21 scoped_ptr<DataElement> element(new DataElement()); | 59 scoped_ptr<DataElement> element(new DataElement()); |
22 element->SetToBytes(data, length); | 60 element->SetToBytes(data, length); |
23 items_.push_back(new BlobDataItem(element.Pass())); | 61 items_.push_back(new BlobDataItem(element.Pass())); |
24 } | 62 } |
25 | 63 |
| 64 void BlobDataBuilder::AppendFutureData(size_t length) { |
| 65 if (!length) |
| 66 return; |
| 67 scoped_ptr<DataElement> element(new DataElement()); |
| 68 element->SetToBytesDescription(length); |
| 69 items_.push_back(new BlobDataItem(element.Pass())); |
| 70 } |
| 71 |
| 72 bool BlobDataBuilder::PopulateFutureData(size_t index, |
| 73 const char* data, |
| 74 size_t offset, |
| 75 size_t length) { |
| 76 if (!length) |
| 77 return true; |
| 78 if (!data) |
| 79 return false; |
| 80 auto& item = items_.at(index); |
| 81 DataElement* element = item->data_element_ptr(); |
| 82 // On the first call, we are description, and allocate then. |
| 83 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { |
| 84 element->SetToAllocatedBytes(element->length()); |
| 85 } |
| 86 if (element->type() != DataElement::TYPE_BYTES) { |
| 87 LOG(ERROR) << "Invalid item type."; |
| 88 return false; |
| 89 } |
| 90 if (offset + length > element->length()) { |
| 91 LOG(ERROR) << "Invalid offset or length."; |
| 92 return false; |
| 93 } |
| 94 std::memcpy(element->mutable_bytes() + offset, data, length); |
| 95 return true; |
| 96 } |
| 97 |
26 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 98 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
27 uint64_t offset, | 99 uint64_t offset, |
28 uint64_t length, | 100 uint64_t length, |
29 const base::Time& expected_modification_time) { | 101 const base::Time& expected_modification_time) { |
30 scoped_ptr<DataElement> element(new DataElement()); | 102 scoped_ptr<DataElement> element(new DataElement()); |
31 element->SetToFilePathRange(file_path, offset, length, | 103 element->SetToFilePathRange(file_path, offset, length, |
32 expected_modification_time); | 104 expected_modification_time); |
33 items_.push_back( | 105 items_.push_back( |
34 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); | 106 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); |
35 } | 107 } |
(...skipping 30 matching lines...) Expand all Loading... |
66 disk_cache::Entry* disk_cache_entry, | 138 disk_cache::Entry* disk_cache_entry, |
67 int disk_cache_stream_index) { | 139 int disk_cache_stream_index) { |
68 scoped_ptr<DataElement> element(new DataElement()); | 140 scoped_ptr<DataElement> element(new DataElement()); |
69 element->SetToDiskCacheEntryRange( | 141 element->SetToDiskCacheEntryRange( |
70 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); | 142 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); |
71 items_.push_back( | 143 items_.push_back( |
72 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, | 144 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, |
73 disk_cache_stream_index)); | 145 disk_cache_stream_index)); |
74 } | 146 } |
75 | 147 |
| 148 void BlobDataBuilder::Clear() { |
| 149 items_.clear(); |
| 150 content_disposition_.clear(); |
| 151 content_type_.clear(); |
| 152 uuid_.clear(); |
| 153 } |
| 154 |
| 155 std::ostream& operator<<(std::ostream& os, const BlobDataBuilder& x) { |
| 156 os << "<BlobDataBuilder>{uuid: " << x.uuid() |
| 157 << ", content_type: " << x.content_type_ |
| 158 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
| 159 for (const auto& item : x.items_) { |
| 160 os << *item << ", "; |
| 161 } |
| 162 os << "]}"; |
| 163 return os; |
| 164 } |
| 165 |
76 } // namespace storage | 166 } // namespace storage |
OLD | NEW |