Chromium Code Reviews| 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 <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 Loading... | |
| 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 void ServiceWorkerStorage::OnNoControllees(ServiceWorkerVersion* version) { | |
| 120 std::map<int64, std::vector<int64> >::iterator it = | |
| 121 deleted_version_resource_ids_.find(version->version_id()); | |
| 122 DCHECK(it != deleted_version_resource_ids_.end()); | |
| 123 StartPurgingResources(it->second); | |
| 124 deleted_version_resource_ids_.erase(it); | |
|
michaeln
2014/06/30 22:31:54
maybe vers->removelistener(this) so theres less of
falken
2014/07/01 06:17:39
Done.
| |
| 125 } | |
| 119 | 126 |
| 120 void ServiceWorkerStorage::FindRegistrationForDocument( | 127 void ServiceWorkerStorage::FindRegistrationForDocument( |
| 121 const GURL& document_url, | 128 const GURL& document_url, |
| 122 const FindRegistrationCallback& callback) { | 129 const FindRegistrationCallback& callback) { |
| 123 DCHECK(!document_url.has_ref()); | 130 DCHECK(!document_url.has_ref()); |
| 124 if (!LazyInitialize(base::Bind( | 131 if (!LazyInitialize(base::Bind( |
| 125 &ServiceWorkerStorage::FindRegistrationForDocument, | 132 &ServiceWorkerStorage::FindRegistrationForDocument, |
| 126 weak_factory_.GetWeakPtr(), document_url, callback))) { | 133 weak_factory_.GetWeakPtr(), document_url, callback))) { |
| 127 if (state_ != INITIALIZING || !context_) { | 134 if (state_ != INITIALIZING || !context_) { |
| 128 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 135 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
| (...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 685 const GURL& origin, | 692 const GURL& origin, |
| 686 const std::vector<int64>& newly_purgeable_resources, | 693 const std::vector<int64>& newly_purgeable_resources, |
| 687 ServiceWorkerDatabase::Status status) { | 694 ServiceWorkerDatabase::Status status) { |
| 688 if (status != ServiceWorkerDatabase::STATUS_OK) { | 695 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 689 ScheduleDeleteAndStartOver(); | 696 ScheduleDeleteAndStartOver(); |
| 690 callback.Run(DatabaseStatusToStatusCode(status)); | 697 callback.Run(DatabaseStatusToStatusCode(status)); |
| 691 return; | 698 return; |
| 692 } | 699 } |
| 693 registered_origins_.insert(origin); | 700 registered_origins_.insert(origin); |
| 694 callback.Run(SERVICE_WORKER_OK); | 701 callback.Run(SERVICE_WORKER_OK); |
| 695 StartPurgingResources(newly_purgeable_resources); | 702 StartPurgingResources(newly_purgeable_resources); |
|
michaeln
2014/06/30 22:31:54
Do these need to be handled in the same fashion?
falken
2014/07/01 06:17:39
Done, I planned to do that in a second patch but e
| |
| 696 } | 703 } |
| 697 | 704 |
| 698 void ServiceWorkerStorage::DidUpdateToActiveState( | 705 void ServiceWorkerStorage::DidUpdateToActiveState( |
| 699 const StatusCallback& callback, | 706 const StatusCallback& callback, |
| 700 ServiceWorkerDatabase::Status status) { | 707 ServiceWorkerDatabase::Status status) { |
| 701 if (status != ServiceWorkerDatabase::STATUS_OK && | 708 if (status != ServiceWorkerDatabase::STATUS_OK && |
| 702 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 709 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
| 703 ScheduleDeleteAndStartOver(); | 710 ScheduleDeleteAndStartOver(); |
| 704 } | 711 } |
| 705 callback.Run(DatabaseStatusToStatusCode(status)); | 712 callback.Run(DatabaseStatusToStatusCode(status)); |
| 706 } | 713 } |
| 707 | 714 |
| 708 void ServiceWorkerStorage::DidDeleteRegistration( | 715 void ServiceWorkerStorage::DidDeleteRegistration( |
| 709 const GURL& origin, | 716 const GURL& origin, |
| 710 const StatusCallback& callback, | 717 const StatusCallback& callback, |
| 711 bool origin_is_deletable, | 718 bool origin_is_deletable, |
| 719 int64 version_id, | |
| 712 const std::vector<int64>& newly_purgeable_resources, | 720 const std::vector<int64>& newly_purgeable_resources, |
| 713 ServiceWorkerDatabase::Status status) { | 721 ServiceWorkerDatabase::Status status) { |
| 714 if (status != ServiceWorkerDatabase::STATUS_OK) { | 722 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 715 ScheduleDeleteAndStartOver(); | 723 ScheduleDeleteAndStartOver(); |
| 716 callback.Run(DatabaseStatusToStatusCode(status)); | 724 callback.Run(DatabaseStatusToStatusCode(status)); |
| 717 return; | 725 return; |
| 718 } | 726 } |
| 719 if (origin_is_deletable) | 727 if (origin_is_deletable) |
| 720 registered_origins_.erase(origin); | 728 registered_origins_.erase(origin); |
| 721 callback.Run(SERVICE_WORKER_OK); | 729 callback.Run(SERVICE_WORKER_OK); |
| 722 StartPurgingResources(newly_purgeable_resources); | 730 |
| 731 DCHECK(deleted_version_resource_ids_.find(version_id) == | |
| 732 deleted_version_resource_ids_.end()); | |
| 733 scoped_refptr<ServiceWorkerVersion> version = | |
| 734 context_ ? context_->GetLiveVersion(version_id) : NULL; | |
| 735 if (version && version->HasControllee()) { | |
| 736 deleted_version_resource_ids_[version_id] = newly_purgeable_resources; | |
| 737 version->AddListener(this); | |
| 738 } else { | |
| 739 StartPurgingResources(newly_purgeable_resources); | |
| 740 } | |
| 723 } | 741 } |
| 724 | 742 |
| 725 scoped_refptr<ServiceWorkerRegistration> | 743 scoped_refptr<ServiceWorkerRegistration> |
| 726 ServiceWorkerStorage::GetOrCreateRegistration( | 744 ServiceWorkerStorage::GetOrCreateRegistration( |
| 727 const ServiceWorkerDatabase::RegistrationData& data, | 745 const ServiceWorkerDatabase::RegistrationData& data, |
| 728 const ResourceList& resources) { | 746 const ResourceList& resources) { |
| 729 scoped_refptr<ServiceWorkerRegistration> registration = | 747 scoped_refptr<ServiceWorkerRegistration> registration = |
| 730 context_->GetLiveRegistration(data.registration_id); | 748 context_->GetLiveRegistration(data.registration_id); |
| 731 if (registration) | 749 if (registration) |
| 732 return registration; | 750 return registration; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 825 LOG(ERROR) << "Failed to open the serviceworker diskcache: " | 843 LOG(ERROR) << "Failed to open the serviceworker diskcache: " |
| 826 << net::ErrorToString(rv); | 844 << net::ErrorToString(rv); |
| 827 ScheduleDeleteAndStartOver(); | 845 ScheduleDeleteAndStartOver(); |
| 828 } | 846 } |
| 829 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK); | 847 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK); |
| 830 } | 848 } |
| 831 | 849 |
| 832 void ServiceWorkerStorage::StartPurgingResources( | 850 void ServiceWorkerStorage::StartPurgingResources( |
| 833 const std::vector<int64>& ids) { | 851 const std::vector<int64>& ids) { |
| 834 for (size_t i = 0; i < ids.size(); ++i) | 852 for (size_t i = 0; i < ids.size(); ++i) |
| 835 purgeable_reource_ids_.push_back(ids[i]); | 853 purgeable_resource_ids_.push_back(ids[i]); |
| 836 ContinuePurgingResources(); | 854 ContinuePurgingResources(); |
| 837 } | 855 } |
| 838 | 856 |
| 839 void ServiceWorkerStorage::StartPurgingResources( | 857 void ServiceWorkerStorage::StartPurgingResources( |
| 840 const ResourceList& resources) { | 858 const ResourceList& resources) { |
| 841 for (size_t i = 0; i < resources.size(); ++i) | 859 for (size_t i = 0; i < resources.size(); ++i) |
| 842 purgeable_reource_ids_.push_back(resources[i].resource_id); | 860 purgeable_resource_ids_.push_back(resources[i].resource_id); |
| 843 ContinuePurgingResources(); | 861 ContinuePurgingResources(); |
| 844 } | 862 } |
| 845 | 863 |
| 846 void ServiceWorkerStorage::ContinuePurgingResources() { | 864 void ServiceWorkerStorage::ContinuePurgingResources() { |
| 847 if (purgeable_reource_ids_.empty() || is_purge_pending_) | 865 if (purgeable_resource_ids_.empty() || is_purge_pending_) |
| 848 return; | 866 return; |
| 849 | 867 |
| 850 // Do one at a time until we're done, use RunSoon to avoid recursion when | 868 // Do one at a time until we're done, use RunSoon to avoid recursion when |
| 851 // DoomEntry returns immediately. | 869 // DoomEntry returns immediately. |
| 852 is_purge_pending_ = true; | 870 is_purge_pending_ = true; |
| 853 int64 id = purgeable_reource_ids_.front(); | 871 int64 id = purgeable_resource_ids_.front(); |
| 854 purgeable_reource_ids_.pop_front(); | 872 purgeable_resource_ids_.pop_front(); |
| 855 RunSoon(FROM_HERE, | 873 RunSoon(FROM_HERE, |
| 856 base::Bind(&ServiceWorkerStorage::PurgeResource, | 874 base::Bind(&ServiceWorkerStorage::PurgeResource, |
| 857 weak_factory_.GetWeakPtr(), id)); | 875 weak_factory_.GetWeakPtr(), id)); |
| 858 } | 876 } |
| 859 | 877 |
| 860 void ServiceWorkerStorage::PurgeResource(int64 id) { | 878 void ServiceWorkerStorage::PurgeResource(int64 id) { |
| 861 DCHECK(is_purge_pending_); | 879 DCHECK(is_purge_pending_); |
| 862 int rv = disk_cache()->DoomEntry( | 880 int rv = disk_cache()->DoomEntry( |
| 863 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, | 881 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, |
| 864 weak_factory_.GetWeakPtr(), id)); | 882 weak_factory_.GetWeakPtr(), id)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 904 } | 922 } |
| 905 | 923 |
| 906 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 924 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 907 ServiceWorkerDatabase* database, | 925 ServiceWorkerDatabase* database, |
| 908 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 926 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 909 int64 registration_id, | 927 int64 registration_id, |
| 910 const GURL& origin, | 928 const GURL& origin, |
| 911 const DeleteRegistrationCallback& callback) { | 929 const DeleteRegistrationCallback& callback) { |
| 912 DCHECK(database); | 930 DCHECK(database); |
| 913 | 931 |
| 932 int64 version_id = kInvalidServiceWorkerVersionId; | |
| 914 std::vector<int64> newly_purgeable_resources; | 933 std::vector<int64> newly_purgeable_resources; |
| 915 ServiceWorkerDatabase::Status status = | 934 ServiceWorkerDatabase::Status status = database->DeleteRegistration( |
| 916 database->DeleteRegistration(registration_id, origin, | 935 registration_id, origin, &version_id, &newly_purgeable_resources); |
| 917 &newly_purgeable_resources); | |
| 918 if (status != ServiceWorkerDatabase::STATUS_OK) { | 936 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 919 original_task_runner->PostTask( | 937 original_task_runner->PostTask(FROM_HERE, |
| 920 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); | 938 base::Bind(callback, |
| 939 false, | |
| 940 kInvalidServiceWorkerVersionId, | |
| 941 std::vector<int64>(), | |
| 942 status)); | |
| 921 return; | 943 return; |
| 922 } | 944 } |
| 923 | 945 |
| 924 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the | 946 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the |
| 925 // unique origin list. | 947 // unique origin list. |
| 926 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | 948 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 927 status = database->GetRegistrationsForOrigin(origin, ®istrations); | 949 status = database->GetRegistrationsForOrigin(origin, ®istrations); |
| 928 if (status != ServiceWorkerDatabase::STATUS_OK) { | 950 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 929 original_task_runner->PostTask( | 951 original_task_runner->PostTask(FROM_HERE, |
| 930 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); | 952 base::Bind(callback, |
| 953 false, | |
| 954 kInvalidServiceWorkerVersionId, | |
| 955 std::vector<int64>(), | |
| 956 status)); | |
| 931 return; | 957 return; |
| 932 } | 958 } |
| 933 | 959 |
| 934 bool deletable = registrations.empty(); | 960 bool deletable = registrations.empty(); |
| 935 original_task_runner->PostTask( | 961 original_task_runner->PostTask( |
| 936 FROM_HERE, base::Bind(callback, deletable, | 962 FROM_HERE, |
| 937 newly_purgeable_resources, status)); | 963 base::Bind( |
| 964 callback, deletable, version_id, newly_purgeable_resources, status)); | |
| 938 } | 965 } |
| 939 | 966 |
| 940 void ServiceWorkerStorage::WriteRegistrationInDB( | 967 void ServiceWorkerStorage::WriteRegistrationInDB( |
| 941 ServiceWorkerDatabase* database, | 968 ServiceWorkerDatabase* database, |
| 942 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 969 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 943 const ServiceWorkerDatabase::RegistrationData& data, | 970 const ServiceWorkerDatabase::RegistrationData& data, |
| 944 const ResourceList& resources, | 971 const ResourceList& resources, |
| 945 const WriteRegistrationCallback& callback) { | 972 const WriteRegistrationCallback& callback) { |
| 946 DCHECK(database); | 973 DCHECK(database); |
| 947 std::vector<int64> newly_purgeable_resources; | 974 std::vector<int64> newly_purgeable_resources; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1089 // Give up the corruption recovery until the browser restarts. | 1116 // Give up the corruption recovery until the browser restarts. |
| 1090 LOG(ERROR) << "Failed to delete the diskcache."; | 1117 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1091 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1118 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1092 return; | 1119 return; |
| 1093 } | 1120 } |
| 1094 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1121 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1095 callback.Run(SERVICE_WORKER_OK); | 1122 callback.Run(SERVICE_WORKER_OK); |
| 1096 } | 1123 } |
| 1097 | 1124 |
| 1098 } // namespace content | 1125 } // namespace content |
| OLD | NEW |