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