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

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

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments Created 4 years, 5 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) 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"
17 #include "base/location.h" 15 #include "base/location.h"
18 #include "base/logging.h" 16 #include "base/logging.h"
19 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
20 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
21 #include "base/metrics/histogram.h" 19 #include "base/metrics/histogram.h"
20 #include "base/numerics/safe_conversions.h"
21 #include "base/numerics/safe_math.h"
22 #include "base/threading/sequenced_worker_pool.h"
22 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
23 #include "base/trace_event/trace_event.h" 24 #include "base/trace_event/trace_event.h"
24 #include "storage/browser/blob/blob_data_builder.h" 25 #include "storage/browser/blob/blob_data_builder.h"
25 #include "storage/browser/blob/blob_data_handle.h" 26 #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"
29 #include "storage/browser/blob/blob_flattener.h"
30 #include "storage/browser/blob/blob_slice.h"
28 #include "storage/browser/blob/shareable_blob_data_item.h" 31 #include "storage/browser/blob/shareable_blob_data_item.h"
29 #include "url/gurl.h" 32 #include "url/gurl.h"
30 33
31 namespace storage { 34 namespace storage {
35 using ItemCopyEntry = BlobStorageRegistry::ItemCopyEntry;
36
37 namespace {
38
39 BlobStatus ConvertReferencedBlobErrorToConstructingError(
40 BlobStatus referenced_blob_error) {
41 switch (referenced_blob_error) {
42 // For most cases we propagate the error.
43 case BlobStatus::FILE_WRITE_FAILED:
44 case BlobStatus::SOURCE_DIED_IN_TRANSIT:
45 case BlobStatus::REFERENCED_BLOB_BROKEN:
46 case BlobStatus::OUT_OF_MEMORY:
47 return referenced_blob_error;
48 // Others we report that the referenced blob is broken, as we don't know
49 // why (the BLOB_DEREFERENCED_WHILE_BUILDING should never happen, as we hold
50 // onto the reference of the blobs we're using).
51 case BlobStatus::BLOB_DEREFERENCED_WHILE_BUILDING:
52 DCHECK(false) << "Referenced blob should never be dereferenced while we "
53 << "are depending on it, as our system holds a handle.";
54 case BlobStatus::INVALID_CONSTRUCTION_ARGUMENTS:
55 return BlobStatus::REFERENCED_BLOB_BROKEN;
56 case BlobStatus::DONE:
57 case BlobStatus::PENDING:
58 NOTREACHED();
59 }
60 NOTREACHED();
61 return BlobStatus::REFERENCED_BLOB_BROKEN;
62 }
63
64 } // namespace
65
32 using BlobRegistryEntry = BlobStorageRegistry::Entry; 66 using BlobRegistryEntry = BlobStorageRegistry::Entry;
33 using BlobState = BlobStorageRegistry::BlobState;
34 67
35 BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} 68 BlobStorageContext::BlobStorageContext() {}
36 69
37 BlobStorageContext::~BlobStorageContext() { 70 BlobStorageContext::~BlobStorageContext() {
38 } 71 }
39 72
40 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( 73 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID(
41 const std::string& uuid) { 74 const std::string& uuid) {
42 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 75 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
43 if (!entry) { 76 if (!entry) {
44 return nullptr; 77 return nullptr;
45 } 78 }
46 return base::WrapUnique( 79 return CreateHandle(uuid, entry);
47 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition,
48 this, base::ThreadTaskRunnerHandle::Get().get()));
49 } 80 }
50 81
51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( 82 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL(
52 const GURL& url) { 83 const GURL& url) {
53 std::string uuid; 84 std::string uuid;
54 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); 85 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid);
55 if (!entry) { 86 if (!entry) {
56 return nullptr; 87 return nullptr;
57 } 88 }
58 return base::WrapUnique( 89 return CreateHandle(uuid, entry);
59 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition,
60 this, base::ThreadTaskRunnerHandle::Get().get()));
61 } 90 }
62 91
63 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( 92 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
64 const BlobDataBuilder& external_builder) { 93 const BlobDataBuilder& external_builder) {
65 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); 94 TRACE_EVENT0("Blob", "Context::AddFinishedBlob");
66 CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, 95
67 external_builder.content_disposition_); 96 return BuildBlob(external_builder, BlobStatusCallback());
68 CompletePendingBlob(external_builder);
69 std::unique_ptr<BlobDataHandle> handle =
70 GetBlobDataFromUUID(external_builder.uuid_);
71 DecrementBlobRefCount(external_builder.uuid_);
72 return handle;
73 } 97 }
74 98
75 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( 99 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
76 const BlobDataBuilder* builder) { 100 const BlobDataBuilder* builder) {
77 DCHECK(builder); 101 DCHECK(builder);
78 return AddFinishedBlob(*builder); 102 return AddFinishedBlob(*builder);
79 } 103 }
80 104
81 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, 105 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url,
82 const std::string& uuid) { 106 const std::string& uuid) {
83 if (!registry_.CreateUrlMapping(blob_url, uuid)) { 107 if (!registry_.CreateUrlMapping(blob_url, uuid)) {
84 return false; 108 return false;
85 } 109 }
86 IncrementBlobRefCount(uuid); 110 IncrementBlobRefCount(uuid);
87 return true; 111 return true;
88 } 112 }
89 113
90 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { 114 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) {
91 std::string uuid; 115 std::string uuid;
92 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { 116 if (!registry_.DeleteURLMapping(blob_url, &uuid)) {
93 return; 117 return;
94 } 118 }
95 DecrementBlobRefCount(uuid); 119 DecrementBlobRefCount(uuid);
96 } 120 }
97 121
98 void BlobStorageContext::CreatePendingBlob( 122 void BlobStorageContext::EnableDisk(
123 const base::FilePath storage_directory,
124 scoped_refptr<base::SequencedWorkerPool> file_worker_pool) {
125 memory_controller_.EnableDisk(storage_directory, std::move(file_worker_pool));
126 }
127
128 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBrokenBlob(
99 const std::string& uuid, 129 const std::string& uuid,
100 const std::string& content_type, 130 const std::string& content_type,
101 const std::string& content_disposition) { 131 const std::string& content_disposition,
102 DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); 132 BlobStatus reason) {
103 registry_.CreateEntry(uuid, content_type, content_disposition); 133 DCHECK(!registry_.HasEntry(uuid));
104 } 134 DCHECK(BlobStatusIsError(reason));
105 135 BlobRegistryEntry* entry =
106 void BlobStorageContext::CompletePendingBlob( 136 registry_.CreateEntry(uuid, content_type, content_disposition);
107 const BlobDataBuilder& external_builder) { 137 entry->status = reason;
108 BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); 138 FinishBuilding(entry);
139 return CreateHandle(uuid, entry);
140 }
141
142 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob(
143 const BlobDataBuilder& content,
144 const BlobStatusCallback& can_populate_memory) {
145 DCHECK(!registry_.HasEntry(content.uuid_));
146
147 BlobRegistryEntry* entry = registry_.CreateEntry(
148 content.uuid_, content.content_type_, content.content_disposition_);
149
150 // This flattens all blob references in the transportion content out and
151 // stores the complete item representation in the internal data.
152 BlobFlattener flattener(content, &(entry->data), &registry_);
153
154 // If we contain broken references (or we reference ourself) we want to fail.
155 if (flattener.contains_broken_references) {
156 LOG(ERROR) << "references don't exist";
157 BreakAndFinishBlob(content.uuid_, BlobStatus::REFERENCED_BLOB_BROKEN);
158 return CreateHandle(content.uuid_, entry);
159 }
160
161 // We check to make sure that our memory calculations didn't overflow, which
162 // would mean that the user is trying to save more memory than is in the
163 // physical address space.
164 if (!flattener.memory_needed.IsValid() || !flattener.total_size.IsValid() ||
165 !memory_controller_.CanFitInSystem(
166 flattener.memory_needed.ValueOrDie())) {
167 LOG(ERROR) << "sizes aren't valid!";
168 BreakAndFinishBlob(content.uuid_, BlobStatus::OUT_OF_MEMORY);
169 return CreateHandle(content.uuid_, entry);
170 }
171 // We know we're < max_size_t now.
172 size_t new_memory_needed =
173 static_cast<size_t>(flattener.memory_needed.ValueOrDie());
174
175 // We store if we're waiting for the user to finish populating data in the
176 // |content| builder object.
177 if (flattener.contains_pending_content) {
178 entry->waiting_until_user_population = flattener.contains_pending_content;
179 entry->ready_for_user_population_callback = can_populate_memory;
180 }
181 entry->data.size_ = flattener.total_size.ValueOrDie();
182
183 std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry);
184
185 std::swap(entry->copies, flattener.copies);
186
187 // We hold a handle to all blobs we're using. This is important, as our memory
188 // accounting can be delayed until OnEnoughSizeForBlobData is called, and we
189 // only free memory on canceling when we've done this accounting. If a
190 // dependent blob is dereferenced, then we're the last blob holding onto that
191 // data item, and we need to account for that. So we prevent that case by
192 // holding onto all blobs.
193 entry->dependent_blobs_building = 0;
194 for (const std::pair<std::string, BlobRegistryEntry*>& pending_blob :
195 flattener.dependent_blobs) {
196 entry->dependent_blobs.push_back(
197 CreateHandle(pending_blob.first, pending_blob.second));
198 if (pending_blob.second->status == BlobStatus::PENDING) {
199 pending_blob.second->build_completion_callbacks.push_back(
200 base::Bind(&BlobStorageContext::OnDependentBlobFinished, AsWeakPtr(),
201 content.uuid_));
202 entry->dependent_blobs_building++;
203 }
204 }
205
206 if (new_memory_needed == 0) {
207 entry->memory_accounted_for = true;
208 if (CanFinishBuilding(entry))
209 FinishBuilding(entry);
210 return handle;
211 }
212
213 // So we need to transport/copy memory.
214 base::Optional<BlobMemoryController::PendingConstructionEntry>
215 pending_construction_entry =
216 memory_controller_.NotifyWhenMemoryCanPopulated(
217 new_memory_needed,
218 base::Bind(&BlobStorageContext::OnEnoughSizeForBlobData,
219 AsWeakPtr(), content.uuid_));
220
221 if (pending_construction_entry) {
222 // This means that we're waiting until the memory is available.
223 entry->can_fit = false;
224 entry->pending_copies_memory_entry = pending_construction_entry.value();
225 } else {
226 entry->memory_accounted_for = true;
227 if (!can_populate_memory.is_null())
228 can_populate_memory.Run(BlobStatus::PENDING);
229 }
230
231 if (CanFinishBuilding(entry))
232 FinishBuilding(entry);
233
234 return handle;
235 }
236
237 void BlobStorageContext::BreakAndFinishBlob(const std::string& uuid,
238 BlobStatus reason) {
239 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
109 DCHECK(entry); 240 DCHECK(entry);
110 DCHECK(!entry->data.get()) << "Blob already constructed: " 241 DCHECK(BlobStatusIsError(reason));
111 << external_builder.uuid(); 242 ClearAndFreeMemory(uuid, entry);
112 // We want to handle storing our broken blob as well. 243 entry->copies.clear();
113 switch (entry->state) { 244 entry->dependent_blobs.clear();
114 case BlobState::PENDING: { 245 if (!entry->ready_for_user_population_callback.is_null()) {
115 entry->data_builder.reset(new InternalBlobData::Builder()); 246 entry->ready_for_user_population_callback.Run(reason);
116 InternalBlobData::Builder* internal_data_builder = 247 entry->ready_for_user_population_callback.Reset();
117 entry->data_builder.get(); 248 }
118 249 entry->data.items_.clear();
119 bool broken = false; 250 entry->data.offsets_.clear();
120 for (const auto& blob_item : external_builder.items_) { 251 entry->data.size_ = 0;
121 IPCBlobCreationCancelCode error_code; 252 entry->status = reason;
122 if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item, 253 entry->waiting_until_user_population = false;
123 internal_data_builder, &error_code)) { 254 entry->can_fit = true;
124 broken = true; 255 entry->memory_accounted_for = true;
125 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); 256 FinishBuilding(entry);
126 entry->state = BlobState::BROKEN; 257 }
127 entry->broken_reason = error_code; 258
128 entry->data_builder.reset(new InternalBlobData::Builder()); 259 void BlobStorageContext::FinishedPopulatingBlob(const std::string& uuid) {
260 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
261 entry->waiting_until_user_population = false;
262 if (CanFinishBuilding(entry)) {
263 FinishBuilding(entry);
264 }
265 }
266
267 std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle(
268 const std::string& uuid,
269 BlobRegistryEntry* entry) {
270 return base::WrapUnique(new BlobDataHandle(
271 uuid, entry->content_type, entry->content_disposition, entry->data.size_,
272 this, base::ThreadTaskRunnerHandle::Get().get()));
273 }
274
275 bool BlobStorageContext::CanFinishBuilding(BlobRegistryEntry* entry) {
276 return entry->status == BlobStatus::PENDING &&
277 entry->dependent_blobs_building == 0 && entry->can_fit &&
278 !entry->waiting_until_user_population;
279 }
280
281 void BlobStorageContext::FinishBuilding(BlobRegistryEntry* entry) {
282 DCHECK(entry);
283
284 if (entry->status == BlobStatus::PENDING) {
285 for (const ItemCopyEntry& copy : entry->copies) {
286 DCHECK_EQ(copy.dest_item->item()->type(),
287 DataElement::TYPE_BYTES_DESCRIPTION);
288
289 // We check to see if our source item has been paged to disk.
290 size_t dest_size = static_cast<size_t>(copy.dest_item->item()->length());
291 switch (copy.source_item->item()->type()) {
292 case DataElement::TYPE_BYTES: {
293 const char* src_data =
294 copy.source_item->item()->bytes() + copy.source_item_offset;
295 copy.dest_item->item()->item_->SetToBytes(src_data, dest_size);
296 } break;
297 case DataElement::TYPE_FILE: {
298 // We've been paged to disk, so free the memory of our temporary item,
299 // and create a new shared item with appropriate offset and length.
300 const DataElement& source_element =
301 copy.source_item->item()->data_element();
302 std::unique_ptr<DataElement> new_element(new DataElement());
303 new_element->SetToFilePathRange(
304 source_element.path(),
305 source_element.offset() + copy.source_item_offset, dest_size,
306 source_element.expected_modification_time());
307 scoped_refptr<BlobDataItem> new_item(new BlobDataItem(
308 std::move(new_element), copy.source_item->item()->data_handle()));
309 copy.dest_item->item_.swap(new_item);
310 memory_controller_.FreeMemory(dest_size);
311 } break;
312 case DataElement::TYPE_UNKNOWN:
313 case DataElement::TYPE_BLOB:
314 case DataElement::TYPE_BYTES_DESCRIPTION:
315 case DataElement::TYPE_FILE_FILESYSTEM:
316 case DataElement::TYPE_DISK_CACHE_ENTRY:
317 NOTREACHED();
129 break; 318 break;
130 }
131 } 319 }
132 entry->data = entry->data_builder->Build(); 320 }
133 entry->data_builder.reset(); 321 entry->copies.clear();
134 entry->state = broken ? BlobState::BROKEN : BlobState::COMPLETE; 322
135 break; 323 entry->status = BlobStatus::DONE;
136 } 324 }
137 case BlobState::BROKEN: { 325 entry->dependent_blobs.clear();
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 326
162 auto runner = base::ThreadTaskRunnerHandle::Get(); 327 auto runner = base::ThreadTaskRunnerHandle::Get();
163 for (const auto& callback : entry->build_completion_callbacks) { 328 for (const auto& callback : entry->build_completion_callbacks) {
164 runner->PostTask(FROM_HERE, 329 runner->PostTask(FROM_HERE, base::Bind(callback, entry->status));
165 base::Bind(callback, entry->state == BlobState::COMPLETE,
166 entry->broken_reason));
167 } 330 }
168 entry->build_completion_callbacks.clear(); 331 entry->build_completion_callbacks.clear();
169 } 332
170 333 for (const auto& shareable_item : entry->data.items_) {
171 void BlobStorageContext::CancelPendingBlob(const std::string& uuid, 334 DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION,
172 IPCBlobCreationCancelCode reason) { 335 shareable_item->item()->type());
336 if (shareable_item->item()->type() != DataElement::TYPE_BYTES)
337 continue;
338 memory_controller_.UpdateBlobItemInRecents(shareable_item.get());
339 }
340 }
341
342 void BlobStorageContext::OnEnoughSizeForBlobData(const std::string& uuid,
343 bool success) {
344 if (!success) {
345 BreakAndFinishBlob(uuid, BlobStatus::OUT_OF_MEMORY);
346 return;
347 }
173 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 348 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
174 DCHECK(entry && entry->state == BlobState::PENDING); 349 if (entry == nullptr) {
175 entry->state = BlobState::BROKEN; 350 return;
176 entry->broken_reason = reason; 351 }
177 CompletePendingBlob(BlobDataBuilder(uuid)); 352 entry->can_fit = true;
353 entry->memory_accounted_for = true;
354 if (!entry->ready_for_user_population_callback.is_null()) {
355 entry->ready_for_user_population_callback.Run(BlobStatus::PENDING);
356 entry->ready_for_user_population_callback.Reset();
357 }
358 if (CanFinishBuilding(entry)) {
359 FinishBuilding(entry);
360 }
361 }
362
363 void BlobStorageContext::OnDependentBlobFinished(
364 const std::string& owning_blob_uuid,
365 BlobStatus status) {
366 BlobRegistryEntry* entry = registry_.GetEntry(owning_blob_uuid);
367 if (!entry) {
368 return;
369 }
370 if (BlobStatusIsError(status)) {
371 BreakAndFinishBlob(owning_blob_uuid,
372 ConvertReferencedBlobErrorToConstructingError(status));
373 return;
374 }
375 DCHECK_GT(entry->dependent_blobs_building, 0u);
376 --entry->dependent_blobs_building;
377 if (CanFinishBuilding(entry)) {
378 FinishBuilding(entry);
379 }
380 }
381
382 void BlobStorageContext::ClearAndFreeMemory(const std::string& uuid,
383 BlobRegistryEntry* entry) {
384 if (entry->status == BlobStatus::PENDING) {
385 if (!entry->can_fit) {
386 memory_controller_.RemovePendingConstructionEntry(
387 entry->pending_copies_memory_entry);
388 }
389 entry->dependent_blobs.clear();
390 }
391 // If this is false, that means we can't fit yet and our memory size hasn't
392 // been recorded in the memory manager yet.
393 if (entry->memory_accounted_for) {
394 memory_controller_.FreeMemory(entry->data.GetUnsharedMemoryUsage());
395 }
396 entry->data.RemoveBlobFromShareableItems(uuid);
397 for (const auto& item_refptr : entry->data.items_) {
398 if (item_refptr->referencing_blobs().size() == 0) {
399 memory_controller_.RemoveBlobItemInRecents(*item_refptr);
400 }
401 }
402 entry->data.items_.clear();
403 entry->data.offsets_.clear();
178 } 404 }
179 405
180 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { 406 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) {
181 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 407 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
182 DCHECK(entry); 408 DCHECK(entry);
183 ++(entry->refcount); 409 ++(entry->refcount);
184 } 410 }
185 411
186 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { 412 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) {
187 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 413 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
188 DCHECK(entry); 414 DCHECK(entry);
189 DCHECK_GT(entry->refcount, 0u); 415 DCHECK_GT(entry->refcount, 0u);
190 if (--(entry->refcount) == 0) { 416 if (--(entry->refcount) == 0) {
191 size_t memory_freeing = 0; 417 ClearAndFreeMemory(uuid, entry);
192 if (entry->state == BlobState::COMPLETE) {
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); 418 registry_.DeleteEntry(uuid);
199 } 419 }
200 } 420 }
201 421
202 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( 422 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot(
203 const std::string& uuid) { 423 const std::string& uuid) {
204 std::unique_ptr<BlobDataSnapshot> result; 424 std::unique_ptr<BlobDataSnapshot> result;
205 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 425 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
206 if (entry->state != BlobState::COMPLETE) { 426 if (entry->status != BlobStatus::DONE) {
207 return result; 427 return result;
208 } 428 }
209 429
210 const InternalBlobData& data = *entry->data; 430 const InternalBlobData& data = entry->data;
211 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( 431 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot(
212 uuid, entry->content_type, entry->content_disposition)); 432 uuid, entry->content_type, entry->content_disposition));
213 snapshot->items_.reserve(data.items().size()); 433 snapshot->items_.reserve(data.items().size());
214 for (const auto& shareable_item : data.items()) { 434 for (const auto& shareable_item : data.items()) {
215 snapshot->items_.push_back(shareable_item->item()); 435 snapshot->items_.push_back(shareable_item->item());
436 memory_controller_.UpdateBlobItemInRecents(shareable_item.get());
216 } 437 }
217 return snapshot; 438 return snapshot;
218 } 439 }
219 440
220 bool BlobStorageContext::IsBroken(const std::string& uuid) const { 441 BlobStatus BlobStorageContext::GetBlobStatus(const std::string& uuid) const {
221 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); 442 const BlobRegistryEntry* entry = registry_.GetEntry(uuid);
222 if (!entry) { 443 if (!entry) {
223 return true; 444 return BlobStatus::INVALID_CONSTRUCTION_ARGUMENTS;
224 } 445 }
225 return entry->state == BlobState::BROKEN; 446 return entry->status;
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 } 447 }
235 448
236 void BlobStorageContext::RunOnConstructionComplete( 449 void BlobStorageContext::RunOnConstructionComplete(
237 const std::string& uuid, 450 const std::string& uuid,
238 const BlobConstructedCallback& done) { 451 const BlobStatusCallback& done) {
239 BlobRegistryEntry* entry = registry_.GetEntry(uuid); 452 BlobRegistryEntry* entry = registry_.GetEntry(uuid);
240 DCHECK(entry); 453 DCHECK(entry);
241 switch (entry->state) { 454 if (entry->status == BlobStatus::PENDING) {
242 case BlobState::COMPLETE: 455 entry->build_completion_callbacks.push_back(done);
243 done.Run(true, IPCBlobCreationCancelCode::UNKNOWN); 456 return;
244 return;
245 case BlobState::BROKEN:
246 done.Run(false, entry->broken_reason);
247 return;
248 case BlobState::PENDING:
249 entry->build_completion_callbacks.push_back(done);
250 return;
251 } 457 }
252 NOTREACHED(); 458 done.Run(entry->status);
253 }
254
255 bool BlobStorageContext::AppendAllocatedBlobItem(
256 const std::string& target_blob_uuid,
257 scoped_refptr<BlobDataItem> blob_item,
258 InternalBlobData::Builder* target_blob_builder,
259 IPCBlobCreationCancelCode* error_code) {
260 DCHECK(error_code);
261 *error_code = IPCBlobCreationCancelCode::UNKNOWN;
262 bool error = false;
263
264 // The blob data is stored in the canonical way which only contains a
265 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items
266 // are expanded into the primitive constituent types and reused if possible.
267 // 1) The Data item is denoted by the raw data and length.
268 // 2) The File item is denoted by the file path, the range and the expected
269 // modification time.
270 // 3) The FileSystem File item is denoted by the FileSystem URL, the range
271 // and the expected modification time.
272 // 4) The Blob item is denoted by the source blob and an offset and size.
273 // Internal items that are fully used by the new blob (not cut by the
274 // offset or size) are shared between the blobs. Otherwise, the relevant
275 // portion of the item is copied.
276
277 DCHECK(blob_item->data_element_ptr());
278 const DataElement& data_element = blob_item->data_element();
279 uint64_t length = data_element.length();
280 uint64_t offset = data_element.offset();
281 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend",
282 memory_usage_ / 1024);
283 switch (data_element.type()) {
284 case DataElement::TYPE_BYTES:
285 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024);
286 DCHECK(!offset);
287 if (memory_usage_ + length > kBlobStorageMaxMemoryUsage) {
288 error = true;
289 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY;
290 break;
291 }
292 memory_usage_ += length;
293 target_blob_builder->AppendSharedBlobItem(
294 new ShareableBlobDataItem(target_blob_uuid, blob_item));
295 break;
296 case DataElement::TYPE_FILE: {
297 bool full_file = (length == std::numeric_limits<uint64_t>::max());
298 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file);
299 if (!full_file) {
300 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File",
301 (length - offset) / 1024);
302 }
303 target_blob_builder->AppendSharedBlobItem(
304 new ShareableBlobDataItem(target_blob_uuid, blob_item));
305 break;
306 }
307 case DataElement::TYPE_FILE_FILESYSTEM: {
308 bool full_file = (length == std::numeric_limits<uint64_t>::max());
309 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown",
310 full_file);
311 if (!full_file) {
312 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem",
313 (length - offset) / 1024);
314 }
315 target_blob_builder->AppendSharedBlobItem(
316 new ShareableBlobDataItem(target_blob_uuid, blob_item));
317 break;
318 }
319 case DataElement::TYPE_BLOB: {
320 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob",
321 (length - offset) / 1024);
322 // We grab the handle to ensure it stays around while we copy it.
323 std::unique_ptr<BlobDataHandle> src =
324 GetBlobDataFromUUID(data_element.blob_uuid());
325 if (!src || src->IsBroken() || src->IsBeingBuilt()) {
326 error = true;
327 *error_code = IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN;
328 break;
329 }
330 BlobRegistryEntry* other_entry =
331 registry_.GetEntry(data_element.blob_uuid());
332 DCHECK(other_entry->data);
333 if (!AppendBlob(target_blob_uuid, *other_entry->data, offset, length,
334 target_blob_builder)) {
335 error = true;
336 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY;
337 }
338 break;
339 }
340 case DataElement::TYPE_DISK_CACHE_ENTRY: {
341 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.CacheEntry",
342 (length - offset) / 1024);
343 target_blob_builder->AppendSharedBlobItem(
344 new ShareableBlobDataItem(target_blob_uuid, blob_item));
345 break;
346 }
347 case DataElement::TYPE_BYTES_DESCRIPTION:
348 case DataElement::TYPE_UNKNOWN:
349 NOTREACHED();
350 break;
351 }
352 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend",
353 memory_usage_ / 1024);
354 return !error;
355 }
356
357 bool BlobStorageContext::AppendBlob(
358 const std::string& target_blob_uuid,
359 const InternalBlobData& blob,
360 uint64_t offset,
361 uint64_t length,
362 InternalBlobData::Builder* target_blob_builder) {
363 DCHECK_GT(length, 0ull);
364
365 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items();
366 auto iter = items.begin();
367 if (offset) {
368 for (; iter != items.end(); ++iter) {
369 const BlobDataItem& item = *(iter->get()->item());
370 if (offset >= item.length())
371 offset -= item.length();
372 else
373 break;
374 }
375 }
376
377 for (; iter != items.end() && length > 0; ++iter) {
378 scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get();
379 const BlobDataItem& item = *(shareable_item->item());
380 uint64_t item_length = item.length();
381 DCHECK_GT(item_length, offset);
382 uint64_t current_length = item_length - offset;
383 uint64_t new_length = current_length > length ? length : current_length;
384
385 bool reusing_blob_item = offset == 0 && new_length == item.length();
386 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item);
387 if (reusing_blob_item) {
388 shareable_item->referencing_blobs().insert(target_blob_uuid);
389 target_blob_builder->AppendSharedBlobItem(shareable_item);
390 length -= new_length;
391 continue;
392 }
393
394 // We need to do copying of the items when we have a different offset or
395 // length
396 switch (item.type()) {
397 case DataElement::TYPE_BYTES: {
398 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes",
399 new_length / 1024);
400 if (memory_usage_ + new_length > kBlobStorageMaxMemoryUsage) {
401 return false;
402 }
403 DCHECK(!item.offset());
404 std::unique_ptr<DataElement> element(new DataElement());
405 element->SetToBytes(item.bytes() + offset,
406 static_cast<int64_t>(new_length));
407 memory_usage_ += new_length;
408 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
409 target_blob_uuid, new BlobDataItem(std::move(element))));
410 } break;
411 case DataElement::TYPE_FILE: {
412 DCHECK_NE(item.length(), std::numeric_limits<uint64_t>::max())
413 << "We cannot use a section of a file with an unknown length";
414 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File",
415 new_length / 1024);
416 std::unique_ptr<DataElement> element(new DataElement());
417 element->SetToFilePathRange(item.path(), item.offset() + offset,
418 new_length,
419 item.expected_modification_time());
420 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
421 target_blob_uuid,
422 new BlobDataItem(std::move(element), item.data_handle_)));
423 } break;
424 case DataElement::TYPE_FILE_FILESYSTEM: {
425 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem",
426 new_length / 1024);
427 std::unique_ptr<DataElement> element(new DataElement());
428 element->SetToFileSystemUrlRange(item.filesystem_url(),
429 item.offset() + offset, new_length,
430 item.expected_modification_time());
431 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
432 target_blob_uuid, new BlobDataItem(std::move(element))));
433 } break;
434 case DataElement::TYPE_DISK_CACHE_ENTRY: {
435 std::unique_ptr<DataElement> element(new DataElement());
436 element->SetToDiskCacheEntryRange(item.offset() + offset,
437 new_length);
438 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem(
439 target_blob_uuid,
440 new BlobDataItem(std::move(element), item.data_handle_,
441 item.disk_cache_entry(),
442 item.disk_cache_stream_index(),
443 item.disk_cache_side_stream_index())));
444 } break;
445 case DataElement::TYPE_BYTES_DESCRIPTION:
446 case DataElement::TYPE_BLOB:
447 case DataElement::TYPE_UNKNOWN:
448 CHECK(false) << "Illegal blob item type: " << item.type();
449 }
450 length -= new_length;
451 offset = 0;
452 }
453 return true;
454 } 459 }
455 460
456 } // namespace storage 461 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698