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