| 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()); |
| 849 if (resources.empty()) |
| 850 return; |
| 851 scoped_refptr<ServiceWorkerVersion> version = |
| 852 context_ ? context_->GetLiveVersion(version_id) : NULL; |
| 853 if (version && version->HasControllee()) { |
| 854 deleted_version_resource_ids_[version_id] = resources; |
| 855 version->AddListener(this); |
| 856 } else { |
| 857 StartPurgingResources(resources); |
| 858 } |
| 859 } |
| 860 |
| 832 void ServiceWorkerStorage::StartPurgingResources( | 861 void ServiceWorkerStorage::StartPurgingResources( |
| 833 const std::vector<int64>& ids) { | 862 const std::vector<int64>& ids) { |
| 834 for (size_t i = 0; i < ids.size(); ++i) | 863 for (size_t i = 0; i < ids.size(); ++i) |
| 835 purgeable_reource_ids_.push_back(ids[i]); | 864 purgeable_resource_ids_.push_back(ids[i]); |
| 836 ContinuePurgingResources(); | 865 ContinuePurgingResources(); |
| 837 } | 866 } |
| 838 | 867 |
| 839 void ServiceWorkerStorage::StartPurgingResources( | 868 void ServiceWorkerStorage::StartPurgingResources( |
| 840 const ResourceList& resources) { | 869 const ResourceList& resources) { |
| 841 for (size_t i = 0; i < resources.size(); ++i) | 870 for (size_t i = 0; i < resources.size(); ++i) |
| 842 purgeable_reource_ids_.push_back(resources[i].resource_id); | 871 purgeable_resource_ids_.push_back(resources[i].resource_id); |
| 843 ContinuePurgingResources(); | 872 ContinuePurgingResources(); |
| 844 } | 873 } |
| 845 | 874 |
| 846 void ServiceWorkerStorage::ContinuePurgingResources() { | 875 void ServiceWorkerStorage::ContinuePurgingResources() { |
| 847 if (purgeable_reource_ids_.empty() || is_purge_pending_) | 876 if (purgeable_resource_ids_.empty() || is_purge_pending_) |
| 848 return; | 877 return; |
| 849 | 878 |
| 850 // Do one at a time until we're done, use RunSoon to avoid recursion when | 879 // Do one at a time until we're done, use RunSoon to avoid recursion when |
| 851 // DoomEntry returns immediately. | 880 // DoomEntry returns immediately. |
| 852 is_purge_pending_ = true; | 881 is_purge_pending_ = true; |
| 853 int64 id = purgeable_reource_ids_.front(); | 882 int64 id = purgeable_resource_ids_.front(); |
| 854 purgeable_reource_ids_.pop_front(); | 883 purgeable_resource_ids_.pop_front(); |
| 855 RunSoon(FROM_HERE, | 884 RunSoon(FROM_HERE, |
| 856 base::Bind(&ServiceWorkerStorage::PurgeResource, | 885 base::Bind(&ServiceWorkerStorage::PurgeResource, |
| 857 weak_factory_.GetWeakPtr(), id)); | 886 weak_factory_.GetWeakPtr(), id)); |
| 858 } | 887 } |
| 859 | 888 |
| 860 void ServiceWorkerStorage::PurgeResource(int64 id) { | 889 void ServiceWorkerStorage::PurgeResource(int64 id) { |
| 861 DCHECK(is_purge_pending_); | 890 DCHECK(is_purge_pending_); |
| 862 int rv = disk_cache()->DoomEntry( | 891 int rv = disk_cache()->DoomEntry( |
| 863 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, | 892 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, |
| 864 weak_factory_.GetWeakPtr(), id)); | 893 weak_factory_.GetWeakPtr(), id)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 } | 933 } |
| 905 | 934 |
| 906 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 935 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 907 ServiceWorkerDatabase* database, | 936 ServiceWorkerDatabase* database, |
| 908 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 937 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 909 int64 registration_id, | 938 int64 registration_id, |
| 910 const GURL& origin, | 939 const GURL& origin, |
| 911 const DeleteRegistrationCallback& callback) { | 940 const DeleteRegistrationCallback& callback) { |
| 912 DCHECK(database); | 941 DCHECK(database); |
| 913 | 942 |
| 943 int64 version_id = kInvalidServiceWorkerVersionId; |
| 914 std::vector<int64> newly_purgeable_resources; | 944 std::vector<int64> newly_purgeable_resources; |
| 915 ServiceWorkerDatabase::Status status = | 945 ServiceWorkerDatabase::Status status = database->DeleteRegistration( |
| 916 database->DeleteRegistration(registration_id, origin, | 946 registration_id, origin, &version_id, &newly_purgeable_resources); |
| 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, |
| 950 false, |
| 951 kInvalidServiceWorkerVersionId, |
| 952 std::vector<int64>(), |
| 953 status)); |
| 921 return; | 954 return; |
| 922 } | 955 } |
| 923 | 956 |
| 924 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the | 957 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the |
| 925 // unique origin list. | 958 // unique origin list. |
| 926 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | 959 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 927 status = database->GetRegistrationsForOrigin(origin, ®istrations); | 960 status = database->GetRegistrationsForOrigin(origin, ®istrations); |
| 928 if (status != ServiceWorkerDatabase::STATUS_OK) { | 961 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 929 original_task_runner->PostTask( | 962 original_task_runner->PostTask(FROM_HERE, |
| 930 FROM_HERE, base::Bind(callback, false, std::vector<int64>(), status)); | 963 base::Bind(callback, |
| 964 false, |
| 965 kInvalidServiceWorkerVersionId, |
| 966 std::vector<int64>(), |
| 967 status)); |
| 931 return; | 968 return; |
| 932 } | 969 } |
| 933 | 970 |
| 934 bool deletable = registrations.empty(); | 971 bool deletable = registrations.empty(); |
| 935 original_task_runner->PostTask( | 972 original_task_runner->PostTask( |
| 936 FROM_HERE, base::Bind(callback, deletable, | 973 FROM_HERE, |
| 937 newly_purgeable_resources, status)); | 974 base::Bind( |
| 975 callback, deletable, version_id, newly_purgeable_resources, status)); |
| 938 } | 976 } |
| 939 | 977 |
| 940 void ServiceWorkerStorage::WriteRegistrationInDB( | 978 void ServiceWorkerStorage::WriteRegistrationInDB( |
| 941 ServiceWorkerDatabase* database, | 979 ServiceWorkerDatabase* database, |
| 942 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 980 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 943 const ServiceWorkerDatabase::RegistrationData& data, | 981 const ServiceWorkerDatabase::RegistrationData& data, |
| 944 const ResourceList& resources, | 982 const ResourceList& resources, |
| 945 const WriteRegistrationCallback& callback) { | 983 const WriteRegistrationCallback& callback) { |
| 946 DCHECK(database); | 984 DCHECK(database); |
| 985 int64 deleted_version_id = kInvalidServiceWorkerVersionId; |
| 947 std::vector<int64> newly_purgeable_resources; | 986 std::vector<int64> newly_purgeable_resources; |
| 948 ServiceWorkerDatabase::Status status = | 987 ServiceWorkerDatabase::Status status = database->WriteRegistration( |
| 949 database->WriteRegistration(data, resources, &newly_purgeable_resources); | 988 data, resources, &deleted_version_id, &newly_purgeable_resources); |
| 950 original_task_runner->PostTask( | 989 original_task_runner->PostTask(FROM_HERE, |
| 951 FROM_HERE, | 990 base::Bind(callback, |
| 952 base::Bind(callback, data.script.GetOrigin(), | 991 data.script.GetOrigin(), |
| 953 newly_purgeable_resources, status)); | 992 deleted_version_id, |
| 993 newly_purgeable_resources, |
| 994 status)); |
| 954 } | 995 } |
| 955 | 996 |
| 956 void ServiceWorkerStorage::FindForDocumentInDB( | 997 void ServiceWorkerStorage::FindForDocumentInDB( |
| 957 ServiceWorkerDatabase* database, | 998 ServiceWorkerDatabase* database, |
| 958 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 999 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 959 const GURL& document_url, | 1000 const GURL& document_url, |
| 960 const FindInDBCallback& callback) { | 1001 const FindInDBCallback& callback) { |
| 961 GURL origin = document_url.GetOrigin(); | 1002 GURL origin = document_url.GetOrigin(); |
| 962 RegistrationList registrations; | 1003 RegistrationList registrations; |
| 963 ServiceWorkerDatabase::Status status = | 1004 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. | 1130 // Give up the corruption recovery until the browser restarts. |
| 1090 LOG(ERROR) << "Failed to delete the diskcache."; | 1131 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1091 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1132 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1092 return; | 1133 return; |
| 1093 } | 1134 } |
| 1094 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1135 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1095 callback.Run(SERVICE_WORKER_OK); | 1136 callback.Run(SERVICE_WORKER_OK); |
| 1096 } | 1137 } |
| 1097 | 1138 |
| 1098 } // namespace content | 1139 } // namespace content |
| OLD | NEW |