| OLD | NEW |
| 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/task_runner.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_item.h" | 26 #include "storage/browser/blob/blob_data_item.h" |
| 27 #include "storage/browser/blob/blob_data_snapshot.h" | 27 #include "storage/browser/blob/blob_data_snapshot.h" |
| 28 #include "storage/browser/blob/blob_flattener.h" |
| 29 #include "storage/browser/blob/blob_slice.h" |
| 30 #include "storage/browser/blob/internal_blob_data.h" |
| 28 #include "storage/browser/blob/shareable_blob_data_item.h" | 31 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 32 #include "storage/common/data_element.h" |
| 29 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 30 | 34 |
| 31 namespace storage { | 35 namespace storage { |
| 32 using BlobRegistryEntry = BlobStorageRegistry::Entry; | 36 using ItemCopyEntry = InternalBlobData::ItemCopyEntry; |
| 33 using BlobState = BlobStorageRegistry::BlobState; | |
| 34 | 37 |
| 35 BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} | 38 BlobStorageContext::BlobStorageContext() {} |
| 36 | 39 |
| 37 BlobStorageContext::~BlobStorageContext() { | 40 BlobStorageContext::~BlobStorageContext() { |
| 38 } | 41 } |
| 39 | 42 |
| 40 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( | 43 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
| 41 const std::string& uuid) { | 44 const std::string& uuid) { |
| 42 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 45 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 43 if (!entry) { | 46 if (!entry) |
| 44 return nullptr; | 47 return nullptr; |
| 45 } | 48 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 } | 49 } |
| 50 | 50 |
| 51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( | 51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
| 52 const GURL& url) { | 52 const GURL& url) { |
| 53 std::string uuid; | 53 std::string uuid; |
| 54 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); | 54 InternalBlobData* entry = registry_.GetEntryFromURL(url, &uuid); |
| 55 if (!entry) { | 55 if (!entry) |
| 56 return nullptr; | 56 return nullptr; |
| 57 } | 57 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 } | 58 } |
| 62 | 59 |
| 63 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 60 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
| 64 const BlobDataBuilder& external_builder) { | 61 const BlobDataBuilder& external_builder) { |
| 65 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); | 62 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); |
| 66 CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, | 63 return BuildBlob(external_builder, BlobStatusCallback()); |
| 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 } | 64 } |
| 74 | 65 |
| 75 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 66 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
| 76 const BlobDataBuilder* builder) { | 67 const BlobDataBuilder* builder) { |
| 77 DCHECK(builder); | 68 DCHECK(builder); |
| 78 return AddFinishedBlob(*builder); | 69 return AddFinishedBlob(*builder); |
| 79 } | 70 } |
| 80 | 71 |
| 72 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddBrokenBlob( |
| 73 const std::string& uuid, |
| 74 const std::string& content_type, |
| 75 const std::string& content_disposition, |
| 76 BlobStatus reason) { |
| 77 DCHECK(!registry_.HasEntry(uuid)); |
| 78 DCHECK(BlobStatusIsError(reason)); |
| 79 InternalBlobData* entry = |
| 80 registry_.CreateEntry(uuid, content_type, content_disposition); |
| 81 entry->status_ = reason; |
| 82 FinishBuilding(entry); |
| 83 return CreateHandle(uuid, entry); |
| 84 } |
| 85 |
| 81 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, | 86 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, |
| 82 const std::string& uuid) { | 87 const std::string& uuid) { |
| 83 if (!registry_.CreateUrlMapping(blob_url, uuid)) { | 88 if (!registry_.CreateUrlMapping(blob_url, uuid)) |
| 84 return false; | 89 return false; |
| 85 } | |
| 86 IncrementBlobRefCount(uuid); | 90 IncrementBlobRefCount(uuid); |
| 87 return true; | 91 return true; |
| 88 } | 92 } |
| 89 | 93 |
| 90 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { | 94 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
| 91 std::string uuid; | 95 std::string uuid; |
| 92 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { | 96 if (!registry_.DeleteURLMapping(blob_url, &uuid)) |
| 93 return; | 97 return; |
| 94 } | |
| 95 DecrementBlobRefCount(uuid); | 98 DecrementBlobRefCount(uuid); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void BlobStorageContext::CreatePendingBlob( | 101 void BlobStorageContext::EnableDisk( |
| 99 const std::string& uuid, | 102 const base::FilePath& storage_directory, |
| 100 const std::string& content_type, | 103 scoped_refptr<base::TaskRunner> file_runner) { |
| 101 const std::string& content_disposition) { | 104 memory_controller_.EnableDisk(storage_directory, std::move(file_runner)); |
| 102 DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); | 105 } |
| 103 registry_.CreateEntry(uuid, content_type, content_disposition); | 106 |
| 104 } | 107 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob( |
| 105 | 108 const BlobDataBuilder& content, |
| 106 void BlobStorageContext::CompletePendingBlob( | 109 const BlobStatusCallback& can_populate_memory) { |
| 107 const BlobDataBuilder& external_builder) { | 110 DCHECK(!registry_.HasEntry(content.uuid_)); |
| 108 BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); | 111 |
| 112 InternalBlobData* entry = registry_.CreateEntry( |
| 113 content.uuid_, content.content_type_, content.content_disposition_); |
| 114 |
| 115 entry->building_state_.reset(new InternalBlobData::BuildingState()); |
| 116 InternalBlobData::BuildingState* building_state = |
| 117 entry->building_state_.get(); |
| 118 |
| 119 // This flattens all blob references in the transportion content out and |
| 120 // stores the complete item representation in the internal data. |
| 121 BlobFlattener flattener(content, entry, ®istry_); |
| 122 |
| 123 // If we contain invalid references we're invalid input. |
| 124 if (flattener.status == BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS) { |
| 125 LOG(ERROR) << "invalid reference args"; |
| 126 BreakAndFinishPendingBlob(content.uuid_, |
| 127 BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS); |
| 128 return CreateHandle(content.uuid_, entry); |
| 129 } |
| 130 |
| 131 // If we contain broken references (or we reference ourself) we want to fail. |
| 132 if (flattener.status == BlobStatus::ERR_REFERENCED_BLOB_BROKEN) { |
| 133 LOG(ERROR) << "references don't exist"; |
| 134 BreakAndFinishPendingBlob(content.uuid_, |
| 135 BlobStatus::ERR_REFERENCED_BLOB_BROKEN); |
| 136 return CreateHandle(content.uuid_, entry); |
| 137 } |
| 138 |
| 139 // This means we have a file with BlobDataBuilder::kUnknownSize where it isn't |
| 140 // the only item. This is invalid for our system. |
| 141 DCHECK(!flattener.memory_needed.IsValid() || flattener.total_size.IsValid()) |
| 142 << "We cannot have a file with unknown size where there are other items " |
| 143 "in the blob. Please read the size before constructing the blob."; |
| 144 |
| 145 // We check to make sure that our memory calculations didn't overflow, which |
| 146 // would mean that the user is trying to save more memory than is in the |
| 147 // physical address space. |
| 148 if (!flattener.memory_needed.IsValid() || !flattener.total_size.IsValid() || |
| 149 !memory_controller_.CanFitInSystem( |
| 150 flattener.memory_needed.ValueOrDie())) { |
| 151 LOG(ERROR) << "sizes aren't valid, or we can't fit."; |
| 152 BreakAndFinishPendingBlob(content.uuid_, BlobStatus::ERR_OUT_OF_MEMORY); |
| 153 return CreateHandle(content.uuid_, entry); |
| 154 } |
| 155 // We know we're < max_size_t now. |
| 156 size_t new_memory_needed = |
| 157 static_cast<size_t>(flattener.memory_needed.ValueOrDie()); |
| 158 |
| 159 // We store if we're waiting for the user to finish populating data in the |
| 160 // |content| builder object. |
| 161 if (flattener.status == BlobStatus::PENDING_DATA_POPULATION) { |
| 162 entry->building_state_->waiting_for_user_population = true; |
| 163 building_state->ready_for_user_population_callback = can_populate_memory; |
| 164 } else { |
| 165 DCHECK_EQ(BlobStatus::DONE, flattener.status); |
| 166 DCHECK(can_populate_memory.is_null()); |
| 167 } |
| 168 |
| 169 entry->size_ = flattener.total_size.ValueOrDie(); |
| 170 |
| 171 std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry); |
| 172 |
| 173 std::swap(building_state->copies, flattener.copies); |
| 174 |
| 175 // We hold a handle to all blobs we're using. This is important, as our memory |
| 176 // accounting can be delayed until OnEnoughSizeForBlobData is called, and we |
| 177 // only free memory on canceling when we've done this accounting. If a |
| 178 // dependent blob is dereferenced, then we're the last blob holding onto that |
| 179 // data item, and we need to account for that. So we prevent that case by |
| 180 // holding onto all blobs. |
| 181 building_state->dependent_blobs_building = 0; |
| 182 for (const std::pair<std::string, InternalBlobData*>& pending_blob : |
| 183 flattener.dependent_blobs) { |
| 184 building_state->dependent_blobs.push_back( |
| 185 CreateHandle(pending_blob.first, pending_blob.second)); |
| 186 if (BlobStatusIsPending(pending_blob.second->status())) { |
| 187 pending_blob.second->building_state_->build_completion_callbacks |
| 188 .push_back(base::Bind(&BlobStorageContext::OnDependentBlobFinished, |
| 189 AsWeakPtr(), content.uuid_)); |
| 190 building_state->dependent_blobs_building++; |
| 191 } |
| 192 } |
| 193 |
| 194 // This means we don't have any transport memory or copies from referenced |
| 195 // blobs. |
| 196 if (new_memory_needed == 0) { |
| 197 entry->status_ = BlobStatus::PENDING_DATA_POPULATION; |
| 198 if (CanFinishBuilding(entry)) |
| 199 FinishBuilding(entry); |
| 200 return handle; |
| 201 } |
| 202 |
| 203 // So we need to transport/copy memory. |
| 204 base::Optional<BlobMemoryController::PendingConstructionEntry> |
| 205 pending_construction_entry = |
| 206 memory_controller_.NotifyWhenMemoryCanPopulated( |
| 207 new_memory_needed, |
| 208 base::Bind(&BlobStorageContext::OnEnoughSizeForBlobData, |
| 209 AsWeakPtr(), content.uuid_)); |
| 210 |
| 211 if (pending_construction_entry) { |
| 212 // This means that we're waiting until the memory is available. |
| 213 entry->status_ = BlobStatus::PENDING_MEMORY_QUOTA; |
| 214 building_state->pending_copies_memory_entry = |
| 215 pending_construction_entry.value(); |
| 216 } else { |
| 217 entry->status_ = BlobStatus::PENDING_DATA_POPULATION; |
| 218 BlobStatusCallback& ready_for_user_population = |
| 219 entry->building_state_->ready_for_user_population_callback; |
| 220 if (!ready_for_user_population.is_null()) |
| 221 ready_for_user_population.Run(BlobStatus::PENDING_DATA_POPULATION); |
| 222 } |
| 223 |
| 224 if (CanFinishBuilding(entry)) |
| 225 FinishBuilding(entry); |
| 226 |
| 227 return handle; |
| 228 } |
| 229 |
| 230 void BlobStorageContext::BreakAndFinishPendingBlob(const std::string& uuid, |
| 231 BlobStatus reason) { |
| 232 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 109 DCHECK(entry); | 233 DCHECK(entry); |
| 110 DCHECK(!entry->data.get()) << "Blob already constructed: " | 234 DCHECK(BlobStatusIsError(reason)); |
| 111 << external_builder.uuid(); | 235 ClearAndFreeMemory(uuid, entry); |
| 112 // We want to handle storing our broken blob as well. | 236 entry->status_ = reason; |
| 113 switch (entry->state) { | 237 BlobStatusCallback ready_for_population_callback = |
| 114 case BlobState::PENDING: { | 238 entry->building_state_->ready_for_user_population_callback; |
| 115 entry->data_builder.reset(new InternalBlobData::Builder()); | 239 if (!ready_for_population_callback.is_null()) |
| 116 InternalBlobData::Builder* internal_data_builder = | 240 ready_for_population_callback.Run(reason); |
| 117 entry->data_builder.get(); | 241 entry->items_.clear(); |
| 118 | 242 entry->offsets_.clear(); |
| 119 bool broken = false; | 243 entry->size_ = 0; |
| 120 for (const auto& blob_item : external_builder.items_) { | 244 FinishBuilding(entry); |
| 121 IPCBlobCreationCancelCode error_code; | 245 } |
| 122 if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item, | 246 |
| 123 internal_data_builder, &error_code)) { | 247 void BlobStorageContext::FinishedPopulatingPendingBlob( |
| 124 broken = true; | 248 const std::string& uuid) { |
| 125 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); | 249 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 126 entry->state = BlobState::BROKEN; | 250 DCHECK_EQ(entry->status(), BlobStatus::PENDING_DATA_POPULATION); |
| 127 entry->broken_reason = error_code; | 251 entry->building_state_->waiting_for_user_population = false; |
| 128 entry->data_builder.reset(new InternalBlobData::Builder()); | 252 if (CanFinishBuilding(entry)) |
| 129 break; | 253 FinishBuilding(entry); |
| 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 } | |
| 170 | |
| 171 void BlobStorageContext::CancelPendingBlob(const std::string& uuid, | |
| 172 IPCBlobCreationCancelCode reason) { | |
| 173 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | |
| 174 DCHECK(entry && entry->state == BlobState::PENDING); | |
| 175 entry->state = BlobState::BROKEN; | |
| 176 entry->broken_reason = reason; | |
| 177 CompletePendingBlob(BlobDataBuilder(uuid)); | |
| 178 } | 254 } |
| 179 | 255 |
| 180 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { | 256 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
| 181 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 257 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 182 DCHECK(entry); | 258 DCHECK(entry); |
| 183 ++(entry->refcount); | 259 ++(entry->refcount_); |
| 184 } | 260 } |
| 185 | 261 |
| 186 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { | 262 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
| 187 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 263 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 188 DCHECK(entry); | 264 DCHECK(entry); |
| 189 DCHECK_GT(entry->refcount, 0u); | 265 DCHECK_GT(entry->refcount_, 0u); |
| 190 if (--(entry->refcount) == 0) { | 266 if (--(entry->refcount_) == 0) { |
| 191 size_t memory_freeing = 0; | 267 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); | 268 registry_.DeleteEntry(uuid); |
| 199 } | 269 } |
| 200 } | 270 } |
| 201 | 271 |
| 202 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( | 272 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( |
| 203 const std::string& uuid) { | 273 const std::string& uuid) { |
| 204 std::unique_ptr<BlobDataSnapshot> result; | 274 std::unique_ptr<BlobDataSnapshot> result; |
| 205 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 275 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 206 if (entry->state != BlobState::COMPLETE) { | 276 if (entry->status() != BlobStatus::DONE) |
| 207 return result; | 277 return result; |
| 208 } | |
| 209 | 278 |
| 210 const InternalBlobData& data = *entry->data; | |
| 211 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( | 279 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( |
| 212 uuid, entry->content_type, entry->content_disposition)); | 280 uuid, entry->content_type(), entry->content_disposition())); |
| 213 snapshot->items_.reserve(data.items().size()); | 281 snapshot->items_.reserve(entry->items().size()); |
| 214 for (const auto& shareable_item : data.items()) { | 282 for (const auto& shareable_item : entry->items()) { |
| 215 snapshot->items_.push_back(shareable_item->item()); | 283 snapshot->items_.push_back(shareable_item->item()); |
| 284 if (shareable_item->item()->type() == DataElement::TYPE_BYTES) |
| 285 memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
| 216 } | 286 } |
| 217 return snapshot; | 287 return snapshot; |
| 218 } | 288 } |
| 219 | 289 |
| 220 bool BlobStorageContext::IsBroken(const std::string& uuid) const { | 290 BlobStatus BlobStorageContext::GetBlobStatus(const std::string& uuid) const { |
| 221 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 291 const InternalBlobData* entry = registry_.GetEntry(uuid); |
| 222 if (!entry) { | 292 if (!entry) |
| 223 return true; | 293 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 224 } | 294 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 } | 295 } |
| 235 | 296 |
| 236 void BlobStorageContext::RunOnConstructionComplete( | 297 void BlobStorageContext::RunOnConstructionComplete( |
| 237 const std::string& uuid, | 298 const std::string& uuid, |
| 238 const BlobConstructedCallback& done) { | 299 const BlobStatusCallback& done) { |
| 239 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 300 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 240 DCHECK(entry); | 301 DCHECK(entry); |
| 241 switch (entry->state) { | 302 if (BlobStatusIsPending(entry->status())) { |
| 242 case BlobState::COMPLETE: | 303 entry->building_state_->build_completion_callbacks.push_back(done); |
| 243 done.Run(true, IPCBlobCreationCancelCode::UNKNOWN); | 304 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 } | 305 } |
| 252 NOTREACHED(); | 306 done.Run(entry->status()); |
| 253 } | 307 } |
| 254 | 308 |
| 255 bool BlobStorageContext::AppendAllocatedBlobItem( | 309 std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle( |
| 256 const std::string& target_blob_uuid, | 310 const std::string& uuid, |
| 257 scoped_refptr<BlobDataItem> blob_item, | 311 InternalBlobData* entry) { |
| 258 InternalBlobData::Builder* target_blob_builder, | 312 return base::WrapUnique(new BlobDataHandle( |
| 259 IPCBlobCreationCancelCode* error_code) { | 313 uuid, entry->content_type_, entry->content_disposition_, entry->size_, |
| 260 DCHECK(error_code); | 314 this, base::ThreadTaskRunnerHandle::Get().get())); |
| 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 } | 315 } |
| 356 | 316 |
| 357 bool BlobStorageContext::AppendBlob( | 317 bool BlobStorageContext::CanFinishBuilding(InternalBlobData* entry) { |
| 358 const std::string& target_blob_uuid, | 318 return entry->status_ == BlobStatus::PENDING_DATA_POPULATION && |
| 359 const InternalBlobData& blob, | 319 entry->building_state_->dependent_blobs_building == 0 && |
| 360 uint64_t offset, | 320 !entry->building_state_->waiting_for_user_population; |
| 361 uint64_t length, | 321 } |
| 362 InternalBlobData::Builder* target_blob_builder) { | |
| 363 DCHECK_GT(length, 0ull); | |
| 364 | 322 |
| 365 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items(); | 323 void BlobStorageContext::FinishBuilding(InternalBlobData* entry) { |
| 366 auto iter = items.begin(); | 324 DCHECK(entry); |
| 367 if (offset) { | 325 |
| 368 for (; iter != items.end(); ++iter) { | 326 if (entry->status_ == BlobStatus::PENDING_DATA_POPULATION) { |
| 369 const BlobDataItem& item = *(iter->get()->item()); | 327 for (const ItemCopyEntry& copy : entry->building_state_->copies) { |
| 370 if (offset >= item.length()) | 328 // Our source item can be a file if it was a slice of an unpopulated file, |
| 371 offset -= item.length(); | 329 // or a slice of data that was then paged to disk. |
| 372 else | 330 size_t dest_size = static_cast<size_t>(copy.dest_item->item()->length()); |
| 373 break; | 331 DataElement::Type dest_type = copy.dest_item->item()->type(); |
| 332 switch (copy.source_item->item()->type()) { |
| 333 case DataElement::TYPE_BYTES: { |
| 334 DCHECK_EQ(dest_type, DataElement::TYPE_BYTES_DESCRIPTION); |
| 335 const char* src_data = |
| 336 copy.source_item->item()->bytes() + copy.source_item_offset; |
| 337 copy.dest_item->item()->item_->SetToBytes(src_data, dest_size); |
| 338 } break; |
| 339 case DataElement::TYPE_FILE: { |
| 340 LOG(ERROR) << "Source item has been paged or we started as a file, " |
| 341 "so we're grabbing file ref"; |
| 342 // We've been paged to disk, so free the memory of our temporary item, |
| 343 // and create a new shared item with appropriate offset and length. |
| 344 const DataElement& source_element = |
| 345 copy.source_item->item()->data_element(); |
| 346 std::unique_ptr<DataElement> new_element(new DataElement()); |
| 347 new_element->SetToFilePathRange( |
| 348 source_element.path(), |
| 349 source_element.offset() + copy.source_item_offset, dest_size, |
| 350 source_element.expected_modification_time()); |
| 351 scoped_refptr<BlobDataItem> new_item(new BlobDataItem( |
| 352 std::move(new_element), copy.source_item->item()->data_handle())); |
| 353 copy.dest_item->item_.swap(new_item); |
| 354 // If we expected a memory item (and our source was paged to disk) we |
| 355 // free that memory. |
| 356 if (dest_type == DataElement::TYPE_BYTES_DESCRIPTION) |
| 357 memory_controller_.FreeMemory(dest_size); |
| 358 } break; |
| 359 case DataElement::TYPE_UNKNOWN: |
| 360 case DataElement::TYPE_BLOB: |
| 361 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 362 case DataElement::TYPE_FILE_FILESYSTEM: |
| 363 case DataElement::TYPE_DISK_CACHE_ENTRY: |
| 364 NOTREACHED(); |
| 365 break; |
| 366 } |
| 374 } | 367 } |
| 368 entry->status_ = BlobStatus::DONE; |
| 369 } |
| 370 DCHECK(!BlobStatusIsPending(entry->status())); |
| 371 |
| 372 std::vector<BlobStatusCallback> callbacks; |
| 373 if (entry->building_state_.get()) { |
| 374 std::swap(callbacks, entry->building_state_->build_completion_callbacks); |
| 375 entry->building_state_.reset(); |
| 375 } | 376 } |
| 376 | 377 |
| 377 for (; iter != items.end() && length > 0; ++iter) { | 378 auto runner = base::ThreadTaskRunnerHandle::Get(); |
| 378 scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get(); | 379 for (const auto& callback : callbacks) |
| 379 const BlobDataItem& item = *(shareable_item->item()); | 380 runner->PostTask(FROM_HERE, base::Bind(callback, entry->status())); |
| 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 | 381 |
| 385 bool reusing_blob_item = offset == 0 && new_length == item.length(); | 382 for (const auto& shareable_item : entry->items()) { |
| 386 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item); | 383 DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION, |
| 387 if (reusing_blob_item) { | 384 shareable_item->item()->type()); |
| 388 shareable_item->referencing_blobs().insert(target_blob_uuid); | 385 if (shareable_item->item()->type() != DataElement::TYPE_BYTES) |
| 389 target_blob_builder->AppendSharedBlobItem(shareable_item); | |
| 390 length -= new_length; | |
| 391 continue; | 386 continue; |
| 392 } | 387 memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
| 388 } |
| 389 } |
| 393 | 390 |
| 394 // We need to do copying of the items when we have a different offset or | 391 void BlobStorageContext::OnEnoughSizeForBlobData(const std::string& uuid, |
| 395 // length | 392 bool success) { |
| 396 switch (item.type()) { | 393 if (!success) { |
| 397 case DataElement::TYPE_BYTES: { | 394 BreakAndFinishPendingBlob(uuid, BlobStatus::ERR_OUT_OF_MEMORY); |
| 398 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes", | 395 return; |
| 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 } | 396 } |
| 453 return true; | 397 InternalBlobData* entry = registry_.GetEntry(uuid); |
| 398 if (!entry || !entry->building_state_.get()) |
| 399 return; |
| 400 entry->status_ = BlobStatus::PENDING_DATA_POPULATION; |
| 401 |
| 402 BlobStatusCallback ready_for_user_population = |
| 403 std::move(entry->building_state_->ready_for_user_population_callback); |
| 404 if (!ready_for_user_population.is_null()) |
| 405 ready_for_user_population.Run(BlobStatus::PENDING_DATA_POPULATION); |
| 406 |
| 407 if (CanFinishBuilding(entry)) |
| 408 FinishBuilding(entry); |
| 409 } |
| 410 |
| 411 void BlobStorageContext::OnDependentBlobFinished( |
| 412 const std::string& owning_blob_uuid, |
| 413 BlobStatus status) { |
| 414 InternalBlobData* entry = registry_.GetEntry(owning_blob_uuid); |
| 415 if (!entry) |
| 416 return; |
| 417 |
| 418 if (BlobStatusIsError(status)) { |
| 419 DCHECK_NE(BlobStatus::ERR_BLOB_DEREFERENCED_WHILE_BUILDING, status) |
| 420 << "Referenced blob should never be dereferenced while we " |
| 421 << "are depending on it, as our system holds a handle."; |
| 422 BreakAndFinishPendingBlob(owning_blob_uuid, |
| 423 BlobStatus::ERR_REFERENCED_BLOB_BROKEN); |
| 424 return; |
| 425 } |
| 426 DCHECK_GT(entry->building_state_->dependent_blobs_building, 0u); |
| 427 --entry->building_state_->dependent_blobs_building; |
| 428 |
| 429 if (CanFinishBuilding(entry)) |
| 430 FinishBuilding(entry); |
| 431 } |
| 432 |
| 433 void BlobStorageContext::ClearAndFreeMemory(const std::string& uuid, |
| 434 InternalBlobData* entry) { |
| 435 BlobStatus status = entry->status(); |
| 436 if (status == BlobStatus::PENDING_MEMORY_QUOTA) { |
| 437 memory_controller_.RemovePendingConstructionEntry( |
| 438 entry->building_state_->pending_copies_memory_entry); |
| 439 } else if (status != BlobStatus::PENDING_MEMORY_REQUEST) { |
| 440 // We only free memory if our memory has been accounted for by the memory |
| 441 // manager. |
| 442 memory_controller_.FreeMemory(entry->GetUnsharedMemoryUsage()); |
| 443 } |
| 444 entry->RemoveBlobFromShareableItems(uuid); |
| 445 |
| 446 for (const auto& item_refptr : entry->items()) { |
| 447 if (item_refptr->referencing_blobs().size() == 0) |
| 448 memory_controller_.RemoveBlobItemInRecents(*item_refptr); |
| 449 } |
| 450 entry->items_.clear(); |
| 451 entry->offsets_.clear(); |
| 454 } | 452 } |
| 455 | 453 |
| 456 } // namespace storage | 454 } // namespace storage |
| OLD | NEW |