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

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

Issue 2339933004: [BlobStorage] BlobMemoryController & tests (Closed)
Patch Set: Fix android & windows build errors 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_";
kinuko 2016/10/12 15:26:41 nit: do we want to use FilePath::CharType kFutureF
dmurph 2016/10/13 00:39:29 Hm... I don't know if that removes any conversions
kinuko 2016/10/19 21:34:09 It's definitely a minor issue, but it removes From
dmurph 2016/10/20 00:09:47 Cool, I think I got it working.
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 std::string file_id_str = base::Uint64ToString(file_id);
33 return base::FilePath::FromUTF8Unsafe(kFutureFileName)
34 .AddExtension(
35 base::FilePath::StringType(file_id_str.begin(), file_id_str.end()));
31 } 36 }
32 BlobDataBuilder::~BlobDataBuilder() { 37
38 /* static */
39 bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) {
40 const std::string prefix(kFutureFileName);
41 const std::string path = element.path().MaybeAsASCII();
42 // The prefix shouldn't occur unless the user used "AppendFutureFile". We
43 // DCHECK on AppendFile to make sure no one appends a future file.
44 return base::StartsWith(path, prefix, base::CompareCase::SENSITIVE);
33 } 45 }
34 46
47 /* static */
48 uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) {
49 DCHECK(IsFutureFileItem(element));
50 uint64_t id = 0;
51 bool success =
52 base::StringToUint64(element.path().Extension().substr(1), &id);
53 DCHECK(success) << element.path().Extension();
54 return id;
55 }
56
57 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) {}
58 BlobDataBuilder::~BlobDataBuilder() {}
59
35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { 60 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) {
36 uint64_t length = ipc_data.length(); 61 uint64_t length = ipc_data.length();
37 switch (ipc_data.type()) { 62 switch (ipc_data.type()) {
38 case DataElement::TYPE_BYTES: 63 case DataElement::TYPE_BYTES:
39 DCHECK(!ipc_data.offset()); 64 DCHECK(!ipc_data.offset());
40 AppendData(ipc_data.bytes(), 65 AppendData(ipc_data.bytes(),
41 base::checked_cast<size_t, uint64_t>(length)); 66 base::checked_cast<size_t, uint64_t>(length));
42 break; 67 break;
43 case DataElement::TYPE_FILE: 68 case DataElement::TYPE_FILE:
44 AppendFile(ipc_data.path(), ipc_data.offset(), length, 69 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))); 101 items_.push_back(new BlobDataItem(std::move(element)));
77 return items_.size() - 1; 102 return items_.size() - 1;
78 } 103 }
79 104
80 bool BlobDataBuilder::PopulateFutureData(size_t index, 105 bool BlobDataBuilder::PopulateFutureData(size_t index,
81 const char* data, 106 const char* data,
82 size_t offset, 107 size_t offset,
83 size_t length) { 108 size_t length) {
84 DCHECK_LT(index, items_.size()); 109 DCHECK_LT(index, items_.size());
85 DCHECK(data); 110 DCHECK(data);
86 DataElement* element = items_.at(index)->data_element_ptr(); 111 DataElement* element = items_[index]->data_element_ptr();
87 112
88 // We lazily allocate our data buffer by waiting until the first 113 // We lazily allocate our data buffer by waiting until the first
89 // PopulateFutureData call. 114 // PopulateFutureData call.
90 // Why? The reason we have the AppendFutureData method is to create our Blob 115 // 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 116 // 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 117 // 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 118 // want to allocate the memory until we're actually receiving the data (which
94 // the browser process only does when it has quota). 119 // the browser process only does when it has quota).
95 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { 120 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) {
96 element->SetToAllocatedBytes(element->length()); 121 element->SetToAllocatedBytes(element->length());
97 // The type of the element is now TYPE_BYTES. 122 // The type of the element is now TYPE_BYTES.
98 } 123 }
99 if (element->type() != DataElement::TYPE_BYTES) { 124 if (element->type() != DataElement::TYPE_BYTES) {
100 DVLOG(1) << "Invalid item type."; 125 DVLOG(1) << "Invalid item type.";
101 return false; 126 return false;
102 } 127 }
103 base::CheckedNumeric<size_t> checked_end = offset; 128 base::CheckedNumeric<size_t> checked_end = offset;
104 checked_end += length; 129 checked_end += length;
105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { 130 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) {
106 DVLOG(1) << "Invalid offset or length."; 131 DVLOG(1) << "Invalid offset or length.";
107 return false; 132 return false;
108 } 133 }
109 std::memcpy(element->mutable_bytes() + offset, data, length); 134 std::memcpy(element->mutable_bytes() + offset, data, length);
110 return true; 135 return true;
111 } 136 }
112 137
113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { 138 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset,
139 uint64_t length,
140 uint64_t file_id) {
114 CHECK_NE(length, 0ull); 141 CHECK_NE(length, 0ull);
115 std::unique_ptr<DataElement> element(new DataElement()); 142 std::unique_ptr<DataElement> element(new DataElement());
116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( 143 element->SetToFilePathRange(GetFutureFileItemPath(file_id), offset, length,
117 kAppendFutureFileTemporaryFileName)), 144 base::Time());
118 offset, length, base::Time());
119 items_.push_back(new BlobDataItem(std::move(element))); 145 items_.push_back(new BlobDataItem(std::move(element)));
120 return items_.size() - 1; 146 return items_.size() - 1;
121 } 147 }
122 148
123 bool BlobDataBuilder::PopulateFutureFile( 149 bool BlobDataBuilder::PopulateFutureFile(
124 size_t index, 150 size_t index,
125 const scoped_refptr<ShareableFileReference>& file_reference, 151 const scoped_refptr<ShareableFileReference>& file_reference,
126 const base::Time& expected_modification_time) { 152 const base::Time& expected_modification_time) {
127 DCHECK_LT(index, items_.size()); 153 DCHECK_LT(index, items_.size());
128 DataElement* old_element = items_.at(index)->data_element_ptr(); 154 DataElement* element = items_[index]->data_element_ptr();
129 155
130 if (old_element->type() != DataElement::TYPE_FILE) { 156 if (element->type() != DataElement::TYPE_FILE) {
131 DVLOG(1) << "Invalid item type."; 157 DVLOG(1) << "Invalid item type.";
132 return false; 158 return false;
133 } else if (old_element->path().AsUTF8Unsafe() != 159 } else if (!IsFutureFileItem(*element)) {
134 std::string(kAppendFutureFileTemporaryFileName)) {
135 DVLOG(1) << "Item not created by AppendFutureFile"; 160 DVLOG(1) << "Item not created by AppendFutureFile";
136 return false; 161 return false;
137 } 162 }
138 uint64_t length = old_element->length(); 163 uint64_t length = element->length();
139 uint64_t offset = old_element->offset(); 164 uint64_t offset = element->offset();
140 std::unique_ptr<DataElement> element(new DataElement()); 165 items_[index]->data_handle_ = std::move(file_reference);
141 element->SetToFilePathRange(file_reference->path(), offset, length, 166 element->SetToFilePathRange(file_reference->path(), offset, length,
142 expected_modification_time); 167 expected_modification_time);
143 items_[index] = new BlobDataItem(std::move(element), file_reference);
144 return true; 168 return true;
145 } 169 }
146 170
147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, 171 void BlobDataBuilder::AppendFile(const base::FilePath& file_path,
148 uint64_t offset, 172 uint64_t offset,
149 uint64_t length, 173 uint64_t length,
150 const base::Time& expected_modification_time) { 174 const base::Time& expected_modification_time) {
151 std::unique_ptr<DataElement> element(new DataElement()); 175 std::unique_ptr<DataElement> element(new DataElement());
152 element->SetToFilePathRange(file_path, offset, length, 176 element->SetToFilePathRange(file_path, offset, length,
153 expected_modification_time); 177 expected_modification_time);
178 DCHECK(!IsFutureFileItem(*element)) << file_path.value();
154 items_.push_back(new BlobDataItem(std::move(element), 179 items_.push_back(new BlobDataItem(std::move(element),
155 ShareableFileReference::Get(file_path))); 180 ShareableFileReference::Get(file_path)));
156 } 181 }
157 182
158 void BlobDataBuilder::AppendBlob(const std::string& uuid, 183 void BlobDataBuilder::AppendBlob(const std::string& uuid,
159 uint64_t offset, 184 uint64_t offset,
160 uint64_t length) { 185 uint64_t length) {
161 DCHECK_GT(length, 0ul); 186 DCHECK_GT(length, 0ul);
162 std::unique_ptr<DataElement> element(new DataElement()); 187 std::unique_ptr<DataElement> element(new DataElement());
163 element->SetToBlobRange(uuid, offset, length); 188 element->SetToBlobRange(uuid, offset, length);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 << ", content_type: " << x.content_type_ 245 << ", content_type: " << x.content_type_
221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; 246 << ", content_disposition: " << x.content_disposition_ << ", items: [";
222 for (const auto& item : x.items_) { 247 for (const auto& item : x.items_) {
223 PrintTo(*item, os); 248 PrintTo(*item, os);
224 *os << ", "; 249 *os << ", ";
225 } 250 }
226 *os << "]}"; 251 *os << "]}";
227 } 252 }
228 253
229 } // namespace storage 254 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698