Chromium Code Reviews| Index: content/browser/service_worker/service_worker_database.cc |
| diff --git a/content/browser/service_worker/service_worker_database.cc b/content/browser/service_worker/service_worker_database.cc |
| index 74d56c8c261b711d2fff75276f3d6fc40e2c40fa..148475e76d8ddd9f531c04a13046bfeacabc2e14 100644 |
| --- a/content/browser/service_worker/service_worker_database.cc |
| +++ b/content/browser/service_worker/service_worker_database.cc |
| @@ -42,9 +42,15 @@ |
| // key: "INITDATA_UNIQUE_ORIGIN:" + <GURL 'origin'> |
| // value: <empty> |
| // |
| +// key: "PRES:" + <int64 'purgeable_resource_id'> |
| +// value: <empty> |
| +// |
| // key: "REG:" + <GURL 'origin'> + '\x00' + <int64 'registration_id'> |
| // (ex. "REG:http://example.com\x00123456") |
| // value: <ServiceWorkerRegistrationData serialized as a string> |
| +// |
| +// key: "URES:" + <int64 'uncommitted_resource_id'> |
| +// value: <empty> |
| namespace content { |
| @@ -59,6 +65,9 @@ const char kUniqueOriginKey[] = "INITDATA_UNIQUE_ORIGIN:"; |
| const char kRegKeyPrefix[] = "REG:"; |
| const char kKeySeparator = '\x00'; |
| +const char kUncommittedResIdKeyPrefix[] = "URES:"; |
| +const char kPurgeableResIdKeyPrefix[] = "PRES:"; |
| + |
| const int64 kCurrentSchemaVersion = 1; |
| bool RemovePrefix(const std::string& str, |
| @@ -84,6 +93,11 @@ std::string CreateUniqueOriginKey(const GURL& origin) { |
| return base::StringPrintf("%s%s", kUniqueOriginKey, origin.spec().c_str()); |
| } |
| +std::string CreateResourceIdKey(const char* key_prefix, int64 resource_id) { |
| + return base::StringPrintf( |
| + "%s%s", key_prefix, base::Int64ToString(resource_id).c_str()); |
| +} |
| + |
| void PutRegistrationDataToBatch( |
| const ServiceWorkerDatabase::RegistrationData& input, |
| leveldb::WriteBatch* batch) { |
| @@ -350,6 +364,138 @@ bool ServiceWorkerDatabase::DeleteRegistration(int64 registration_id, |
| return WriteBatch(&batch); |
| } |
| +bool ServiceWorkerDatabase::GetUncommittedResourceIds(std::set<int64>* ids) { |
|
michaeln
2014/04/29 23:50:03
I think the only difference between the Uncommitte
nhiroki
2014/04/30 03:05:58
That's right. Added Read/Write/DeleteResourceIds()
|
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + DCHECK(ids); |
| + |
| + if (!LazyOpen(false) || is_disabled_) |
| + return false; |
| + |
| + scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
| + for (itr->Seek(kUncommittedResIdKeyPrefix); itr->Valid(); itr->Next()) { |
| + if (!itr->status().ok()) { |
| + HandleError(FROM_HERE, itr->status()); |
| + ids->clear(); |
| + return false; |
| + } |
| + |
| + std::string key = itr->key().ToString(); |
| + std::string unprefixed; |
| + if (!RemovePrefix(key, kUncommittedResIdKeyPrefix, &unprefixed)) |
| + break; |
| + |
| + int64 resource_id; |
| + if (!base::StringToInt64(unprefixed, &resource_id)) { |
| + HandleError(FROM_HERE, leveldb::Status::Corruption("failed to parse")); |
| + ids->clear(); |
| + return false; |
| + } |
| + ids->insert(resource_id); |
| + } |
| + return true; |
| +} |
| + |
| +bool ServiceWorkerDatabase::WriteUncommittedResourceIds( |
| + const std::set<int64>& ids) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + if (!LazyOpen(true) || is_disabled_) |
| + return false; |
| + if (ids.empty()) |
| + return true; |
| + |
| + leveldb::WriteBatch batch; |
| + for (std::set<int64>::const_iterator itr = ids.begin(); |
| + itr != ids.end(); ++itr) { |
| + // Value should be empty. |
| + batch.Put(CreateResourceIdKey(kUncommittedResIdKeyPrefix, *itr), ""); |
| + } |
| + return WriteBatch(&batch); |
| +} |
| + |
| +bool ServiceWorkerDatabase::ClearUncommittedResourceIds( |
| + const std::set<int64>& ids) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + if (!LazyOpen(true) || is_disabled_) |
| + return false; |
| + if (ids.empty()) |
| + return true; |
| + |
| + leveldb::WriteBatch batch; |
| + for (std::set<int64>::const_iterator itr = ids.begin(); |
| + itr != ids.end(); ++itr) { |
| + batch.Delete(CreateResourceIdKey(kUncommittedResIdKeyPrefix, *itr)); |
| + } |
| + return WriteBatch(&batch); |
| +} |
| + |
| +bool ServiceWorkerDatabase::GetPurgeableResourceIds(std::set<int64>* ids) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + DCHECK(ids); |
| + |
| + if (!LazyOpen(false) || is_disabled_) |
| + return false; |
| + |
| + scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
| + for (itr->Seek(kPurgeableResIdKeyPrefix); itr->Valid(); itr->Next()) { |
| + if (!itr->status().ok()) { |
| + HandleError(FROM_HERE, itr->status()); |
| + ids->clear(); |
| + return false; |
| + } |
| + |
| + std::string key = itr->key().ToString(); |
| + std::string unprefixed; |
| + if (!RemovePrefix(key, kPurgeableResIdKeyPrefix, &unprefixed)) |
| + break; |
| + |
| + int64 resource_id; |
| + if (!base::StringToInt64(unprefixed, &resource_id)) { |
| + HandleError(FROM_HERE, leveldb::Status::Corruption("failed to parse")); |
| + ids->clear(); |
| + return false; |
| + } |
| + ids->insert(resource_id); |
| + } |
| + return true; |
| +} |
| + |
| +bool ServiceWorkerDatabase::WritePurgeableResourceIds( |
| + const std::set<int64>& ids) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + if (!LazyOpen(true) || is_disabled_) |
| + return false; |
| + if (ids.empty()) |
| + return true; |
| + |
| + leveldb::WriteBatch batch; |
| + for (std::set<int64>::const_iterator itr = ids.begin(); |
| + itr != ids.end(); ++itr) { |
| + // Value should be empty. |
| + batch.Put(CreateResourceIdKey(kPurgeableResIdKeyPrefix, *itr), ""); |
| + } |
| + return WriteBatch(&batch); |
| +} |
| + |
| +bool ServiceWorkerDatabase::ClearPurgeableResourceIds( |
| + const std::set<int64>& ids) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + if (!LazyOpen(true) || is_disabled_) |
| + return false; |
| + if (ids.empty()) |
| + return true; |
| + |
| + leveldb::WriteBatch batch; |
| + for (std::set<int64>::const_iterator itr = ids.begin(); |
| + itr != ids.end(); ++itr) { |
| + batch.Delete(CreateResourceIdKey(kPurgeableResIdKeyPrefix, *itr)); |
| + } |
| + return WriteBatch(&batch); |
| +} |
| + |
| bool ServiceWorkerDatabase::LazyOpen(bool create_if_needed) { |
| DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| if (IsOpen()) |