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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 FROM_HERE, | 433 FROM_HERE, |
434 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, | 434 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, |
435 base::Unretained(database_.get()), | 435 base::Unretained(database_.get()), |
436 base::Unretained(registrations)), | 436 base::Unretained(registrations)), |
437 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, | 437 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, |
438 weak_factory_.GetWeakPtr(), | 438 weak_factory_.GetWeakPtr(), |
439 callback, | 439 callback, |
440 base::Owned(registrations))); | 440 base::Owned(registrations))); |
441 } | 441 } |
442 | 442 |
443 void ServiceWorkerStorage::GetAllUsageByOrigin( | |
444 const OriginUsageCallback& callback) { | |
445 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | |
446 | |
447 RegistrationList* registrations = new RegistrationList; | |
448 PostTaskAndReplyWithResult( | |
449 database_task_manager_->GetTaskRunner(), | |
450 FROM_HERE, | |
451 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, | |
452 base::Unretained(database_.get()), | |
453 base::Unretained(registrations)), | |
454 base::Bind(&ServiceWorkerStorage::DidGetRegistrationsForUsage, | |
455 weak_factory_.GetWeakPtr(), | |
456 callback, | |
457 base::Owned(registrations))); | |
458 } | |
459 | |
443 void ServiceWorkerStorage::StoreRegistration( | 460 void ServiceWorkerStorage::StoreRegistration( |
444 ServiceWorkerRegistration* registration, | 461 ServiceWorkerRegistration* registration, |
445 ServiceWorkerVersion* version, | 462 ServiceWorkerVersion* version, |
446 const StatusCallback& callback) { | 463 const StatusCallback& callback) { |
447 DCHECK(registration); | 464 DCHECK(registration); |
448 DCHECK(version); | 465 DCHECK(version); |
449 | 466 |
450 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 467 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
451 if (IsDisabled() || !context_) { | 468 if (IsDisabled() || !context_) { |
452 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 469 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
(...skipping 19 matching lines...) Expand all Loading... | |
472 data.resources_total_size_bytes = resources_total_size_bytes; | 489 data.resources_total_size_bytes = resources_total_size_bytes; |
473 | 490 |
474 if (!has_checked_for_stale_resources_) | 491 if (!has_checked_for_stale_resources_) |
475 DeleteStaleResources(); | 492 DeleteStaleResources(); |
476 | 493 |
477 database_task_manager_->GetTaskRunner()->PostTask( | 494 database_task_manager_->GetTaskRunner()->PostTask( |
478 FROM_HERE, | 495 FROM_HERE, |
479 base::Bind(&WriteRegistrationInDB, | 496 base::Bind(&WriteRegistrationInDB, |
480 database_.get(), | 497 database_.get(), |
481 base::MessageLoopProxy::current(), | 498 base::MessageLoopProxy::current(), |
482 data, resources, | 499 data, |
500 resources, | |
483 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, | 501 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, |
484 weak_factory_.GetWeakPtr(), | 502 weak_factory_.GetWeakPtr(), |
485 callback))); | 503 callback, |
504 data))); | |
486 | 505 |
487 registration->set_is_deleted(false); | 506 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 } | 507 } |
499 | 508 |
500 void ServiceWorkerStorage::UpdateToActiveState( | 509 void ServiceWorkerStorage::UpdateToActiveState( |
501 ServiceWorkerRegistration* registration, | 510 ServiceWorkerRegistration* registration, |
502 const StatusCallback& callback) { | 511 const StatusCallback& callback) { |
503 DCHECK(registration); | 512 DCHECK(registration); |
504 | 513 |
505 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 514 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
506 if (IsDisabled() || !context_) { | 515 if (IsDisabled() || !context_) { |
507 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 516 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
969 for (RegistrationRefsById::const_iterator it = | 978 for (RegistrationRefsById::const_iterator it = |
970 installing_registrations_.begin(); | 979 installing_registrations_.begin(); |
971 it != installing_registrations_.end(); ++it) { | 980 it != installing_registrations_.end(); ++it) { |
972 if (pushed_registrations.insert(it->first).second) | 981 if (pushed_registrations.insert(it->first).second) |
973 infos.push_back(it->second->GetInfo()); | 982 infos.push_back(it->second->GetInfo()); |
974 } | 983 } |
975 | 984 |
976 callback.Run(infos); | 985 callback.Run(infos); |
977 } | 986 } |
978 | 987 |
988 void ServiceWorkerStorage::DidGetRegistrationsForUsage( | |
989 const OriginUsageCallback& callback, | |
990 RegistrationList* registrations, | |
991 ServiceWorkerDatabase::Status status) { | |
992 DCHECK(registrations); | |
993 if (status != ServiceWorkerDatabase::STATUS_OK && | |
994 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | |
995 ScheduleDeleteAndStartOver(); | |
996 callback.Run(std::map<GURL, int64>()); | |
997 return; | |
998 } | |
999 | |
1000 std::map<GURL, int64> usage_map; | |
1001 for (const auto& registration : *registrations) { | |
1002 usage_map[registration.scope.GetOrigin()] += | |
1003 registration.resources_total_size_bytes; | |
1004 } | |
1005 callback.Run(usage_map); | |
1006 } | |
1007 | |
979 void ServiceWorkerStorage::DidStoreRegistration( | 1008 void ServiceWorkerStorage::DidStoreRegistration( |
980 const StatusCallback& callback, | 1009 const StatusCallback& callback, |
1010 const ServiceWorkerDatabase::RegistrationData& new_version, | |
981 const GURL& origin, | 1011 const GURL& origin, |
982 int64 deleted_version_id, | 1012 const ServiceWorkerDatabase::RegistrationData& deleted_version, |
983 const std::vector<int64>& newly_purgeable_resources, | 1013 const std::vector<int64>& newly_purgeable_resources, |
984 ServiceWorkerDatabase::Status status) { | 1014 ServiceWorkerDatabase::Status status) { |
985 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1015 if (status != ServiceWorkerDatabase::STATUS_OK) { |
986 ScheduleDeleteAndStartOver(); | 1016 ScheduleDeleteAndStartOver(); |
987 callback.Run(DatabaseStatusToStatusCode(status)); | 1017 callback.Run(DatabaseStatusToStatusCode(status)); |
988 return; | 1018 return; |
989 } | 1019 } |
1020 // TODO(dmurph): Add correct byte delta. | |
michaeln
2014/10/24 23:15:47
it looks correct, is there something missing?
dmurph
2014/10/27 21:37:06
Done.
| |
1021 if (quota_manager_proxy_.get()) { | |
1022 // Can be nullptr in tests. | |
1023 quota_manager_proxy_->NotifyStorageModified( | |
1024 storage::QuotaClient::kServiceWorker, | |
1025 origin, | |
1026 storage::StorageType::kStorageTypeTemporary, | |
1027 new_version.resources_total_size_bytes - | |
1028 deleted_version.resources_total_size_bytes); | |
1029 } | |
990 registered_origins_.insert(origin); | 1030 registered_origins_.insert(origin); |
991 callback.Run(SERVICE_WORKER_OK); | 1031 callback.Run(SERVICE_WORKER_OK); |
992 | 1032 |
993 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) | 1033 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id)) |
994 StartPurgingResources(newly_purgeable_resources); | 1034 StartPurgingResources(newly_purgeable_resources); |
995 } | 1035 } |
996 | 1036 |
997 void ServiceWorkerStorage::DidUpdateToActiveState( | 1037 void ServiceWorkerStorage::DidUpdateToActiveState( |
998 const StatusCallback& callback, | 1038 const StatusCallback& callback, |
999 ServiceWorkerDatabase::Status status) { | 1039 ServiceWorkerDatabase::Status status) { |
1000 if (status != ServiceWorkerDatabase::STATUS_OK && | 1040 if (status != ServiceWorkerDatabase::STATUS_OK && |
1001 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1041 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
1002 ScheduleDeleteAndStartOver(); | 1042 ScheduleDeleteAndStartOver(); |
1003 } | 1043 } |
1004 callback.Run(DatabaseStatusToStatusCode(status)); | 1044 callback.Run(DatabaseStatusToStatusCode(status)); |
1005 } | 1045 } |
1006 | 1046 |
1007 void ServiceWorkerStorage::DidDeleteRegistration( | 1047 void ServiceWorkerStorage::DidDeleteRegistration( |
1008 const DidDeleteRegistrationParams& params, | 1048 const DidDeleteRegistrationParams& params, |
1009 bool origin_is_deletable, | 1049 bool origin_is_deletable, |
1010 int64 version_id, | 1050 int64 version_id, |
1011 const std::vector<int64>& newly_purgeable_resources, | 1051 const std::vector<int64>& newly_purgeable_resources, |
1012 ServiceWorkerDatabase::Status status) { | 1052 ServiceWorkerDatabase::Status status) { |
1013 pending_deletions_.erase(params.registration_id); | 1053 pending_deletions_.erase(params.registration_id); |
1014 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1054 if (status != ServiceWorkerDatabase::STATUS_OK) { |
1015 ScheduleDeleteAndStartOver(); | 1055 ScheduleDeleteAndStartOver(); |
1016 params.callback.Run(DatabaseStatusToStatusCode(status)); | 1056 params.callback.Run(DatabaseStatusToStatusCode(status)); |
1017 return; | 1057 return; |
1018 } | 1058 } |
michaeln
2014/10/24 23:15:47
i think we need a NotifyStorageModified call in he
dmurph
2014/10/27 21:37:06
Done.
| |
1019 if (origin_is_deletable) | 1059 if (origin_is_deletable) |
1020 registered_origins_.erase(params.origin); | 1060 registered_origins_.erase(params.origin); |
1021 params.callback.Run(SERVICE_WORKER_OK); | 1061 params.callback.Run(SERVICE_WORKER_OK); |
1022 | 1062 |
1023 if (!context_ || !context_->GetLiveVersion(version_id)) | 1063 if (!context_ || !context_->GetLiveVersion(version_id)) |
1024 StartPurgingResources(newly_purgeable_resources); | 1064 StartPurgingResources(newly_purgeable_resources); |
1025 } | 1065 } |
1026 | 1066 |
1027 scoped_refptr<ServiceWorkerRegistration> | 1067 scoped_refptr<ServiceWorkerRegistration> |
1028 ServiceWorkerStorage::GetOrCreateRegistration( | 1068 ServiceWorkerStorage::GetOrCreateRegistration( |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1329 callback, deletable, version_id, newly_purgeable_resources, status)); | 1369 callback, deletable, version_id, newly_purgeable_resources, status)); |
1330 } | 1370 } |
1331 | 1371 |
1332 void ServiceWorkerStorage::WriteRegistrationInDB( | 1372 void ServiceWorkerStorage::WriteRegistrationInDB( |
1333 ServiceWorkerDatabase* database, | 1373 ServiceWorkerDatabase* database, |
1334 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1374 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
1335 const ServiceWorkerDatabase::RegistrationData& data, | 1375 const ServiceWorkerDatabase::RegistrationData& data, |
1336 const ResourceList& resources, | 1376 const ResourceList& resources, |
1337 const WriteRegistrationCallback& callback) { | 1377 const WriteRegistrationCallback& callback) { |
1338 DCHECK(database); | 1378 DCHECK(database); |
1339 int64 deleted_version_id = kInvalidServiceWorkerVersionId; | 1379 ServiceWorkerDatabase::RegistrationData deleted_version; |
1380 deleted_version.registration_id = kInvalidServiceWorkerVersionId; | |
1340 std::vector<int64> newly_purgeable_resources; | 1381 std::vector<int64> newly_purgeable_resources; |
1341 ServiceWorkerDatabase::Status status = database->WriteRegistration( | 1382 ServiceWorkerDatabase::Status status = database->WriteRegistration( |
1342 data, resources, &deleted_version_id, &newly_purgeable_resources); | 1383 data, resources, &deleted_version, &newly_purgeable_resources); |
1343 original_task_runner->PostTask(FROM_HERE, | 1384 original_task_runner->PostTask(FROM_HERE, |
1344 base::Bind(callback, | 1385 base::Bind(callback, |
1345 data.script.GetOrigin(), | 1386 data.script.GetOrigin(), |
1346 deleted_version_id, | 1387 deleted_version, |
1347 newly_purgeable_resources, | 1388 newly_purgeable_resources, |
1348 status)); | 1389 status)); |
1349 } | 1390 } |
1350 | 1391 |
1351 void ServiceWorkerStorage::FindForDocumentInDB( | 1392 void ServiceWorkerStorage::FindForDocumentInDB( |
1352 ServiceWorkerDatabase* database, | 1393 ServiceWorkerDatabase* database, |
1353 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1394 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
1354 const GURL& document_url, | 1395 const GURL& document_url, |
1355 const FindInDBCallback& callback) { | 1396 const FindInDBCallback& callback) { |
1356 GURL origin = document_url.GetOrigin(); | 1397 GURL origin = document_url.GetOrigin(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1452 // database should not disable itself when an error occurs and the storage | 1493 // database should not disable itself when an error occurs and the storage |
1453 // controls it instead. | 1494 // controls it instead. |
1454 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() { | 1495 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() { |
1455 if (state_ == DISABLED) { | 1496 if (state_ == DISABLED) { |
1456 // Recovery process has already been scheduled. | 1497 // Recovery process has already been scheduled. |
1457 return; | 1498 return; |
1458 } | 1499 } |
1459 Disable(); | 1500 Disable(); |
1460 | 1501 |
1461 DVLOG(1) << "Schedule to delete the context and start over."; | 1502 DVLOG(1) << "Schedule to delete the context and start over."; |
1462 context_->ScheduleDeleteAndStartOver(); | 1503 context_->ScheduleDeleteAndStartOver(); |
michaeln
2014/10/24 23:15:47
also in the deleteAndStartOVer case i think we nee
dmurph
2014/10/27 21:37:06
Hm, I'm unsure how to do this best. Should we be
michaeln
2014/10/28 22:13:11
There might be some trickiness with that but sound
| |
1463 } | 1504 } |
1464 | 1505 |
1465 void ServiceWorkerStorage::DidDeleteDatabase( | 1506 void ServiceWorkerStorage::DidDeleteDatabase( |
1466 const StatusCallback& callback, | 1507 const StatusCallback& callback, |
1467 ServiceWorkerDatabase::Status status) { | 1508 ServiceWorkerDatabase::Status status) { |
1468 DCHECK_EQ(DISABLED, state_); | 1509 DCHECK_EQ(DISABLED, state_); |
1469 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1510 if (status != ServiceWorkerDatabase::STATUS_OK) { |
1470 // Give up the corruption recovery until the browser restarts. | 1511 // Give up the corruption recovery until the browser restarts. |
1471 LOG(ERROR) << "Failed to delete the database: " | 1512 LOG(ERROR) << "Failed to delete the database: " |
1472 << ServiceWorkerDatabase::StatusToString(status); | 1513 << ServiceWorkerDatabase::StatusToString(status); |
(...skipping 22 matching lines...) Expand all Loading... | |
1495 // Give up the corruption recovery until the browser restarts. | 1536 // Give up the corruption recovery until the browser restarts. |
1496 LOG(ERROR) << "Failed to delete the diskcache."; | 1537 LOG(ERROR) << "Failed to delete the diskcache."; |
1497 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1538 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
1498 return; | 1539 return; |
1499 } | 1540 } |
1500 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1541 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
1501 callback.Run(SERVICE_WORKER_OK); | 1542 callback.Run(SERVICE_WORKER_OK); |
1502 } | 1543 } |
1503 | 1544 |
1504 } // namespace content | 1545 } // namespace content |
OLD | NEW |