OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "content/browser/service_worker/service_worker_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
6 | 6 |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
529 return make_scoped_ptr( | 529 return make_scoped_ptr( |
530 new ServiceWorkerResponseWriter(response_id, disk_cache())); | 530 new ServiceWorkerResponseWriter(response_id, disk_cache())); |
531 } | 531 } |
532 | 532 |
533 scoped_ptr<ServiceWorkerResponseMetadataWriter> | 533 scoped_ptr<ServiceWorkerResponseMetadataWriter> |
534 ServiceWorkerStorage::CreateResponseMetadataWriter(int64 response_id) { | 534 ServiceWorkerStorage::CreateResponseMetadataWriter(int64 response_id) { |
535 return make_scoped_ptr( | 535 return make_scoped_ptr( |
536 new ServiceWorkerResponseMetadataWriter(response_id, disk_cache())); | 536 new ServiceWorkerResponseMetadataWriter(response_id, disk_cache())); |
537 } | 537 } |
538 | 538 |
539 void ServiceWorkerStorage::StoreUncommittedResponseId(int64 id) { | 539 void ServiceWorkerStorage::StoreUncommittedResourceId(int64 resource_id) { |
540 DCHECK_NE(kInvalidServiceWorkerResponseId, id); | 540 DCHECK_NE(kInvalidServiceWorkerResponseId, resource_id); |
falken
2015/10/15 05:03:40
kInvalidServiceWorkerResourceId?
probably we want
nhiroki
2015/10/16 09:15:40
Yeah, I'll make a follow-up patch to deprecate kIn
| |
541 DCHECK_EQ(INITIALIZED, state_); | 541 DCHECK_EQ(INITIALIZED, state_); |
542 | 542 |
543 if (!has_checked_for_stale_resources_) | 543 if (!has_checked_for_stale_resources_) |
544 DeleteStaleResources(); | 544 DeleteStaleResources(); |
545 | 545 |
546 database_task_manager_->GetTaskRunner()->PostTask( | 546 PostTaskAndReplyWithResult( |
547 FROM_HERE, | 547 database_task_manager_->GetTaskRunner(), FROM_HERE, |
548 base::Bind(base::IgnoreResult( | 548 base::Bind(&ServiceWorkerDatabase::WriteUncommittedResourceIds, |
549 &ServiceWorkerDatabase::WriteUncommittedResourceIds), | 549 base::Unretained(database_.get()), |
550 base::Unretained(database_.get()), | 550 std::set<int64>(&resource_id, &resource_id + 1)), |
551 std::set<int64>(&id, &id + 1))); | 551 base::Bind(&ServiceWorkerStorage::DidWriteUncommittedResourceIds, |
552 weak_factory_.GetWeakPtr())); | |
552 } | 553 } |
553 | 554 |
554 void ServiceWorkerStorage::DoomUncommittedResponse(int64 id) { | 555 void ServiceWorkerStorage::DoomUncommittedResource(int64 resource_id) { |
555 DCHECK_NE(kInvalidServiceWorkerResponseId, id); | 556 DCHECK_NE(kInvalidServiceWorkerResponseId, resource_id); |
556 database_task_manager_->GetTaskRunner()->PostTask( | 557 DoomUncommittedResources(std::set<int64>(&resource_id, &resource_id + 1)); |
557 FROM_HERE, | 558 } |
558 base::Bind(base::IgnoreResult( | 559 |
559 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), | 560 void ServiceWorkerStorage::DoomUncommittedResources( |
560 base::Unretained(database_.get()), | 561 const std::set<int64>& resource_ids) { |
561 std::set<int64>(&id, &id + 1))); | 562 PostTaskAndReplyWithResult( |
562 StartPurgingResources(std::vector<int64>(1, id)); | 563 database_task_manager_->GetTaskRunner(), FROM_HERE, |
564 base::Bind(&ServiceWorkerDatabase::PurgeUncommittedResourceIds, | |
565 base::Unretained(database_.get()), resource_ids), | |
566 base::Bind(&ServiceWorkerStorage::DidPurgeUncommittedResourceIds, | |
567 weak_factory_.GetWeakPtr(), resource_ids)); | |
563 } | 568 } |
564 | 569 |
565 void ServiceWorkerStorage::StoreUserData( | 570 void ServiceWorkerStorage::StoreUserData( |
566 int64 registration_id, | 571 int64 registration_id, |
567 const GURL& origin, | 572 const GURL& origin, |
568 const std::string& key, | 573 const std::string& key, |
569 const std::string& data, | 574 const std::string& data, |
570 const StatusCallback& callback) { | 575 const StatusCallback& callback) { |
571 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 576 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
572 if (IsDisabled()) { | 577 if (IsDisabled()) { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
722 | 727 |
723 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( | 728 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( |
724 ServiceWorkerRegistration* registration, | 729 ServiceWorkerRegistration* registration, |
725 ServiceWorkerVersion* version, | 730 ServiceWorkerVersion* version, |
726 ServiceWorkerStatusCode status) { | 731 ServiceWorkerStatusCode status) { |
727 installing_registrations_.erase(registration->id()); | 732 installing_registrations_.erase(registration->id()); |
728 if (status != SERVICE_WORKER_OK && version) { | 733 if (status != SERVICE_WORKER_OK && version) { |
729 ResourceList resources; | 734 ResourceList resources; |
730 version->script_cache_map()->GetResources(&resources); | 735 version->script_cache_map()->GetResources(&resources); |
731 | 736 |
732 std::set<int64> ids; | 737 std::set<int64> resource_ids; |
733 for (const auto& resource : resources) | 738 for (const auto& resource : resources) |
734 ids.insert(resource.resource_id); | 739 resource_ids.insert(resource.resource_id); |
735 | 740 DoomUncommittedResources(resource_ids); |
736 database_task_manager_->GetTaskRunner()->PostTask( | |
737 FROM_HERE, | |
738 base::Bind(base::IgnoreResult( | |
739 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), | |
740 base::Unretained(database_.get()), | |
741 ids)); | |
742 } | 741 } |
743 } | 742 } |
744 | 743 |
745 void ServiceWorkerStorage::NotifyUninstallingRegistration( | 744 void ServiceWorkerStorage::NotifyUninstallingRegistration( |
746 ServiceWorkerRegistration* registration) { | 745 ServiceWorkerRegistration* registration) { |
747 DCHECK(uninstalling_registrations_.find(registration->id()) == | 746 DCHECK(uninstalling_registrations_.find(registration->id()) == |
748 uninstalling_registrations_.end()); | 747 uninstalling_registrations_.end()); |
749 uninstalling_registrations_[registration->id()] = registration; | 748 uninstalling_registrations_[registration->id()] = registration; |
750 } | 749 } |
751 | 750 |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1145 -deleted_version.resources_total_size_bytes); | 1144 -deleted_version.resources_total_size_bytes); |
1146 } | 1145 } |
1147 if (origin_is_deletable) | 1146 if (origin_is_deletable) |
1148 registered_origins_.erase(params.origin); | 1147 registered_origins_.erase(params.origin); |
1149 params.callback.Run(SERVICE_WORKER_OK); | 1148 params.callback.Run(SERVICE_WORKER_OK); |
1150 | 1149 |
1151 if (!context_->GetLiveVersion(deleted_version.version_id)) | 1150 if (!context_->GetLiveVersion(deleted_version.version_id)) |
1152 StartPurgingResources(newly_purgeable_resources); | 1151 StartPurgingResources(newly_purgeable_resources); |
1153 } | 1152 } |
1154 | 1153 |
1154 void ServiceWorkerStorage::DidWriteUncommittedResourceIds( | |
1155 ServiceWorkerDatabase::Status status) { | |
1156 if (status != ServiceWorkerDatabase::STATUS_OK) | |
falken
2015/10/15 05:03:40
Do we have/want UMA for delete and start over freq
nhiroki
2015/10/16 09:15:40
What does "frequency" means? The ratio of DeleteAn
falken
2015/10/19 04:09:35
Yea... I didn't have something specific in mind, j
nhiroki
2015/10/19 09:50:42
We'd like to monitor two things: (1) how many user
| |
1157 ScheduleDeleteAndStartOver(); | |
1158 } | |
1159 | |
1160 void ServiceWorkerStorage::DidPurgeUncommittedResourceIds( | |
1161 const std::set<int64>& resource_ids, | |
1162 ServiceWorkerDatabase::Status status) { | |
1163 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
1164 ScheduleDeleteAndStartOver(); | |
1165 return; | |
1166 } | |
1167 StartPurgingResources(resource_ids); | |
1168 } | |
1169 | |
1155 void ServiceWorkerStorage::DidStoreUserData( | 1170 void ServiceWorkerStorage::DidStoreUserData( |
1156 const StatusCallback& callback, | 1171 const StatusCallback& callback, |
1157 ServiceWorkerDatabase::Status status) { | 1172 ServiceWorkerDatabase::Status status) { |
1158 // |status| can be NOT_FOUND when the associated registration did not exist in | 1173 // |status| can be NOT_FOUND when the associated registration did not exist in |
1159 // the database. In the case, we don't have to schedule the corruption | 1174 // the database. In the case, we don't have to schedule the corruption |
1160 // recovery. | 1175 // recovery. |
1161 if (status != ServiceWorkerDatabase::STATUS_OK && | 1176 if (status != ServiceWorkerDatabase::STATUS_OK && |
1162 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1177 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
1163 ScheduleDeleteAndStartOver(); | 1178 ScheduleDeleteAndStartOver(); |
1164 } | 1179 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1381 void ServiceWorkerStorage::DeleteOldDiskCache() { | 1396 void ServiceWorkerStorage::DeleteOldDiskCache() { |
1382 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 1397 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
1383 if (IsDisabled()) | 1398 if (IsDisabled()) |
1384 return; | 1399 return; |
1385 database_task_manager_->GetTaskRunner()->PostTask( | 1400 database_task_manager_->GetTaskRunner()->PostTask( |
1386 FROM_HERE, base::Bind(&ServiceWorkerStorage::DeleteOldDiskCacheInDB, | 1401 FROM_HERE, base::Bind(&ServiceWorkerStorage::DeleteOldDiskCacheInDB, |
1387 database_.get(), GetOldDiskCachePath())); | 1402 database_.get(), GetOldDiskCachePath())); |
1388 } | 1403 } |
1389 | 1404 |
1390 void ServiceWorkerStorage::StartPurgingResources( | 1405 void ServiceWorkerStorage::StartPurgingResources( |
1391 const std::vector<int64>& ids) { | 1406 const std::set<int64>& resource_ids) { |
1392 DCHECK(has_checked_for_stale_resources_); | 1407 DCHECK(has_checked_for_stale_resources_); |
1393 for (const auto& id : ids) | 1408 for (int64 resource_id : resource_ids) |
1394 purgeable_resource_ids_.push_back(id); | 1409 purgeable_resource_ids_.push_back(resource_id); |
1395 ContinuePurgingResources(); | 1410 ContinuePurgingResources(); |
1396 } | 1411 } |
1397 | 1412 |
1413 void ServiceWorkerStorage::StartPurgingResources( | |
1414 const std::vector<int64>& resource_ids) { | |
1415 DCHECK(has_checked_for_stale_resources_); | |
1416 for (int64 resource_id : resource_ids) | |
1417 purgeable_resource_ids_.push_back(resource_id); | |
1418 ContinuePurgingResources(); | |
1419 } | |
1420 | |
1398 void ServiceWorkerStorage::StartPurgingResources( | 1421 void ServiceWorkerStorage::StartPurgingResources( |
1399 const ResourceList& resources) { | 1422 const ResourceList& resources) { |
1400 DCHECK(has_checked_for_stale_resources_); | 1423 DCHECK(has_checked_for_stale_resources_); |
1401 for (const auto& resource : resources) | 1424 for (const auto& resource : resources) |
1402 purgeable_resource_ids_.push_back(resource.resource_id); | 1425 purgeable_resource_ids_.push_back(resource.resource_id); |
1403 ContinuePurgingResources(); | 1426 ContinuePurgingResources(); |
1404 } | 1427 } |
1405 | 1428 |
1406 void ServiceWorkerStorage::ContinuePurgingResources() { | 1429 void ServiceWorkerStorage::ContinuePurgingResources() { |
1407 if (purgeable_resource_ids_.empty() || is_purge_pending_) | 1430 if (purgeable_resource_ids_.empty() || is_purge_pending_) |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1820 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1843 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
1821 return; | 1844 return; |
1822 } | 1845 } |
1823 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1846 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
1824 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( | 1847 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( |
1825 ServiceWorkerMetrics::DELETE_OK); | 1848 ServiceWorkerMetrics::DELETE_OK); |
1826 callback.Run(SERVICE_WORKER_OK); | 1849 callback.Run(SERVICE_WORKER_OK); |
1827 } | 1850 } |
1828 | 1851 |
1829 } // namespace content | 1852 } // namespace content |
OLD | NEW |