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

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

Issue 2448353002: [BlobAsync] Moving async handling into BlobStorageContext & quota out. (Closed)
Patch Set: comments Created 4 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_storage_context.h" 5 #include "storage/browser/blob/blob_storage_context.h"
6 6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <algorithm> 7 #include <algorithm>
11 #include <limits> 8 #include <limits>
12 #include <memory> 9 #include <memory>
10 #include <set>
13 #include <utility> 11 #include <utility>
14 12
15 #include "base/bind.h" 13 #include "base/bind.h"
16 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/callback_helpers.h"
17 #include "base/location.h" 16 #include "base/location.h"
18 #include "base/logging.h" 17 #include "base/logging.h"
19 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
20 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
21 #include "base/metrics/histogram_macros.h" 20 #include "base/metrics/histogram_macros.h"
21 #include "base/numerics/safe_conversions.h"
22 #include "base/numerics/safe_math.h"
23 #include "base/task_runner.h"
22 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
23 #include "base/trace_event/trace_event.h" 25 #include "base/trace_event/trace_event.h"
24 #include "storage/browser/blob/blob_data_builder.h" 26 #include "storage/browser/blob/blob_data_builder.h"
25 #include "storage/browser/blob/blob_data_handle.h"
26 #include "storage/browser/blob/blob_data_item.h" 27 #include "storage/browser/blob/blob_data_item.h"
27 #include "storage/browser/blob/blob_data_snapshot.h" 28 #include "storage/browser/blob/blob_data_snapshot.h"
28 #include "storage/browser/blob/shareable_blob_data_item.h" 29 #include "storage/browser/blob/shareable_blob_data_item.h"
30 #include "storage/common/data_element.h"
29 #include "url/gurl.h" 31 #include "url/gurl.h"
30 32
31 namespace storage { 33 namespace storage {
32 using BlobRegistryEntry = BlobStorageRegistry::Entry; 34 namespace {
33 using BlobState = BlobStorageRegistry::BlobState; 35 using ItemCopyEntry = BlobEntry::ItemCopyEntry;
34 36 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask;
35 BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} 37
38 bool IsBytes(DataElement::Type type) {
39 return type == DataElement::TYPE_BYTES ||
40 type == DataElement::TYPE_BYTES_DESCRIPTION;
41 }
42
43 void RecordBlobItemSizeStats(const DataElement& input_element) {
44 uint64_t length = input_element.length();
45
46 switch (input_element.type()) {
47 case DataElement::TYPE_BYTES:
48 case DataElement::TYPE_BYTES_DESCRIPTION:
49 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024);
50 break;
51 case DataElement::TYPE_BLOB:
52 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob",
53 (length - input_element.offset()) / 1024);
54 break;
55 case DataElement::TYPE_FILE: {
56 bool full_file = (length == std::numeric_limits<uint64_t>::max());
57 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file);
58 if (!full_file) {
59 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File",
60 (length - input_element.offset()) / 1024);
61 }
62 break;
63 }
64 case DataElement::TYPE_FILE_FILESYSTEM: {
65 bool full_file = (length == std::numeric_limits<uint64_t>::max());
66 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown",
67 full_file);
68 if (!full_file) {
69 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem",
70 (length - input_element.offset()) / 1024);
71 }
72 break;
73 }
74 case DataElement::TYPE_DISK_CACHE_ENTRY:
75 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.CacheEntry",
76 (length - input_element.offset()) / 1024);
77 break;
78 case DataElement::TYPE_UNKNOWN:
79 NOTREACHED();
80 break;
81 }
82 }
83 } // namespace
84
85 BlobStorageContext::BlobFlattener::BlobFlattener(
86 const BlobDataBuilder& input_builder,
87 BlobEntry* output_blob,
88 BlobStorageRegistry* registry) {
89 const std::string& uuid = input_builder.uuid_;
90 std::set<std::string> dependent_blob_uuids;
91
92 size_t num_files_with_unknown_size = 0;
93 size_t num_building_dependent_blobs = 0;
94
95 base::CheckedNumeric<uint64_t> checked_total_size = 0;
96 base::CheckedNumeric<uint64_t> checked_total_memory_size = 0;
97 base::CheckedNumeric<uint64_t> checked_memory_quota_needed = 0;
98
99 for (scoped_refptr<BlobDataItem> input_item : input_builder.items_) {
100 const DataElement& input_element = input_item->data_element();
101 DataElement::Type type = input_element.type();
102 uint64_t length = input_element.length();
103
104 RecordBlobItemSizeStats(input_element);
105
106 if (IsBytes(type)) {
107 DCHECK_NE(0 + DataElement::kUnknownSize, input_element.length());
108 checked_memory_quota_needed += length;
109 checked_total_size += length;
110 scoped_refptr<ShareableBlobDataItem> item = new ShareableBlobDataItem(
111 std::move(input_item), ShareableBlobDataItem::QUOTA_NEEDED);
112 pending_memory_items.push_back(item);
113 transport_items.push_back(item.get());
114 output_blob->AppendSharedBlobItem(std::move(item));
115 continue;
116 }
117 if (type == DataElement::TYPE_BLOB) {
118 BlobEntry* ref_entry = registry->GetEntry(input_element.blob_uuid());
119
120 if (!ref_entry || input_element.blob_uuid() == uuid) {
121 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
122 return;
123 }
124
125 if (BlobStatusIsError(ref_entry->status())) {
126 status = BlobStatus::ERR_REFERENCED_BLOB_BROKEN;
127 return;
128 }
129
130 if (ref_entry->total_size() == DataElement::kUnknownSize) {
131 // We can't reference a blob with unknown size.
132 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
133 return;
134 }
135
136 if (dependent_blob_uuids.find(input_element.blob_uuid()) ==
137 dependent_blob_uuids.end()) {
138 dependent_blobs.push_back(
139 std::make_pair(input_element.blob_uuid(), ref_entry));
140 dependent_blob_uuids.insert(input_element.blob_uuid());
141 if (BlobStatusIsPending(ref_entry->status())) {
142 num_building_dependent_blobs++;
143 }
144 }
145
146 length = length == DataElement::kUnknownSize ? ref_entry->total_size()
147 : input_element.length();
148 checked_total_size += length;
149
150 // If we're referencing the whole blob, then we don't need to slice.
151 if (input_element.offset() == 0 && length == ref_entry->total_size()) {
152 for (const auto& shareable_item : ref_entry->items()) {
153 output_blob->AppendSharedBlobItem(shareable_item);
154 }
155 continue;
156 }
157
158 // Validate our reference has good offset & length.
159 if (input_element.offset() + length > ref_entry->total_size()) {
160 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
161 return;
162 }
163
164 BlobSlice slice(*ref_entry, input_element.offset(), length);
165
166 if (!slice.copying_memory_size.IsValid() ||
167 !slice.total_memory_size.IsValid()) {
168 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
169 return;
170 }
171 checked_total_memory_size += slice.total_memory_size;
172
173 if (slice.first_source_item) {
174 copies.push_back(ItemCopyEntry(slice.first_source_item,
175 slice.first_item_slice_offset,
176 slice.dest_items.front()));
177 pending_memory_items.push_back(slice.dest_items.front());
178 }
179 if (slice.last_source_item) {
180 copies.push_back(
181 ItemCopyEntry(slice.last_source_item, 0, slice.dest_items.back()));
182 pending_memory_items.push_back(slice.dest_items.back());
183 }
184 checked_memory_quota_needed += slice.copying_memory_size;
185
186 for (auto& shareable_item : slice.dest_items) {
187 output_blob->AppendSharedBlobItem(std::move(shareable_item));
188 }
189 continue;
190 }
191
192 DCHECK(!BlobDataBuilder::IsFutureFileItem(input_element))
193 << "File allocation not implemented.";
194 if (length == DataElement::kUnknownSize)
195 num_files_with_unknown_size++;
196
197 scoped_refptr<ShareableBlobDataItem> item = new ShareableBlobDataItem(
198 std::move(input_item), ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA);
199
200 checked_total_size += length;
201 output_blob->AppendSharedBlobItem(std::move(item));
202 }
203
204 if (num_files_with_unknown_size > 1 && input_builder.items_.size() > 1) {
205 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
206 return;
207 }
208 if (!checked_total_size.IsValid() || !checked_total_memory_size.IsValid() ||
209 !checked_memory_quota_needed.IsValid()) {
210 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
211 return;
212 }
213 total_size = checked_total_size.ValueOrDie();
214 total_memory_size = checked_total_memory_size.ValueOrDie();
215 memory_quota_needed = checked_memory_quota_needed.ValueOrDie();
216 if (memory_quota_needed) {
217 status = BlobStatus::PENDING_QUOTA;
218 } else {
219 status = BlobStatus::PENDING_INTERNALS;
220 }
221 }
222
223 BlobStorageContext::BlobFlattener::~BlobFlattener() {}
224
225 BlobStorageContext::BlobSlice::BlobSlice(const BlobEntry& source,
226 uint64_t slice_offset,
227 uint64_t slice_size) {
228 const auto& source_items = source.items();
229 const auto& offsets = source.offsets();
230 DCHECK_LE(slice_offset + slice_size, source.total_size());
231 size_t item_index =
232 std::upper_bound(offsets.begin(), offsets.end(), slice_offset) -
233 offsets.begin();
234 uint64_t item_offset =
235 item_index == 0 ? slice_offset : slice_offset - offsets[item_index - 1];
236 size_t num_items = source_items.size();
237
238 size_t first_item_index = item_index;
239
240 // Read starting from 'first_item_index' and 'item_offset'.
241 for (uint64_t total_sliced = 0;
242 item_index < num_items && total_sliced < slice_size; item_index++) {
243 const scoped_refptr<BlobDataItem>& source_item =
244 source_items[item_index]->item();
245 uint64_t source_length = source_item->length();
246 DataElement::Type type = source_item->type();
247 DCHECK_NE(source_length, std::numeric_limits<uint64_t>::max());
248 DCHECK_NE(source_length, 0ull);
249
250 uint64_t read_size =
251 std::min(source_length - item_offset, slice_size - total_sliced);
252 total_sliced += read_size;
253
254 bool reusing_blob_item = (read_size == source_length);
255 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item);
256 if (reusing_blob_item) {
257 // We can share the entire item.
258 dest_items.push_back(source_items[item_index]);
259 if (IsBytes(type)) {
260 total_memory_size += source_length;
261 }
262 continue;
263 }
264
265 scoped_refptr<BlobDataItem> data_item;
266 ShareableBlobDataItem::State state =
267 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA;
268 switch (type) {
269 case DataElement::TYPE_BYTES_DESCRIPTION:
270 case DataElement::TYPE_BYTES: {
271 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes",
272 read_size / 1024);
273 if (item_index == first_item_index) {
274 first_item_slice_offset = item_offset;
275 first_source_item = source_items[item_index];
276 } else {
277 last_source_item = source_items[item_index];
278 }
279 copying_memory_size += read_size;
280 total_memory_size += read_size;
281 // Since we don't have quota yet for memory, we create temporary items
282 // for this data. When our blob is finished constructing, all dependent
283 // blobs are done, and we have enough memory quota, we'll copy the data
284 // over.
285 std::unique_ptr<DataElement> element(new DataElement());
286 element->SetToBytesDescription(base::checked_cast<size_t>(read_size));
287 data_item = new BlobDataItem(std::move(element));
288 state = ShareableBlobDataItem::QUOTA_NEEDED;
289 break;
290 }
291 case DataElement::TYPE_FILE: {
292 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File",
293 read_size / 1024);
294 std::unique_ptr<DataElement> element(new DataElement());
295 element->SetToFilePathRange(
296 source_item->path(), source_item->offset() + item_offset, read_size,
297 source_item->expected_modification_time());
298 data_item =
299 new BlobDataItem(std::move(element), source_item->data_handle_);
300 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes",
301 read_size / 1024);
302
303 DCHECK(!BlobDataBuilder::IsFutureFileItem(source_item->data_element()))
304 << "File allocation unimplemented.";
305 break;
306 }
307 case DataElement::TYPE_FILE_FILESYSTEM: {
308 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem",
309 read_size / 1024);
310 std::unique_ptr<DataElement> element(new DataElement());
311 element->SetToFileSystemUrlRange(
312 source_item->filesystem_url(), source_item->offset() + item_offset,
313 read_size, source_item->expected_modification_time());
314 data_item = new BlobDataItem(std::move(element));
315 break;
316 }
317 case DataElement::TYPE_DISK_CACHE_ENTRY: {
318 std::unique_ptr<DataElement> element(new DataElement());
319 element->SetToDiskCacheEntryRange(source_item->offset() + item_offset,
320 read_size);
321 data_item =
322 new BlobDataItem(std::move(element), source_item->data_handle_,
323 source_item->disk_cache_entry(),
324 source_item->disk_cache_stream_index(),
325 source_item->disk_cache_side_stream_index());
326 break;
327 }
328 case DataElement::TYPE_BLOB:
329 case DataElement::TYPE_UNKNOWN:
330 CHECK(false) << "Illegal blob item type: " << type;
331 }
332 dest_items.push_back(
333 new ShareableBlobDataItem(std::move(data_item), state));
334 item_offset = 0;
335 }
336 }
337
338 BlobStorageContext::BlobSlice::~BlobSlice() {}
339
340 BlobStorageContext::BlobStorageContext()
341 : memory_controller_(base::FilePath(), scoped_refptr<base::TaskRunner>()),
342 ptr_factory_(this) {}
36 343
37 BlobStorageContext::~BlobStorageContext() { 344 BlobStorageContext::~BlobStorageContext() {
38 } 345 }
39 346
40 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( 347 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID(
41 const std::string& uuid) { 348 const std::string& uuid) {
42 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 349 BlobEntry* entry = registry_.GetEntry(uuid);
43 if (!entry) { 350 if (!entry)
44 return nullptr; 351 return nullptr;
45 } 352 return CreateHandle(uuid, entry);
46 return base::WrapUnique(
47 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition,
48 this, base::ThreadTaskRunnerHandle::Get().get()));
49 } 353 }
50 354
51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( 355 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL(
52 const GURL& url) { 356 const GURL& url) {
53 std::string uuid; 357 std::string uuid;
54 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); 358 BlobEntry* entry = registry_.GetEntryFromURL(url, &uuid);
55 if (!entry) { 359 if (!entry)
56 return nullptr; 360 return nullptr;
57 } 361 return CreateHandle(uuid, entry);
58 return base::WrapUnique(
59 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition,
60 this, base::ThreadTaskRunnerHandle::Get().get()));
61 } 362 }
62 363
63 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( 364 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
64 const BlobDataBuilder& external_builder) { 365 const BlobDataBuilder& external_builder) {
65 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); 366 TRACE_EVENT0("Blob", "Context::AddFinishedBlob");
66 CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, 367 return BuildBlob(external_builder, TransportAllowedCallback());
67 external_builder.content_disposition_);
68 CompletePendingBlob(external_builder);
69 std::unique_ptr<BlobDataHandle> handle =
70 GetBlobDataFromUUID(external_builder.uuid_);
71 DecrementBlobRefCount(external_builder.uuid_);
72 return handle;
73 } 368 }
74 369
75 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( 370 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
76 const BlobDataBuilder* builder) { 371 const BlobDataBuilder* builder) {
77 DCHECK(builder); 372 DCHECK(builder);
78 return AddFinishedBlob(*builder); 373 return AddFinishedBlob(*builder);
79 } 374 }
80 375
376 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddBrokenBlob(
377 const std::string& uuid,
378 const std::string& content_type,
379 const std::string& content_disposition,
380 BlobStatus reason) {
381 DCHECK(!registry_.HasEntry(uuid));
382 DCHECK(BlobStatusIsError(reason));
383 BlobEntry* entry =
384 registry_.CreateEntry(uuid, content_type, content_disposition);
385 entry->set_status(reason);
386 FinishBuilding(entry);
387 return CreateHandle(uuid, entry);
388 }
389
81 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, 390 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url,
82 const std::string& uuid) { 391 const std::string& uuid) {
83 if (!registry_.CreateUrlMapping(blob_url, uuid)) { 392 if (!registry_.CreateUrlMapping(blob_url, uuid))
84 return false; 393 return false;
85 }
86 IncrementBlobRefCount(uuid); 394 IncrementBlobRefCount(uuid);
87 return true; 395 return true;
88 } 396 }
89 397
90 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { 398 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) {
91 std::string uuid; 399 std::string uuid;
92 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { 400 if (!registry_.DeleteURLMapping(blob_url, &uuid))
93 return; 401 return;
94 }
95 DecrementBlobRefCount(uuid); 402 DecrementBlobRefCount(uuid);
96 } 403 }
97 404
98 void BlobStorageContext::CreatePendingBlob( 405 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob(
99 const std::string& uuid, 406 const BlobDataBuilder& content,
100 const std::string& content_type, 407 const TransportAllowedCallback& transport_allowed_callback) {
101 const std::string& content_disposition) { 408 DCHECK(!registry_.HasEntry(content.uuid_));
102 DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); 409
103 registry_.CreateEntry(uuid, content_type, content_disposition); 410 BlobEntry* entry = registry_.CreateEntry(
411 content.uuid(), content.content_type_, content.content_disposition_);
412
413 // This flattens all blob references in the transportion content out and
414 // stores the complete item representation in the internal data.
415 BlobFlattener flattener(content, entry, &registry_);
416
417 DCHECK(flattener.status != BlobStatus::PENDING_TRANSPORT ||
418 !transport_allowed_callback)
419 << "There is no pending content for the user to populate, so the "
420 "callback should be null.";
421 DCHECK(flattener.status != BlobStatus::PENDING_TRANSPORT ||
422 transport_allowed_callback)
423 << "If we have pending content then there needs to be a callback "
424 "present.";
425
426 entry->set_size(flattener.total_size);
427 entry->set_status(flattener.status);
428 std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry);
429
430 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->items().size());
431 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize",
432 flattener.total_memory_size / 1024);
433 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize",
434 flattener.memory_quota_needed / 1024);
435
436 size_t num_building_dependent_blobs = 0;
437 std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs;
438 // We hold a handle to all blobs we're using. This is important, as our memory
439 // accounting can be delayed until OnEnoughSizeForBlobData is called, and we
440 // only free memory on canceling when we've done this accounting. If a
441 // dependent blob is dereferenced, then we're the last blob holding onto that
442 // data item, and we need to account for that. So we prevent that case by
443 // holding onto all blobs.
444 for (const std::pair<std::string, BlobEntry*>& pending_blob :
445 flattener.dependent_blobs) {
446 dependent_blobs.push_back(
447 CreateHandle(pending_blob.first, pending_blob.second));
448 if (BlobStatusIsPending(pending_blob.second->status())) {
449 pending_blob.second->building_state_->build_completion_callbacks
450 .push_back(base::Bind(&BlobStorageContext::OnDependentBlobFinished,
451 ptr_factory_.GetWeakPtr(), content.uuid_));
452 num_building_dependent_blobs++;
453 }
454 }
455
456 entry->set_building_state(base::MakeUnique<BlobEntry::BuildingState>(
457 !flattener.transport_items.empty(), transport_allowed_callback,
458 num_building_dependent_blobs));
459 BlobEntry::BuildingState* building_state = entry->building_state_.get();
460 std::swap(building_state->copies, flattener.copies);
461 std::swap(building_state->dependent_blobs, dependent_blobs);
462 std::swap(building_state->transport_items, flattener.transport_items);
463
464 // Break ourselves if we have an error. BuildingState must be set first so the
465 // callback is called correctly.
466 if (BlobStatusIsError(flattener.status)) {
467 CancelBuildingBlobInternal(entry, flattener.status);
468 return handle;
469 }
470
471 if (!memory_controller_.CanReserveQuota(flattener.memory_quota_needed)) {
472 CancelBuildingBlobInternal(entry, BlobStatus::ERR_OUT_OF_MEMORY);
473 return handle;
474 }
475
476 if (flattener.memory_quota_needed > 0) {
477 // The blob can complete during the execution of this line.
478 base::WeakPtr<QuotaAllocationTask> pending_request =
479 memory_controller_.ReserveMemoryQuota(
480 std::move(flattener.pending_memory_items),
481 base::Bind(&BlobStorageContext::OnEnoughSizeForMemory,
482 ptr_factory_.GetWeakPtr(), content.uuid_));
483 // Building state will be null if the blob is already finished.
484 if (entry->building_state_)
485 entry->building_state_->memory_quota_request = std::move(pending_request);
486 }
487
488 if (entry->CanFinishBuilding())
489 FinishBuilding(entry);
490
491 return handle;
104 } 492 }
105 493
106 void BlobStorageContext::CompletePendingBlob( 494 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid,
107 const BlobDataBuilder& external_builder) { 495 BlobStatus reason) {
108 BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); 496 CancelBuildingBlobInternal(registry_.GetEntry(uuid), reason);
109 DCHECK(entry);
110 DCHECK(!entry->data.get()) << "Blob already constructed: "
111 << external_builder.uuid();
112 // We want to handle storing our broken blob as well.
113 switch (entry->state) {
114 case BlobState::PENDING: {
115 entry->data_builder.reset(new InternalBlobData::Builder());
116 InternalBlobData::Builder* internal_data_builder =
117 entry->data_builder.get();
118
119 bool broken = false;
120 for (const auto& blob_item : external_builder.items_) {
121 IPCBlobCreationCancelCode error_code;
122 if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item,
123 internal_data_builder, &error_code)) {
124 broken = true;
125 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage();
126 entry->state = BlobState::BROKEN;
127 entry->broken_reason = error_code;
128 entry->data_builder.reset(new InternalBlobData::Builder());
129 break;
130 }
131 }
132 entry->data = entry->data_builder->Build();
133 entry->data_builder.reset();
134 entry->state = broken ? BlobState::BROKEN : BlobState::COMPLETE;
135 break;
136 }
137 case BlobState::BROKEN: {
138 InternalBlobData::Builder builder;
139 entry->data = builder.Build();
140 break;
141 }
142 case BlobState::COMPLETE:
143 DCHECK(false) << "Blob already constructed: " << external_builder.uuid();
144 return;
145 }
146
147 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size());
148 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.Broken",
149 entry->state == BlobState::BROKEN);
150 if (entry->state == BlobState::BROKEN) {
151 UMA_HISTOGRAM_ENUMERATION(
152 "Storage.Blob.BrokenReason", static_cast<int>(entry->broken_reason),
153 (static_cast<int>(IPCBlobCreationCancelCode::LAST) + 1));
154 }
155 size_t total_memory = 0, nonshared_memory = 0;
156 entry->data->GetMemoryUsage(&total_memory, &nonshared_memory);
157 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize", total_memory / 1024);
158 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize",
159 nonshared_memory / 1024);
160 TRACE_COUNTER1("Blob", "MemoryStoreUsageBytes", memory_usage_);
161
162 auto runner = base::ThreadTaskRunnerHandle::Get();
163 for (const auto& callback : entry->build_completion_callbacks) {
164 runner->PostTask(FROM_HERE,
165 base::Bind(callback, entry->state == BlobState::COMPLETE,
166 entry->broken_reason));
167 }
168 entry->build_completion_callbacks.clear();
169 } 497 }
170 498
171 void BlobStorageContext::CancelPendingBlob(const std::string& uuid, 499 void BlobStorageContext::NotifyTransportComplete(const std::string& uuid) {
172 IPCBlobCreationCancelCode reason) { 500 BlobEntry* entry = registry_.GetEntry(uuid);
173 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 501 CHECK(entry) << "There is no blob entry with uuid " << uuid;
174 DCHECK(entry && entry->state == BlobState::PENDING); 502 DCHECK(BlobStatusIsPending(entry->status()));
175 entry->state = BlobState::BROKEN; 503 NotifyTransportCompleteInternal(entry);
176 entry->broken_reason = reason;
177 CompletePendingBlob(BlobDataBuilder(uuid));
178 } 504 }
179 505
180 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { 506 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) {
181 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 507 BlobEntry* entry = registry_.GetEntry(uuid);
182 DCHECK(entry); 508 DCHECK(entry);
183 ++(entry->refcount); 509 entry->IncrementRefCount();
184 } 510 }
185 511
186 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { 512 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) {
187 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 513 BlobEntry* entry = registry_.GetEntry(uuid);
188 DCHECK(entry); 514 DCHECK(entry);
189 DCHECK_GT(entry->refcount, 0u); 515 DCHECK_GT(entry->refcount(), 0u);
190 if (--(entry->refcount) == 0) { 516 entry->DecrementRefCount();
191 size_t memory_freeing = 0; 517 if (entry->refcount() == 0) {
192 if (entry->state == BlobState::COMPLETE) { 518 ClearAndFreeMemory(entry);
193 memory_freeing = entry->data->GetUnsharedMemoryUsage();
194 entry->data->RemoveBlobFromShareableItems(uuid);
195 }
196 DCHECK_LE(memory_freeing, memory_usage_);
197 memory_usage_ -= memory_freeing;
198 registry_.DeleteEntry(uuid); 519 registry_.DeleteEntry(uuid);
199 } 520 }
200 } 521 }
201 522
202 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( 523 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot(
203 const std::string& uuid) { 524 const std::string& uuid) {
204 std::unique_ptr<BlobDataSnapshot> result; 525 std::unique_ptr<BlobDataSnapshot> result;
205 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 526 BlobEntry* entry = registry_.GetEntry(uuid);
206 if (entry->state != BlobState::COMPLETE) { 527 if (entry->status() != BlobStatus::DONE)
207 return result; 528 return result;
208 } 529
209
210 const InternalBlobData& data = *entry->data;
211 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( 530 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot(
212 uuid, entry->content_type, entry->content_disposition)); 531 uuid, entry->content_type(), entry->content_disposition()));
213 snapshot->items_.reserve(data.items().size()); 532 snapshot->items_.reserve(entry->items().size());
214 for (const auto& shareable_item : data.items()) { 533 for (const auto& shareable_item : entry->items()) {
215 snapshot->items_.push_back(shareable_item->item()); 534 snapshot->items_.push_back(shareable_item->item());
216 } 535 }
536 memory_controller_.NotifyMemoryItemsUsed(entry->items());
217 return snapshot; 537 return snapshot;
218 } 538 }
219 539
220 bool BlobStorageContext::IsBroken(const std::string& uuid) const { 540 BlobStatus BlobStorageContext::GetBlobStatus(const std::string& uuid) const {
221 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); 541 const BlobEntry* entry = registry_.GetEntry(uuid);
222 if (!entry) { 542 if (!entry)
223 return true; 543 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
224 } 544 return entry->status();
225 return entry->state == BlobState::BROKEN;
226 }
227
228 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) const {
229 const BlobRegistryEntry* entry = registry_.GetEntry(uuid);
230 if (!entry) {
231 return false;
232 }
233 return entry->state == BlobState::PENDING;
234 } 545 }
235 546
236 void BlobStorageContext::RunOnConstructionComplete( 547 void BlobStorageContext::RunOnConstructionComplete(
237 const std::string& uuid, 548 const std::string& uuid,
238 const BlobConstructedCallback& done) { 549 const BlobStatusCallback& done) {
239 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 550 BlobEntry* entry = registry_.GetEntry(uuid);
240 DCHECK(entry); 551 DCHECK(entry);
241 switch (entry->state) { 552 if (BlobStatusIsPending(entry->status())) {
242 case BlobState::COMPLETE: 553 entry->building_state_->build_completion_callbacks.push_back(done);
243 done.Run(true, IPCBlobCreationCancelCode::UNKNOWN); 554 return;
244 return; 555 }
245 case BlobState::BROKEN: 556 done.Run(entry->status());
246 done.Run(false, entry->broken_reason); 557 }
247 return; 558
248 case BlobState::PENDING: 559 std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle(
249 entry->build_completion_callbacks.push_back(done); 560 const std::string& uuid,
250 return; 561 BlobEntry* entry) {
251 } 562 return base::WrapUnique(new BlobDataHandle(
252 NOTREACHED(); 563 uuid, entry->content_type_, entry->content_disposition_, entry->size_,
253 } 564 this, base::ThreadTaskRunnerHandle::Get().get()));
254 565 }
255 bool BlobStorageContext::AppendAllocatedBlobItem( 566
256 const std::string& target_blob_uuid, 567 void BlobStorageContext::NotifyTransportCompleteInternal(BlobEntry* entry) {
257 scoped_refptr<BlobDataItem> blob_item, 568 DCHECK(entry);
258 InternalBlobData::Builder* target_blob_builder, 569 for (ShareableBlobDataItem* shareable_item :
259 IPCBlobCreationCancelCode* error_code) { 570 entry->building_state_->transport_items) {
260 DCHECK(error_code); 571 DCHECK(shareable_item->state() == ShareableBlobDataItem::QUOTA_GRANTED);
261 *error_code = IPCBlobCreationCancelCode::UNKNOWN; 572 shareable_item->set_state(ShareableBlobDataItem::POPULATED_WITH_QUOTA);
262 bool error = false; 573 }
263 574 entry->set_status(BlobStatus::PENDING_INTERNALS);
264 // The blob data is stored in the canonical way which only contains a 575 if (entry->CanFinishBuilding())
265 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items 576 FinishBuilding(entry);
266 // are expanded into the primitive constituent types and reused if possible. 577 }
267 // 1) The Data item is denoted by the raw data and length. 578
268 // 2) The File item is denoted by the file path, the range and the expected 579 void BlobStorageContext::CancelBuildingBlobInternal(BlobEntry* entry,
269 // modification time. 580 BlobStatus reason) {
270 // 3) The FileSystem File item is denoted by the FileSystem URL, the range 581 DCHECK(entry);
271 // and the expected modification time. 582 DCHECK(BlobStatusIsError(reason));
272 // 4) The Blob item is denoted by the source blob and an offset and size. 583 TransportAllowedCallback transport_allowed_callback;
273 // Internal items that are fully used by the new blob (not cut by the 584 if (entry->building_state_ &&
274 // offset or size) are shared between the blobs. Otherwise, the relevant 585 entry->building_state_->transport_allowed_callback) {
275 // portion of the item is copied. 586 transport_allowed_callback =
276 587 entry->building_state_->transport_allowed_callback;
277 DCHECK(blob_item->data_element_ptr()); 588 entry->building_state_->transport_allowed_callback.Reset();
278 const DataElement& data_element = blob_item->data_element(); 589 }
279 uint64_t length = data_element.length(); 590 ClearAndFreeMemory(entry);
280 uint64_t offset = data_element.offset(); 591 entry->set_status(reason);
281 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", 592 if (transport_allowed_callback) {
282 memory_usage_ / 1024); 593 transport_allowed_callback.Run(
283 switch (data_element.type()) { 594 reason, std::vector<BlobMemoryController::FileCreationInfo>());
284 case DataElement::TYPE_BYTES: 595 }
285 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); 596 FinishBuilding(entry);
286 DCHECK(!offset); 597 }
287 if (memory_usage_ + length > kBlobStorageMaxMemoryUsage) { 598
288 error = true; 599 void BlobStorageContext::FinishBuilding(BlobEntry* entry) {
289 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY; 600 DCHECK(entry);
290 break; 601
602 BlobStatus status = entry->status_;
603 DCHECK_NE(BlobStatus::DONE, status);
604
605 bool error = BlobStatusIsError(status);
606 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.Broken", error);
607 if (error) {
608 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.BrokenReason",
609 static_cast<int>(status),
610 (static_cast<int>(BlobStatus::LAST_ERROR) + 1));
611 }
612
613 if (BlobStatusIsPending(entry->status_)) {
614 for (const ItemCopyEntry& copy : entry->building_state_->copies) {
615 // Our source item can be a file if it was a slice of an unpopulated file,
616 // or a slice of data that was then paged to disk.
617 size_t dest_size = static_cast<size_t>(copy.dest_item->item()->length());
618 DataElement::Type dest_type = copy.dest_item->item()->type();
619 switch (copy.source_item->item()->type()) {
620 case DataElement::TYPE_BYTES: {
621 DCHECK_EQ(dest_type, DataElement::TYPE_BYTES_DESCRIPTION);
622 const char* src_data =
623 copy.source_item->item()->bytes() + copy.source_item_offset;
624 copy.dest_item->item()->item_->SetToBytes(src_data, dest_size);
625 } break;
626 case DataElement::TYPE_FILE:
627 case DataElement::TYPE_UNKNOWN:
628 case DataElement::TYPE_BLOB:
629 case DataElement::TYPE_BYTES_DESCRIPTION:
630 case DataElement::TYPE_FILE_FILESYSTEM:
631 case DataElement::TYPE_DISK_CACHE_ENTRY:
632 NOTREACHED();
633 break;
291 } 634 }
292 memory_usage_ += length; 635 copy.dest_item->set_state(ShareableBlobDataItem::POPULATED_WITH_QUOTA);
293 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
294 target_blob_uuid, blob_item,
295 ShareableBlobDataItem::POPULATED_WITH_QUOTA));
296 break;
297 case DataElement::TYPE_FILE: {
298 bool full_file = (length == std::numeric_limits<uint64_t>::max());
299 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file);
300 if (!full_file) {
301 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File",
302 (length - offset) / 1024);
303 }
304 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
305 target_blob_uuid, blob_item,
306 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA));
307 break;
308 } 636 }
309 case DataElement::TYPE_FILE_FILESYSTEM: { 637
310 bool full_file = (length == std::numeric_limits<uint64_t>::max()); 638 entry->set_status(BlobStatus::DONE);
311 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown", 639 }
312 full_file); 640
313 if (!full_file) { 641 std::vector<BlobStatusCallback> callbacks;
314 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", 642 if (entry->building_state_.get()) {
315 (length - offset) / 1024); 643 std::swap(callbacks, entry->building_state_->build_completion_callbacks);
316 } 644 entry->building_state_.reset();
317 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( 645 }
318 target_blob_uuid, blob_item, 646
319 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); 647 memory_controller_.NotifyMemoryItemsUsed(entry->items());
320 break; 648
649 auto runner = base::ThreadTaskRunnerHandle::Get();
650 for (const auto& callback : callbacks)
651 runner->PostTask(FROM_HERE, base::Bind(callback, entry->status()));
652
653 for (const auto& shareable_item : entry->items()) {
654 DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION,
655 shareable_item->item()->type());
656 DCHECK(shareable_item->IsPopulated()) << shareable_item->state();
657 }
658 }
659
660 void BlobStorageContext::RequestTransport(
661 BlobEntry* entry,
662 std::vector<BlobMemoryController::FileCreationInfo> files) {
663 BlobEntry::BuildingState* building_state = entry->building_state_.get();
664 if (building_state->transport_allowed_callback) {
665 base::ResetAndReturn(&building_state->transport_allowed_callback)
666 .Run(BlobStatus::PENDING_TRANSPORT, std::move(files));
667 return;
668 }
669 DCHECK(files.empty());
670 NotifyTransportCompleteInternal(entry);
671 }
672
673 void BlobStorageContext::OnEnoughSizeForMemory(const std::string& uuid,
674 bool success) {
675 if (!success) {
676 CancelBuildingBlob(uuid, BlobStatus::ERR_OUT_OF_MEMORY);
677 return;
678 }
679 BlobEntry* entry = registry_.GetEntry(uuid);
680 if (!entry || !entry->building_state_.get())
681 return;
682 BlobEntry::BuildingState& building_state = *entry->building_state_;
683 DCHECK(!building_state.memory_quota_request);
684
685 if (building_state.transport_items_present) {
686 entry->set_status(BlobStatus::PENDING_TRANSPORT);
687 RequestTransport(entry,
688 std::vector<BlobMemoryController::FileCreationInfo>());
689 } else {
690 entry->set_status(BlobStatus::PENDING_INTERNALS);
691 }
692
693 if (entry->CanFinishBuilding())
694 FinishBuilding(entry);
695 }
696
697 void BlobStorageContext::OnDependentBlobFinished(
698 const std::string& owning_blob_uuid,
699 BlobStatus status) {
700 BlobEntry* entry = registry_.GetEntry(owning_blob_uuid);
701 if (!entry || !entry->building_state_)
702 return;
703
704 if (BlobStatusIsError(status)) {
705 DCHECK_NE(BlobStatus::ERR_BLOB_DEREFERENCED_WHILE_BUILDING, status)
706 << "Referenced blob should never be dereferenced while we "
707 << "are depending on it, as our system holds a handle.";
708 CancelBuildingBlobInternal(entry, BlobStatus::ERR_REFERENCED_BLOB_BROKEN);
709 return;
710 }
711 DCHECK_GT(entry->building_state_->num_building_dependent_blobs, 0u);
712 --entry->building_state_->num_building_dependent_blobs;
713
714 if (entry->CanFinishBuilding())
715 FinishBuilding(entry);
716 }
717
718 void BlobStorageContext::ClearAndFreeMemory(BlobEntry* entry) {
719 if (entry->building_state_) {
720 BlobEntry::BuildingState* building_state = entry->building_state_.get();
721 if (building_state->memory_quota_request) {
722 building_state->memory_quota_request->Cancel();
321 } 723 }
322 case DataElement::TYPE_BLOB: { 724 }
323 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", 725 entry->ClearItems();
324 (length - offset) / 1024); 726 entry->ClearOffsets();
325 // We grab the handle to ensure it stays around while we copy it. 727 entry->set_size(0);
326 std::unique_ptr<BlobDataHandle> src =
327 GetBlobDataFromUUID(data_element.blob_uuid());
328 if (!src || src->IsBroken() || src->IsBeingBuilt()) {
329 error = true;
330 *error_code = IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN;
331 break;
332 }
333 BlobRegistryEntry* other_entry =
334 registry_.GetEntry(data_element.blob_uuid());
335 DCHECK(other_entry->data);
336 if (!AppendBlob(target_blob_uuid, *other_entry->data, offset, length,
337 target_blob_builder)) {
338 error = true;
339 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY;
340 }
341 break;
342 }
343 case DataElement::TYPE_DISK_CACHE_ENTRY: {
344 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.CacheEntry",
345 (length - offset) / 1024);
346 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
347 target_blob_uuid, blob_item,
348 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA));
349 break;
350 }
351 case DataElement::TYPE_BYTES_DESCRIPTION:
352 case DataElement::TYPE_UNKNOWN:
353 NOTREACHED();
354 break;
355 }
356 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend",
357 memory_usage_ / 1024);
358 return !error;
359 }
360
361 bool BlobStorageContext::AppendBlob(
362 const std::string& target_blob_uuid,
363 const InternalBlobData& blob,
364 uint64_t offset,
365 uint64_t length,
366 InternalBlobData::Builder* target_blob_builder) {
367 DCHECK_GT(length, 0ull);
368
369 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items();
370 auto iter = items.begin();
371 if (offset) {
372 for (; iter != items.end(); ++iter) {
373 const BlobDataItem& item = *(iter->get()->item());
374 if (offset >= item.length())
375 offset -= item.length();
376 else
377 break;
378 }
379 }
380
381 for (; iter != items.end() && length > 0; ++iter) {
382 scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get();
383 const BlobDataItem& item = *(shareable_item->item());
384 uint64_t item_length = item.length();
385 DCHECK_GT(item_length, offset);
386 uint64_t current_length = item_length - offset;
387 uint64_t new_length = current_length > length ? length : current_length;
388
389 bool reusing_blob_item = offset == 0 && new_length == item.length();
390 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item);
391 if (reusing_blob_item) {
392 shareable_item->referencing_blobs_mutable()->insert(target_blob_uuid);
393 target_blob_builder->AppendSharedBlobItem(shareable_item);
394 length -= new_length;
395 continue;
396 }
397
398 // We need to do copying of the items when we have a different offset or
399 // length
400 switch (item.type()) {
401 case DataElement::TYPE_BYTES: {
402 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes",
403 new_length / 1024);
404 if (memory_usage_ + new_length > kBlobStorageMaxMemoryUsage) {
405 return false;
406 }
407 DCHECK(!item.offset());
408 std::unique_ptr<DataElement> element(new DataElement());
409 element->SetToBytes(item.bytes() + offset,
410 static_cast<int64_t>(new_length));
411 memory_usage_ += new_length;
412 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
413 target_blob_uuid, new BlobDataItem(std::move(element)),
414 ShareableBlobDataItem::POPULATED_WITH_QUOTA));
415 } break;
416 case DataElement::TYPE_FILE: {
417 DCHECK_NE(item.length(), std::numeric_limits<uint64_t>::max())
418 << "We cannot use a section of a file with an unknown length";
419 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File",
420 new_length / 1024);
421 std::unique_ptr<DataElement> element(new DataElement());
422 element->SetToFilePathRange(item.path(), item.offset() + offset,
423 new_length,
424 item.expected_modification_time());
425 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
426 target_blob_uuid,
427 new BlobDataItem(std::move(element), item.data_handle_),
428 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA));
429 } break;
430 case DataElement::TYPE_FILE_FILESYSTEM: {
431 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem",
432 new_length / 1024);
433 std::unique_ptr<DataElement> element(new DataElement());
434 element->SetToFileSystemUrlRange(item.filesystem_url(),
435 item.offset() + offset, new_length,
436 item.expected_modification_time());
437 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
438 target_blob_uuid, new BlobDataItem(std::move(element)),
439 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA));
440 } break;
441 case DataElement::TYPE_DISK_CACHE_ENTRY: {
442 std::unique_ptr<DataElement> element(new DataElement());
443 element->SetToDiskCacheEntryRange(item.offset() + offset,
444 new_length);
445 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
446 target_blob_uuid,
447 new BlobDataItem(std::move(element), item.data_handle_,
448 item.disk_cache_entry(),
449 item.disk_cache_stream_index(),
450 item.disk_cache_side_stream_index()),
451 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA));
452 } break;
453 case DataElement::TYPE_BYTES_DESCRIPTION:
454 case DataElement::TYPE_BLOB:
455 case DataElement::TYPE_UNKNOWN:
456 CHECK(false) << "Illegal blob item type: " << item.type();
457 }
458 length -= new_length;
459 offset = 0;
460 }
461 return true;
462 } 728 }
463 729
464 } // namespace storage 730 } // namespace storage
OLDNEW
« no previous file with comments | « storage/browser/blob/blob_storage_context.h ('k') | storage/browser/blob/blob_storage_registry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698