Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: content/browser/service_worker/service_worker_storage.cc

Issue 355163003: Don't prematurely delete script resources when registration is deleted (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 7 #include <string>
8 8
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 base::WeakPtr<ServiceWorkerContextCore> context, 109 base::WeakPtr<ServiceWorkerContextCore> context,
110 ServiceWorkerStorage* old_storage) { 110 ServiceWorkerStorage* old_storage) {
111 return make_scoped_ptr( 111 return make_scoped_ptr(
112 new ServiceWorkerStorage(old_storage->path_, 112 new ServiceWorkerStorage(old_storage->path_,
113 context, 113 context,
114 old_storage->database_task_runner_, 114 old_storage->database_task_runner_,
115 old_storage->disk_cache_thread_, 115 old_storage->disk_cache_thread_,
116 old_storage->quota_manager_proxy_)); 116 old_storage->quota_manager_proxy_));
117 } 117 }
118 118
119
120 void ServiceWorkerStorage::FindRegistrationForDocument( 119 void ServiceWorkerStorage::FindRegistrationForDocument(
121 const GURL& document_url, 120 const GURL& document_url,
122 const FindRegistrationCallback& callback) { 121 const FindRegistrationCallback& callback) {
123 DCHECK(!document_url.has_ref()); 122 DCHECK(!document_url.has_ref());
124 if (!LazyInitialize(base::Bind( 123 if (!LazyInitialize(base::Bind(
125 &ServiceWorkerStorage::FindRegistrationForDocument, 124 &ServiceWorkerStorage::FindRegistrationForDocument,
126 weak_factory_.GetWeakPtr(), document_url, callback))) { 125 weak_factory_.GetWeakPtr(), document_url, callback))) {
127 if (state_ != INITIALIZING || !context_) { 126 if (state_ != INITIALIZING || !context_) {
128 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), 127 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(),
129 SERVICE_WORKER_ERROR_FAILED, callback); 128 SERVICE_WORKER_ERROR_FAILED, callback);
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 registration_id, origin, 341 registration_id, origin,
343 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, 342 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration,
344 weak_factory_.GetWeakPtr(), origin, callback))); 343 weak_factory_.GetWeakPtr(), origin, callback)));
345 344
346 // TODO(michaeln): Either its instance should also be 345 // TODO(michaeln): Either its instance should also be
347 // removed from liveregistrations map or the live object 346 // removed from liveregistrations map or the live object
348 // should marked as deleted in some way and not 'findable' 347 // should marked as deleted in some way and not 'findable'
349 // thereafter. 348 // thereafter.
350 } 349 }
351 350
351 void ServiceWorkerStorage::DeleteVersionResources(
352 int64 version_id,
353 const StatusCallback& callback) {
354 DCHECK(state_ == INITIALIZED || state_ == DISABLED);
355 if (state_ != INITIALIZED || !context_) {
356 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
357 return;
358 }
359
360 database_task_runner_->PostTask(
361 FROM_HERE,
362 base::Bind(&DeleteVersionResourcesFromDB,
363 database_.get(),
364 base::MessageLoopProxy::current(),
365 version_id,
366 base::Bind(&ServiceWorkerStorage::DidDeleteVersionResources,
367 weak_factory_.GetWeakPtr(),
368 callback)));
369 }
370
352 scoped_ptr<ServiceWorkerResponseReader> 371 scoped_ptr<ServiceWorkerResponseReader>
353 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { 372 ServiceWorkerStorage::CreateResponseReader(int64 response_id) {
354 return make_scoped_ptr( 373 return make_scoped_ptr(
355 new ServiceWorkerResponseReader(response_id, disk_cache())); 374 new ServiceWorkerResponseReader(response_id, disk_cache()));
356 } 375 }
357 376
358 scoped_ptr<ServiceWorkerResponseWriter> 377 scoped_ptr<ServiceWorkerResponseWriter>
359 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { 378 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) {
360 return make_scoped_ptr( 379 return make_scoped_ptr(
361 new ServiceWorkerResponseWriter(response_id, disk_cache())); 380 new ServiceWorkerResponseWriter(response_id, disk_cache()));
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 const GURL& origin, 704 const GURL& origin,
686 const std::vector<int64>& newly_purgeable_resources, 705 const std::vector<int64>& newly_purgeable_resources,
687 ServiceWorkerDatabase::Status status) { 706 ServiceWorkerDatabase::Status status) {
688 if (status != ServiceWorkerDatabase::STATUS_OK) { 707 if (status != ServiceWorkerDatabase::STATUS_OK) {
689 ScheduleDeleteAndStartOver(); 708 ScheduleDeleteAndStartOver();
690 callback.Run(DatabaseStatusToStatusCode(status)); 709 callback.Run(DatabaseStatusToStatusCode(status));
691 return; 710 return;
692 } 711 }
693 registered_origins_.insert(origin); 712 registered_origins_.insert(origin);
694 callback.Run(SERVICE_WORKER_OK); 713 callback.Run(SERVICE_WORKER_OK);
695 StartPurgingResources(newly_purgeable_resources); 714 StartPurgingResources(newly_purgeable_resources);
michaeln 2014/06/28 02:29:31 ditto defer this call...
696 } 715 }
697 716
698 void ServiceWorkerStorage::DidUpdateToActiveState( 717 void ServiceWorkerStorage::DidUpdateToActiveState(
699 const StatusCallback& callback, 718 const StatusCallback& callback,
700 ServiceWorkerDatabase::Status status) { 719 ServiceWorkerDatabase::Status status) {
701 if (status != ServiceWorkerDatabase::STATUS_OK && 720 if (status != ServiceWorkerDatabase::STATUS_OK &&
702 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { 721 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) {
703 ScheduleDeleteAndStartOver(); 722 ScheduleDeleteAndStartOver();
704 } 723 }
705 callback.Run(DatabaseStatusToStatusCode(status)); 724 callback.Run(DatabaseStatusToStatusCode(status));
706 } 725 }
707 726
708 void ServiceWorkerStorage::DidDeleteRegistration( 727 void ServiceWorkerStorage::DidDeleteRegistration(
709 const GURL& origin, 728 const GURL& origin,
710 const StatusCallback& callback, 729 const StatusCallback& callback,
711 bool origin_is_deletable, 730 bool origin_is_deletable,
712 const std::vector<int64>& newly_purgeable_resources,
713 ServiceWorkerDatabase::Status status) { 731 ServiceWorkerDatabase::Status status) {
714 if (status != ServiceWorkerDatabase::STATUS_OK) { 732 if (status != ServiceWorkerDatabase::STATUS_OK) {
715 ScheduleDeleteAndStartOver(); 733 ScheduleDeleteAndStartOver();
716 callback.Run(DatabaseStatusToStatusCode(status)); 734 callback.Run(DatabaseStatusToStatusCode(status));
717 return; 735 return;
718 } 736 }
719 if (origin_is_deletable) 737 if (origin_is_deletable)
720 registered_origins_.erase(origin); 738 registered_origins_.erase(origin);
721 callback.Run(SERVICE_WORKER_OK); 739 callback.Run(SERVICE_WORKER_OK);
740 }
741
742 void ServiceWorkerStorage::DidDeleteVersionResources(
743 const StatusCallback& callback,
744 const std::vector<int64>& newly_purgeable_resources,
745 ServiceWorkerDatabase::Status status) {
746 if (status != ServiceWorkerDatabase::STATUS_OK) {
747 ScheduleDeleteAndStartOver();
748 callback.Run(DatabaseStatusToStatusCode(status));
749 return;
750 }
751 callback.Run(SERVICE_WORKER_OK);
722 StartPurgingResources(newly_purgeable_resources); 752 StartPurgingResources(newly_purgeable_resources);
michaeln 2014/06/28 02:29:31 Just the startpurging should be deferred till nomo
723 } 753 }
724 754
725 scoped_refptr<ServiceWorkerRegistration> 755 scoped_refptr<ServiceWorkerRegistration>
726 ServiceWorkerStorage::GetOrCreateRegistration( 756 ServiceWorkerStorage::GetOrCreateRegistration(
727 const ServiceWorkerDatabase::RegistrationData& data, 757 const ServiceWorkerDatabase::RegistrationData& data,
728 const ResourceList& resources) { 758 const ResourceList& resources) {
729 scoped_refptr<ServiceWorkerRegistration> registration = 759 scoped_refptr<ServiceWorkerRegistration> registration =
730 context_->GetLiveRegistration(data.registration_id); 760 context_->GetLiveRegistration(data.registration_id);
731 if (registration) 761 if (registration)
732 return registration; 762 return registration;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 LOG(ERROR) << "Failed to open the serviceworker diskcache: " 855 LOG(ERROR) << "Failed to open the serviceworker diskcache: "
826 << net::ErrorToString(rv); 856 << net::ErrorToString(rv);
827 ScheduleDeleteAndStartOver(); 857 ScheduleDeleteAndStartOver();
828 } 858 }
829 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK); 859 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK);
830 } 860 }
831 861
832 void ServiceWorkerStorage::StartPurgingResources( 862 void ServiceWorkerStorage::StartPurgingResources(
833 const std::vector<int64>& ids) { 863 const std::vector<int64>& ids) {
834 for (size_t i = 0; i < ids.size(); ++i) 864 for (size_t i = 0; i < ids.size(); ++i)
835 purgeable_reource_ids_.push_back(ids[i]); 865 purgeable_resource_ids_.push_back(ids[i]);
836 ContinuePurgingResources(); 866 ContinuePurgingResources();
837 } 867 }
838 868
839 void ServiceWorkerStorage::StartPurgingResources( 869 void ServiceWorkerStorage::StartPurgingResources(
840 const ResourceList& resources) { 870 const ResourceList& resources) {
841 for (size_t i = 0; i < resources.size(); ++i) 871 for (size_t i = 0; i < resources.size(); ++i)
842 purgeable_reource_ids_.push_back(resources[i].resource_id); 872 purgeable_resource_ids_.push_back(resources[i].resource_id);
843 ContinuePurgingResources(); 873 ContinuePurgingResources();
844 } 874 }
845 875
846 void ServiceWorkerStorage::ContinuePurgingResources() { 876 void ServiceWorkerStorage::ContinuePurgingResources() {
847 if (purgeable_reource_ids_.empty() || is_purge_pending_) 877 if (purgeable_resource_ids_.empty() || is_purge_pending_)
848 return; 878 return;
849 879
850 // Do one at a time until we're done, use RunSoon to avoid recursion when 880 // Do one at a time until we're done, use RunSoon to avoid recursion when
851 // DoomEntry returns immediately. 881 // DoomEntry returns immediately.
852 is_purge_pending_ = true; 882 is_purge_pending_ = true;
853 int64 id = purgeable_reource_ids_.front(); 883 int64 id = purgeable_resource_ids_.front();
854 purgeable_reource_ids_.pop_front(); 884 purgeable_resource_ids_.pop_front();
855 RunSoon(FROM_HERE, 885 RunSoon(FROM_HERE,
856 base::Bind(&ServiceWorkerStorage::PurgeResource, 886 base::Bind(&ServiceWorkerStorage::PurgeResource,
857 weak_factory_.GetWeakPtr(), id)); 887 weak_factory_.GetWeakPtr(), id));
858 } 888 }
859 889
860 void ServiceWorkerStorage::PurgeResource(int64 id) { 890 void ServiceWorkerStorage::PurgeResource(int64 id) {
861 DCHECK(is_purge_pending_); 891 DCHECK(is_purge_pending_);
862 int rv = disk_cache()->DoomEntry( 892 int rv = disk_cache()->DoomEntry(
863 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, 893 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged,
864 weak_factory_.GetWeakPtr(), id)); 894 weak_factory_.GetWeakPtr(), id));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 void ServiceWorkerStorage::DeleteRegistrationFromDB( 936 void ServiceWorkerStorage::DeleteRegistrationFromDB(
907 ServiceWorkerDatabase* database, 937 ServiceWorkerDatabase* database,
908 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 938 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
909 int64 registration_id, 939 int64 registration_id,
910 const GURL& origin, 940 const GURL& origin,
911 const DeleteRegistrationCallback& callback) { 941 const DeleteRegistrationCallback& callback) {
912 DCHECK(database); 942 DCHECK(database);
913 943
914 std::vector<int64> newly_purgeable_resources; 944 std::vector<int64> newly_purgeable_resources;
915 ServiceWorkerDatabase::Status status = 945 ServiceWorkerDatabase::Status status =
916 database->DeleteRegistration(registration_id, origin, 946 database->DeleteRegistration(registration_id, origin);
917 &newly_purgeable_resources);
918 if (status != ServiceWorkerDatabase::STATUS_OK) { 947 if (status != ServiceWorkerDatabase::STATUS_OK) {
919 original_task_runner->PostTask( 948 original_task_runner->PostTask(FROM_HERE,
920 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); 949 base::Bind(callback, false, status));
921 return; 950 return;
922 } 951 }
923 952
924 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the 953 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the
925 // unique origin list. 954 // unique origin list.
926 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; 955 std::vector<ServiceWorkerDatabase::RegistrationData> registrations;
927 status = database->GetRegistrationsForOrigin(origin, &registrations); 956 status = database->GetRegistrationsForOrigin(origin, &registrations);
928 if (status != ServiceWorkerDatabase::STATUS_OK) { 957 if (status != ServiceWorkerDatabase::STATUS_OK) {
929 original_task_runner->PostTask( 958 original_task_runner->PostTask(FROM_HERE,
930 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); 959 base::Bind(callback, false, status));
931 return; 960 return;
932 } 961 }
933 962
934 bool deletable = registrations.empty(); 963 bool deletable = registrations.empty();
964 original_task_runner->PostTask(FROM_HERE,
965 base::Bind(callback, deletable, status));
966 }
967
968 void ServiceWorkerStorage::DeleteVersionResourcesFromDB(
969 ServiceWorkerDatabase* database,
970 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
971 int64 version_id,
972 const DeleteVersionResourcesCallback& callback) {
973 DCHECK(database);
974
975 std::vector<int64> newly_purgeable_resources;
976 ServiceWorkerDatabase::Status status =
977 database->DeleteVersionResources(version_id, &newly_purgeable_resources);
978 if (status != ServiceWorkerDatabase::STATUS_OK) {
979 original_task_runner->PostTask(
980 FROM_HERE, base::Bind(callback, std::vector<int64>(), status));
981 return;
982 }
983
935 original_task_runner->PostTask( 984 original_task_runner->PostTask(
936 FROM_HERE, base::Bind(callback, deletable, 985 FROM_HERE, base::Bind(callback, newly_purgeable_resources, status));
937 newly_purgeable_resources, status));
938 } 986 }
939 987
940 void ServiceWorkerStorage::WriteRegistrationInDB( 988 void ServiceWorkerStorage::WriteRegistrationInDB(
941 ServiceWorkerDatabase* database, 989 ServiceWorkerDatabase* database,
942 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 990 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
943 const ServiceWorkerDatabase::RegistrationData& data, 991 const ServiceWorkerDatabase::RegistrationData& data,
944 const ResourceList& resources, 992 const ResourceList& resources,
945 const WriteRegistrationCallback& callback) { 993 const WriteRegistrationCallback& callback) {
946 DCHECK(database); 994 DCHECK(database);
947 std::vector<int64> newly_purgeable_resources; 995 std::vector<int64> newly_purgeable_resources;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 // Give up the corruption recovery until the browser restarts. 1137 // Give up the corruption recovery until the browser restarts.
1090 LOG(ERROR) << "Failed to delete the diskcache."; 1138 LOG(ERROR) << "Failed to delete the diskcache.";
1091 callback.Run(SERVICE_WORKER_ERROR_FAILED); 1139 callback.Run(SERVICE_WORKER_ERROR_FAILED);
1092 return; 1140 return;
1093 } 1141 }
1094 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; 1142 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully.";
1095 callback.Run(SERVICE_WORKER_OK); 1143 callback.Run(SERVICE_WORKER_OK);
1096 } 1144 }
1097 1145
1098 } // namespace content 1146 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698