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 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 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 if (quota_manager_proxy_.get()) { | |
| 1021 // Can be nullptr in tests. | |
| 1022 quota_manager_proxy_->NotifyStorageModified( | |
| 1023 storage::QuotaClient::kServiceWorker, | |
| 1024 origin, | |
| 1025 storage::StorageType::kStorageTypeTemporary, | |
| 1026 new_version.resources_total_size_bytes - | |
| 1027 deleted_version->resources_total_size_bytes); | |
| 1028 } | |
| 990 registered_origins_.insert(origin); | 1029 registered_origins_.insert(origin); |
| 991 callback.Run(SERVICE_WORKER_OK); | 1030 callback.Run(SERVICE_WORKER_OK); |
| 992 | 1031 |
| 993 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) | 1032 if (!context_ || !context_->GetLiveVersion(deleted_version->version_id)) |
| 994 StartPurgingResources(newly_purgeable_resources); | 1033 StartPurgingResources(newly_purgeable_resources); |
| 995 } | 1034 } |
| 996 | 1035 |
| 997 void ServiceWorkerStorage::DidUpdateToActiveState( | 1036 void ServiceWorkerStorage::DidUpdateToActiveState( |
| 998 const StatusCallback& callback, | 1037 const StatusCallback& callback, |
| 999 ServiceWorkerDatabase::Status status) { | 1038 ServiceWorkerDatabase::Status status) { |
| 1000 if (status != ServiceWorkerDatabase::STATUS_OK && | 1039 if (status != ServiceWorkerDatabase::STATUS_OK && |
| 1001 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1040 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
| 1002 ScheduleDeleteAndStartOver(); | 1041 ScheduleDeleteAndStartOver(); |
| 1003 } | 1042 } |
| 1004 callback.Run(DatabaseStatusToStatusCode(status)); | 1043 callback.Run(DatabaseStatusToStatusCode(status)); |
| 1005 } | 1044 } |
| 1006 | 1045 |
| 1007 void ServiceWorkerStorage::DidDeleteRegistration( | 1046 void ServiceWorkerStorage::DidDeleteRegistration( |
| 1008 const DidDeleteRegistrationParams& params, | 1047 const DidDeleteRegistrationParams& params, |
| 1009 bool origin_is_deletable, | 1048 bool origin_is_deletable, |
| 1010 int64 version_id, | 1049 ServiceWorkerDatabase::RegistrationData* deleted_version, |
| 1011 const std::vector<int64>& newly_purgeable_resources, | 1050 const std::vector<int64>& newly_purgeable_resources, |
| 1012 ServiceWorkerDatabase::Status status) { | 1051 ServiceWorkerDatabase::Status status) { |
| 1052 DCHECK(deleted_version); | |
| 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 } |
| 1059 if (quota_manager_proxy_.get()) { | |
| 1060 // Can be nullptr in tests. | |
| 1061 quota_manager_proxy_->NotifyStorageModified( | |
| 1062 storage::QuotaClient::kServiceWorker, | |
| 1063 params.origin, | |
| 1064 storage::StorageType::kStorageTypeTemporary, | |
| 1065 -deleted_version->resources_total_size_bytes); | |
| 1066 } | |
| 1019 if (origin_is_deletable) | 1067 if (origin_is_deletable) |
| 1020 registered_origins_.erase(params.origin); | 1068 registered_origins_.erase(params.origin); |
| 1021 params.callback.Run(SERVICE_WORKER_OK); | 1069 params.callback.Run(SERVICE_WORKER_OK); |
| 1022 | 1070 |
| 1023 if (!context_ || !context_->GetLiveVersion(version_id)) | 1071 if (!context_ || !context_->GetLiveVersion(deleted_version->version_id)) |
| 1024 StartPurgingResources(newly_purgeable_resources); | 1072 StartPurgingResources(newly_purgeable_resources); |
| 1025 } | 1073 } |
| 1026 | 1074 |
| 1027 scoped_refptr<ServiceWorkerRegistration> | 1075 scoped_refptr<ServiceWorkerRegistration> |
| 1028 ServiceWorkerStorage::GetOrCreateRegistration( | 1076 ServiceWorkerStorage::GetOrCreateRegistration( |
| 1029 const ServiceWorkerDatabase::RegistrationData& data, | 1077 const ServiceWorkerDatabase::RegistrationData& data, |
| 1030 const ResourceList& resources) { | 1078 const ResourceList& resources) { |
| 1031 scoped_refptr<ServiceWorkerRegistration> registration = | 1079 scoped_refptr<ServiceWorkerRegistration> registration = |
| 1032 context_->GetLiveRegistration(data.registration_id); | 1080 context_->GetLiveRegistration(data.registration_id); |
| 1033 if (registration.get()) | 1081 if (registration.get()) |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1287 } | 1335 } |
| 1288 | 1336 |
| 1289 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 1337 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 1290 ServiceWorkerDatabase* database, | 1338 ServiceWorkerDatabase* database, |
| 1291 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1339 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1292 int64 registration_id, | 1340 int64 registration_id, |
| 1293 const GURL& origin, | 1341 const GURL& origin, |
| 1294 const DeleteRegistrationCallback& callback) { | 1342 const DeleteRegistrationCallback& callback) { |
| 1295 DCHECK(database); | 1343 DCHECK(database); |
| 1296 | 1344 |
| 1297 int64 version_id = kInvalidServiceWorkerVersionId; | 1345 auto deleted_version = |
| 1346 make_scoped_ptr(new ServiceWorkerDatabase::RegistrationData()); | |
| 1347 deleted_version->version_id = kInvalidServiceWorkerVersionId; | |
|
michaeln
2014/10/28 22:00:40
this assignement isn't needed since its constructe
dmurph
2014/10/29 22:18:25
Done.
| |
| 1298 std::vector<int64> newly_purgeable_resources; | 1348 std::vector<int64> newly_purgeable_resources; |
| 1299 ServiceWorkerDatabase::Status status = database->DeleteRegistration( | 1349 ServiceWorkerDatabase::Status status = |
| 1300 registration_id, origin, &version_id, &newly_purgeable_resources); | 1350 database->DeleteRegistration(registration_id, |
| 1351 origin, | |
| 1352 deleted_version.get(), | |
| 1353 &newly_purgeable_resources); | |
| 1301 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1354 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1302 original_task_runner->PostTask(FROM_HERE, | 1355 original_task_runner->PostTask( |
| 1303 base::Bind(callback, | 1356 FROM_HERE, |
| 1304 false, | 1357 base::Bind(callback, |
| 1305 kInvalidServiceWorkerVersionId, | 1358 false, |
| 1306 std::vector<int64>(), | 1359 base::Owned(deleted_version.release()), |
|
michaeln
2014/10/28 22:00:40
when working with scoped_ptrs<> we usually used ba
dmurph
2014/10/29 22:18:25
Acknowledged.
| |
| 1307 status)); | 1360 std::vector<int64>(), |
| 1361 status)); | |
| 1308 return; | 1362 return; |
| 1309 } | 1363 } |
| 1310 | 1364 |
| 1311 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the | 1365 // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the |
| 1312 // unique origin list. | 1366 // unique origin list. |
| 1313 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | 1367 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 1314 status = database->GetRegistrationsForOrigin(origin, ®istrations); | 1368 status = database->GetRegistrationsForOrigin(origin, ®istrations); |
| 1315 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1369 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1316 original_task_runner->PostTask(FROM_HERE, | 1370 original_task_runner->PostTask( |
| 1317 base::Bind(callback, | 1371 FROM_HERE, |
| 1318 false, | 1372 base::Bind(callback, |
| 1319 kInvalidServiceWorkerVersionId, | 1373 false, |
| 1320 std::vector<int64>(), | 1374 base::Owned(deleted_version.release()), |
| 1321 status)); | 1375 std::vector<int64>(), |
| 1376 status)); | |
| 1322 return; | 1377 return; |
| 1323 } | 1378 } |
| 1324 | 1379 |
| 1325 bool deletable = registrations.empty(); | 1380 bool deletable = registrations.empty(); |
| 1326 original_task_runner->PostTask( | 1381 original_task_runner->PostTask( |
| 1327 FROM_HERE, | 1382 FROM_HERE, |
| 1328 base::Bind( | 1383 base::Bind(callback, |
| 1329 callback, deletable, version_id, newly_purgeable_resources, status)); | 1384 deletable, |
| 1385 base::Owned(deleted_version.release()), | |
| 1386 newly_purgeable_resources, | |
| 1387 status)); | |
| 1330 } | 1388 } |
| 1331 | 1389 |
| 1332 void ServiceWorkerStorage::WriteRegistrationInDB( | 1390 void ServiceWorkerStorage::WriteRegistrationInDB( |
| 1333 ServiceWorkerDatabase* database, | 1391 ServiceWorkerDatabase* database, |
| 1334 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1392 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1335 const ServiceWorkerDatabase::RegistrationData& data, | 1393 const ServiceWorkerDatabase::RegistrationData& data, |
| 1336 const ResourceList& resources, | 1394 const ResourceList& resources, |
| 1337 const WriteRegistrationCallback& callback) { | 1395 const WriteRegistrationCallback& callback) { |
| 1338 DCHECK(database); | 1396 DCHECK(database); |
| 1339 int64 deleted_version_id = kInvalidServiceWorkerVersionId; | 1397 auto deleted_version = |
| 1398 make_scoped_ptr(new ServiceWorkerDatabase::RegistrationData()); | |
| 1399 deleted_version->registration_id = kInvalidServiceWorkerVersionId; | |
| 1340 std::vector<int64> newly_purgeable_resources; | 1400 std::vector<int64> newly_purgeable_resources; |
| 1341 ServiceWorkerDatabase::Status status = database->WriteRegistration( | 1401 ServiceWorkerDatabase::Status status = database->WriteRegistration( |
| 1342 data, resources, &deleted_version_id, &newly_purgeable_resources); | 1402 data, resources, deleted_version.get(), &newly_purgeable_resources); |
| 1343 original_task_runner->PostTask(FROM_HERE, | 1403 original_task_runner->PostTask( |
| 1344 base::Bind(callback, | 1404 FROM_HERE, |
| 1345 data.script.GetOrigin(), | 1405 base::Bind(callback, |
| 1346 deleted_version_id, | 1406 data.script.GetOrigin(), |
| 1347 newly_purgeable_resources, | 1407 base::Owned(deleted_version.release()), |
| 1348 status)); | 1408 newly_purgeable_resources, |
| 1409 status)); | |
| 1349 } | 1410 } |
| 1350 | 1411 |
| 1351 void ServiceWorkerStorage::FindForDocumentInDB( | 1412 void ServiceWorkerStorage::FindForDocumentInDB( |
| 1352 ServiceWorkerDatabase* database, | 1413 ServiceWorkerDatabase* database, |
| 1353 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1414 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1354 const GURL& document_url, | 1415 const GURL& document_url, |
| 1355 const FindInDBCallback& callback) { | 1416 const FindInDBCallback& callback) { |
| 1356 GURL origin = document_url.GetOrigin(); | 1417 GURL origin = document_url.GetOrigin(); |
| 1357 RegistrationList registrations; | 1418 RegistrationList registrations; |
| 1358 ServiceWorkerDatabase::Status status = | 1419 ServiceWorkerDatabase::Status status = |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1495 // Give up the corruption recovery until the browser restarts. | 1556 // Give up the corruption recovery until the browser restarts. |
| 1496 LOG(ERROR) << "Failed to delete the diskcache."; | 1557 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1497 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1558 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1498 return; | 1559 return; |
| 1499 } | 1560 } |
| 1500 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1561 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1501 callback.Run(SERVICE_WORKER_OK); | 1562 callback.Run(SERVICE_WORKER_OK); |
| 1502 } | 1563 } |
| 1503 | 1564 |
| 1504 } // namespace content | 1565 } // namespace content |
| OLD | NEW |