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

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

Issue 2339933004: [BlobStorage] BlobMemoryController & tests (Closed)
Patch Set: comments, more tests are next Created 4 years, 2 months 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
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/numerics/safe_conversions.h" 13 #include "base/numerics/safe_conversions.h"
14 #include "base/numerics/safe_math.h" 14 #include "base/numerics/safe_math.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
15 #include "base/time/time.h" 17 #include "base/time/time.h"
16 #include "net/disk_cache/disk_cache.h" 18 #include "net/disk_cache/disk_cache.h"
17 #include "storage/browser/blob/shareable_file_reference.h"
18 19
19 namespace storage { 20 namespace storage {
20 21
21 namespace { 22 namespace {
22 23
23 const static int kInvalidDiskCacheSideStreamIndex = -1; 24 const static int kInvalidDiskCacheSideStreamIndex = -1;
24 25
25 } // namespace 26 } // namespace
26 27
27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = 28 const char kFutureFileName[] = "_future_name_";
28 "kFakeFilenameToBeChangedByPopulateFutureFile";
29 29
30 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { 30 /* static */
31 base::FilePath BlobDataBuilder::GetFutureFileItemPath(uint64_t file_id) {
32 return base::FilePath::FromUTF8Unsafe(kFutureFileName)
33 .AddExtension(base::Uint64ToString(file_id));
31 } 34 }
32 BlobDataBuilder::~BlobDataBuilder() { 35
36 /* static */
37 bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) {
38 const std::string prefix(kFutureFileName);
39 const std::string path = element.path().MaybeAsASCII();
40 // The prefix shouldn't occur unless the user used "AppendFutureFile". We
41 // DCHECK on AppendFile to make sure no one appends a future file.
42 return base::StartsWith(path, prefix, base::CompareCase::SENSITIVE);
33 } 43 }
34 44
45 /* static */
46 uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) {
47 DCHECK(IsFutureFileItem(element));
48 uint64_t id = 0;
49 bool success =
50 base::StringToUint64(element.path().Extension().substr(1), &id);
51 DCHECK(success) << element.path().Extension();
52 return id;
53 }
54
55 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) {}
56 BlobDataBuilder::~BlobDataBuilder() {}
57
35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { 58 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) {
36 uint64_t length = ipc_data.length(); 59 uint64_t length = ipc_data.length();
37 switch (ipc_data.type()) { 60 switch (ipc_data.type()) {
38 case DataElement::TYPE_BYTES: 61 case DataElement::TYPE_BYTES:
39 DCHECK(!ipc_data.offset()); 62 DCHECK(!ipc_data.offset());
40 AppendData(ipc_data.bytes(), 63 AppendData(ipc_data.bytes(),
41 base::checked_cast<size_t, uint64_t>(length)); 64 base::checked_cast<size_t, uint64_t>(length));
42 break; 65 break;
43 case DataElement::TYPE_FILE: 66 case DataElement::TYPE_FILE:
44 AppendFile(ipc_data.path(), ipc_data.offset(), length, 67 AppendFile(ipc_data.path(), ipc_data.offset(), length,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 items_.push_back(new BlobDataItem(std::move(element))); 99 items_.push_back(new BlobDataItem(std::move(element)));
77 return items_.size() - 1; 100 return items_.size() - 1;
78 } 101 }
79 102
80 bool BlobDataBuilder::PopulateFutureData(size_t index, 103 bool BlobDataBuilder::PopulateFutureData(size_t index,
81 const char* data, 104 const char* data,
82 size_t offset, 105 size_t offset,
83 size_t length) { 106 size_t length) {
84 DCHECK_LT(index, items_.size()); 107 DCHECK_LT(index, items_.size());
85 DCHECK(data); 108 DCHECK(data);
86 DataElement* element = items_.at(index)->data_element_ptr(); 109 DataElement* element = items_[index]->data_element_ptr();
87 110
88 // We lazily allocate our data buffer by waiting until the first 111 // We lazily allocate our data buffer by waiting until the first
89 // PopulateFutureData call. 112 // PopulateFutureData call.
90 // Why? The reason we have the AppendFutureData method is to create our Blob 113 // Why? The reason we have the AppendFutureData method is to create our Blob
91 // record when the Renderer tells us about the blob without actually 114 // record when the Renderer tells us about the blob without actually
92 // allocating the memory yet, as we might not have the quota yet. So we don't 115 // allocating the memory yet, as we might not have the quota yet. So we don't
93 // want to allocate the memory until we're actually receiving the data (which 116 // want to allocate the memory until we're actually receiving the data (which
94 // the browser process only does when it has quota). 117 // the browser process only does when it has quota).
95 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { 118 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) {
96 element->SetToAllocatedBytes(element->length()); 119 element->SetToAllocatedBytes(element->length());
97 // The type of the element is now TYPE_BYTES. 120 // The type of the element is now TYPE_BYTES.
98 } 121 }
99 if (element->type() != DataElement::TYPE_BYTES) { 122 if (element->type() != DataElement::TYPE_BYTES) {
100 DVLOG(1) << "Invalid item type."; 123 DVLOG(1) << "Invalid item type.";
101 return false; 124 return false;
102 } 125 }
103 base::CheckedNumeric<size_t> checked_end = offset; 126 base::CheckedNumeric<size_t> checked_end = offset;
104 checked_end += length; 127 checked_end += length;
105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { 128 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) {
106 DVLOG(1) << "Invalid offset or length."; 129 DVLOG(1) << "Invalid offset or length.";
107 return false; 130 return false;
108 } 131 }
109 std::memcpy(element->mutable_bytes() + offset, data, length); 132 std::memcpy(element->mutable_bytes() + offset, data, length);
110 return true; 133 return true;
111 } 134 }
112 135
113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { 136 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset,
137 uint64_t length,
138 uint64_t file_id) {
114 CHECK_NE(length, 0ull); 139 CHECK_NE(length, 0ull);
115 std::unique_ptr<DataElement> element(new DataElement()); 140 std::unique_ptr<DataElement> element(new DataElement());
116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( 141 element->SetToFilePathRange(GetFutureFileItemPath(file_id), offset, length,
117 kAppendFutureFileTemporaryFileName)), 142 base::Time());
118 offset, length, base::Time());
119 items_.push_back(new BlobDataItem(std::move(element))); 143 items_.push_back(new BlobDataItem(std::move(element)));
120 return items_.size() - 1; 144 return items_.size() - 1;
121 } 145 }
122 146
123 bool BlobDataBuilder::PopulateFutureFile( 147 bool BlobDataBuilder::PopulateFutureFile(
124 size_t index, 148 size_t index,
125 const scoped_refptr<ShareableFileReference>& file_reference, 149 const scoped_refptr<ShareableFileReference>& file_reference,
126 const base::Time& expected_modification_time) { 150 const base::Time& expected_modification_time) {
127 DCHECK_LT(index, items_.size()); 151 DCHECK_LT(index, items_.size());
128 DataElement* old_element = items_.at(index)->data_element_ptr(); 152 DataElement* element = items_[index]->data_element_ptr();
129 153
130 if (old_element->type() != DataElement::TYPE_FILE) { 154 if (element->type() != DataElement::TYPE_FILE) {
131 DVLOG(1) << "Invalid item type."; 155 DVLOG(1) << "Invalid item type.";
132 return false; 156 return false;
133 } else if (old_element->path().AsUTF8Unsafe() != 157 } else if (!IsFutureFileItem(*element)) {
134 std::string(kAppendFutureFileTemporaryFileName)) {
135 DVLOG(1) << "Item not created by AppendFutureFile"; 158 DVLOG(1) << "Item not created by AppendFutureFile";
136 return false; 159 return false;
137 } 160 }
138 uint64_t length = old_element->length(); 161 uint64_t length = element->length();
139 uint64_t offset = old_element->offset(); 162 uint64_t offset = element->offset();
140 std::unique_ptr<DataElement> element(new DataElement()); 163 items_[index]->data_handle_ = std::move(file_reference);
141 element->SetToFilePathRange(file_reference->path(), offset, length, 164 element->SetToFilePathRange(file_reference->path(), offset, length,
142 expected_modification_time); 165 expected_modification_time);
143 items_[index] = new BlobDataItem(std::move(element), file_reference);
144 return true; 166 return true;
145 } 167 }
146 168
147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, 169 void BlobDataBuilder::AppendFile(const base::FilePath& file_path,
148 uint64_t offset, 170 uint64_t offset,
149 uint64_t length, 171 uint64_t length,
150 const base::Time& expected_modification_time) { 172 const base::Time& expected_modification_time) {
151 std::unique_ptr<DataElement> element(new DataElement()); 173 std::unique_ptr<DataElement> element(new DataElement());
152 element->SetToFilePathRange(file_path, offset, length, 174 element->SetToFilePathRange(file_path, offset, length,
153 expected_modification_time); 175 expected_modification_time);
176 DCHECK(!IsFutureFileItem(*element));
154 items_.push_back(new BlobDataItem(std::move(element), 177 items_.push_back(new BlobDataItem(std::move(element),
155 ShareableFileReference::Get(file_path))); 178 ShareableFileReference::Get(file_path)));
156 } 179 }
157 180
158 void BlobDataBuilder::AppendBlob(const std::string& uuid, 181 void BlobDataBuilder::AppendBlob(const std::string& uuid,
159 uint64_t offset, 182 uint64_t offset,
160 uint64_t length) { 183 uint64_t length) {
161 DCHECK_GT(length, 0ul); 184 DCHECK_GT(length, 0ul);
162 std::unique_ptr<DataElement> element(new DataElement()); 185 std::unique_ptr<DataElement> element(new DataElement());
163 element->SetToBlobRange(uuid, offset, length); 186 element->SetToBlobRange(uuid, offset, length);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 << ", content_type: " << x.content_type_ 243 << ", content_type: " << x.content_type_
221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; 244 << ", content_disposition: " << x.content_disposition_ << ", items: [";
222 for (const auto& item : x.items_) { 245 for (const auto& item : x.items_) {
223 PrintTo(*item, os); 246 PrintTo(*item, os);
224 *os << ", "; 247 *os << ", ";
225 } 248 }
226 *os << "]}"; 249 *os << "]}";
227 } 250 }
228 251
229 } // namespace storage 252 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698