| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_database.h" | 5 #include "content/browser/service_worker/service_worker_database.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 | 452 |
| 453 origins->insert(origin); | 453 origins->insert(origin); |
| 454 } | 454 } |
| 455 | 455 |
| 456 HandleReadResult(FROM_HERE, status); | 456 HandleReadResult(FROM_HERE, status); |
| 457 return status; | 457 return status; |
| 458 } | 458 } |
| 459 | 459 |
| 460 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( | 460 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( |
| 461 const GURL& origin, | 461 const GURL& origin, |
| 462 std::vector<RegistrationData>* registrations) { | 462 std::vector<RegistrationData>* registrations, |
| 463 std::vector<std::vector<ResourceRecord>>* opt_resources_list) { |
| 463 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 464 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 464 DCHECK(registrations->empty()); | 465 DCHECK(registrations->empty()); |
| 465 | 466 |
| 466 Status status = LazyOpen(false); | 467 Status status = LazyOpen(false); |
| 467 if (IsNewOrNonexistentDatabase(status)) | 468 if (IsNewOrNonexistentDatabase(status)) |
| 468 return STATUS_OK; | 469 return STATUS_OK; |
| 469 if (status != STATUS_OK) | 470 if (status != STATUS_OK) |
| 470 return status; | 471 return status; |
| 471 | 472 |
| 472 std::string prefix = CreateRegistrationKeyPrefix(origin); | 473 std::string prefix = CreateRegistrationKeyPrefix(origin); |
| 473 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 474 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
| 474 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 475 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
| 475 status = LevelDBStatusToStatus(itr->status()); | 476 status = LevelDBStatusToStatus(itr->status()); |
| 476 if (status != STATUS_OK) { | 477 if (status != STATUS_OK) { |
| 477 HandleReadResult(FROM_HERE, status); | 478 HandleReadResult(FROM_HERE, status); |
| 478 registrations->clear(); | 479 registrations->clear(); |
| 480 if (opt_resources_list) |
| 481 opt_resources_list->clear(); |
| 479 return status; | 482 return status; |
| 480 } | 483 } |
| 481 | 484 |
| 482 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) | 485 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) |
| 483 break; | 486 break; |
| 484 | 487 |
| 485 RegistrationData registration; | 488 RegistrationData registration; |
| 486 status = ParseRegistrationData(itr->value().ToString(), ®istration); | 489 status = ParseRegistrationData(itr->value().ToString(), ®istration); |
| 487 if (status != STATUS_OK) { | 490 if (status != STATUS_OK) { |
| 488 HandleReadResult(FROM_HERE, status); | 491 HandleReadResult(FROM_HERE, status); |
| 489 registrations->clear(); | 492 registrations->clear(); |
| 493 if (opt_resources_list) |
| 494 opt_resources_list->clear(); |
| 490 return status; | 495 return status; |
| 491 } | 496 } |
| 492 registrations->push_back(registration); | 497 registrations->push_back(registration); |
| 498 |
| 499 if (opt_resources_list) { |
| 500 std::vector<ResourceRecord> resources; |
| 501 status = ReadResourceRecords(registration.version_id, &resources); |
| 502 if (status != STATUS_OK) { |
| 503 HandleReadResult(FROM_HERE, status); |
| 504 registrations->clear(); |
| 505 opt_resources_list->clear(); |
| 506 return status; |
| 507 } |
| 508 opt_resources_list->push_back(resources); |
| 509 } |
| 493 } | 510 } |
| 494 | 511 |
| 495 HandleReadResult(FROM_HERE, status); | 512 HandleReadResult(FROM_HERE, status); |
| 496 return status; | 513 return status; |
| 497 } | 514 } |
| 498 | 515 |
| 499 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( | 516 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( |
| 500 std::vector<RegistrationData>* registrations) { | 517 std::vector<RegistrationData>* registrations) { |
| 501 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 518 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 502 DCHECK(registrations->empty()); | 519 DCHECK(registrations->empty()); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 return status; | 755 return status; |
| 739 if (!origin.is_valid()) | 756 if (!origin.is_valid()) |
| 740 return STATUS_ERROR_FAILED; | 757 return STATUS_ERROR_FAILED; |
| 741 | 758 |
| 742 leveldb::WriteBatch batch; | 759 leveldb::WriteBatch batch; |
| 743 | 760 |
| 744 // Remove |origin| from unique origins if a registration specified by | 761 // Remove |origin| from unique origins if a registration specified by |
| 745 // |registration_id| is the only one for |origin|. | 762 // |registration_id| is the only one for |origin|. |
| 746 // TODO(nhiroki): Check the uniqueness by more efficient way. | 763 // TODO(nhiroki): Check the uniqueness by more efficient way. |
| 747 std::vector<RegistrationData> registrations; | 764 std::vector<RegistrationData> registrations; |
| 748 status = GetRegistrationsForOrigin(origin, ®istrations); | 765 status = GetRegistrationsForOrigin(origin, ®istrations, nullptr); |
| 749 if (status != STATUS_OK) | 766 if (status != STATUS_OK) |
| 750 return status; | 767 return status; |
| 751 | 768 |
| 752 if (registrations.size() == 1 && | 769 if (registrations.size() == 1 && |
| 753 registrations[0].registration_id == registration_id) { | 770 registrations[0].registration_id == registration_id) { |
| 754 batch.Delete(CreateUniqueOriginKey(origin)); | 771 batch.Delete(CreateUniqueOriginKey(origin)); |
| 755 } | 772 } |
| 756 | 773 |
| 757 // Delete a registration specified by |registration_id|. | 774 // Delete a registration specified by |registration_id|. |
| 758 batch.Delete(CreateRegistrationKey(registration_id, origin)); | 775 batch.Delete(CreateRegistrationKey(registration_id, origin)); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 leveldb::WriteBatch batch; | 972 leveldb::WriteBatch batch; |
| 956 | 973 |
| 957 for (const GURL& origin : origins) { | 974 for (const GURL& origin : origins) { |
| 958 if (!origin.is_valid()) | 975 if (!origin.is_valid()) |
| 959 return STATUS_ERROR_FAILED; | 976 return STATUS_ERROR_FAILED; |
| 960 | 977 |
| 961 // Delete from the unique origin list. | 978 // Delete from the unique origin list. |
| 962 batch.Delete(CreateUniqueOriginKey(origin)); | 979 batch.Delete(CreateUniqueOriginKey(origin)); |
| 963 | 980 |
| 964 std::vector<RegistrationData> registrations; | 981 std::vector<RegistrationData> registrations; |
| 965 status = GetRegistrationsForOrigin(origin, ®istrations); | 982 status = GetRegistrationsForOrigin(origin, ®istrations, nullptr); |
| 966 if (status != STATUS_OK) | 983 if (status != STATUS_OK) |
| 967 return status; | 984 return status; |
| 968 | 985 |
| 969 // Delete registrations, resource records and user data. | 986 // Delete registrations, resource records and user data. |
| 970 for (const RegistrationData& data : registrations) { | 987 for (const RegistrationData& data : registrations) { |
| 971 batch.Delete(CreateRegistrationKey(data.registration_id, origin)); | 988 batch.Delete(CreateRegistrationKey(data.registration_id, origin)); |
| 972 batch.Delete(CreateRegistrationIdToOriginKey(data.registration_id)); | 989 batch.Delete(CreateRegistrationIdToOriginKey(data.registration_id)); |
| 973 | 990 |
| 974 status = DeleteResourceRecords( | 991 status = DeleteResourceRecords( |
| 975 data.version_id, newly_purgeable_resources, &batch); | 992 data.version_id, newly_purgeable_resources, &batch); |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1480 | 1497 |
| 1481 void ServiceWorkerDatabase::HandleWriteResult( | 1498 void ServiceWorkerDatabase::HandleWriteResult( |
| 1482 const tracked_objects::Location& from_here, | 1499 const tracked_objects::Location& from_here, |
| 1483 Status status) { | 1500 Status status) { |
| 1484 if (status != STATUS_OK) | 1501 if (status != STATUS_OK) |
| 1485 Disable(from_here, status); | 1502 Disable(from_here, status); |
| 1486 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1503 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1487 } | 1504 } |
| 1488 | 1505 |
| 1489 } // namespace content | 1506 } // namespace content |
| OLD | NEW |