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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 bool ServiceWorkerDatabase::WritePurgeableResourceIds( | 492 bool ServiceWorkerDatabase::WritePurgeableResourceIds( |
| 493 const std::set<int64>& ids) { | 493 const std::set<int64>& ids) { |
| 494 return WriteResourceIds(kPurgeableResIdKeyPrefix, ids); | 494 return WriteResourceIds(kPurgeableResIdKeyPrefix, ids); |
| 495 } | 495 } |
| 496 | 496 |
| 497 bool ServiceWorkerDatabase::ClearPurgeableResourceIds( | 497 bool ServiceWorkerDatabase::ClearPurgeableResourceIds( |
| 498 const std::set<int64>& ids) { | 498 const std::set<int64>& ids) { |
| 499 return DeleteResourceIds(kPurgeableResIdKeyPrefix, ids); | 499 return DeleteResourceIds(kPurgeableResIdKeyPrefix, ids); |
| 500 } | 500 } |
| 501 | 501 |
| 502 bool ServiceWorkerDatabase::DeleteAllDataForOrigin(const GURL& origin) { | |
| 503 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 504 if (!LazyOpen(true) || is_disabled_ || !origin.is_valid()) | |
| 505 return false; | |
| 506 | |
| 507 leveldb::WriteBatch batch; | |
| 508 | |
| 509 // Delete from the unique origin list. | |
| 510 batch.Delete(CreateUniqueOriginKey(origin)); | |
| 511 | |
| 512 std::vector<RegistrationData> registrations; | |
| 513 if (!GetRegistrationsForOrigin(origin, ®istrations)) | |
| 514 return false; | |
| 515 | |
| 516 // Delete registrations and resource records. | |
| 517 for (std::vector<RegistrationData>::const_iterator itr = | |
| 518 registrations.begin(); itr != registrations.end(); ++itr) { | |
| 519 batch.Delete(CreateRegistrationKey(itr->registration_id, origin)); | |
| 520 if (!DeleteResourceRecords(itr->version_id, &batch)) | |
| 521 return false; | |
| 522 } | |
| 523 | |
| 524 return WriteBatch(&batch); | |
| 525 } | |
| 526 | |
| 527 bool ServiceWorkerDatabase::DeleteAllData() { | |
| 528 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 529 if (!LazyOpen(true) || is_disabled_) | |
| 530 return false; | |
| 531 | |
| 532 leveldb::WriteBatch batch; | |
| 533 | |
| 534 // Delete until the iterator reaches the end of records. | |
|
nhiroki
2014/05/02 16:28:04
Is this expected behavior? I wonder if you want to
jsbell
2014/05/02 17:32:56
If we're going to delete everything, why not just
michaeln
2014/05/05 21:46:36
Right, we probably don't need this method at all.
nhiroki
2014/05/06 15:11:32
Okay, removed.
| |
| 535 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | |
| 536 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) { | |
| 537 if (!itr->status().ok()) { | |
| 538 HandleError(FROM_HERE, itr->status()); | |
| 539 return false; | |
| 540 } | |
| 541 batch.Delete(itr->key()); | |
| 542 } | |
| 543 | |
| 544 if (!WriteBatch(&batch)) | |
| 545 return false; | |
| 546 | |
| 547 // Initialize on-memory data. | |
| 548 next_avail_registration_id_ = 0; | |
| 549 next_avail_resource_id_ = 0; | |
| 550 next_avail_version_id_ = 0; | |
| 551 is_initialized_ = false; | |
| 552 | |
| 553 return false; | |
| 554 } | |
| 555 | |
| 502 bool ServiceWorkerDatabase::LazyOpen(bool create_if_needed) { | 556 bool ServiceWorkerDatabase::LazyOpen(bool create_if_needed) { |
| 503 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 557 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 504 if (IsOpen()) | 558 if (IsOpen()) |
| 505 return true; | 559 return true; |
| 506 | 560 |
| 507 // Do not try to open a database if we tried and failed once. | 561 // Do not try to open a database if we tried and failed once. |
| 508 if (is_disabled_) | 562 if (is_disabled_) |
| 509 return false; | 563 return false; |
| 510 | 564 |
| 511 // When |path_| is empty, open a database in-memory. | 565 // When |path_| is empty, open a database in-memory. |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 807 // TODO(nhiroki): Add an UMA histogram. | 861 // TODO(nhiroki): Add an UMA histogram. |
| 808 DLOG(ERROR) << "Failed at: " << from_here.ToString() | 862 DLOG(ERROR) << "Failed at: " << from_here.ToString() |
| 809 << " with error: " << status.ToString(); | 863 << " with error: " << status.ToString(); |
| 810 is_disabled_ = true; | 864 is_disabled_ = true; |
| 811 if (status.IsCorruption()) | 865 if (status.IsCorruption()) |
| 812 was_corruption_detected_ = true; | 866 was_corruption_detected_ = true; |
| 813 db_.reset(); | 867 db_.reset(); |
| 814 } | 868 } |
| 815 | 869 |
| 816 } // namespace content | 870 } // namespace content |
| OLD | NEW |