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