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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 // (ex. "RES:123456\x00654321") | 65 // (ex. "RES:123456\x00654321") |
| 66 // value: <ServiceWorkerResourceRecord serialized as a string> | 66 // value: <ServiceWorkerResourceRecord serialized as a string> |
| 67 // | 67 // |
| 68 // key: "URES:" + <int64 'uncommitted_resource_id'> | 68 // key: "URES:" + <int64 'uncommitted_resource_id'> |
| 69 // value: <empty> | 69 // value: <empty> |
| 70 // | 70 // |
| 71 // Version 2 | 71 // Version 2 |
| 72 // | 72 // |
| 73 // key: "REGID_TO_ORIGIN:" + <int64 'registration_id'> | 73 // key: "REGID_TO_ORIGIN:" + <int64 'registration_id'> |
| 74 // value: <GURL 'origin'> | 74 // value: <GURL 'origin'> |
| 75 // | |
| 76 // key: "INITDATA_DISKCACHE_VERSION" | |
|
michaeln
2015/06/11 00:52:38
It feels a little early to generalize a diskcache
nhiroki
2015/06/11 21:04:50
I was thinking to keep these fields after migratio
| |
| 77 // value: <int64 'current_disk_cache_version'> | |
| 78 // | |
| 79 // key: "INITDATA_PURGEABLE_FILE:" + <string 'filename'> | |
| 80 // value: <empty> | |
| 75 namespace content { | 81 namespace content { |
| 76 | 82 |
| 77 namespace { | 83 namespace { |
| 78 | 84 |
| 79 const char kDatabaseVersionKey[] = "INITDATA_DB_VERSION"; | 85 const char kDatabaseVersionKey[] = "INITDATA_DB_VERSION"; |
| 80 const char kNextRegIdKey[] = "INITDATA_NEXT_REGISTRATION_ID"; | 86 const char kNextRegIdKey[] = "INITDATA_NEXT_REGISTRATION_ID"; |
| 81 const char kNextResIdKey[] = "INITDATA_NEXT_RESOURCE_ID"; | 87 const char kNextResIdKey[] = "INITDATA_NEXT_RESOURCE_ID"; |
| 82 const char kNextVerIdKey[] = "INITDATA_NEXT_VERSION_ID"; | 88 const char kNextVerIdKey[] = "INITDATA_NEXT_VERSION_ID"; |
| 83 const char kUniqueOriginKey[] = "INITDATA_UNIQUE_ORIGIN:"; | 89 const char kUniqueOriginKey[] = "INITDATA_UNIQUE_ORIGIN:"; |
| 90 const char kDiskCacheVersionKey[] = "INITDATA_DISKCACHE_VERSION"; | |
| 91 const char kPurgeableFileKey[] = "INITDATA_PURGEABLE_FILE:"; | |
| 84 | 92 |
| 85 const char kRegKeyPrefix[] = "REG:"; | 93 const char kRegKeyPrefix[] = "REG:"; |
| 86 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; | 94 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; |
| 87 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; | 95 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; |
| 88 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; | 96 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; |
| 89 const char kResKeyPrefix[] = "RES:"; | 97 const char kResKeyPrefix[] = "RES:"; |
| 90 const char kKeySeparator = '\x00'; | 98 const char kKeySeparator = '\x00'; |
| 99 const char kEmptyValue[] = ""; | |
| 91 | 100 |
| 92 const char kUncommittedResIdKeyPrefix[] = "URES:"; | 101 const char kUncommittedResIdKeyPrefix[] = "URES:"; |
| 93 const char kPurgeableResIdKeyPrefix[] = "PRES:"; | 102 const char kPurgeableResIdKeyPrefix[] = "PRES:"; |
| 94 | 103 |
| 95 const int64 kCurrentSchemaVersion = 2; | 104 const int64 kCurrentSchemaVersion = 2; |
| 96 | 105 |
| 97 bool RemovePrefix(const std::string& str, | 106 bool RemovePrefix(const std::string& str, |
| 98 const std::string& prefix, | 107 const std::string& prefix, |
| 99 std::string* out) { | 108 std::string* out) { |
| 100 if (!StartsWithASCII(str, prefix, true)) | 109 if (!StartsWithASCII(str, prefix, true)) |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 status = ReadNextAvailableId(kNextResIdKey, &next_avail_resource_id_); | 410 status = ReadNextAvailableId(kNextResIdKey, &next_avail_resource_id_); |
| 402 if (status != STATUS_OK) | 411 if (status != STATUS_OK) |
| 403 return status; | 412 return status; |
| 404 | 413 |
| 405 *next_avail_registration_id = next_avail_registration_id_; | 414 *next_avail_registration_id = next_avail_registration_id_; |
| 406 *next_avail_version_id = next_avail_version_id_; | 415 *next_avail_version_id = next_avail_version_id_; |
| 407 *next_avail_resource_id = next_avail_resource_id_; | 416 *next_avail_resource_id = next_avail_resource_id_; |
| 408 return STATUS_OK; | 417 return STATUS_OK; |
| 409 } | 418 } |
| 410 | 419 |
| 420 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadDiskCacheVersion( | |
| 421 int64* disk_cache_version) { | |
| 422 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 423 | |
| 424 Status status = LazyOpen(false); | |
| 425 if (IsNewOrNonexistentDatabase(status)) { | |
| 426 *disk_cache_version = 0; | |
|
michaeln
2015/06/11 00:52:38
should this be kCurrentDiskCacheVersion?
nhiroki
2015/06/11 21:04:50
(This code is gone)
| |
| 427 return STATUS_OK; | |
| 428 } | |
| 429 if (status != STATUS_OK) | |
| 430 return status; | |
| 431 | |
| 432 std::string value; | |
| 433 status = LevelDBStatusToStatus( | |
| 434 db_->Get(leveldb::ReadOptions(), kDiskCacheVersionKey, &value)); | |
| 435 if (status == STATUS_ERROR_NOT_FOUND) { | |
| 436 *disk_cache_version = 0; | |
| 437 HandleReadResult(FROM_HERE, STATUS_OK); | |
| 438 return STATUS_OK; | |
| 439 } | |
| 440 | |
| 441 if (status != STATUS_OK) { | |
| 442 HandleReadResult(FROM_HERE, status); | |
| 443 return status; | |
| 444 } | |
| 445 | |
| 446 status = ParseId(value, disk_cache_version); | |
| 447 HandleReadResult(FROM_HERE, status); | |
| 448 return status; | |
| 449 } | |
| 450 | |
| 451 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteDiskCacheVersion( | |
| 452 int64 disk_cache_version) { | |
| 453 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 454 | |
| 455 Status status = LazyOpen(false); | |
| 456 if (status != STATUS_OK) | |
| 457 return status; | |
| 458 | |
| 459 leveldb::WriteBatch batch; | |
| 460 batch.Put(kDiskCacheVersionKey, base::Int64ToString(disk_cache_version)); | |
| 461 return WriteBatch(&batch); | |
| 462 } | |
| 463 | |
| 411 ServiceWorkerDatabase::Status | 464 ServiceWorkerDatabase::Status |
| 412 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { | 465 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { |
| 413 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 466 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 414 DCHECK(origins->empty()); | 467 DCHECK(origins->empty()); |
| 415 | 468 |
| 416 Status status = LazyOpen(false); | 469 Status status = LazyOpen(false); |
| 417 if (IsNewOrNonexistentDatabase(status)) | 470 if (IsNewOrNonexistentDatabase(status)) |
| 418 return STATUS_OK; | 471 return STATUS_OK; |
| 419 if (status != STATUS_OK) | 472 if (status != STATUS_OK) |
| 420 return status; | 473 return status; |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 926 Status status = DeleteResourceIdsInBatch( | 979 Status status = DeleteResourceIdsInBatch( |
| 927 kUncommittedResIdKeyPrefix, ids, &batch); | 980 kUncommittedResIdKeyPrefix, ids, &batch); |
| 928 if (status != STATUS_OK) | 981 if (status != STATUS_OK) |
| 929 return status; | 982 return status; |
| 930 status = WriteResourceIdsInBatch(kPurgeableResIdKeyPrefix, ids, &batch); | 983 status = WriteResourceIdsInBatch(kPurgeableResIdKeyPrefix, ids, &batch); |
| 931 if (status != STATUS_OK) | 984 if (status != STATUS_OK) |
| 932 return status; | 985 return status; |
| 933 return WriteBatch(&batch); | 986 return WriteBatch(&batch); |
| 934 } | 987 } |
| 935 | 988 |
| 989 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetPurgeableFiles( | |
| 990 std::vector<std::string>* filenames) { | |
| 991 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 992 DCHECK(filenames->empty()); | |
| 993 | |
| 994 Status status = LazyOpen(false); | |
| 995 if (IsNewOrNonexistentDatabase(status)) | |
| 996 return STATUS_OK; | |
| 997 if (status != STATUS_OK) | |
| 998 return status; | |
| 999 | |
| 1000 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | |
| 1001 for (itr->Seek(kPurgeableFileKey); itr->Valid(); itr->Next()) { | |
| 1002 status = LevelDBStatusToStatus(itr->status()); | |
| 1003 if (status != STATUS_OK) { | |
| 1004 HandleReadResult(FROM_HERE, status); | |
| 1005 filenames->clear(); | |
| 1006 return status; | |
| 1007 } | |
| 1008 | |
| 1009 std::string filename; | |
| 1010 if (!RemovePrefix(itr->key().ToString(), kPurgeableFileKey, &filename)) | |
| 1011 break; | |
| 1012 | |
| 1013 if (filename.empty()) { | |
| 1014 HandleReadResult(FROM_HERE, STATUS_ERROR_CORRUPTED); | |
| 1015 filenames->clear(); | |
| 1016 return status; | |
| 1017 } | |
| 1018 filenames->push_back(filename); | |
| 1019 } | |
| 1020 | |
| 1021 HandleReadResult(FROM_HERE, status); | |
| 1022 return status; | |
| 1023 } | |
| 1024 | |
| 1025 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WritePurgeableFile( | |
| 1026 const std::string& filename) { | |
| 1027 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 1028 if (filename.empty()) | |
|
michaeln
2015/06/11 00:52:38
when would we ever expect empty names here?
nhiroki
2015/06/11 21:04:50
(This code is gone)
| |
| 1029 return ServiceWorkerDatabase::STATUS_OK; | |
| 1030 | |
| 1031 Status status = LazyOpen(true); | |
| 1032 if (status != STATUS_OK) | |
| 1033 return status; | |
| 1034 | |
| 1035 leveldb::WriteBatch batch; | |
| 1036 batch.Put(std::string(kPurgeableFileKey).append(filename), kEmptyValue); | |
| 1037 return WriteBatch(&batch); | |
| 1038 } | |
| 1039 | |
| 1040 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ClearPurgeableFile( | |
| 1041 const std::string& filename) { | |
| 1042 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 1043 if (filename.empty()) | |
| 1044 return ServiceWorkerDatabase::STATUS_OK; | |
| 1045 | |
| 1046 Status status = LazyOpen(false); | |
| 1047 if (IsNewOrNonexistentDatabase(status)) | |
| 1048 return STATUS_OK; | |
| 1049 if (status != STATUS_OK) | |
| 1050 return status; | |
| 1051 | |
| 1052 leveldb::WriteBatch batch; | |
| 1053 batch.Delete(std::string(kPurgeableFileKey).append(filename)); | |
| 1054 return WriteBatch(&batch); | |
| 1055 } | |
| 1056 | |
| 936 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteAllDataForOrigins( | 1057 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteAllDataForOrigins( |
| 937 const std::set<GURL>& origins, | 1058 const std::set<GURL>& origins, |
| 938 std::vector<int64>* newly_purgeable_resources) { | 1059 std::vector<int64>* newly_purgeable_resources) { |
| 939 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1060 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 940 Status status = LazyOpen(false); | 1061 Status status = LazyOpen(false); |
| 941 if (IsNewOrNonexistentDatabase(status)) | 1062 if (IsNewOrNonexistentDatabase(status)) |
| 942 return STATUS_OK; | 1063 return STATUS_OK; |
| 943 if (status != STATUS_OK) | 1064 if (status != STATUS_OK) |
| 944 return status; | 1065 return status; |
| 945 leveldb::WriteBatch batch; | 1066 leveldb::WriteBatch batch; |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1466 | 1587 |
| 1467 void ServiceWorkerDatabase::HandleWriteResult( | 1588 void ServiceWorkerDatabase::HandleWriteResult( |
| 1468 const tracked_objects::Location& from_here, | 1589 const tracked_objects::Location& from_here, |
| 1469 Status status) { | 1590 Status status) { |
| 1470 if (status != STATUS_OK) | 1591 if (status != STATUS_OK) |
| 1471 Disable(from_here, status); | 1592 Disable(from_here, status); |
| 1472 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1593 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1473 } | 1594 } |
| 1474 | 1595 |
| 1475 } // namespace content | 1596 } // namespace content |
| OLD | NEW |