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 | |
| 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 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 676 if (pushed_registrations.insert(it->first).second) | 675 if (pushed_registrations.insert(it->first).second) |
| 677 infos.push_back(it->second->GetInfo()); | 676 infos.push_back(it->second->GetInfo()); |
| 678 } | 677 } |
| 679 | 678 |
| 680 callback.Run(infos); | 679 callback.Run(infos); |
| 681 } | 680 } |
| 682 | 681 |
| 683 void ServiceWorkerStorage::DidStoreRegistration( | 682 void ServiceWorkerStorage::DidStoreRegistration( |
| 684 const StatusCallback& callback, | 683 const StatusCallback& callback, |
| 685 const GURL& origin, | 684 const GURL& origin, |
| 685 int64 deleted_version_id, | |
| 686 const std::vector<int64>& newly_purgeable_resources, | 686 const std::vector<int64>& newly_purgeable_resources, |
| 687 ServiceWorkerDatabase::Status status) { | 687 ServiceWorkerDatabase::Status status) { |
| 688 if (status != ServiceWorkerDatabase::STATUS_OK) { | 688 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 689 ScheduleDeleteAndStartOver(); | 689 ScheduleDeleteAndStartOver(); |
| 690 callback.Run(DatabaseStatusToStatusCode(status)); | 690 callback.Run(DatabaseStatusToStatusCode(status)); |
| 691 return; | 691 return; |
| 692 } | 692 } |
| 693 registered_origins_.insert(origin); | 693 registered_origins_.insert(origin); |
| 694 callback.Run(SERVICE_WORKER_OK); | 694 callback.Run(SERVICE_WORKER_OK); |
| 695 StartPurgingResources(newly_purgeable_resources); | 695 |
| 696 SchedulePurgeResources(deleted_version_id, newly_purgeable_resources); | |
| 696 } | 697 } |
| 697 | 698 |
| 698 void ServiceWorkerStorage::DidUpdateToActiveState( | 699 void ServiceWorkerStorage::DidUpdateToActiveState( |
| 699 const StatusCallback& callback, | 700 const StatusCallback& callback, |
| 700 ServiceWorkerDatabase::Status status) { | 701 ServiceWorkerDatabase::Status status) { |
| 701 if (status != ServiceWorkerDatabase::STATUS_OK && | 702 if (status != ServiceWorkerDatabase::STATUS_OK && |
| 702 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 703 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
| 703 ScheduleDeleteAndStartOver(); | 704 ScheduleDeleteAndStartOver(); |
| 704 } | 705 } |
| 705 callback.Run(DatabaseStatusToStatusCode(status)); | 706 callback.Run(DatabaseStatusToStatusCode(status)); |
| 706 } | 707 } |
| 707 | 708 |
| 708 void ServiceWorkerStorage::DidDeleteRegistration( | 709 void ServiceWorkerStorage::DidDeleteRegistration( |
| 709 const GURL& origin, | 710 const GURL& origin, |
| 710 const StatusCallback& callback, | 711 const StatusCallback& callback, |
| 711 bool origin_is_deletable, | 712 bool origin_is_deletable, |
| 713 int64 version_id, | |
| 712 const std::vector<int64>& newly_purgeable_resources, | 714 const std::vector<int64>& newly_purgeable_resources, |
| 713 ServiceWorkerDatabase::Status status) { | 715 ServiceWorkerDatabase::Status status) { |
| 714 if (status != ServiceWorkerDatabase::STATUS_OK) { | 716 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 715 ScheduleDeleteAndStartOver(); | 717 ScheduleDeleteAndStartOver(); |
| 716 callback.Run(DatabaseStatusToStatusCode(status)); | 718 callback.Run(DatabaseStatusToStatusCode(status)); |
| 717 return; | 719 return; |
| 718 } | 720 } |
| 719 if (origin_is_deletable) | 721 if (origin_is_deletable) |
| 720 registered_origins_.erase(origin); | 722 registered_origins_.erase(origin); |
| 721 callback.Run(SERVICE_WORKER_OK); | 723 callback.Run(SERVICE_WORKER_OK); |
| 722 StartPurgingResources(newly_purgeable_resources); | 724 |
| 725 SchedulePurgeResources(version_id, newly_purgeable_resources); | |
| 723 } | 726 } |
| 724 | 727 |
| 725 scoped_refptr<ServiceWorkerRegistration> | 728 scoped_refptr<ServiceWorkerRegistration> |
| 726 ServiceWorkerStorage::GetOrCreateRegistration( | 729 ServiceWorkerStorage::GetOrCreateRegistration( |
| 727 const ServiceWorkerDatabase::RegistrationData& data, | 730 const ServiceWorkerDatabase::RegistrationData& data, |
| 728 const ResourceList& resources) { | 731 const ResourceList& resources) { |
| 729 scoped_refptr<ServiceWorkerRegistration> registration = | 732 scoped_refptr<ServiceWorkerRegistration> registration = |
| 730 context_->GetLiveRegistration(data.registration_id); | 733 context_->GetLiveRegistration(data.registration_id); |
| 731 if (registration) | 734 if (registration) |
| 732 return registration; | 735 return registration; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 822 | 825 |
| 823 void ServiceWorkerStorage::OnDiskCacheInitialized(int rv) { | 826 void ServiceWorkerStorage::OnDiskCacheInitialized(int rv) { |
| 824 if (rv != net::OK) { | 827 if (rv != net::OK) { |
| 825 LOG(ERROR) << "Failed to open the serviceworker diskcache: " | 828 LOG(ERROR) << "Failed to open the serviceworker diskcache: " |
| 826 << net::ErrorToString(rv); | 829 << net::ErrorToString(rv); |
| 827 ScheduleDeleteAndStartOver(); | 830 ScheduleDeleteAndStartOver(); |
| 828 } | 831 } |
| 829 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK); | 832 ServiceWorkerHistograms::CountInitDiskCacheResult(rv == net::OK); |
| 830 } | 833 } |
| 831 | 834 |
| 835 void ServiceWorkerStorage::OnNoControllees(ServiceWorkerVersion* version) { | |
| 836 std::map<int64, std::vector<int64> >::iterator it = | |
| 837 deleted_version_resource_ids_.find(version->version_id()); | |
| 838 DCHECK(it != deleted_version_resource_ids_.end()); | |
| 839 StartPurgingResources(it->second); | |
| 840 deleted_version_resource_ids_.erase(it); | |
| 841 version->RemoveListener(this); | |
| 842 } | |
| 843 | |
| 844 void ServiceWorkerStorage::SchedulePurgeResources( | |
| 845 int64 version_id, | |
| 846 const std::vector<int64>& resources) { | |
| 847 DCHECK(deleted_version_resource_ids_.find(version_id) == | |
| 848 deleted_version_resource_ids_.end()); | |
|
michaeln
2014/07/01 20:54:06
if newly_purgeable_resources.empty(), don't bother
falken
2014/07/02 04:04:24
good point, done
| |
| 849 scoped_refptr<ServiceWorkerVersion> version = | |
| 850 context_ ? context_->GetLiveVersion(version_id) : NULL; | |
| 851 if (version && version->HasControllee()) { | |
| 852 deleted_version_resource_ids_[version_id] = resources; | |
| 853 version->AddListener(this); | |
| 854 } else { | |
| 855 StartPurgingResources(resources); | |
| 856 } | |
| 857 } | |
| 858 | |
| 832 void ServiceWorkerStorage::StartPurgingResources( | 859 void ServiceWorkerStorage::StartPurgingResources( |
| 833 const std::vector<int64>& ids) { | 860 const std::vector<int64>& ids) { |
| 834 for (size_t i = 0; i < ids.size(); ++i) | 861 for (size_t i = 0; i < ids.size(); ++i) |
| 835 purgeable_reource_ids_.push_back(ids[i]); | 862 purgeable_resource_ids_.push_back(ids[i]); |
| 836 ContinuePurgingResources(); | 863 ContinuePurgingResources(); |
| 837 } | 864 } |
| 838 | 865 |
| 839 void ServiceWorkerStorage::StartPurgingResources( | 866 void ServiceWorkerStorage::StartPurgingResources( |
| 840 const ResourceList& resources) { | 867 const ResourceList& resources) { |
| 841 for (size_t i = 0; i < resources.size(); ++i) | 868 for (size_t i = 0; i < resources.size(); ++i) |
| 842 purgeable_reource_ids_.push_back(resources[i].resource_id); | 869 purgeable_resource_ids_.push_back(resources[i].resource_id); |
| 843 ContinuePurgingResources(); | 870 ContinuePurgingResources(); |
| 844 } | 871 } |
| 845 | 872 |
| 846 void ServiceWorkerStorage::ContinuePurgingResources() { | 873 void ServiceWorkerStorage::ContinuePurgingResources() { |
| 847 if (purgeable_reource_ids_.empty() || is_purge_pending_) | 874 if (purgeable_resource_ids_.empty() || is_purge_pending_) |
| 848 return; | 875 return; |
| 849 | 876 |
| 850 // Do one at a time until we're done, use RunSoon to avoid recursion when | 877 // Do one at a time until we're done, use RunSoon to avoid recursion when |
| 851 // DoomEntry returns immediately. | 878 // DoomEntry returns immediately. |
| 852 is_purge_pending_ = true; | 879 is_purge_pending_ = true; |
| 853 int64 id = purgeable_reource_ids_.front(); | 880 int64 id = purgeable_resource_ids_.front(); |
| 854 purgeable_reource_ids_.pop_front(); | 881 purgeable_resource_ids_.pop_front(); |
| 855 RunSoon(FROM_HERE, | 882 RunSoon(FROM_HERE, |
| 856 base::Bind(&ServiceWorkerStorage::PurgeResource, | 883 base::Bind(&ServiceWorkerStorage::PurgeResource, |
| 857 weak_factory_.GetWeakPtr(), id)); | 884 weak_factory_.GetWeakPtr(), id)); |
| 858 } | 885 } |
| 859 | 886 |
| 860 void ServiceWorkerStorage::PurgeResource(int64 id) { | 887 void ServiceWorkerStorage::PurgeResource(int64 id) { |
| 861 DCHECK(is_purge_pending_); | 888 DCHECK(is_purge_pending_); |
| 862 int rv = disk_cache()->DoomEntry( | 889 int rv = disk_cache()->DoomEntry( |
| 863 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, | 890 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, |
| 864 weak_factory_.GetWeakPtr(), id)); | 891 weak_factory_.GetWeakPtr(), id)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 904 } | 931 } |
| 905 | 932 |
| 906 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 933 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 907 ServiceWorkerDatabase* database, | 934 ServiceWorkerDatabase* database, |
| 908 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 935 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 909 int64 registration_id, | 936 int64 registration_id, |
| 910 const GURL& origin, | 937 const GURL& origin, |
| 911 const DeleteRegistrationCallback& callback) { | 938 const DeleteRegistrationCallback& callback) { |
| 912 DCHECK(database); | 939 DCHECK(database); |
| 913 | 940 |
| 941 int64 version_id = kInvalidServiceWorkerVersionId; | |
| 914 std::vector<int64> newly_purgeable_resources; | 942 std::vector<int64> newly_purgeable_resources; |
| 915 ServiceWorkerDatabase::Status status = | 943 ServiceWorkerDatabase::Status status = database->DeleteRegistration( |
| 916 database->DeleteRegistration(registration_id, origin, | 944 registration_id, origin, &version_id, &newly_purgeable_resources); |
| 917 &newly_purgeable_resources); | |
| 918 if (status != ServiceWorkerDatabase::STATUS_OK) { | 945 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 919 original_task_runner->PostTask( | 946 original_task_runner->PostTask(FROM_HERE, |
| 920 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); | 947 base::Bind(callback, |
| 948 false, | |
| 949 kInvalidServiceWorkerVersionId, | |
| 950 std::vector<int64>(), | |
| 951 status)); | |
| 921 return; | 952 return; |
| 922 } | 953 } |
| 923 | 954 |
| 924 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the | 955 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the |
| 925 // unique origin list. | 956 // unique origin list. |
| 926 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | 957 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 927 status = database->GetRegistrationsForOrigin(origin, ®istrations); | 958 status = database->GetRegistrationsForOrigin(origin, ®istrations); |
| 928 if (status != ServiceWorkerDatabase::STATUS_OK) { | 959 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 929 original_task_runner->PostTask( | 960 original_task_runner->PostTask(FROM_HERE, |
| 930 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); | 961 base::Bind(callback, |
| 962 false, | |
| 963 kInvalidServiceWorkerVersionId, | |
| 964 std::vector<int64>(), | |
| 965 status)); | |
| 931 return; | 966 return; |
| 932 } | 967 } |
| 933 | 968 |
| 934 bool deletable = registrations.empty(); | 969 bool deletable = registrations.empty(); |
| 935 original_task_runner->PostTask( | 970 original_task_runner->PostTask( |
| 936 FROM_HERE, base::Bind(callback, deletable, | 971 FROM_HERE, |
| 937 newly_purgeable_resources, status)); | 972 base::Bind( |
| 973 callback, deletable, version_id, newly_purgeable_resources, status)); | |
| 938 } | 974 } |
| 939 | 975 |
| 940 void ServiceWorkerStorage::WriteRegistrationInDB( | 976 void ServiceWorkerStorage::WriteRegistrationInDB( |
| 941 ServiceWorkerDatabase* database, | 977 ServiceWorkerDatabase* database, |
| 942 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 978 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 943 const ServiceWorkerDatabase::RegistrationData& data, | 979 const ServiceWorkerDatabase::RegistrationData& data, |
| 944 const ResourceList& resources, | 980 const ResourceList& resources, |
| 945 const WriteRegistrationCallback& callback) { | 981 const WriteRegistrationCallback& callback) { |
| 946 DCHECK(database); | 982 DCHECK(database); |
| 983 int64 version_id = kInvalidServiceWorkerVersionId; | |
|
michaeln
2014/07/01 20:54:07
nit: maybe call this deleted_version_id
falken
2014/07/02 04:04:24
Done.
| |
| 947 std::vector<int64> newly_purgeable_resources; | 984 std::vector<int64> newly_purgeable_resources; |
| 948 ServiceWorkerDatabase::Status status = | 985 ServiceWorkerDatabase::Status status = database->WriteRegistration( |
| 949 database->WriteRegistration(data, resources, &newly_purgeable_resources); | 986 data, resources, &version_id, &newly_purgeable_resources); |
| 950 original_task_runner->PostTask( | 987 original_task_runner->PostTask(FROM_HERE, |
| 951 FROM_HERE, | 988 base::Bind(callback, |
| 952 base::Bind(callback, data.script.GetOrigin(), | 989 data.script.GetOrigin(), |
| 953 newly_purgeable_resources, status)); | 990 version_id, |
| 991 newly_purgeable_resources, | |
| 992 status)); | |
| 954 } | 993 } |
| 955 | 994 |
| 956 void ServiceWorkerStorage::FindForDocumentInDB( | 995 void ServiceWorkerStorage::FindForDocumentInDB( |
| 957 ServiceWorkerDatabase* database, | 996 ServiceWorkerDatabase* database, |
| 958 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 997 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 959 const GURL& document_url, | 998 const GURL& document_url, |
| 960 const FindInDBCallback& callback) { | 999 const FindInDBCallback& callback) { |
| 961 GURL origin = document_url.GetOrigin(); | 1000 GURL origin = document_url.GetOrigin(); |
| 962 RegistrationList registrations; | 1001 RegistrationList registrations; |
| 963 ServiceWorkerDatabase::Status status = | 1002 ServiceWorkerDatabase::Status status = |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1089 // Give up the corruption recovery until the browser restarts. | 1128 // Give up the corruption recovery until the browser restarts. |
| 1090 LOG(ERROR) << "Failed to delete the diskcache."; | 1129 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1091 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1130 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1092 return; | 1131 return; |
| 1093 } | 1132 } |
| 1094 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1133 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1095 callback.Run(SERVICE_WORKER_OK); | 1134 callback.Run(SERVICE_WORKER_OK); |
| 1096 } | 1135 } |
| 1097 | 1136 |
| 1098 } // namespace content | 1137 } // namespace content |
| OLD | NEW |