OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/blob/blob_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <algorithm> | 7 #include <algorithm> |
11 #include <limits> | 8 #include <limits> |
12 #include <memory> | 9 #include <memory> |
| 10 #include <set> |
13 #include <utility> | 11 #include <utility> |
14 | 12 |
15 #include "base/bind.h" | 13 #include "base/bind.h" |
16 #include "base/callback.h" | 14 #include "base/callback.h" |
17 #include "base/location.h" | 15 #include "base/location.h" |
18 #include "base/logging.h" | 16 #include "base/logging.h" |
19 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
20 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
21 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/numerics/safe_conversions.h" |
| 21 #include "base/numerics/safe_math.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
24 #include "storage/browser/blob/blob_data_builder.h" | 24 #include "storage/browser/blob/blob_data_builder.h" |
25 #include "storage/browser/blob/blob_data_handle.h" | 25 #include "storage/browser/blob/blob_data_handle.h" |
26 #include "storage/browser/blob/blob_data_item.h" | 26 #include "storage/browser/blob/blob_data_item.h" |
27 #include "storage/browser/blob/blob_data_snapshot.h" | 27 #include "storage/browser/blob/blob_data_snapshot.h" |
| 28 #include "storage/browser/blob/blob_flattener.h" |
28 #include "storage/browser/blob/shareable_blob_data_item.h" | 29 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 30 #include "storage/browser/blob/blob_slice.h" |
29 #include "url/gurl.h" | 31 #include "url/gurl.h" |
30 | 32 |
31 namespace storage { | 33 namespace storage { |
| 34 using ItemCopyEntry = BlobStorageRegistry::ItemCopyEntry; |
| 35 |
| 36 namespace { |
| 37 |
| 38 IPCBlobCreationCancelCode ConvertReferencedBlobErrorToConstructingError( |
| 39 IPCBlobCreationCancelCode referenced_blob_error) { |
| 40 switch (referenced_blob_error) { |
| 41 // For most cases we propagate the error. |
| 42 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: |
| 43 case IPCBlobCreationCancelCode::SOURCE_DIED_IN_TRANSIT: |
| 44 case IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN: |
| 45 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: |
| 46 return referenced_blob_error; |
| 47 // Others we report that the referenced blob is broken, as we don't know |
| 48 // why (the BLOB_DEREFERENCED_WHILE_BUILDING should never happen, as we hold |
| 49 // onto the reference of the blobs we're using). |
| 50 case IPCBlobCreationCancelCode::BLOB_DEREFERENCED_WHILE_BUILDING: |
| 51 DCHECK(false) << "Referenced blob should never be dereferenced while we " |
| 52 << "are depending on it, as our system holds a handle."; |
| 53 case IPCBlobCreationCancelCode::UNKNOWN: |
| 54 return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; |
| 55 } |
| 56 NOTREACHED(); |
| 57 return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
32 using BlobRegistryEntry = BlobStorageRegistry::Entry; | 62 using BlobRegistryEntry = BlobStorageRegistry::Entry; |
33 using BlobState = BlobStorageRegistry::BlobState; | 63 using BlobState = BlobStorageRegistry::BlobState; |
34 | 64 |
35 BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} | 65 BlobStorageContext::BlobStorageContext() {} |
36 | 66 |
37 BlobStorageContext::~BlobStorageContext() { | 67 BlobStorageContext::~BlobStorageContext() { |
38 } | 68 } |
39 | 69 |
40 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( | 70 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
41 const std::string& uuid) { | 71 const std::string& uuid) { |
42 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 72 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
43 if (!entry) { | 73 if (!entry) { |
44 return nullptr; | 74 return nullptr; |
45 } | 75 } |
46 return base::WrapUnique( | 76 return CreateHandle(uuid, entry); |
47 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, | |
48 this, base::ThreadTaskRunnerHandle::Get().get())); | |
49 } | 77 } |
50 | 78 |
51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( | 79 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
52 const GURL& url) { | 80 const GURL& url) { |
53 std::string uuid; | 81 std::string uuid; |
54 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); | 82 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); |
55 if (!entry) { | 83 if (!entry) { |
56 return nullptr; | 84 return nullptr; |
57 } | 85 } |
58 return base::WrapUnique( | 86 return CreateHandle(uuid, entry); |
59 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, | |
60 this, base::ThreadTaskRunnerHandle::Get().get())); | |
61 } | 87 } |
62 | 88 |
63 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 89 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
64 const BlobDataBuilder& external_builder) { | 90 const BlobDataBuilder& external_builder) { |
65 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); | 91 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); |
66 CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, | 92 |
67 external_builder.content_disposition_); | 93 return BuildBlob(external_builder, false); |
68 CompletePendingBlob(external_builder); | |
69 std::unique_ptr<BlobDataHandle> handle = | |
70 GetBlobDataFromUUID(external_builder.uuid_); | |
71 DecrementBlobRefCount(external_builder.uuid_); | |
72 return handle; | |
73 } | 94 } |
74 | 95 |
75 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 96 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
76 const BlobDataBuilder* builder) { | 97 const BlobDataBuilder* builder) { |
77 DCHECK(builder); | 98 DCHECK(builder); |
78 return AddFinishedBlob(*builder); | 99 return AddFinishedBlob(*builder); |
79 } | 100 } |
80 | 101 |
81 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, | 102 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, |
82 const std::string& uuid) { | 103 const std::string& uuid) { |
83 if (!registry_.CreateUrlMapping(blob_url, uuid)) { | 104 if (!registry_.CreateUrlMapping(blob_url, uuid)) { |
84 return false; | 105 return false; |
85 } | 106 } |
86 IncrementBlobRefCount(uuid); | 107 IncrementBlobRefCount(uuid); |
87 return true; | 108 return true; |
88 } | 109 } |
89 | 110 |
90 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { | 111 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
91 std::string uuid; | 112 std::string uuid; |
92 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { | 113 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { |
93 return; | 114 return; |
94 } | 115 } |
95 DecrementBlobRefCount(uuid); | 116 DecrementBlobRefCount(uuid); |
96 } | 117 } |
97 | 118 |
98 void BlobStorageContext::CreatePendingBlob( | 119 void BlobStorageContext::EnableDisk( |
| 120 const base::FilePath storage_directory, |
| 121 scoped_refptr<base::SingleThreadTaskRunner> file_runner) { |
| 122 memory_controller_.EnableDisk(storage_directory, std::move(file_runner)); |
| 123 } |
| 124 |
| 125 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBrokenBlob( |
99 const std::string& uuid, | 126 const std::string& uuid, |
100 const std::string& content_type, | 127 const std::string& content_type, |
101 const std::string& content_disposition) { | 128 const std::string& content_disposition, |
102 DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); | 129 IPCBlobCreationCancelCode reason) { |
103 registry_.CreateEntry(uuid, content_type, content_disposition); | 130 DCHECK(!registry_.HasEntry(uuid)); |
104 } | 131 BlobRegistryEntry* entry = |
105 | 132 registry_.CreateEntry(uuid, content_type, content_disposition); |
106 void BlobStorageContext::CompletePendingBlob( | 133 entry->state = BlobState::BROKEN; |
107 const BlobDataBuilder& external_builder) { | 134 FinishBuilding(entry); |
108 BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); | 135 return CreateHandle(uuid, entry); |
109 DCHECK(entry); | 136 } |
110 DCHECK(!entry->data.get()) << "Blob already constructed: " | 137 |
111 << external_builder.uuid(); | 138 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob( |
112 // We want to handle storing our broken blob as well. | 139 const BlobDataBuilder& content, |
113 switch (entry->state) { | 140 bool content_pending) { |
114 case BlobState::PENDING: { | 141 DCHECK(!registry_.HasEntry(content.uuid_)); |
115 entry->data_builder.reset(new InternalBlobData::Builder()); | 142 |
116 InternalBlobData::Builder* internal_data_builder = | 143 BlobRegistryEntry* entry = registry_.CreateEntry( |
117 entry->data_builder.get(); | 144 content.uuid_, content.content_type_, content.content_disposition_); |
118 | 145 |
119 bool broken = false; | 146 entry->waiting_until_user_population = content_pending; |
120 for (const auto& blob_item : external_builder.items_) { | 147 |
121 IPCBlobCreationCancelCode error_code; | 148 base::CheckedNumeric<uint64_t> total_size = 0; |
122 if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item, | 149 base::CheckedNumeric<size_t> new_memory_size = 0; |
123 internal_data_builder, &error_code)) { | 150 base::CheckedNumeric<size_t> copying_size = 0; |
124 broken = true; | 151 const std::vector<scoped_refptr<BlobDataItem>>& items = content.items_; |
125 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); | 152 |
126 entry->state = BlobState::BROKEN; | 153 InternalBlobData& data = entry->data; |
127 entry->broken_reason = error_code; | 154 |
128 entry->data_builder.reset(new InternalBlobData::Builder()); | 155 std::map<std::string, BlobSlice> slice_map; |
129 break; | 156 |
| 157 std::set<std::string> dependent_blob_uuids; |
| 158 std::vector<BlobRegistryEntry*> pending_dependent_blobs; |
| 159 |
| 160 bool all_dependant_blobs_built = true; |
| 161 |
| 162 // First we calculate the size. |
| 163 for (const scoped_refptr<BlobDataItem>& item : items) { |
| 164 uint64_t length = item->length(); |
| 165 DCHECK(item->type() != DataElement::TYPE_BYTES_DESCRIPTION || |
| 166 content_pending); |
| 167 DCHECK(item->type() != DataElement::TYPE_BYTES || !content_pending); |
| 168 |
| 169 total_size += length; |
| 170 if (item->type() == DataElement::TYPE_BYTES) { |
| 171 DCHECK_NE(length, std::numeric_limits<uint64_t>::max()); |
| 172 DCHECK(!content_pending); |
| 173 new_memory_size += item->length(); |
| 174 } else if (item->type() == DataElement::TYPE_BLOB) { |
| 175 const std::string& ref_blob_uuid = item->blob_uuid(); |
| 176 if (ref_blob_uuid == content.uuid_) { |
| 177 BreakAndFinishBlob(content.uuid_, |
| 178 IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN); |
| 179 return CreateHandle(content.uuid_, entry); |
| 180 } |
| 181 BlobRegistryEntry* ref_entry = registry_.GetEntry(ref_blob_uuid); |
| 182 DCHECK(ref_entry); |
| 183 |
| 184 if (ref_entry->state == BlobState::BROKEN) { |
| 185 BreakAndFinishBlob(content.uuid_, |
| 186 IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN); |
| 187 return CreateHandle(content.uuid_, entry); |
| 188 } |
| 189 |
| 190 if (dependent_blob_uuids.find(ref_blob_uuid) == |
| 191 dependent_blob_uuids.end()) { |
| 192 entry->dependent_blobs.emplace_back(GetBlobDataFromUUID(ref_blob_uuid)); |
| 193 dependent_blob_uuids.insert(ref_blob_uuid); |
| 194 if (ref_entry->state != BlobState::COMPLETE) { |
| 195 all_dependant_blobs_built = false; |
| 196 pending_dependent_blobs.push_back(ref_entry); |
130 } | 197 } |
131 } | 198 } |
132 entry->data = entry->data_builder->Build(); | 199 |
133 entry->data_builder.reset(); | 200 const InternalBlobData& source = ref_entry->data; |
134 entry->state = broken ? BlobState::BROKEN : BlobState::COMPLETE; | 201 BlobSlice slice(source, item->offset(), item->length(), |
135 break; | 202 ref_entry->state != BlobState::PENDING); |
136 } | 203 new_memory_size += slice.copying_memory_size(); |
137 case BlobState::BROKEN: { | 204 copying_size += slice.copying_memory_size(); |
138 InternalBlobData::Builder builder; | 205 slice_map.insert(std::make_pair(item->blob_uuid(), std::move(slice))); |
139 entry->data = builder.Build(); | 206 } |
140 break; | 207 } |
141 } | 208 entry->dependent_blobs_building = pending_dependent_blobs.size(); |
142 case BlobState::COMPLETE: | 209 data.copying_size_ = copying_size.ValueOrDie(); |
143 DCHECK(false) << "Blob already constructed: " << external_builder.uuid(); | 210 |
144 return; | 211 std::vector<BlobStorageRegistry::ItemCopyEntry> copies_from_pending_reference; |
145 } | 212 BlobFlattener::FlattenBlob( |
146 | 213 content.uuid_, &(entry->data), &entry->copies_from_built_reference, |
147 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); | 214 &copies_from_pending_reference, std::move(slice_map), content); |
148 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.Broken", | 215 |
149 entry->state == BlobState::BROKEN); | 216 for (BlobStorageRegistry::ItemCopyEntry& copy : |
150 if (entry->state == BlobState::BROKEN) { | 217 copies_from_pending_reference) { |
151 UMA_HISTOGRAM_ENUMERATION( | 218 BlobRegistryEntry* ref_entry = registry_.GetEntry(copy.uuid); |
152 "Storage.Blob.BrokenReason", static_cast<int>(entry->broken_reason), | 219 copy.uuid = content.uuid_; |
153 (static_cast<int>(IPCBlobCreationCancelCode::LAST) + 1)); | 220 DCHECK(ref_entry->state == BlobState::PENDING); |
154 } | 221 ref_entry->copies_to_when_built.push_back(copy); |
155 size_t total_memory = 0, nonshared_memory = 0; | 222 } |
156 entry->data->GetMemoryUsage(&total_memory, &nonshared_memory); | 223 |
157 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize", total_memory / 1024); | 224 std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry); |
158 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize", | 225 DecrementBlobRefCount(content.uuid_); |
159 nonshared_memory / 1024); | 226 |
160 TRACE_COUNTER1("Blob", "MemoryStoreUsageBytes", memory_usage_); | 227 if (!all_dependant_blobs_built) { |
| 228 for (BlobRegistryEntry* entry : pending_dependent_blobs) { |
| 229 entry->build_completion_callbacks.push_back( |
| 230 base::Bind(&BlobStorageContext::OnDependentBlobFinished, AsWeakPtr(), |
| 231 content.uuid_)); |
| 232 } |
| 233 } |
| 234 |
| 235 if (!memory_controller_.MaybeFitInMemoryNow(data.copying_size_ + |
| 236 new_memory_size.ValueOrDie())) { |
| 237 entry->can_fit_copies = false; |
| 238 entry->pending_copies_memory_entry = |
| 239 memory_controller_.NotifyWhenMemoryCanPopulated( |
| 240 data.copying_size_, |
| 241 base::Bind(&BlobStorageContext::OnEnoughSizeForDependentCopies, |
| 242 AsWeakPtr(), content.uuid_)); |
| 243 } |
| 244 |
| 245 if (CanFinishBuilding(entry)) { |
| 246 FinishBuilding(entry); |
| 247 } |
| 248 |
| 249 return handle; |
| 250 } |
| 251 |
| 252 void BlobStorageContext::BreakAndFinishBlob(const std::string& uuid, |
| 253 IPCBlobCreationCancelCode reason) { |
| 254 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
| 255 DCHECK(entry); |
| 256 ClearAndFreeMemory(uuid, entry); |
| 257 entry->copies_from_built_reference.clear(); |
| 258 entry->copies_to_when_built.clear(); |
| 259 entry->dependent_blobs.clear(); |
| 260 entry->state = BlobState::BROKEN; |
| 261 entry->data.items_.clear(); |
| 262 entry->data.offsets_.clear(); |
| 263 entry->data.size_ = 0; |
| 264 entry->broken_reason = reason; |
| 265 entry->waiting_until_user_population = false; |
| 266 entry->can_fit_copies = true; |
| 267 FinishBuilding(entry); |
| 268 } |
| 269 |
| 270 void BlobStorageContext::FinishedPopulatingBlob(const std::string& uuid) { |
| 271 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
| 272 entry->waiting_until_user_population = false; |
| 273 if (CanFinishBuilding(entry)) { |
| 274 FinishBuilding(entry); |
| 275 } |
| 276 } |
| 277 |
| 278 static uint64_t sNextItemId = 0; |
| 279 |
| 280 /* static */ |
| 281 uint64_t BlobStorageContext::GetAndIncrementItemId() { |
| 282 return sNextItemId++; |
| 283 } |
| 284 |
| 285 std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle( |
| 286 const std::string& uuid, |
| 287 BlobRegistryEntry* entry) { |
| 288 return base::WrapUnique(new BlobDataHandle( |
| 289 uuid, entry->content_type, entry->content_disposition, entry->data.size_, |
| 290 this, base::ThreadTaskRunnerHandle::Get().get())); |
| 291 } |
| 292 |
| 293 bool BlobStorageContext::CanFinishBuilding(BlobRegistryEntry* entry) { |
| 294 return entry->dependent_blobs_building == 0 && entry->can_fit_copies && |
| 295 !entry->waiting_until_user_population; |
| 296 } |
| 297 |
| 298 void BlobStorageContext::FinishBuilding(BlobRegistryEntry* entry) { |
| 299 DCHECK(entry); |
| 300 |
| 301 if (entry->state == BlobState::PENDING) { |
| 302 LOG(ERROR) << "copying from other blobs"; |
| 303 for (const ItemCopyEntry& copy : entry->copies_from_built_reference) { |
| 304 PerformCopy(copy, ®istry_.GetEntry(copy.uuid)->data, &entry->data); |
| 305 } |
| 306 |
| 307 LOG(ERROR) << "copying to other blobs"; |
| 308 for (const ItemCopyEntry& copy : entry->copies_to_when_built) { |
| 309 PerformCopy(copy, &entry->data, ®istry_.GetEntry(copy.uuid)->data); |
| 310 } |
| 311 |
| 312 entry->state = BlobState::COMPLETE; |
| 313 } |
| 314 entry->dependent_blobs.clear(); |
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, |
165 base::Bind(callback, entry->state == BlobState::COMPLETE, | 319 base::Bind(callback, entry->state == BlobState::COMPLETE, |
166 entry->broken_reason)); | 320 entry->broken_reason)); |
167 } | 321 } |
168 entry->build_completion_callbacks.clear(); | 322 entry->build_completion_callbacks.clear(); |
| 323 |
| 324 LOG(ERROR) << "updating recent items"; |
| 325 for (const auto& shareable_item : entry->data.items_) { |
| 326 DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION, |
| 327 shareable_item->item()->type()); |
| 328 if (shareable_item->item()->type() != DataElement::TYPE_BYTES) |
| 329 continue; |
| 330 memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
| 331 } |
169 } | 332 } |
170 | 333 |
171 void BlobStorageContext::CancelPendingBlob(const std::string& uuid, | 334 void BlobStorageContext::OnEnoughSizeForDependentCopies(const std::string& uuid, |
172 IPCBlobCreationCancelCode reason) { | 335 bool success) { |
| 336 if (!success) { |
| 337 BreakAndFinishBlob(uuid, IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
| 338 return; |
| 339 } |
173 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 340 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
174 DCHECK(entry && entry->state == BlobState::PENDING); | 341 if (entry == nullptr) { |
175 entry->state = BlobState::BROKEN; | 342 return; |
176 entry->broken_reason = reason; | 343 } |
177 CompletePendingBlob(BlobDataBuilder(uuid)); | 344 entry->can_fit_copies = true; |
| 345 if (CanFinishBuilding(entry)) { |
| 346 FinishBuilding(entry); |
| 347 } |
| 348 } |
| 349 |
| 350 void BlobStorageContext::OnDependentBlobFinished( |
| 351 const std::string& owning_blob_uuid, |
| 352 bool construction_success, |
| 353 IPCBlobCreationCancelCode reason) { |
| 354 BlobRegistryEntry* entry = registry_.GetEntry(owning_blob_uuid); |
| 355 if (entry == nullptr) { |
| 356 return; |
| 357 } |
| 358 if (!construction_success) { |
| 359 BreakAndFinishBlob(owning_blob_uuid, |
| 360 ConvertReferencedBlobErrorToConstructingError(reason)); |
| 361 return; |
| 362 } |
| 363 DCHECK_GT(entry->dependent_blobs_building, 0u); |
| 364 --entry->dependent_blobs_building; |
| 365 if (CanFinishBuilding(entry)) { |
| 366 FinishBuilding(entry); |
| 367 } |
| 368 } |
| 369 |
| 370 void BlobStorageContext::ClearAndFreeMemory(const std::string& uuid, |
| 371 BlobRegistryEntry* entry) { |
| 372 if (entry->state == BlobState::PENDING) { |
| 373 if (!entry->can_fit_copies) { |
| 374 memory_controller_.RemovePendingConstructionEntry( |
| 375 entry->pending_copies_memory_entry); |
| 376 } |
| 377 entry->dependent_blobs.clear(); |
| 378 } |
| 379 memory_controller_.FreeMemory(entry->data.GetUnsharedMemoryUsage()); |
| 380 entry->data.RemoveBlobFromShareableItems(uuid); |
| 381 for (const auto& item_refptr : entry->data.items_) { |
| 382 if (item_refptr->referencing_blobs().size() == 0) { |
| 383 memory_controller_.RemoveBlobItemInRecents(item_refptr->item_id()); |
| 384 } |
| 385 } |
| 386 entry->data.items_.clear(); |
| 387 entry->data.offsets_.clear(); |
178 } | 388 } |
179 | 389 |
180 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { | 390 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
181 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 391 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
182 DCHECK(entry); | 392 DCHECK(entry); |
183 ++(entry->refcount); | 393 ++(entry->refcount); |
184 } | 394 } |
185 | 395 |
186 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { | 396 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
187 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 397 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
188 DCHECK(entry); | 398 DCHECK(entry); |
189 DCHECK_GT(entry->refcount, 0u); | 399 DCHECK_GT(entry->refcount, 0u); |
190 if (--(entry->refcount) == 0) { | 400 if (--(entry->refcount) == 0) { |
191 size_t memory_freeing = 0; | 401 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); | 402 registry_.DeleteEntry(uuid); |
199 } | 403 } |
200 } | 404 } |
201 | 405 |
202 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( | 406 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( |
203 const std::string& uuid) { | 407 const std::string& uuid) { |
204 std::unique_ptr<BlobDataSnapshot> result; | 408 std::unique_ptr<BlobDataSnapshot> result; |
205 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 409 BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
206 if (entry->state != BlobState::COMPLETE) { | 410 if (entry->state != BlobState::COMPLETE) { |
207 return result; | 411 return result; |
208 } | 412 } |
209 | 413 |
210 const InternalBlobData& data = *entry->data; | 414 const InternalBlobData& data = entry->data; |
211 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( | 415 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( |
212 uuid, entry->content_type, entry->content_disposition)); | 416 uuid, entry->content_type, entry->content_disposition)); |
213 snapshot->items_.reserve(data.items().size()); | 417 snapshot->items_.reserve(data.items().size()); |
214 for (const auto& shareable_item : data.items()) { | 418 for (const auto& shareable_item : data.items()) { |
215 snapshot->items_.push_back(shareable_item->item()); | 419 snapshot->items_.push_back(shareable_item->item()); |
| 420 memory_controller_.UpdateBlobItemInRecents(shareable_item.get()); |
216 } | 421 } |
217 return snapshot; | 422 return snapshot; |
218 } | 423 } |
219 | 424 |
220 bool BlobStorageContext::IsBroken(const std::string& uuid) const { | 425 bool BlobStorageContext::IsBroken(const std::string& uuid) const { |
221 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 426 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); |
222 if (!entry) { | 427 if (!entry) { |
223 return true; | 428 return true; |
224 } | 429 } |
225 return entry->state == BlobState::BROKEN; | 430 return entry->state == BlobState::BROKEN; |
(...skipping 19 matching lines...) Expand all Loading... |
245 case BlobState::BROKEN: | 450 case BlobState::BROKEN: |
246 done.Run(false, entry->broken_reason); | 451 done.Run(false, entry->broken_reason); |
247 return; | 452 return; |
248 case BlobState::PENDING: | 453 case BlobState::PENDING: |
249 entry->build_completion_callbacks.push_back(done); | 454 entry->build_completion_callbacks.push_back(done); |
250 return; | 455 return; |
251 } | 456 } |
252 NOTREACHED(); | 457 NOTREACHED(); |
253 } | 458 } |
254 | 459 |
255 bool BlobStorageContext::AppendAllocatedBlobItem( | 460 void BlobStorageContext::UpdateItemsInRecents(BlobRegistryEntry* entry) { |
256 const std::string& target_blob_uuid, | 461 for (const auto& item_refptr : entry->data.items_) { |
257 scoped_refptr<BlobDataItem> blob_item, | 462 memory_controller_.UpdateBlobItemInRecents(item_refptr.get()); |
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 } | 463 } |
352 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", | |
353 memory_usage_ / 1024); | |
354 return !error; | |
355 } | 464 } |
356 | 465 |
357 bool BlobStorageContext::AppendBlob( | 466 void BlobStorageContext::PerformCopy(const ItemCopyEntry& copy, |
358 const std::string& target_blob_uuid, | 467 InternalBlobData* source, |
359 const InternalBlobData& blob, | 468 InternalBlobData* destination) { |
360 uint64_t offset, | 469 const auto& source_item = source->items()[copy.source_item_index]; |
361 uint64_t length, | 470 const auto& dest_item = destination->items()[copy.dest_item_index]; |
362 InternalBlobData::Builder* target_blob_builder) { | |
363 DCHECK_GT(length, 0ull); | |
364 | 471 |
365 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items(); | 472 DCHECK_EQ(source_item->item()->type(), DataElement::TYPE_BYTES); |
366 auto iter = items.begin(); | 473 DCHECK_EQ(dest_item->item()->type(), DataElement::TYPE_BYTES_DESCRIPTION) |
367 if (offset) { | 474 << copy.dest_item_index; |
368 for (; iter != items.end(); ++iter) { | 475 const char* src_data = source_item->item()->bytes() + copy.source_item_offset; |
369 const BlobDataItem& item = *(iter->get()->item()); | 476 dest_item->item()->item_->SetToBytes(src_data, copy.size); |
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 } | 477 } |
455 | 478 |
456 } // namespace storage | 479 } // namespace storage |
OLD | NEW |