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 1007 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 return status; | 1018 return status; |
1019 } | 1019 } |
1020 | 1020 |
1021 ServiceWorkerDatabase::Status | 1021 ServiceWorkerDatabase::Status |
1022 ServiceWorkerDatabase::GetUncommittedResourceIds(std::set<int64>* ids) { | 1022 ServiceWorkerDatabase::GetUncommittedResourceIds(std::set<int64>* ids) { |
1023 return ReadResourceIds(kUncommittedResIdKeyPrefix, ids); | 1023 return ReadResourceIds(kUncommittedResIdKeyPrefix, ids); |
1024 } | 1024 } |
1025 | 1025 |
1026 ServiceWorkerDatabase::Status | 1026 ServiceWorkerDatabase::Status |
1027 ServiceWorkerDatabase::WriteUncommittedResourceIds(const std::set<int64>& ids) { | 1027 ServiceWorkerDatabase::WriteUncommittedResourceIds(const std::set<int64>& ids) { |
1028 return WriteResourceIds(kUncommittedResIdKeyPrefix, ids); | 1028 leveldb::WriteBatch batch; |
| 1029 Status status = |
| 1030 WriteResourceIdsInBatch(kUncommittedResIdKeyPrefix, ids, &batch); |
| 1031 if (status != STATUS_OK) |
| 1032 return status; |
| 1033 return WriteBatch(&batch); |
1029 } | 1034 } |
1030 | 1035 |
1031 ServiceWorkerDatabase::Status | 1036 ServiceWorkerDatabase::Status |
1032 ServiceWorkerDatabase::ClearUncommittedResourceIds(const std::set<int64>& ids) { | |
1033 return DeleteResourceIds(kUncommittedResIdKeyPrefix, ids); | |
1034 } | |
1035 | |
1036 ServiceWorkerDatabase::Status | |
1037 ServiceWorkerDatabase::GetPurgeableResourceIds(std::set<int64>* ids) { | 1037 ServiceWorkerDatabase::GetPurgeableResourceIds(std::set<int64>* ids) { |
1038 return ReadResourceIds(kPurgeableResIdKeyPrefix, ids); | 1038 return ReadResourceIds(kPurgeableResIdKeyPrefix, ids); |
1039 } | 1039 } |
1040 | 1040 |
1041 ServiceWorkerDatabase::Status | 1041 ServiceWorkerDatabase::Status |
1042 ServiceWorkerDatabase::WritePurgeableResourceIds(const std::set<int64>& ids) { | |
1043 return WriteResourceIds(kPurgeableResIdKeyPrefix, ids); | |
1044 } | |
1045 | |
1046 ServiceWorkerDatabase::Status | |
1047 ServiceWorkerDatabase::ClearPurgeableResourceIds(const std::set<int64>& ids) { | 1042 ServiceWorkerDatabase::ClearPurgeableResourceIds(const std::set<int64>& ids) { |
1048 return DeleteResourceIds(kPurgeableResIdKeyPrefix, ids); | 1043 leveldb::WriteBatch batch; |
| 1044 Status status = |
| 1045 DeleteResourceIdsInBatch(kPurgeableResIdKeyPrefix, ids, &batch); |
| 1046 if (status != STATUS_OK) |
| 1047 return status; |
| 1048 return WriteBatch(&batch); |
1049 } | 1049 } |
1050 | 1050 |
1051 ServiceWorkerDatabase::Status | 1051 ServiceWorkerDatabase::Status |
1052 ServiceWorkerDatabase::PurgeUncommittedResourceIds( | 1052 ServiceWorkerDatabase::PurgeUncommittedResourceIds( |
1053 const std::set<int64>& ids) { | 1053 const std::set<int64>& ids) { |
1054 leveldb::WriteBatch batch; | 1054 leveldb::WriteBatch batch; |
1055 Status status = DeleteResourceIdsInBatch( | 1055 Status status = DeleteResourceIdsInBatch( |
1056 kUncommittedResIdKeyPrefix, ids, &batch); | 1056 kUncommittedResIdKeyPrefix, ids, &batch); |
1057 if (status != STATUS_OK) | 1057 if (status != STATUS_OK) |
1058 return status; | 1058 return status; |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1397 ids->clear(); | 1397 ids->clear(); |
1398 return status; | 1398 return status; |
1399 } | 1399 } |
1400 ids->insert(resource_id); | 1400 ids->insert(resource_id); |
1401 } | 1401 } |
1402 | 1402 |
1403 HandleReadResult(FROM_HERE, status); | 1403 HandleReadResult(FROM_HERE, status); |
1404 return status; | 1404 return status; |
1405 } | 1405 } |
1406 | 1406 |
1407 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIds( | |
1408 const char* id_key_prefix, | |
1409 const std::set<int64>& ids) { | |
1410 leveldb::WriteBatch batch; | |
1411 Status status = WriteResourceIdsInBatch(id_key_prefix, ids, &batch); | |
1412 if (status != STATUS_OK) | |
1413 return status; | |
1414 return WriteBatch(&batch); | |
1415 } | |
1416 | |
1417 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( | 1407 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( |
1418 const char* id_key_prefix, | 1408 const char* id_key_prefix, |
1419 const std::set<int64>& ids, | 1409 const std::set<int64>& ids, |
1420 leveldb::WriteBatch* batch) { | 1410 leveldb::WriteBatch* batch) { |
1421 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1411 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
1422 DCHECK(id_key_prefix); | 1412 DCHECK(id_key_prefix); |
1423 | 1413 |
1424 Status status = LazyOpen(true); | 1414 Status status = LazyOpen(true); |
1425 if (status != STATUS_OK) | 1415 if (status != STATUS_OK) |
1426 return status; | 1416 return status; |
1427 | 1417 |
1428 if (ids.empty()) | 1418 if (ids.empty()) |
1429 return STATUS_OK; | 1419 return STATUS_OK; |
1430 for (std::set<int64>::const_iterator itr = ids.begin(); | 1420 for (std::set<int64>::const_iterator itr = ids.begin(); |
1431 itr != ids.end(); ++itr) { | 1421 itr != ids.end(); ++itr) { |
1432 // Value should be empty. | 1422 // Value should be empty. |
1433 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); | 1423 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); |
1434 } | 1424 } |
1435 // std::set is sorted, so the last element is the largest. | 1425 // std::set is sorted, so the last element is the largest. |
1436 BumpNextResourceIdIfNeeded(*ids.rbegin(), batch); | 1426 BumpNextResourceIdIfNeeded(*ids.rbegin(), batch); |
1437 return STATUS_OK; | 1427 return STATUS_OK; |
1438 } | 1428 } |
1439 | 1429 |
1440 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIds( | |
1441 const char* id_key_prefix, | |
1442 const std::set<int64>& ids) { | |
1443 leveldb::WriteBatch batch; | |
1444 Status status = DeleteResourceIdsInBatch(id_key_prefix, ids, &batch); | |
1445 if (status != STATUS_OK) | |
1446 return status; | |
1447 return WriteBatch(&batch); | |
1448 } | |
1449 | |
1450 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIdsInBatch( | 1430 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIdsInBatch( |
1451 const char* id_key_prefix, | 1431 const char* id_key_prefix, |
1452 const std::set<int64>& ids, | 1432 const std::set<int64>& ids, |
1453 leveldb::WriteBatch* batch) { | 1433 leveldb::WriteBatch* batch) { |
1454 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1434 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
1455 DCHECK(id_key_prefix); | 1435 DCHECK(id_key_prefix); |
1456 | 1436 |
1457 Status status = LazyOpen(false); | 1437 Status status = LazyOpen(false); |
1458 if (IsNewOrNonexistentDatabase(status)) | 1438 if (IsNewOrNonexistentDatabase(status)) |
1459 return STATUS_OK; | 1439 return STATUS_OK; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1599 if (status != STATUS_OK) | 1579 if (status != STATUS_OK) |
1600 Disable(from_here, status); | 1580 Disable(from_here, status); |
1601 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1581 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
1602 } | 1582 } |
1603 | 1583 |
1604 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1584 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
1605 return path_.empty(); | 1585 return path_.empty(); |
1606 } | 1586 } |
1607 | 1587 |
1608 } // namespace content | 1588 } // namespace content |
OLD | NEW |