Index: storage/browser/blob/blob_storage_context.cc |
diff --git a/storage/browser/blob/blob_storage_context.cc b/storage/browser/blob/blob_storage_context.cc |
index 6ce45e53f90663cdda7d7b9d1293715ec92aabeb..ab0d2285b384851f0a5c62d373e1176491cfbf7f 100644 |
--- a/storage/browser/blob/blob_storage_context.cc |
+++ b/storage/browser/blob/blob_storage_context.cc |
@@ -4,12 +4,10 @@ |
#include "storage/browser/blob/blob_storage_context.h" |
-#include <stddef.h> |
-#include <stdint.h> |
- |
#include <algorithm> |
#include <limits> |
#include <memory> |
+#include <set> |
#include <utility> |
#include "base/bind.h" |
@@ -19,20 +17,52 @@ |
#include "base/memory/ptr_util.h" |
#include "base/message_loop/message_loop.h" |
#include "base/metrics/histogram.h" |
+#include "base/numerics/safe_conversions.h" |
+#include "base/numerics/safe_math.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "base/trace_event/trace_event.h" |
#include "storage/browser/blob/blob_data_builder.h" |
#include "storage/browser/blob/blob_data_handle.h" |
#include "storage/browser/blob/blob_data_item.h" |
#include "storage/browser/blob/blob_data_snapshot.h" |
+#include "storage/browser/blob/blob_flattener.h" |
#include "storage/browser/blob/shareable_blob_data_item.h" |
+#include "storage/browser/blob/blob_slice.h" |
#include "url/gurl.h" |
namespace storage { |
+using ItemCopyEntry = BlobStorageRegistry::ItemCopyEntry; |
+ |
+namespace { |
+ |
+IPCBlobCreationCancelCode ConvertReferencedBlobErrorToConstructingError( |
+ IPCBlobCreationCancelCode referenced_blob_error) { |
+ switch (referenced_blob_error) { |
+ // For most cases we propagate the error. |
+ case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: |
+ case IPCBlobCreationCancelCode::SOURCE_DIED_IN_TRANSIT: |
+ case IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN: |
+ case IPCBlobCreationCancelCode::OUT_OF_MEMORY: |
+ return referenced_blob_error; |
+ // Others we report that the referenced blob is broken, as we don't know |
+ // why (the BLOB_DEREFERENCED_WHILE_BUILDING should never happen, as we hold |
+ // onto the reference of the blobs we're using). |
+ case IPCBlobCreationCancelCode::BLOB_DEREFERENCED_WHILE_BUILDING: |
+ DCHECK(false) << "Referenced blob should never be dereferenced while we " |
+ << "are depending on it, as our system holds a handle."; |
+ case IPCBlobCreationCancelCode::UNKNOWN: |
+ return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; |
+ } |
+ NOTREACHED(); |
+ return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; |
+} |
+ |
+} // namespace |
+ |
using BlobRegistryEntry = BlobStorageRegistry::Entry; |
using BlobState = BlobStorageRegistry::BlobState; |
-BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} |
+BlobStorageContext::BlobStorageContext() {} |
BlobStorageContext::~BlobStorageContext() { |
} |
@@ -43,9 +73,7 @@ std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
if (!entry) { |
return nullptr; |
} |
- return base::WrapUnique( |
- new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, |
- this, base::ThreadTaskRunnerHandle::Get().get())); |
+ return CreateHandle(uuid, entry); |
} |
std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
@@ -55,21 +83,14 @@ std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
if (!entry) { |
return nullptr; |
} |
- return base::WrapUnique( |
- new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, |
- this, base::ThreadTaskRunnerHandle::Get().get())); |
+ return CreateHandle(uuid, entry); |
} |
std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
const BlobDataBuilder& external_builder) { |
TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); |
- CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, |
- external_builder.content_disposition_); |
- CompletePendingBlob(external_builder); |
- std::unique_ptr<BlobDataHandle> handle = |
- GetBlobDataFromUUID(external_builder.uuid_); |
- DecrementBlobRefCount(external_builder.uuid_); |
- return handle; |
+ |
+ return BuildBlob(external_builder, false); |
} |
std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
@@ -95,69 +116,202 @@ void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
DecrementBlobRefCount(uuid); |
} |
-void BlobStorageContext::CreatePendingBlob( |
+void BlobStorageContext::EnableDisk( |
+ const base::FilePath storage_directory, |
+ scoped_refptr<base::SingleThreadTaskRunner> file_runner) { |
+ memory_controller_.EnableDisk(storage_directory, std::move(file_runner)); |
+} |
+ |
+std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBrokenBlob( |
const std::string& uuid, |
const std::string& content_type, |
- const std::string& content_disposition) { |
- DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); |
- registry_.CreateEntry(uuid, content_type, content_disposition); |
+ const std::string& content_disposition, |
+ IPCBlobCreationCancelCode reason) { |
+ DCHECK(!registry_.HasEntry(uuid)); |
+ BlobRegistryEntry* entry = |
+ registry_.CreateEntry(uuid, content_type, content_disposition); |
+ entry->state = BlobState::BROKEN; |
+ FinishBuilding(entry); |
+ return CreateHandle(uuid, entry); |
} |
-void BlobStorageContext::CompletePendingBlob( |
- const BlobDataBuilder& external_builder) { |
- BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); |
- DCHECK(entry); |
- DCHECK(!entry->data.get()) << "Blob already constructed: " |
- << external_builder.uuid(); |
- // We want to handle storing our broken blob as well. |
- switch (entry->state) { |
- case BlobState::PENDING: { |
- entry->data_builder.reset(new InternalBlobData::Builder()); |
- InternalBlobData::Builder* internal_data_builder = |
- entry->data_builder.get(); |
- |
- bool broken = false; |
- for (const auto& blob_item : external_builder.items_) { |
- IPCBlobCreationCancelCode error_code; |
- if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item, |
- internal_data_builder, &error_code)) { |
- broken = true; |
- memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); |
- entry->state = BlobState::BROKEN; |
- entry->broken_reason = error_code; |
- entry->data_builder.reset(new InternalBlobData::Builder()); |
- break; |
+std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob( |
+ const BlobDataBuilder& content, |
+ bool content_pending) { |
+ DCHECK(!registry_.HasEntry(content.uuid_)); |
+ |
+ BlobRegistryEntry* entry = registry_.CreateEntry( |
+ content.uuid_, content.content_type_, content.content_disposition_); |
+ |
+ entry->waiting_until_user_population = content_pending; |
+ |
+ base::CheckedNumeric<uint64_t> total_size = 0; |
+ base::CheckedNumeric<size_t> new_memory_size = 0; |
+ base::CheckedNumeric<size_t> copying_size = 0; |
+ const std::vector<scoped_refptr<BlobDataItem>>& items = content.items_; |
+ |
+ InternalBlobData& data = entry->data; |
+ |
+ std::map<std::string, BlobSlice> slice_map; |
+ |
+ std::set<std::string> dependent_blob_uuids; |
+ std::vector<BlobRegistryEntry*> pending_dependent_blobs; |
+ |
+ bool all_dependant_blobs_built = true; |
+ |
+ // First we calculate the size. |
+ for (const scoped_refptr<BlobDataItem>& item : items) { |
+ uint64_t length = item->length(); |
+ DCHECK(item->type() != DataElement::TYPE_BYTES_DESCRIPTION || |
+ content_pending); |
+ DCHECK(item->type() != DataElement::TYPE_BYTES || !content_pending); |
+ |
+ total_size += length; |
+ if (item->type() == DataElement::TYPE_BYTES) { |
+ DCHECK_NE(length, std::numeric_limits<uint64_t>::max()); |
+ DCHECK(!content_pending); |
+ new_memory_size += item->length(); |
+ } else if (item->type() == DataElement::TYPE_BLOB) { |
+ const std::string& ref_blob_uuid = item->blob_uuid(); |
+ if (ref_blob_uuid == content.uuid_) { |
+ BreakAndFinishBlob(content.uuid_, |
+ IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN); |
+ return CreateHandle(content.uuid_, entry); |
+ } |
+ BlobRegistryEntry* ref_entry = registry_.GetEntry(ref_blob_uuid); |
+ DCHECK(ref_entry); |
+ |
+ if (ref_entry->state == BlobState::BROKEN) { |
+ BreakAndFinishBlob(content.uuid_, |
+ IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN); |
+ return CreateHandle(content.uuid_, entry); |
+ } |
+ |
+ if (dependent_blob_uuids.find(ref_blob_uuid) == |
+ dependent_blob_uuids.end()) { |
+ entry->dependent_blobs.emplace_back(GetBlobDataFromUUID(ref_blob_uuid)); |
+ dependent_blob_uuids.insert(ref_blob_uuid); |
+ if (ref_entry->state != BlobState::COMPLETE) { |
+ all_dependant_blobs_built = false; |
+ pending_dependent_blobs.push_back(ref_entry); |
} |
} |
- entry->data = entry->data_builder->Build(); |
- entry->data_builder.reset(); |
- entry->state = broken ? BlobState::BROKEN : BlobState::COMPLETE; |
- break; |
+ |
+ const InternalBlobData& source = ref_entry->data; |
+ BlobSlice slice(source, item->offset(), item->length(), |
+ ref_entry->state != BlobState::PENDING); |
+ new_memory_size += slice.copying_memory_size(); |
+ copying_size += slice.copying_memory_size(); |
+ slice_map.insert(std::make_pair(item->blob_uuid(), std::move(slice))); |
} |
- case BlobState::BROKEN: { |
- InternalBlobData::Builder builder; |
- entry->data = builder.Build(); |
- break; |
+ } |
+ entry->dependent_blobs_building = pending_dependent_blobs.size(); |
+ data.copying_size_ = copying_size.ValueOrDie(); |
+ |
+ std::vector<BlobStorageRegistry::ItemCopyEntry> copies_from_pending_reference; |
+ BlobFlattener::FlattenBlob( |
+ content.uuid_, &(entry->data), &entry->copies_from_built_reference, |
+ &copies_from_pending_reference, std::move(slice_map), content); |
+ |
+ for (BlobStorageRegistry::ItemCopyEntry& copy : |
+ copies_from_pending_reference) { |
+ BlobRegistryEntry* ref_entry = registry_.GetEntry(copy.uuid); |
+ copy.uuid = content.uuid_; |
+ DCHECK(ref_entry->state == BlobState::PENDING); |
+ ref_entry->copies_to_when_built.push_back(copy); |
+ } |
+ |
+ std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry); |
+ DecrementBlobRefCount(content.uuid_); |
+ |
+ if (!all_dependant_blobs_built) { |
+ for (BlobRegistryEntry* entry : pending_dependent_blobs) { |
+ entry->build_completion_callbacks.push_back( |
+ base::Bind(&BlobStorageContext::OnDependentBlobFinished, AsWeakPtr(), |
+ content.uuid_)); |
} |
- case BlobState::COMPLETE: |
- DCHECK(false) << "Blob already constructed: " << external_builder.uuid(); |
- return; |
} |
- UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); |
- UMA_HISTOGRAM_BOOLEAN("Storage.Blob.Broken", |
- entry->state == BlobState::BROKEN); |
- if (entry->state == BlobState::BROKEN) { |
- UMA_HISTOGRAM_ENUMERATION( |
- "Storage.Blob.BrokenReason", static_cast<int>(entry->broken_reason), |
- (static_cast<int>(IPCBlobCreationCancelCode::LAST) + 1)); |
+ if (!memory_controller_.MaybeFitInMemoryNow(data.copying_size_ + |
+ new_memory_size.ValueOrDie())) { |
+ entry->can_fit_copies = false; |
+ entry->pending_copies_memory_entry = |
+ memory_controller_.NotifyWhenMemoryCanPopulated( |
+ data.copying_size_, |
+ base::Bind(&BlobStorageContext::OnEnoughSizeForDependentCopies, |
+ AsWeakPtr(), content.uuid_)); |
+ } |
+ |
+ if (CanFinishBuilding(entry)) { |
+ FinishBuilding(entry); |
+ } |
+ |
+ return handle; |
+} |
+ |
+void BlobStorageContext::BreakAndFinishBlob(const std::string& uuid, |
+ IPCBlobCreationCancelCode reason) { |
+ BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
+ DCHECK(entry); |
+ ClearAndFreeMemory(uuid, entry); |
+ entry->copies_from_built_reference.clear(); |
+ entry->copies_to_when_built.clear(); |
+ entry->dependent_blobs.clear(); |
+ entry->state = BlobState::BROKEN; |
+ entry->data.items_.clear(); |
+ entry->data.offsets_.clear(); |
+ entry->data.size_ = 0; |
+ entry->broken_reason = reason; |
+ entry->waiting_until_user_population = false; |
+ entry->can_fit_copies = true; |
+ FinishBuilding(entry); |
+} |
+ |
+void BlobStorageContext::FinishedPopulatingBlob(const std::string& uuid) { |
+ BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
+ entry->waiting_until_user_population = false; |
+ if (CanFinishBuilding(entry)) { |
+ FinishBuilding(entry); |
+ } |
+} |
+ |
+static uint64_t sNextItemId = 0; |
+ |
+/* static */ |
+uint64_t BlobStorageContext::GetAndIncrementItemId() { |
+ return sNextItemId++; |
+} |
+ |
+std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle( |
+ const std::string& uuid, |
+ BlobRegistryEntry* entry) { |
+ return base::WrapUnique(new BlobDataHandle( |
+ uuid, entry->content_type, entry->content_disposition, entry->data.size_, |
+ this, base::ThreadTaskRunnerHandle::Get().get())); |
+} |
+ |
+bool BlobStorageContext::CanFinishBuilding(BlobRegistryEntry* entry) { |
+ return entry->dependent_blobs_building == 0 && entry->can_fit_copies && |
+ !entry->waiting_until_user_population; |
+} |
+ |
+void BlobStorageContext::FinishBuilding(BlobRegistryEntry* entry) { |
+ DCHECK(entry); |
+ |
+ if (entry->state == BlobState::PENDING) { |
+ LOG(ERROR) << "copying from other blobs"; |
+ for (const ItemCopyEntry& copy : entry->copies_from_built_reference) { |
+ PerformCopy(copy, ®istry_.GetEntry(copy.uuid)->data, &entry->data); |
+ } |
+ |
+ LOG(ERROR) << "copying to other blobs"; |
+ for (const ItemCopyEntry& copy : entry->copies_to_when_built) { |
+ PerformCopy(copy, &entry->data, ®istry_.GetEntry(copy.uuid)->data); |
+ } |
+ |
+ entry->state = BlobState::COMPLETE; |
} |
- size_t total_memory = 0, nonshared_memory = 0; |
- entry->data->GetMemoryUsage(&total_memory, &nonshared_memory); |
- UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize", total_memory / 1024); |
- UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize", |
- nonshared_memory / 1024); |
- TRACE_COUNTER1("Blob", "MemoryStoreUsageBytes", memory_usage_); |
+ entry->dependent_blobs.clear(); |
auto runner = base::ThreadTaskRunnerHandle::Get(); |
for (const auto& callback : entry->build_completion_callbacks) { |
@@ -166,15 +320,71 @@ void BlobStorageContext::CompletePendingBlob( |
entry->broken_reason)); |
} |
entry->build_completion_callbacks.clear(); |
+ |
+ LOG(ERROR) << "updating recent items"; |
+ for (const auto& shareable_item : entry->data.items_) { |
+ DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION, |
+ shareable_item->item()->type()); |
+ if (shareable_item->item()->type() != DataElement::TYPE_BYTES) |
+ continue; |
+ memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
+ } |
} |
-void BlobStorageContext::CancelPendingBlob(const std::string& uuid, |
- IPCBlobCreationCancelCode reason) { |
+void BlobStorageContext::OnEnoughSizeForDependentCopies(const std::string& uuid, |
+ bool success) { |
+ if (!success) { |
+ BreakAndFinishBlob(uuid, IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
+ return; |
+ } |
BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
- DCHECK(entry && entry->state == BlobState::PENDING); |
- entry->state = BlobState::BROKEN; |
- entry->broken_reason = reason; |
- CompletePendingBlob(BlobDataBuilder(uuid)); |
+ if (entry == nullptr) { |
+ return; |
+ } |
+ entry->can_fit_copies = true; |
+ if (CanFinishBuilding(entry)) { |
+ FinishBuilding(entry); |
+ } |
+} |
+ |
+void BlobStorageContext::OnDependentBlobFinished( |
+ const std::string& owning_blob_uuid, |
+ bool construction_success, |
+ IPCBlobCreationCancelCode reason) { |
+ BlobRegistryEntry* entry = registry_.GetEntry(owning_blob_uuid); |
+ if (entry == nullptr) { |
+ return; |
+ } |
+ if (!construction_success) { |
+ BreakAndFinishBlob(owning_blob_uuid, |
+ ConvertReferencedBlobErrorToConstructingError(reason)); |
+ return; |
+ } |
+ DCHECK_GT(entry->dependent_blobs_building, 0u); |
+ --entry->dependent_blobs_building; |
+ if (CanFinishBuilding(entry)) { |
+ FinishBuilding(entry); |
+ } |
+} |
+ |
+void BlobStorageContext::ClearAndFreeMemory(const std::string& uuid, |
+ BlobRegistryEntry* entry) { |
+ if (entry->state == BlobState::PENDING) { |
+ if (!entry->can_fit_copies) { |
+ memory_controller_.RemovePendingConstructionEntry( |
+ entry->pending_copies_memory_entry); |
+ } |
+ entry->dependent_blobs.clear(); |
+ } |
+ memory_controller_.FreeMemory(entry->data.GetUnsharedMemoryUsage()); |
+ entry->data.RemoveBlobFromShareableItems(uuid); |
+ for (const auto& item_refptr : entry->data.items_) { |
+ if (item_refptr->referencing_blobs().size() == 0) { |
+ memory_controller_.RemoveBlobItemInRecents(item_refptr->item_id()); |
+ } |
+ } |
+ entry->data.items_.clear(); |
+ entry->data.offsets_.clear(); |
} |
void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
@@ -188,13 +398,7 @@ void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
DCHECK(entry); |
DCHECK_GT(entry->refcount, 0u); |
if (--(entry->refcount) == 0) { |
- size_t memory_freeing = 0; |
- if (entry->state == BlobState::COMPLETE) { |
- memory_freeing = entry->data->GetUnsharedMemoryUsage(); |
- entry->data->RemoveBlobFromShareableItems(uuid); |
- } |
- DCHECK_LE(memory_freeing, memory_usage_); |
- memory_usage_ -= memory_freeing; |
+ ClearAndFreeMemory(uuid, entry); |
registry_.DeleteEntry(uuid); |
} |
} |
@@ -207,12 +411,13 @@ std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( |
return result; |
} |
- const InternalBlobData& data = *entry->data; |
+ const InternalBlobData& data = entry->data; |
std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( |
uuid, entry->content_type, entry->content_disposition)); |
snapshot->items_.reserve(data.items().size()); |
for (const auto& shareable_item : data.items()) { |
snapshot->items_.push_back(shareable_item->item()); |
+ memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
} |
return snapshot; |
} |
@@ -252,205 +457,23 @@ void BlobStorageContext::RunOnConstructionComplete( |
NOTREACHED(); |
} |
-bool BlobStorageContext::AppendAllocatedBlobItem( |
- const std::string& target_blob_uuid, |
- scoped_refptr<BlobDataItem> blob_item, |
- InternalBlobData::Builder* target_blob_builder, |
- IPCBlobCreationCancelCode* error_code) { |
- DCHECK(error_code); |
- *error_code = IPCBlobCreationCancelCode::UNKNOWN; |
- bool error = false; |
- |
- // The blob data is stored in the canonical way which only contains a |
- // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items |
- // are expanded into the primitive constituent types and reused if possible. |
- // 1) The Data item is denoted by the raw data and length. |
- // 2) The File item is denoted by the file path, the range and the expected |
- // modification time. |
- // 3) The FileSystem File item is denoted by the FileSystem URL, the range |
- // and the expected modification time. |
- // 4) The Blob item is denoted by the source blob and an offset and size. |
- // Internal items that are fully used by the new blob (not cut by the |
- // offset or size) are shared between the blobs. Otherwise, the relevant |
- // portion of the item is copied. |
- |
- DCHECK(blob_item->data_element_ptr()); |
- const DataElement& data_element = blob_item->data_element(); |
- uint64_t length = data_element.length(); |
- uint64_t offset = data_element.offset(); |
- UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", |
- memory_usage_ / 1024); |
- switch (data_element.type()) { |
- case DataElement::TYPE_BYTES: |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); |
- DCHECK(!offset); |
- if (memory_usage_ + length > kBlobStorageMaxMemoryUsage) { |
- error = true; |
- *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY; |
- break; |
- } |
- memory_usage_ += length; |
- target_blob_builder->AppendSharedBlobItem( |
- new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
- break; |
- case DataElement::TYPE_FILE: { |
- bool full_file = (length == std::numeric_limits<uint64_t>::max()); |
- UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file); |
- if (!full_file) { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", |
- (length - offset) / 1024); |
- } |
- target_blob_builder->AppendSharedBlobItem( |
- new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
- break; |
- } |
- case DataElement::TYPE_FILE_FILESYSTEM: { |
- bool full_file = (length == std::numeric_limits<uint64_t>::max()); |
- UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown", |
- full_file); |
- if (!full_file) { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", |
- (length - offset) / 1024); |
- } |
- target_blob_builder->AppendSharedBlobItem( |
- new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
- break; |
- } |
- case DataElement::TYPE_BLOB: { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", |
- (length - offset) / 1024); |
- // We grab the handle to ensure it stays around while we copy it. |
- std::unique_ptr<BlobDataHandle> src = |
- GetBlobDataFromUUID(data_element.blob_uuid()); |
- if (!src || src->IsBroken() || src->IsBeingBuilt()) { |
- error = true; |
- *error_code = IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; |
- break; |
- } |
- BlobRegistryEntry* other_entry = |
- registry_.GetEntry(data_element.blob_uuid()); |
- DCHECK(other_entry->data); |
- if (!AppendBlob(target_blob_uuid, *other_entry->data, offset, length, |
- target_blob_builder)) { |
- error = true; |
- *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY; |
- } |
- break; |
- } |
- case DataElement::TYPE_DISK_CACHE_ENTRY: { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.CacheEntry", |
- (length - offset) / 1024); |
- target_blob_builder->AppendSharedBlobItem( |
- new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
- break; |
- } |
- case DataElement::TYPE_BYTES_DESCRIPTION: |
- case DataElement::TYPE_UNKNOWN: |
- NOTREACHED(); |
- break; |
+void BlobStorageContext::UpdateItemsInRecents(BlobRegistryEntry* entry) { |
+ for (const auto& item_refptr : entry->data.items_) { |
+ memory_controller_.UpdateBlobItemInRecents(item_refptr.get()); |
} |
- UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", |
- memory_usage_ / 1024); |
- return !error; |
} |
-bool BlobStorageContext::AppendBlob( |
- const std::string& target_blob_uuid, |
- const InternalBlobData& blob, |
- uint64_t offset, |
- uint64_t length, |
- InternalBlobData::Builder* target_blob_builder) { |
- DCHECK_GT(length, 0ull); |
- |
- const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items(); |
- auto iter = items.begin(); |
- if (offset) { |
- for (; iter != items.end(); ++iter) { |
- const BlobDataItem& item = *(iter->get()->item()); |
- if (offset >= item.length()) |
- offset -= item.length(); |
- else |
- break; |
- } |
- } |
- |
- for (; iter != items.end() && length > 0; ++iter) { |
- scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get(); |
- const BlobDataItem& item = *(shareable_item->item()); |
- uint64_t item_length = item.length(); |
- DCHECK_GT(item_length, offset); |
- uint64_t current_length = item_length - offset; |
- uint64_t new_length = current_length > length ? length : current_length; |
- |
- bool reusing_blob_item = offset == 0 && new_length == item.length(); |
- UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item); |
- if (reusing_blob_item) { |
- shareable_item->referencing_blobs().insert(target_blob_uuid); |
- target_blob_builder->AppendSharedBlobItem(shareable_item); |
- length -= new_length; |
- continue; |
- } |
- |
- // We need to do copying of the items when we have a different offset or |
- // length |
- switch (item.type()) { |
- case DataElement::TYPE_BYTES: { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes", |
- new_length / 1024); |
- if (memory_usage_ + new_length > kBlobStorageMaxMemoryUsage) { |
- return false; |
- } |
- DCHECK(!item.offset()); |
- std::unique_ptr<DataElement> element(new DataElement()); |
- element->SetToBytes(item.bytes() + offset, |
- static_cast<int64_t>(new_length)); |
- memory_usage_ += new_length; |
- target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
- target_blob_uuid, new BlobDataItem(std::move(element)))); |
- } break; |
- case DataElement::TYPE_FILE: { |
- DCHECK_NE(item.length(), std::numeric_limits<uint64_t>::max()) |
- << "We cannot use a section of a file with an unknown length"; |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File", |
- new_length / 1024); |
- std::unique_ptr<DataElement> element(new DataElement()); |
- element->SetToFilePathRange(item.path(), item.offset() + offset, |
- new_length, |
- item.expected_modification_time()); |
- target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
- target_blob_uuid, |
- new BlobDataItem(std::move(element), item.data_handle_))); |
- } break; |
- case DataElement::TYPE_FILE_FILESYSTEM: { |
- UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem", |
- new_length / 1024); |
- std::unique_ptr<DataElement> element(new DataElement()); |
- element->SetToFileSystemUrlRange(item.filesystem_url(), |
- item.offset() + offset, new_length, |
- item.expected_modification_time()); |
- target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
- target_blob_uuid, new BlobDataItem(std::move(element)))); |
- } break; |
- case DataElement::TYPE_DISK_CACHE_ENTRY: { |
- std::unique_ptr<DataElement> element(new DataElement()); |
- element->SetToDiskCacheEntryRange(item.offset() + offset, |
- new_length); |
- target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
- target_blob_uuid, |
- new BlobDataItem(std::move(element), item.data_handle_, |
- item.disk_cache_entry(), |
- item.disk_cache_stream_index(), |
- item.disk_cache_side_stream_index()))); |
- } break; |
- case DataElement::TYPE_BYTES_DESCRIPTION: |
- case DataElement::TYPE_BLOB: |
- case DataElement::TYPE_UNKNOWN: |
- CHECK(false) << "Illegal blob item type: " << item.type(); |
- } |
- length -= new_length; |
- offset = 0; |
- } |
- return true; |
+void BlobStorageContext::PerformCopy(const ItemCopyEntry& copy, |
+ InternalBlobData* source, |
+ InternalBlobData* destination) { |
+ const auto& source_item = source->items()[copy.source_item_index]; |
+ const auto& dest_item = destination->items()[copy.dest_item_index]; |
+ |
+ DCHECK_EQ(source_item->item()->type(), DataElement::TYPE_BYTES); |
+ DCHECK_EQ(dest_item->item()->type(), DataElement::TYPE_BYTES_DESCRIPTION) |
+ << copy.dest_item_index; |
+ const char* src_data = source_item->item()->bytes() + copy.source_item_offset; |
+ dest_item->item()->item_->SetToBytes(src_data, copy.size); |
} |
} // namespace storage |