Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: storage/browser/blob/blob_data_builder.cc

Issue 1288373002: [BlobAsync] Patch 2: Common Constants (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@async1
Patch Set: safe math Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « storage/browser/blob/blob_data_builder.h ('k') | storage/browser/blob/blob_data_item.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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(),
26 base::saturated_cast<size_t, uint64>(length));
palmer 2015/10/30 23:11:43 Hmm, is saturation the right semantics here? I don
dmurph 2015/10/30 23:22:07 well, it's probably not valid if someone wants to
27 break;
28 case DataElement::TYPE_FILE:
29 AppendFile(ipc_data.path(), ipc_data.offset(), length,
30 ipc_data.expected_modification_time());
31 break;
32 case DataElement::TYPE_FILE_FILESYSTEM:
33 AppendFileSystemFile(ipc_data.filesystem_url(), ipc_data.offset(), length,
34 ipc_data.expected_modification_time());
35 break;
36 case DataElement::TYPE_BLOB:
37 // This is a temporary item that will be deconstructed later in
38 // BlobStorageContext.
39 AppendBlob(ipc_data.blob_uuid(), ipc_data.offset(), ipc_data.length());
40 break;
41 case DataElement::TYPE_BYTES_DESCRIPTION:
42 case DataElement::TYPE_UNKNOWN:
43 case DataElement::TYPE_DISK_CACHE_ENTRY: // This type can't be sent by IPC.
44 NOTREACHED();
45 break;
46 }
47 }
48
18 void BlobDataBuilder::AppendData(const char* data, size_t length) { 49 void BlobDataBuilder::AppendData(const char* data, size_t length) {
19 if (!length) 50 if (!length)
20 return; 51 return;
21 scoped_ptr<DataElement> element(new DataElement()); 52 scoped_ptr<DataElement> element(new DataElement());
22 element->SetToBytes(data, length); 53 element->SetToBytes(data, length);
23 items_.push_back(new BlobDataItem(element.Pass())); 54 items_.push_back(new BlobDataItem(element.Pass()));
24 } 55 }
25 56
57 size_t BlobDataBuilder::AppendFutureData(size_t length) {
58 CHECK_NE(length, 0u);
59 scoped_ptr<DataElement> element(new DataElement());
60 element->SetToBytesDescription(length);
61 items_.push_back(new BlobDataItem(element.Pass()));
62 return items_.size() - 1;
63 }
64
65 bool BlobDataBuilder::PopulateFutureData(size_t index,
66 const char* data,
67 size_t offset,
68 size_t length) {
69 DCHECK(data);
70 DataElement* element = items_.at(index)->data_element_ptr();
71
72 // We lazily allocate our data buffer by waiting until the first
73 // PopulateFutureData call.
74 // Why? The reason we have the AppendFutureData method is to create our Blob
75 // record when the Renderer tells us about the blob without actually
76 // allocating the memory yet, as we might not have the quota yet. So we don't
77 // want to allocate the memory until we're actually receiving the data (which
78 // the browser process only does when it has quota).
79 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) {
80 element->SetToAllocatedBytes(element->length());
81 // The type of the element is now TYPE_BYTES.
82 }
83 if (element->type() != DataElement::TYPE_BYTES) {
84 DVLOG(1) << "Invalid item type.";
85 return false;
86 }
87 base::CheckedNumeric<size_t> checked_end = offset;
88 checked_end = checked_end + length;
palmer 2015/10/30 23:11:43 Nit: checked_end += length is more concise (IMO).
dmurph 2015/10/30 23:22:07 Done.
89 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) {
90 DVLOG(1) << "Invalid offset or length.";
91 return false;
92 }
93 std::memcpy(element->mutable_bytes() + offset, data, length);
94 return true;
95 }
96
26 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, 97 void BlobDataBuilder::AppendFile(const base::FilePath& file_path,
27 uint64_t offset, 98 uint64_t offset,
28 uint64_t length, 99 uint64_t length,
29 const base::Time& expected_modification_time) { 100 const base::Time& expected_modification_time) {
30 scoped_ptr<DataElement> element(new DataElement()); 101 scoped_ptr<DataElement> element(new DataElement());
31 element->SetToFilePathRange(file_path, offset, length, 102 element->SetToFilePathRange(file_path, offset, length,
32 expected_modification_time); 103 expected_modification_time);
33 items_.push_back( 104 items_.push_back(
34 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); 105 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path)));
35 } 106 }
(...skipping 11 matching lines...) Expand all
47 scoped_ptr<DataElement> element(new DataElement()); 118 scoped_ptr<DataElement> element(new DataElement());
48 element->SetToBlob(uuid); 119 element->SetToBlob(uuid);
49 items_.push_back(new BlobDataItem(element.Pass())); 120 items_.push_back(new BlobDataItem(element.Pass()));
50 } 121 }
51 122
52 void BlobDataBuilder::AppendFileSystemFile( 123 void BlobDataBuilder::AppendFileSystemFile(
53 const GURL& url, 124 const GURL& url,
54 uint64_t offset, 125 uint64_t offset,
55 uint64_t length, 126 uint64_t length,
56 const base::Time& expected_modification_time) { 127 const base::Time& expected_modification_time) {
57 DCHECK(length > 0); 128 DCHECK_GT(length, 0ul);
58 scoped_ptr<DataElement> element(new DataElement()); 129 scoped_ptr<DataElement> element(new DataElement());
59 element->SetToFileSystemUrlRange(url, offset, length, 130 element->SetToFileSystemUrlRange(url, offset, length,
60 expected_modification_time); 131 expected_modification_time);
61 items_.push_back(new BlobDataItem(element.Pass())); 132 items_.push_back(new BlobDataItem(element.Pass()));
62 } 133 }
63 134
64 void BlobDataBuilder::AppendDiskCacheEntry( 135 void BlobDataBuilder::AppendDiskCacheEntry(
65 const scoped_refptr<DataHandle>& data_handle, 136 const scoped_refptr<DataHandle>& data_handle,
66 disk_cache::Entry* disk_cache_entry, 137 disk_cache::Entry* disk_cache_entry,
67 int disk_cache_stream_index) { 138 int disk_cache_stream_index) {
68 scoped_ptr<DataElement> element(new DataElement()); 139 scoped_ptr<DataElement> element(new DataElement());
69 element->SetToDiskCacheEntryRange( 140 element->SetToDiskCacheEntryRange(
70 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); 141 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index));
71 items_.push_back( 142 items_.push_back(
72 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, 143 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry,
73 disk_cache_stream_index)); 144 disk_cache_stream_index));
74 } 145 }
75 146
147 void BlobDataBuilder::Clear() {
148 items_.clear();
149 content_disposition_.clear();
150 content_type_.clear();
151 uuid_.clear();
152 }
153
154 void PrintTo(const BlobDataBuilder& x, std::ostream* os) {
155 DCHECK(os);
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 PrintTo(*item, os);
161 *os << ", ";
162 }
163 *os << "]}";
164 }
165
76 } // namespace storage 166 } // namespace storage
OLDNEW
« no previous file with comments | « storage/browser/blob/blob_data_builder.h ('k') | storage/browser/blob/blob_data_item.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698