| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 break; | 430 break; |
| 431 origins->insert(GURL(origin)); | 431 origins->insert(GURL(origin)); |
| 432 } | 432 } |
| 433 | 433 |
| 434 HandleReadResult(FROM_HERE, status); | 434 HandleReadResult(FROM_HERE, status); |
| 435 return status; | 435 return status; |
| 436 } | 436 } |
| 437 | 437 |
| 438 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( | 438 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( |
| 439 const GURL& origin, | 439 const GURL& origin, |
| 440 std::vector<RegistrationData>* registrations) { | 440 std::vector<RegistrationData>* registrations, |
| 441 std::vector<std::vector<ResourceRecord>>* resource_lists) { |
| 441 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 442 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 442 DCHECK(registrations->empty()); | 443 DCHECK(registrations->empty()); |
| 443 | 444 |
| 444 Status status = LazyOpen(false); | 445 Status status = LazyOpen(false); |
| 445 if (IsNewOrNonexistentDatabase(status)) | 446 if (IsNewOrNonexistentDatabase(status)) |
| 446 return STATUS_OK; | 447 return STATUS_OK; |
| 447 if (status != STATUS_OK) | 448 if (status != STATUS_OK) |
| 448 return status; | 449 return status; |
| 449 | 450 |
| 450 // Create a key prefix for registrations. | 451 // Create a key prefix for registrations. |
| 451 std::string prefix = base::StringPrintf( | 452 std::string prefix = base::StringPrintf( |
| 452 "%s%s%c", kRegKeyPrefix, origin.spec().c_str(), kKeySeparator); | 453 "%s%s%c", kRegKeyPrefix, origin.spec().c_str(), kKeySeparator); |
| 453 | 454 |
| 454 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 455 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
| 455 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 456 size_t index = 0; |
| 457 for (itr->Seek(prefix); itr->Valid(); itr->Next(), ++index) { |
| 456 status = LevelDBStatusToStatus(itr->status()); | 458 status = LevelDBStatusToStatus(itr->status()); |
| 457 if (status != STATUS_OK) { | 459 if (status != STATUS_OK) { |
| 458 HandleReadResult(FROM_HERE, status); | 460 HandleReadResult(FROM_HERE, status); |
| 459 registrations->clear(); | 461 registrations->clear(); |
| 460 return status; | 462 return status; |
| 461 } | 463 } |
| 462 | 464 |
| 463 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) | 465 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) |
| 464 break; | 466 break; |
| 465 | 467 |
| 466 RegistrationData registration; | 468 RegistrationData registration; |
| 467 status = ParseRegistrationData(itr->value().ToString(), ®istration); | 469 status = ParseRegistrationData(itr->value().ToString(), ®istration); |
| 468 if (status != STATUS_OK) { | 470 if (status != STATUS_OK) { |
| 469 HandleReadResult(FROM_HERE, status); | 471 HandleReadResult(FROM_HERE, status); |
| 470 registrations->clear(); | 472 registrations->clear(); |
| 471 return status; | 473 return status; |
| 472 } | 474 } |
| 473 registrations->push_back(registration); | 475 registrations->push_back(registration); |
| 476 |
| 477 std::vector<ResourceRecord> resources; |
| 478 status = ReadResourceRecords(registration.version_id, &resources); |
| 479 if (status != STATUS_OK) { |
| 480 HandleReadResult(FROM_HERE, status); |
| 481 resource_lists->clear(); |
| 482 return status; |
| 483 } |
| 484 resource_lists->push_back(resources); |
| 474 } | 485 } |
| 475 | 486 |
| 476 HandleReadResult(FROM_HERE, status); | 487 HandleReadResult(FROM_HERE, status); |
| 477 return status; | 488 return status; |
| 478 } | 489 } |
| 479 | 490 |
| 480 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( | 491 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( |
| 481 std::vector<RegistrationData>* registrations) { | 492 std::vector<RegistrationData>* registrations) { |
| 482 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 493 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 483 DCHECK(registrations->empty()); | 494 DCHECK(registrations->empty()); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 return status; | 725 return status; |
| 715 if (!origin.is_valid()) | 726 if (!origin.is_valid()) |
| 716 return STATUS_ERROR_FAILED; | 727 return STATUS_ERROR_FAILED; |
| 717 | 728 |
| 718 leveldb::WriteBatch batch; | 729 leveldb::WriteBatch batch; |
| 719 | 730 |
| 720 // Remove |origin| from unique origins if a registration specified by | 731 // Remove |origin| from unique origins if a registration specified by |
| 721 // |registration_id| is the only one for |origin|. | 732 // |registration_id| is the only one for |origin|. |
| 722 // TODO(nhiroki): Check the uniqueness by more efficient way. | 733 // TODO(nhiroki): Check the uniqueness by more efficient way. |
| 723 std::vector<RegistrationData> registrations; | 734 std::vector<RegistrationData> registrations; |
| 724 status = GetRegistrationsForOrigin(origin, ®istrations); | 735 std::vector<std::vector<ResourceRecord>> resource_lists; |
| 736 status = GetRegistrationsForOrigin(origin, ®istrations, &resource_lists); |
| 725 if (status != STATUS_OK) | 737 if (status != STATUS_OK) |
| 726 return status; | 738 return status; |
| 727 | 739 |
| 728 if (registrations.size() == 1 && | 740 if (registrations.size() == 1 && |
| 729 registrations[0].registration_id == registration_id) { | 741 registrations[0].registration_id == registration_id) { |
| 730 batch.Delete(CreateUniqueOriginKey(origin)); | 742 batch.Delete(CreateUniqueOriginKey(origin)); |
| 731 } | 743 } |
| 732 | 744 |
| 733 // Delete a registration specified by |registration_id|. | 745 // Delete a registration specified by |registration_id|. |
| 734 batch.Delete(CreateRegistrationKey(registration_id, origin)); | 746 batch.Delete(CreateRegistrationKey(registration_id, origin)); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 leveldb::WriteBatch batch; | 943 leveldb::WriteBatch batch; |
| 932 | 944 |
| 933 for (const GURL& origin : origins) { | 945 for (const GURL& origin : origins) { |
| 934 if (!origin.is_valid()) | 946 if (!origin.is_valid()) |
| 935 return STATUS_ERROR_FAILED; | 947 return STATUS_ERROR_FAILED; |
| 936 | 948 |
| 937 // Delete from the unique origin list. | 949 // Delete from the unique origin list. |
| 938 batch.Delete(CreateUniqueOriginKey(origin)); | 950 batch.Delete(CreateUniqueOriginKey(origin)); |
| 939 | 951 |
| 940 std::vector<RegistrationData> registrations; | 952 std::vector<RegistrationData> registrations; |
| 941 status = GetRegistrationsForOrigin(origin, ®istrations); | 953 std::vector<std::vector<ResourceRecord>> resource_lists; |
| 954 status = GetRegistrationsForOrigin(origin, ®istrations, &resource_lists); |
| 942 if (status != STATUS_OK) | 955 if (status != STATUS_OK) |
| 943 return status; | 956 return status; |
| 944 | 957 |
| 945 // Delete registrations, resource records and user data. | 958 // Delete registrations, resource records and user data. |
| 946 for (const RegistrationData& data : registrations) { | 959 for (const RegistrationData& data : registrations) { |
| 947 batch.Delete(CreateRegistrationKey(data.registration_id, origin)); | 960 batch.Delete(CreateRegistrationKey(data.registration_id, origin)); |
| 948 batch.Delete(CreateRegistrationIdToOriginKey(data.registration_id)); | 961 batch.Delete(CreateRegistrationIdToOriginKey(data.registration_id)); |
| 949 | 962 |
| 950 status = DeleteResourceRecords( | 963 status = DeleteResourceRecords( |
| 951 data.version_id, newly_purgeable_resources, &batch); | 964 data.version_id, newly_purgeable_resources, &batch); |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 | 1465 |
| 1453 void ServiceWorkerDatabase::HandleWriteResult( | 1466 void ServiceWorkerDatabase::HandleWriteResult( |
| 1454 const tracked_objects::Location& from_here, | 1467 const tracked_objects::Location& from_here, |
| 1455 Status status) { | 1468 Status status) { |
| 1456 if (status != STATUS_OK) | 1469 if (status != STATUS_OK) |
| 1457 Disable(from_here, status); | 1470 Disable(from_here, status); |
| 1458 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1471 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1459 } | 1472 } |
| 1460 | 1473 |
| 1461 } // namespace content | 1474 } // namespace content |
| OLD | NEW |