Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: content/browser/service_worker/service_worker_storage.cc

Issue 672813002: [ServiceWorker] Added size deltas and total size computation for QuotaM. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed last comment Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 data.is_active = (version == registration->active_version()); 463 data.is_active = (version == registration->active_version());
464 464
465 ResourceList resources; 465 ResourceList resources;
466 version->script_cache_map()->GetResources(&resources); 466 version->script_cache_map()->GetResources(&resources);
467 467
468 uint64 resources_total_size_bytes = 0; 468 uint64 resources_total_size_bytes = 0;
469 for (const auto& resource : resources) { 469 for (const auto& resource : resources) {
470 resources_total_size_bytes += resource.size_bytes; 470 resources_total_size_bytes += resource.size_bytes;
471 } 471 }
472 data.resources_total_size_bytes = resources_total_size_bytes; 472 data.resources_total_size_bytes = resources_total_size_bytes;
473 registration->set_resources_total_size_bytes(resources_total_size_bytes);
michaeln 2014/10/30 01:32:27 I think this should happen in DidStoreRegistration
dmurph 2014/10/30 22:06:16 Done.
474 version->set_resources_total_size_bytes(resources_total_size_bytes);
473 475
474 if (!has_checked_for_stale_resources_) 476 if (!has_checked_for_stale_resources_)
475 DeleteStaleResources(); 477 DeleteStaleResources();
476 478
477 database_task_manager_->GetTaskRunner()->PostTask( 479 database_task_manager_->GetTaskRunner()->PostTask(
478 FROM_HERE, 480 FROM_HERE,
479 base::Bind(&WriteRegistrationInDB, 481 base::Bind(&WriteRegistrationInDB,
480 database_.get(), 482 database_.get(),
481 base::MessageLoopProxy::current(), 483 base::MessageLoopProxy::current(),
482 data, resources, 484 data,
485 resources,
483 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, 486 base::Bind(&ServiceWorkerStorage::DidStoreRegistration,
484 weak_factory_.GetWeakPtr(), 487 weak_factory_.GetWeakPtr(),
485 callback))); 488 callback,
489 data)));
486 490
487 registration->set_is_deleted(false); 491 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 } 492 }
499 493
500 void ServiceWorkerStorage::UpdateToActiveState( 494 void ServiceWorkerStorage::UpdateToActiveState(
501 ServiceWorkerRegistration* registration, 495 ServiceWorkerRegistration* registration,
502 const StatusCallback& callback) { 496 const StatusCallback& callback) {
503 DCHECK(registration); 497 DCHECK(registration);
504 498
505 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; 499 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
506 if (IsDisabled() || !context_) { 500 if (IsDisabled() || !context_) {
507 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 501 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 it != installing_registrations_.end(); ++it) { 965 it != installing_registrations_.end(); ++it) {
972 if (pushed_registrations.insert(it->first).second) 966 if (pushed_registrations.insert(it->first).second)
973 infos.push_back(it->second->GetInfo()); 967 infos.push_back(it->second->GetInfo());
974 } 968 }
975 969
976 callback.Run(infos); 970 callback.Run(infos);
977 } 971 }
978 972
979 void ServiceWorkerStorage::DidStoreRegistration( 973 void ServiceWorkerStorage::DidStoreRegistration(
980 const StatusCallback& callback, 974 const StatusCallback& callback,
975 const ServiceWorkerDatabase::RegistrationData& new_version,
981 const GURL& origin, 976 const GURL& origin,
982 int64 deleted_version_id, 977 const ServiceWorkerDatabase::RegistrationData& deleted_version,
983 const std::vector<int64>& newly_purgeable_resources, 978 const std::vector<int64>& newly_purgeable_resources,
984 ServiceWorkerDatabase::Status status) { 979 ServiceWorkerDatabase::Status status) {
985 if (status != ServiceWorkerDatabase::STATUS_OK) { 980 if (status != ServiceWorkerDatabase::STATUS_OK) {
986 ScheduleDeleteAndStartOver(); 981 ScheduleDeleteAndStartOver();
987 callback.Run(DatabaseStatusToStatusCode(status)); 982 callback.Run(DatabaseStatusToStatusCode(status));
988 return; 983 return;
989 } 984 }
985 if (quota_manager_proxy_.get()) {
986 // Can be nullptr in tests.
987 quota_manager_proxy_->NotifyStorageModified(
988 storage::QuotaClient::kServiceWorker,
989 origin,
990 storage::StorageType::kStorageTypeTemporary,
991 new_version.resources_total_size_bytes -
992 deleted_version.resources_total_size_bytes);
993 }
990 registered_origins_.insert(origin); 994 registered_origins_.insert(origin);
991 callback.Run(SERVICE_WORKER_OK); 995 callback.Run(SERVICE_WORKER_OK);
992 996
993 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) 997 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id))
994 StartPurgingResources(newly_purgeable_resources); 998 StartPurgingResources(newly_purgeable_resources);
995 } 999 }
996 1000
997 void ServiceWorkerStorage::DidUpdateToActiveState( 1001 void ServiceWorkerStorage::DidUpdateToActiveState(
998 const StatusCallback& callback, 1002 const StatusCallback& callback,
999 ServiceWorkerDatabase::Status status) { 1003 ServiceWorkerDatabase::Status status) {
1000 if (status != ServiceWorkerDatabase::STATUS_OK && 1004 if (status != ServiceWorkerDatabase::STATUS_OK &&
1001 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { 1005 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) {
1002 ScheduleDeleteAndStartOver(); 1006 ScheduleDeleteAndStartOver();
1003 } 1007 }
1004 callback.Run(DatabaseStatusToStatusCode(status)); 1008 callback.Run(DatabaseStatusToStatusCode(status));
1005 } 1009 }
1006 1010
1007 void ServiceWorkerStorage::DidDeleteRegistration( 1011 void ServiceWorkerStorage::DidDeleteRegistration(
1008 const DidDeleteRegistrationParams& params, 1012 const DidDeleteRegistrationParams& params,
1009 bool origin_is_deletable, 1013 bool origin_is_deletable,
1010 int64 version_id, 1014 const ServiceWorkerDatabase::RegistrationData& deleted_version,
1011 const std::vector<int64>& newly_purgeable_resources, 1015 const std::vector<int64>& newly_purgeable_resources,
1012 ServiceWorkerDatabase::Status status) { 1016 ServiceWorkerDatabase::Status status) {
1013 pending_deletions_.erase(params.registration_id); 1017 pending_deletions_.erase(params.registration_id);
1014 if (status != ServiceWorkerDatabase::STATUS_OK) { 1018 if (status != ServiceWorkerDatabase::STATUS_OK) {
1015 ScheduleDeleteAndStartOver(); 1019 ScheduleDeleteAndStartOver();
1016 params.callback.Run(DatabaseStatusToStatusCode(status)); 1020 params.callback.Run(DatabaseStatusToStatusCode(status));
1017 return; 1021 return;
1018 } 1022 }
1023 if (quota_manager_proxy_.get()) {
1024 // Can be nullptr in tests.
1025 quota_manager_proxy_->NotifyStorageModified(
1026 storage::QuotaClient::kServiceWorker,
1027 params.origin,
1028 storage::StorageType::kStorageTypeTemporary,
1029 -deleted_version.resources_total_size_bytes);
1030 }
1019 if (origin_is_deletable) 1031 if (origin_is_deletable)
1020 registered_origins_.erase(params.origin); 1032 registered_origins_.erase(params.origin);
1021 params.callback.Run(SERVICE_WORKER_OK); 1033 params.callback.Run(SERVICE_WORKER_OK);
1022 1034
1023 if (!context_ || !context_->GetLiveVersion(version_id)) 1035 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id))
1024 StartPurgingResources(newly_purgeable_resources); 1036 StartPurgingResources(newly_purgeable_resources);
1025 } 1037 }
1026 1038
1027 scoped_refptr<ServiceWorkerRegistration> 1039 scoped_refptr<ServiceWorkerRegistration>
1028 ServiceWorkerStorage::GetOrCreateRegistration( 1040 ServiceWorkerStorage::GetOrCreateRegistration(
1029 const ServiceWorkerDatabase::RegistrationData& data, 1041 const ServiceWorkerDatabase::RegistrationData& data,
1030 const ResourceList& resources) { 1042 const ResourceList& resources) {
1031 scoped_refptr<ServiceWorkerRegistration> registration = 1043 scoped_refptr<ServiceWorkerRegistration> registration =
1032 context_->GetLiveRegistration(data.registration_id); 1044 context_->GetLiveRegistration(data.registration_id);
1033 if (registration.get()) 1045 if (registration.get())
1034 return registration; 1046 return registration;
1035 1047
1036 registration = new ServiceWorkerRegistration( 1048 registration = new ServiceWorkerRegistration(
1037 data.scope, data.registration_id, context_); 1049 data.scope, data.registration_id, context_);
1050 registration->set_resources_total_size_bytes(data.resources_total_size_bytes);
1038 registration->set_last_update_check(data.last_update_check); 1051 registration->set_last_update_check(data.last_update_check);
1039 if (pending_deletions_.find(data.registration_id) != 1052 if (pending_deletions_.find(data.registration_id) !=
1040 pending_deletions_.end()) { 1053 pending_deletions_.end()) {
1041 registration->set_is_deleted(true); 1054 registration->set_is_deleted(true);
1042 } 1055 }
1043 scoped_refptr<ServiceWorkerVersion> version = 1056 scoped_refptr<ServiceWorkerVersion> version =
1044 context_->GetLiveVersion(data.version_id); 1057 context_->GetLiveVersion(data.version_id);
1045 if (!version.get()) { 1058 if (!version.get()) {
1046 version = new ServiceWorkerVersion( 1059 version = new ServiceWorkerVersion(
1047 registration.get(), data.script, data.version_id, context_); 1060 registration.get(), data.script, data.version_id, context_);
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 } 1300 }
1288 1301
1289 void ServiceWorkerStorage::DeleteRegistrationFromDB( 1302 void ServiceWorkerStorage::DeleteRegistrationFromDB(
1290 ServiceWorkerDatabase* database, 1303 ServiceWorkerDatabase* database,
1291 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1304 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1292 int64 registration_id, 1305 int64 registration_id,
1293 const GURL& origin, 1306 const GURL& origin,
1294 const DeleteRegistrationCallback& callback) { 1307 const DeleteRegistrationCallback& callback) {
1295 DCHECK(database); 1308 DCHECK(database);
1296 1309
1297 int64 version_id = kInvalidServiceWorkerVersionId; 1310 ServiceWorkerDatabase::RegistrationData deleted_version;
1311 deleted_version.version_id = kInvalidServiceWorkerVersionId;
1298 std::vector<int64> newly_purgeable_resources; 1312 std::vector<int64> newly_purgeable_resources;
1299 ServiceWorkerDatabase::Status status = database->DeleteRegistration( 1313 ServiceWorkerDatabase::Status status = database->DeleteRegistration(
1300 registration_id, origin, &version_id, &newly_purgeable_resources); 1314 registration_id, origin, &deleted_version, &newly_purgeable_resources);
1301 if (status != ServiceWorkerDatabase::STATUS_OK) { 1315 if (status != ServiceWorkerDatabase::STATUS_OK) {
1302 original_task_runner->PostTask(FROM_HERE, 1316 original_task_runner->PostTask(
1303 base::Bind(callback, 1317 FROM_HERE,
1304 false, 1318 base::Bind(
1305 kInvalidServiceWorkerVersionId, 1319 callback, false, deleted_version, std::vector<int64>(), status));
1306 std::vector<int64>(),
1307 status));
1308 return; 1320 return;
1309 } 1321 }
1310 1322
1311 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the 1323 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the
1312 // unique origin list. 1324 // unique origin list.
1313 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; 1325 std::vector<ServiceWorkerDatabase::RegistrationData> registrations;
1314 status = database->GetRegistrationsForOrigin(origin, &registrations); 1326 status = database->GetRegistrationsForOrigin(origin, &registrations);
1315 if (status != ServiceWorkerDatabase::STATUS_OK) { 1327 if (status != ServiceWorkerDatabase::STATUS_OK) {
1316 original_task_runner->PostTask(FROM_HERE, 1328 original_task_runner->PostTask(
1317 base::Bind(callback, 1329 FROM_HERE,
1318 false, 1330 base::Bind(
1319 kInvalidServiceWorkerVersionId, 1331 callback, false, deleted_version, std::vector<int64>(), status));
1320 std::vector<int64>(),
1321 status));
1322 return; 1332 return;
1323 } 1333 }
1324 1334
1325 bool deletable = registrations.empty(); 1335 bool deletable = registrations.empty();
1326 original_task_runner->PostTask( 1336 original_task_runner->PostTask(FROM_HERE,
1327 FROM_HERE, 1337 base::Bind(callback,
1328 base::Bind( 1338 deletable,
1329 callback, deletable, version_id, newly_purgeable_resources, status)); 1339 deleted_version,
1340 newly_purgeable_resources,
1341 status));
1330 } 1342 }
1331 1343
1332 void ServiceWorkerStorage::WriteRegistrationInDB( 1344 void ServiceWorkerStorage::WriteRegistrationInDB(
1333 ServiceWorkerDatabase* database, 1345 ServiceWorkerDatabase* database,
1334 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1346 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1335 const ServiceWorkerDatabase::RegistrationData& data, 1347 const ServiceWorkerDatabase::RegistrationData& data,
1336 const ResourceList& resources, 1348 const ResourceList& resources,
1337 const WriteRegistrationCallback& callback) { 1349 const WriteRegistrationCallback& callback) {
1338 DCHECK(database); 1350 DCHECK(database);
1339 int64 deleted_version_id = kInvalidServiceWorkerVersionId; 1351 ServiceWorkerDatabase::RegistrationData deleted_version;
1352 deleted_version.registration_id = kInvalidServiceWorkerVersionId;
1340 std::vector<int64> newly_purgeable_resources; 1353 std::vector<int64> newly_purgeable_resources;
1341 ServiceWorkerDatabase::Status status = database->WriteRegistration( 1354 ServiceWorkerDatabase::Status status = database->WriteRegistration(
1342 data, resources, &deleted_version_id, &newly_purgeable_resources); 1355 data, resources, &deleted_version, &newly_purgeable_resources);
1343 original_task_runner->PostTask(FROM_HERE, 1356 original_task_runner->PostTask(FROM_HERE,
1344 base::Bind(callback, 1357 base::Bind(callback,
1345 data.script.GetOrigin(), 1358 data.script.GetOrigin(),
1346 deleted_version_id, 1359 deleted_version,
1347 newly_purgeable_resources, 1360 newly_purgeable_resources,
1348 status)); 1361 status));
1349 } 1362 }
1350 1363
1351 void ServiceWorkerStorage::FindForDocumentInDB( 1364 void ServiceWorkerStorage::FindForDocumentInDB(
1352 ServiceWorkerDatabase* database, 1365 ServiceWorkerDatabase* database,
1353 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1366 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1354 const GURL& document_url, 1367 const GURL& document_url,
1355 const FindInDBCallback& callback) { 1368 const FindInDBCallback& callback) {
1356 GURL origin = document_url.GetOrigin(); 1369 GURL origin = document_url.GetOrigin();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 // Give up the corruption recovery until the browser restarts. 1508 // Give up the corruption recovery until the browser restarts.
1496 LOG(ERROR) << "Failed to delete the diskcache."; 1509 LOG(ERROR) << "Failed to delete the diskcache.";
1497 callback.Run(SERVICE_WORKER_ERROR_FAILED); 1510 callback.Run(SERVICE_WORKER_ERROR_FAILED);
1498 return; 1511 return;
1499 } 1512 }
1500 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; 1513 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully.";
1501 callback.Run(SERVICE_WORKER_OK); 1514 callback.Run(SERVICE_WORKER_OK);
1502 } 1515 }
1503 1516
1504 } // namespace content 1517 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698