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/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 472 data.resources_total_size_bytes = resources_total_size_bytes; | 472 data.resources_total_size_bytes = resources_total_size_bytes; |
| 473 | 473 |
| 474 if (!has_checked_for_stale_resources_) | 474 if (!has_checked_for_stale_resources_) |
| 475 DeleteStaleResources(); | 475 DeleteStaleResources(); |
| 476 | 476 |
| 477 database_task_manager_->GetTaskRunner()->PostTask( | 477 database_task_manager_->GetTaskRunner()->PostTask( |
| 478 FROM_HERE, | 478 FROM_HERE, |
| 479 base::Bind(&WriteRegistrationInDB, | 479 base::Bind(&WriteRegistrationInDB, |
| 480 database_.get(), | 480 database_.get(), |
| 481 base::MessageLoopProxy::current(), | 481 base::MessageLoopProxy::current(), |
| 482 data, resources, | 482 data, |
| 483 resources, | |
| 483 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, | 484 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, |
| 484 weak_factory_.GetWeakPtr(), | 485 weak_factory_.GetWeakPtr(), |
| 485 callback))); | 486 callback, |
| 487 data))); | |
| 486 | 488 |
| 487 registration->set_is_deleted(false); | 489 registration->set_is_deleted(false); |
| 488 | |
| 489 // TODO(dmurph): Add correct byte delta. | |
| 490 if (quota_manager_proxy_.get()) { | |
| 491 // Can be nullptr in tests. | |
| 492 quota_manager_proxy_->NotifyStorageModified( | |
| 493 storage::QuotaClient::kServiceWorker, | |
| 494 registration->pattern().GetOrigin(), | |
| 495 storage::StorageType::kStorageTypeTemporary, | |
| 496 0); | |
| 497 } | |
| 498 } | 490 } |
| 499 | 491 |
| 500 void ServiceWorkerStorage::UpdateToActiveState( | 492 void ServiceWorkerStorage::UpdateToActiveState( |
| 501 ServiceWorkerRegistration* registration, | 493 ServiceWorkerRegistration* registration, |
| 502 const StatusCallback& callback) { | 494 const StatusCallback& callback) { |
| 503 DCHECK(registration); | 495 DCHECK(registration); |
| 504 | 496 |
| 505 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 497 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
| 506 if (IsDisabled() || !context_) { | 498 if (IsDisabled() || !context_) { |
| 507 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 499 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 971 it != installing_registrations_.end(); ++it) { | 963 it != installing_registrations_.end(); ++it) { |
| 972 if (pushed_registrations.insert(it->first).second) | 964 if (pushed_registrations.insert(it->first).second) |
| 973 infos.push_back(it->second->GetInfo()); | 965 infos.push_back(it->second->GetInfo()); |
| 974 } | 966 } |
| 975 | 967 |
| 976 callback.Run(infos); | 968 callback.Run(infos); |
| 977 } | 969 } |
| 978 | 970 |
| 979 void ServiceWorkerStorage::DidStoreRegistration( | 971 void ServiceWorkerStorage::DidStoreRegistration( |
| 980 const StatusCallback& callback, | 972 const StatusCallback& callback, |
| 973 const ServiceWorkerDatabase::RegistrationData& new_version, | |
| 981 const GURL& origin, | 974 const GURL& origin, |
| 982 int64 deleted_version_id, | 975 const ServiceWorkerDatabase::RegistrationData& deleted_version, |
| 983 const std::vector<int64>& newly_purgeable_resources, | 976 const std::vector<int64>& newly_purgeable_resources, |
| 984 ServiceWorkerDatabase::Status status) { | 977 ServiceWorkerDatabase::Status status) { |
| 985 if (status != ServiceWorkerDatabase::STATUS_OK) { | 978 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 986 ScheduleDeleteAndStartOver(); | 979 ScheduleDeleteAndStartOver(); |
| 987 callback.Run(DatabaseStatusToStatusCode(status)); | 980 callback.Run(DatabaseStatusToStatusCode(status)); |
| 988 return; | 981 return; |
| 989 } | 982 } |
| 983 | |
| 984 // Doing all size updates | |
|
michaeln
2014/10/30 22:43:37
comment may not be needed
dmurph
2014/10/31 19:10:49
Done.
| |
| 985 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 986 context_->GetLiveRegistration(new_version.registration_id); | |
| 987 registration->set_resources_total_size_bytes( | |
| 988 new_version.resources_total_size_bytes); | |
| 989 if (quota_manager_proxy_.get()) { | |
| 990 // Can be nullptr in tests. | |
| 991 quota_manager_proxy_->NotifyStorageModified( | |
| 992 storage::QuotaClient::kServiceWorker, | |
| 993 origin, | |
| 994 storage::StorageType::kStorageTypeTemporary, | |
| 995 new_version.resources_total_size_bytes - | |
| 996 deleted_version.resources_total_size_bytes); | |
| 997 } | |
| 998 | |
| 990 registered_origins_.insert(origin); | 999 registered_origins_.insert(origin); |
| 991 callback.Run(SERVICE_WORKER_OK); | 1000 callback.Run(SERVICE_WORKER_OK); |
| 992 | 1001 |
| 993 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) | 1002 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id)) |
| 994 StartPurgingResources(newly_purgeable_resources); | 1003 StartPurgingResources(newly_purgeable_resources); |
| 995 } | 1004 } |
| 996 | 1005 |
| 997 void ServiceWorkerStorage::DidUpdateToActiveState( | 1006 void ServiceWorkerStorage::DidUpdateToActiveState( |
| 998 const StatusCallback& callback, | 1007 const StatusCallback& callback, |
| 999 ServiceWorkerDatabase::Status status) { | 1008 ServiceWorkerDatabase::Status status) { |
| 1000 if (status != ServiceWorkerDatabase::STATUS_OK && | 1009 if (status != ServiceWorkerDatabase::STATUS_OK && |
| 1001 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1010 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
| 1002 ScheduleDeleteAndStartOver(); | 1011 ScheduleDeleteAndStartOver(); |
| 1003 } | 1012 } |
| 1004 callback.Run(DatabaseStatusToStatusCode(status)); | 1013 callback.Run(DatabaseStatusToStatusCode(status)); |
| 1005 } | 1014 } |
| 1006 | 1015 |
| 1007 void ServiceWorkerStorage::DidDeleteRegistration( | 1016 void ServiceWorkerStorage::DidDeleteRegistration( |
| 1008 const DidDeleteRegistrationParams& params, | 1017 const DidDeleteRegistrationParams& params, |
| 1009 bool origin_is_deletable, | 1018 bool origin_is_deletable, |
| 1010 int64 version_id, | 1019 const ServiceWorkerDatabase::RegistrationData& deleted_version, |
| 1011 const std::vector<int64>& newly_purgeable_resources, | 1020 const std::vector<int64>& newly_purgeable_resources, |
| 1012 ServiceWorkerDatabase::Status status) { | 1021 ServiceWorkerDatabase::Status status) { |
| 1013 pending_deletions_.erase(params.registration_id); | 1022 pending_deletions_.erase(params.registration_id); |
| 1014 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1023 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1015 ScheduleDeleteAndStartOver(); | 1024 ScheduleDeleteAndStartOver(); |
| 1016 params.callback.Run(DatabaseStatusToStatusCode(status)); | 1025 params.callback.Run(DatabaseStatusToStatusCode(status)); |
| 1017 return; | 1026 return; |
| 1018 } | 1027 } |
| 1028 if (quota_manager_proxy_.get()) { | |
| 1029 // Can be nullptr in tests. | |
| 1030 quota_manager_proxy_->NotifyStorageModified( | |
| 1031 storage::QuotaClient::kServiceWorker, | |
| 1032 params.origin, | |
| 1033 storage::StorageType::kStorageTypeTemporary, | |
| 1034 -deleted_version.resources_total_size_bytes); | |
| 1035 } | |
| 1019 if (origin_is_deletable) | 1036 if (origin_is_deletable) |
| 1020 registered_origins_.erase(params.origin); | 1037 registered_origins_.erase(params.origin); |
| 1021 params.callback.Run(SERVICE_WORKER_OK); | 1038 params.callback.Run(SERVICE_WORKER_OK); |
| 1022 | 1039 |
| 1023 if (!context_ || !context_->GetLiveVersion(version_id)) | 1040 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id)) |
| 1024 StartPurgingResources(newly_purgeable_resources); | 1041 StartPurgingResources(newly_purgeable_resources); |
| 1025 } | 1042 } |
| 1026 | 1043 |
| 1027 scoped_refptr<ServiceWorkerRegistration> | 1044 scoped_refptr<ServiceWorkerRegistration> |
| 1028 ServiceWorkerStorage::GetOrCreateRegistration( | 1045 ServiceWorkerStorage::GetOrCreateRegistration( |
| 1029 const ServiceWorkerDatabase::RegistrationData& data, | 1046 const ServiceWorkerDatabase::RegistrationData& data, |
| 1030 const ResourceList& resources) { | 1047 const ResourceList& resources) { |
| 1031 scoped_refptr<ServiceWorkerRegistration> registration = | 1048 scoped_refptr<ServiceWorkerRegistration> registration = |
| 1032 context_->GetLiveRegistration(data.registration_id); | 1049 context_->GetLiveRegistration(data.registration_id); |
| 1033 if (registration.get()) | 1050 if (registration.get()) |
| 1034 return registration; | 1051 return registration; |
| 1035 | 1052 |
| 1036 registration = new ServiceWorkerRegistration( | 1053 registration = new ServiceWorkerRegistration( |
| 1037 data.scope, data.registration_id, context_); | 1054 data.scope, data.registration_id, context_); |
| 1055 registration->set_resources_total_size_bytes(data.resources_total_size_bytes); | |
| 1038 registration->set_last_update_check(data.last_update_check); | 1056 registration->set_last_update_check(data.last_update_check); |
| 1039 if (pending_deletions_.find(data.registration_id) != | 1057 if (pending_deletions_.find(data.registration_id) != |
| 1040 pending_deletions_.end()) { | 1058 pending_deletions_.end()) { |
| 1041 registration->set_is_deleted(true); | 1059 registration->set_is_deleted(true); |
| 1042 } | 1060 } |
| 1043 scoped_refptr<ServiceWorkerVersion> version = | 1061 scoped_refptr<ServiceWorkerVersion> version = |
| 1044 context_->GetLiveVersion(data.version_id); | 1062 context_->GetLiveVersion(data.version_id); |
| 1045 if (!version.get()) { | 1063 if (!version.get()) { |
| 1046 version = new ServiceWorkerVersion( | 1064 version = new ServiceWorkerVersion( |
| 1047 registration.get(), data.script, data.version_id, context_); | 1065 registration.get(), data.script, data.version_id, context_); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1287 } | 1305 } |
| 1288 | 1306 |
| 1289 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 1307 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 1290 ServiceWorkerDatabase* database, | 1308 ServiceWorkerDatabase* database, |
| 1291 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1309 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1292 int64 registration_id, | 1310 int64 registration_id, |
| 1293 const GURL& origin, | 1311 const GURL& origin, |
| 1294 const DeleteRegistrationCallback& callback) { | 1312 const DeleteRegistrationCallback& callback) { |
| 1295 DCHECK(database); | 1313 DCHECK(database); |
| 1296 | 1314 |
| 1297 int64 version_id = kInvalidServiceWorkerVersionId; | 1315 ServiceWorkerDatabase::RegistrationData deleted_version; |
| 1298 std::vector<int64> newly_purgeable_resources; | 1316 std::vector<int64> newly_purgeable_resources; |
| 1299 ServiceWorkerDatabase::Status status = database->DeleteRegistration( | 1317 ServiceWorkerDatabase::Status status = database->DeleteRegistration( |
| 1300 registration_id, origin, &version_id, &newly_purgeable_resources); | 1318 registration_id, origin, &deleted_version, &newly_purgeable_resources); |
| 1301 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1319 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1302 original_task_runner->PostTask(FROM_HERE, | 1320 original_task_runner->PostTask( |
| 1303 base::Bind(callback, | 1321 FROM_HERE, |
| 1304 false, | 1322 base::Bind( |
| 1305 kInvalidServiceWorkerVersionId, | 1323 callback, false, deleted_version, std::vector<int64>(), status)); |
| 1306 std::vector<int64>(), | |
| 1307 status)); | |
| 1308 return; | 1324 return; |
| 1309 } | 1325 } |
| 1310 | 1326 |
| 1311 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the | 1327 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the |
| 1312 // unique origin list. | 1328 // unique origin list. |
| 1313 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | 1329 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 1314 status = database->GetRegistrationsForOrigin(origin, ®istrations); | 1330 status = database->GetRegistrationsForOrigin(origin, ®istrations); |
| 1315 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1331 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1316 original_task_runner->PostTask(FROM_HERE, | 1332 original_task_runner->PostTask( |
| 1317 base::Bind(callback, | 1333 FROM_HERE, |
| 1318 false, | 1334 base::Bind( |
| 1319 kInvalidServiceWorkerVersionId, | 1335 callback, false, deleted_version, std::vector<int64>(), status)); |
| 1320 std::vector<int64>(), | |
| 1321 status)); | |
| 1322 return; | 1336 return; |
| 1323 } | 1337 } |
| 1324 | 1338 |
| 1325 bool deletable = registrations.empty(); | 1339 bool deletable = registrations.empty(); |
| 1326 original_task_runner->PostTask( | 1340 original_task_runner->PostTask(FROM_HERE, |
|
michaeln
2014/10/30 22:43:37
might as well make the line wrapping style consist
dmurph
2014/10/31 19:10:49
It no longer fits
| |
| 1327 FROM_HERE, | 1341 base::Bind(callback, |
| 1328 base::Bind( | 1342 deletable, |
| 1329 callback, deletable, version_id, newly_purgeable_resources, status)); | 1343 deleted_version, |
| 1344 newly_purgeable_resources, | |
| 1345 status)); | |
| 1330 } | 1346 } |
| 1331 | 1347 |
| 1332 void ServiceWorkerStorage::WriteRegistrationInDB( | 1348 void ServiceWorkerStorage::WriteRegistrationInDB( |
| 1333 ServiceWorkerDatabase* database, | 1349 ServiceWorkerDatabase* database, |
| 1334 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1350 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1335 const ServiceWorkerDatabase::RegistrationData& data, | 1351 const ServiceWorkerDatabase::RegistrationData& data, |
| 1336 const ResourceList& resources, | 1352 const ResourceList& resources, |
| 1337 const WriteRegistrationCallback& callback) { | 1353 const WriteRegistrationCallback& callback) { |
| 1338 DCHECK(database); | 1354 DCHECK(database); |
| 1339 int64 deleted_version_id = kInvalidServiceWorkerVersionId; | 1355 ServiceWorkerDatabase::RegistrationData deleted_version; |
| 1340 std::vector<int64> newly_purgeable_resources; | 1356 std::vector<int64> newly_purgeable_resources; |
| 1341 ServiceWorkerDatabase::Status status = database->WriteRegistration( | 1357 ServiceWorkerDatabase::Status status = database->WriteRegistration( |
| 1342 data, resources, &deleted_version_id, &newly_purgeable_resources); | 1358 data, resources, &deleted_version, &newly_purgeable_resources); |
| 1343 original_task_runner->PostTask(FROM_HERE, | 1359 original_task_runner->PostTask(FROM_HERE, |
| 1344 base::Bind(callback, | 1360 base::Bind(callback, |
| 1345 data.script.GetOrigin(), | 1361 data.script.GetOrigin(), |
| 1346 deleted_version_id, | 1362 deleted_version, |
| 1347 newly_purgeable_resources, | 1363 newly_purgeable_resources, |
| 1348 status)); | 1364 status)); |
| 1349 } | 1365 } |
| 1350 | 1366 |
| 1351 void ServiceWorkerStorage::FindForDocumentInDB( | 1367 void ServiceWorkerStorage::FindForDocumentInDB( |
| 1352 ServiceWorkerDatabase* database, | 1368 ServiceWorkerDatabase* database, |
| 1353 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1369 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1354 const GURL& document_url, | 1370 const GURL& document_url, |
| 1355 const FindInDBCallback& callback) { | 1371 const FindInDBCallback& callback) { |
| 1356 GURL origin = document_url.GetOrigin(); | 1372 GURL origin = document_url.GetOrigin(); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1495 // Give up the corruption recovery until the browser restarts. | 1511 // Give up the corruption recovery until the browser restarts. |
| 1496 LOG(ERROR) << "Failed to delete the diskcache."; | 1512 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1497 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1513 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1498 return; | 1514 return; |
| 1499 } | 1515 } |
| 1500 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1516 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1501 callback.Run(SERVICE_WORKER_OK); | 1517 callback.Run(SERVICE_WORKER_OK); |
| 1502 } | 1518 } |
| 1503 | 1519 |
| 1504 } // namespace content | 1520 } // namespace content |
| OLD | NEW |