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