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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 namespace content { | 32 namespace content { |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 void RunSoon(const tracked_objects::Location& from_here, | 36 void RunSoon(const tracked_objects::Location& from_here, |
37 const base::Closure& closure) { | 37 const base::Closure& closure) { |
38 base::ThreadTaskRunnerHandle::Get()->PostTask(from_here, closure); | 38 base::ThreadTaskRunnerHandle::Get()->PostTask(from_here, closure); |
39 } | 39 } |
40 | 40 |
41 void CompleteFindNow( | 41 void CompleteFindNow( |
42 scoped_refptr<ServiceWorkerRegistration> registration, | 42 const scoped_refptr<ServiceWorkerRegistration>& registration, |
43 ServiceWorkerStatusCode status, | 43 ServiceWorkerStatusCode status, |
44 const ServiceWorkerStorage::FindRegistrationCallback& callback) { | 44 const ServiceWorkerStorage::FindRegistrationCallback& callback) { |
45 if (registration && registration->is_deleted()) { | 45 if (registration && registration->is_deleted()) { |
46 // It's past the point of no return and no longer findable. | 46 // It's past the point of no return and no longer findable. |
47 callback.Run(SERVICE_WORKER_ERROR_NOT_FOUND, nullptr); | 47 callback.Run(SERVICE_WORKER_ERROR_NOT_FOUND, nullptr); |
48 return; | 48 return; |
49 } | 49 } |
50 callback.Run(status, std::move(registration)); | 50 callback.Run(status, registration); |
51 } | 51 } |
52 | 52 |
53 void CompleteFindSoon( | 53 void CompleteFindSoon( |
54 const tracked_objects::Location& from_here, | 54 const tracked_objects::Location& from_here, |
55 scoped_refptr<ServiceWorkerRegistration> registration, | 55 const scoped_refptr<ServiceWorkerRegistration>& registration, |
56 ServiceWorkerStatusCode status, | 56 ServiceWorkerStatusCode status, |
57 const ServiceWorkerStorage::FindRegistrationCallback& callback) { | 57 const ServiceWorkerStorage::FindRegistrationCallback& callback) { |
58 RunSoon(from_here, base::Bind(&CompleteFindNow, std::move(registration), | 58 RunSoon(from_here, |
59 status, callback)); | 59 base::Bind(&CompleteFindNow, registration, status, callback)); |
60 } | 60 } |
61 | 61 |
62 const base::FilePath::CharType kDatabaseName[] = | 62 const base::FilePath::CharType kDatabaseName[] = |
63 FILE_PATH_LITERAL("Database"); | 63 FILE_PATH_LITERAL("Database"); |
64 const base::FilePath::CharType kDiskCacheName[] = | 64 const base::FilePath::CharType kDiskCacheName[] = |
65 FILE_PATH_LITERAL("ScriptCache"); | 65 FILE_PATH_LITERAL("ScriptCache"); |
66 | 66 |
67 const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; | 67 const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; |
68 const int kMaxDiskCacheSize = 250 * 1024 * 1024; | 68 const int kMaxDiskCacheSize = 250 * 1024 * 1024; |
69 | 69 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 FindInstallingRegistrationForDocument(document_url); | 160 FindInstallingRegistrationForDocument(document_url); |
161 ServiceWorkerStatusCode status = installing_registration | 161 ServiceWorkerStatusCode status = installing_registration |
162 ? SERVICE_WORKER_OK | 162 ? SERVICE_WORKER_OK |
163 : SERVICE_WORKER_ERROR_NOT_FOUND; | 163 : SERVICE_WORKER_ERROR_NOT_FOUND; |
164 TRACE_EVENT_INSTANT2( | 164 TRACE_EVENT_INSTANT2( |
165 "ServiceWorker", | 165 "ServiceWorker", |
166 "ServiceWorkerStorage::FindRegistrationForDocument:CheckInstalling", | 166 "ServiceWorkerStorage::FindRegistrationForDocument:CheckInstalling", |
167 TRACE_EVENT_SCOPE_THREAD, | 167 TRACE_EVENT_SCOPE_THREAD, |
168 "URL", document_url.spec(), | 168 "URL", document_url.spec(), |
169 "Status", ServiceWorkerStatusToString(status)); | 169 "Status", ServiceWorkerStatusToString(status)); |
170 CompleteFindNow(std::move(installing_registration), status, callback); | 170 CompleteFindNow(installing_registration, |
| 171 status, |
| 172 callback); |
171 return; | 173 return; |
172 } | 174 } |
173 | 175 |
174 // To connect this TRACE_EVENT with the callback, TimeTicks is used for | 176 // To connect this TRACE_EVENT with the callback, TimeTicks is used for |
175 // callback id. | 177 // callback id. |
176 int64_t callback_id = base::TimeTicks::Now().ToInternalValue(); | 178 int64_t callback_id = base::TimeTicks::Now().ToInternalValue(); |
177 TRACE_EVENT_ASYNC_BEGIN1( | 179 TRACE_EVENT_ASYNC_BEGIN1( |
178 "ServiceWorker", | 180 "ServiceWorker", |
179 "ServiceWorkerStorage::FindRegistrationForDocument", | 181 "ServiceWorkerStorage::FindRegistrationForDocument", |
180 callback_id, | 182 callback_id, |
(...skipping 24 matching lines...) Expand all Loading... |
205 } | 207 } |
206 return; | 208 return; |
207 } | 209 } |
208 DCHECK_EQ(INITIALIZED, state_); | 210 DCHECK_EQ(INITIALIZED, state_); |
209 | 211 |
210 // See if there are any stored registrations for the origin. | 212 // See if there are any stored registrations for the origin. |
211 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { | 213 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { |
212 // Look for something currently being installed. | 214 // Look for something currently being installed. |
213 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 215 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
214 FindInstallingRegistrationForPattern(scope); | 216 FindInstallingRegistrationForPattern(scope); |
215 ServiceWorkerStatusCode installing_status = | 217 CompleteFindSoon(FROM_HERE, installing_registration, |
216 installing_registration ? SERVICE_WORKER_OK | 218 installing_registration ? SERVICE_WORKER_OK |
217 : SERVICE_WORKER_ERROR_NOT_FOUND; | 219 : SERVICE_WORKER_ERROR_NOT_FOUND, |
218 CompleteFindSoon(FROM_HERE, std::move(installing_registration), | 220 callback); |
219 installing_status, callback); | |
220 return; | 221 return; |
221 } | 222 } |
222 | 223 |
223 database_task_manager_->GetTaskRunner()->PostTask( | 224 database_task_manager_->GetTaskRunner()->PostTask( |
224 FROM_HERE, | 225 FROM_HERE, |
225 base::Bind( | 226 base::Bind( |
226 &FindForPatternInDB, | 227 &FindForPatternInDB, |
227 database_.get(), | 228 database_.get(), |
228 base::ThreadTaskRunnerHandle::Get(), | 229 base::ThreadTaskRunnerHandle::Get(), |
229 scope, | 230 scope, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 CompleteFindNow(installing_registration, | 270 CompleteFindNow(installing_registration, |
270 installing_registration ? SERVICE_WORKER_OK | 271 installing_registration ? SERVICE_WORKER_OK |
271 : SERVICE_WORKER_ERROR_NOT_FOUND, | 272 : SERVICE_WORKER_ERROR_NOT_FOUND, |
272 callback); | 273 callback); |
273 return; | 274 return; |
274 } | 275 } |
275 | 276 |
276 scoped_refptr<ServiceWorkerRegistration> registration = | 277 scoped_refptr<ServiceWorkerRegistration> registration = |
277 context_->GetLiveRegistration(registration_id); | 278 context_->GetLiveRegistration(registration_id); |
278 if (registration) { | 279 if (registration) { |
279 CompleteFindNow(std::move(registration), SERVICE_WORKER_OK, callback); | 280 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); |
280 return; | 281 return; |
281 } | 282 } |
282 | 283 |
283 database_task_manager_->GetTaskRunner()->PostTask( | 284 database_task_manager_->GetTaskRunner()->PostTask( |
284 FROM_HERE, | 285 FROM_HERE, |
285 base::Bind(&FindForIdInDB, | 286 base::Bind(&FindForIdInDB, |
286 database_.get(), | 287 database_.get(), |
287 base::ThreadTaskRunnerHandle::Get(), | 288 base::ThreadTaskRunnerHandle::Get(), |
288 registration_id, | 289 registration_id, |
289 origin, | 290 origin, |
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 return; | 889 return; |
889 } | 890 } |
890 | 891 |
891 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 892 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
892 // Look for something currently being installed. | 893 // Look for something currently being installed. |
893 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 894 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
894 FindInstallingRegistrationForDocument(document_url); | 895 FindInstallingRegistrationForDocument(document_url); |
895 ServiceWorkerStatusCode installing_status = | 896 ServiceWorkerStatusCode installing_status = |
896 installing_registration ? SERVICE_WORKER_OK | 897 installing_registration ? SERVICE_WORKER_OK |
897 : SERVICE_WORKER_ERROR_NOT_FOUND; | 898 : SERVICE_WORKER_ERROR_NOT_FOUND; |
898 callback.Run(installing_status, std::move(installing_registration)); | 899 callback.Run(installing_status, installing_registration); |
899 TRACE_EVENT_ASYNC_END2( | 900 TRACE_EVENT_ASYNC_END2( |
900 "ServiceWorker", | 901 "ServiceWorker", |
901 "ServiceWorkerStorage::FindRegistrationForDocument", | 902 "ServiceWorkerStorage::FindRegistrationForDocument", |
902 callback_id, | 903 callback_id, |
903 "Status", ServiceWorkerDatabase::StatusToString(status), | 904 "Status", ServiceWorkerDatabase::StatusToString(status), |
904 "Info", | 905 "Info", |
905 (installing_status == SERVICE_WORKER_OK) ? | 906 (installing_status == SERVICE_WORKER_OK) ? |
906 "Installing registration is found" : | 907 "Installing registration is found" : |
907 "Any registrations are not found"); | 908 "Any registrations are not found"); |
908 return; | 909 return; |
(...skipping 16 matching lines...) Expand all Loading... |
925 const ResourceList& resources, | 926 const ResourceList& resources, |
926 ServiceWorkerDatabase::Status status) { | 927 ServiceWorkerDatabase::Status status) { |
927 if (status == ServiceWorkerDatabase::STATUS_OK) { | 928 if (status == ServiceWorkerDatabase::STATUS_OK) { |
928 ReturnFoundRegistration(callback, data, resources); | 929 ReturnFoundRegistration(callback, data, resources); |
929 return; | 930 return; |
930 } | 931 } |
931 | 932 |
932 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 933 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
933 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 934 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
934 FindInstallingRegistrationForPattern(scope); | 935 FindInstallingRegistrationForPattern(scope); |
935 ServiceWorkerStatusCode installing_status = | 936 callback.Run(installing_registration ? SERVICE_WORKER_OK |
936 installing_registration ? SERVICE_WORKER_OK | 937 : SERVICE_WORKER_ERROR_NOT_FOUND, |
937 : SERVICE_WORKER_ERROR_NOT_FOUND; | 938 installing_registration); |
938 callback.Run(installing_status, std::move(installing_registration)); | |
939 return; | 939 return; |
940 } | 940 } |
941 | 941 |
942 ScheduleDeleteAndStartOver(); | 942 ScheduleDeleteAndStartOver(); |
943 callback.Run(DatabaseStatusToStatusCode(status), | 943 callback.Run(DatabaseStatusToStatusCode(status), |
944 scoped_refptr<ServiceWorkerRegistration>()); | 944 scoped_refptr<ServiceWorkerRegistration>()); |
945 } | 945 } |
946 | 946 |
947 void ServiceWorkerStorage::DidFindRegistrationForId( | 947 void ServiceWorkerStorage::DidFindRegistrationForId( |
948 const FindRegistrationCallback& callback, | 948 const FindRegistrationCallback& callback, |
(...skipping 17 matching lines...) Expand all Loading... |
966 scoped_refptr<ServiceWorkerRegistration>()); | 966 scoped_refptr<ServiceWorkerRegistration>()); |
967 } | 967 } |
968 | 968 |
969 void ServiceWorkerStorage::ReturnFoundRegistration( | 969 void ServiceWorkerStorage::ReturnFoundRegistration( |
970 const FindRegistrationCallback& callback, | 970 const FindRegistrationCallback& callback, |
971 const ServiceWorkerDatabase::RegistrationData& data, | 971 const ServiceWorkerDatabase::RegistrationData& data, |
972 const ResourceList& resources) { | 972 const ResourceList& resources) { |
973 DCHECK(!resources.empty()); | 973 DCHECK(!resources.empty()); |
974 scoped_refptr<ServiceWorkerRegistration> registration = | 974 scoped_refptr<ServiceWorkerRegistration> registration = |
975 GetOrCreateRegistration(data, resources); | 975 GetOrCreateRegistration(data, resources); |
976 CompleteFindNow(std::move(registration), SERVICE_WORKER_OK, callback); | 976 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); |
977 } | 977 } |
978 | 978 |
979 void ServiceWorkerStorage::DidGetRegistrations( | 979 void ServiceWorkerStorage::DidGetRegistrations( |
980 const GetRegistrationsCallback& callback, | 980 const GetRegistrationsCallback& callback, |
981 RegistrationList* registration_data_list, | 981 RegistrationList* registration_data_list, |
982 std::vector<ResourceList>* resources_list, | 982 std::vector<ResourceList>* resources_list, |
983 const GURL& origin_filter, | 983 const GURL& origin_filter, |
984 ServiceWorkerDatabase::Status status) { | 984 ServiceWorkerDatabase::Status status) { |
985 DCHECK(registration_data_list); | 985 DCHECK(registration_data_list); |
986 DCHECK(resources_list); | 986 DCHECK(resources_list); |
(...skipping 18 matching lines...) Expand all Loading... |
1005 | 1005 |
1006 // Add unstored registrations that are being installed. | 1006 // Add unstored registrations that are being installed. |
1007 for (const auto& registration : installing_registrations_) { | 1007 for (const auto& registration : installing_registrations_) { |
1008 if ((!origin_filter.is_valid() || | 1008 if ((!origin_filter.is_valid() || |
1009 registration.second->pattern().GetOrigin() == origin_filter) && | 1009 registration.second->pattern().GetOrigin() == origin_filter) && |
1010 registration_ids.insert(registration.first).second) { | 1010 registration_ids.insert(registration.first).second) { |
1011 registrations.push_back(registration.second); | 1011 registrations.push_back(registration.second); |
1012 } | 1012 } |
1013 } | 1013 } |
1014 | 1014 |
1015 callback.Run(SERVICE_WORKER_OK, std::move(registrations)); | 1015 callback.Run(SERVICE_WORKER_OK, registrations); |
1016 } | 1016 } |
1017 | 1017 |
1018 void ServiceWorkerStorage::DidGetRegistrationsInfos( | 1018 void ServiceWorkerStorage::DidGetRegistrationsInfos( |
1019 const GetRegistrationsInfosCallback& callback, | 1019 const GetRegistrationsInfosCallback& callback, |
1020 RegistrationList* registration_data_list, | 1020 RegistrationList* registration_data_list, |
1021 const GURL& origin_filter, | 1021 const GURL& origin_filter, |
1022 ServiceWorkerDatabase::Status status) { | 1022 ServiceWorkerDatabase::Status status) { |
1023 DCHECK(registration_data_list); | 1023 DCHECK(registration_data_list); |
1024 if (status != ServiceWorkerDatabase::STATUS_OK && | 1024 if (status != ServiceWorkerDatabase::STATUS_OK && |
1025 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1025 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1770 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1770 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
1771 return; | 1771 return; |
1772 } | 1772 } |
1773 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1773 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
1774 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( | 1774 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( |
1775 ServiceWorkerMetrics::DELETE_OK); | 1775 ServiceWorkerMetrics::DELETE_OK); |
1776 callback.Run(SERVICE_WORKER_OK); | 1776 callback.Run(SERVICE_WORKER_OK); |
1777 } | 1777 } |
1778 | 1778 |
1779 } // namespace content | 1779 } // namespace content |
OLD | NEW |