Chromium Code Reviews| 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>>* opt_resources_list) { | |
| 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) { | |
|
falken
2015/06/08 04:59:09
|index| is not used?
jungkees
2015/06/08 16:00:19
No, it isn't. I removed it.
| |
| 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 if (opt_resources_list) { | |
| 478 std::vector<ResourceRecord> resources; | |
| 479 status = ReadResourceRecords(registration.version_id, &resources); | |
| 480 if (status != STATUS_OK) { | |
| 481 HandleReadResult(FROM_HERE, status); | |
| 482 opt_resources_list->clear(); | |
| 483 return status; | |
| 484 } | |
| 485 opt_resources_list->push_back(resources); | |
| 486 } | |
| 474 } | 487 } |
| 475 | 488 |
| 476 HandleReadResult(FROM_HERE, status); | 489 HandleReadResult(FROM_HERE, status); |
| 477 return status; | 490 return status; |
| 478 } | 491 } |
| 479 | 492 |
| 480 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( | 493 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( |
| 481 std::vector<RegistrationData>* registrations) { | 494 std::vector<RegistrationData>* registrations) { |
| 482 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 495 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 483 DCHECK(registrations->empty()); | 496 DCHECK(registrations->empty()); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 714 return status; | 727 return status; |
| 715 if (!origin.is_valid()) | 728 if (!origin.is_valid()) |
| 716 return STATUS_ERROR_FAILED; | 729 return STATUS_ERROR_FAILED; |
| 717 | 730 |
| 718 leveldb::WriteBatch batch; | 731 leveldb::WriteBatch batch; |
| 719 | 732 |
| 720 // Remove |origin| from unique origins if a registration specified by | 733 // Remove |origin| from unique origins if a registration specified by |
| 721 // |registration_id| is the only one for |origin|. | 734 // |registration_id| is the only one for |origin|. |
| 722 // TODO(nhiroki): Check the uniqueness by more efficient way. | 735 // TODO(nhiroki): Check the uniqueness by more efficient way. |
| 723 std::vector<RegistrationData> registrations; | 736 std::vector<RegistrationData> registrations; |
| 724 status = GetRegistrationsForOrigin(origin, ®istrations); | 737 status = GetRegistrationsForOrigin(origin, ®istrations, nullptr); |
| 725 if (status != STATUS_OK) | 738 if (status != STATUS_OK) |
| 726 return status; | 739 return status; |
| 727 | 740 |
| 728 if (registrations.size() == 1 && | 741 if (registrations.size() == 1 && |
| 729 registrations[0].registration_id == registration_id) { | 742 registrations[0].registration_id == registration_id) { |
| 730 batch.Delete(CreateUniqueOriginKey(origin)); | 743 batch.Delete(CreateUniqueOriginKey(origin)); |
| 731 } | 744 } |
| 732 | 745 |
| 733 // Delete a registration specified by |registration_id|. | 746 // Delete a registration specified by |registration_id|. |
| 734 batch.Delete(CreateRegistrationKey(registration_id, origin)); | 747 batch.Delete(CreateRegistrationKey(registration_id, origin)); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 931 leveldb::WriteBatch batch; | 944 leveldb::WriteBatch batch; |
| 932 | 945 |
| 933 for (const GURL& origin : origins) { | 946 for (const GURL& origin : origins) { |
| 934 if (!origin.is_valid()) | 947 if (!origin.is_valid()) |
| 935 return STATUS_ERROR_FAILED; | 948 return STATUS_ERROR_FAILED; |
| 936 | 949 |
| 937 // Delete from the unique origin list. | 950 // Delete from the unique origin list. |
| 938 batch.Delete(CreateUniqueOriginKey(origin)); | 951 batch.Delete(CreateUniqueOriginKey(origin)); |
| 939 | 952 |
| 940 std::vector<RegistrationData> registrations; | 953 std::vector<RegistrationData> registrations; |
| 941 status = GetRegistrationsForOrigin(origin, ®istrations); | 954 status = GetRegistrationsForOrigin(origin, ®istrations, nullptr); |
| 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 |