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