Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Unified Diff: content/browser/service_worker/service_worker_database.cc

Issue 254733002: ServiceWorker: Manipulate uncommitted/purgeable resource ids (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..56ded5d22ef07854c117411f7055a8c5ffcb0524 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,52 @@ bool ServiceWorkerDatabase::DeleteRegistration(int64 registration_id,
return WriteBatch(&batch);
}
+bool ServiceWorkerDatabase::GetUncommittedResourceIds(std::set<int64>* ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(false) || is_disabled_)
+ return false;
+ return ReadResourceIds(kUncommittedResIdKeyPrefix, ids);
+}
+
+bool ServiceWorkerDatabase::WriteUncommittedResourceIds(
+ const std::set<int64>& ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(true) || is_disabled_)
+ return false;
+ return WriteResourceIds(kUncommittedResIdKeyPrefix, ids);
+}
+
+bool ServiceWorkerDatabase::ClearUncommittedResourceIds(
+ const std::set<int64>& ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(true) || is_disabled_)
+ return false;
+ return DeleteResourceIds(kUncommittedResIdKeyPrefix, ids);
+}
+
+bool ServiceWorkerDatabase::GetPurgeableResourceIds(std::set<int64>* ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(false) || is_disabled_)
+ return false;
+ return ReadResourceIds(kPurgeableResIdKeyPrefix, ids);
+}
+
+bool ServiceWorkerDatabase::WritePurgeableResourceIds(
+ const std::set<int64>& ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(true) || is_disabled_)
+ return false;
+ return WriteResourceIds(kPurgeableResIdKeyPrefix, ids);
+}
+
+bool ServiceWorkerDatabase::ClearPurgeableResourceIds(
+ const std::set<int64>& ids) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ if (!LazyOpen(true) || is_disabled_)
michaeln 2014/04/30 03:23:57 These tests and the dcheck could also be in the co
nhiroki 2014/04/30 04:04:06 That's just my preference. Basically I prefer such
+ return false;
+ return DeleteResourceIds(kPurgeableResIdKeyPrefix, ids);
+}
+
bool ServiceWorkerDatabase::LazyOpen(bool create_if_needed) {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
if (IsOpen())
@@ -451,6 +511,61 @@ bool ServiceWorkerDatabase::ReadRegistrationData(
return true;
}
+bool ServiceWorkerDatabase::ReadResourceIds(const char* id_key_prefix,
+ std::set<int64>* ids) {
+ DCHECK(id_key_prefix);
+ DCHECK(ids);
+
+ scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
+ for (itr->Seek(id_key_prefix); itr->Valid(); itr->Next()) {
+ if (!itr->status().ok()) {
+ HandleError(FROM_HERE, itr->status());
+ ids->clear();
+ return false;
+ }
+
+ std::string unprefixed;
+ if (!RemovePrefix(itr->key().ToString(), id_key_prefix, &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::WriteResourceIds(const char* id_key_prefix,
+ const std::set<int64>& ids) {
+ DCHECK(id_key_prefix);
+ 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(id_key_prefix, *itr), "");
+ }
+ return WriteBatch(&batch);
+}
+
+bool ServiceWorkerDatabase::DeleteResourceIds(const char* id_key_prefix,
+ const std::set<int64>& ids) {
+ DCHECK(id_key_prefix);
+ if (ids.empty())
+ return true;
+ leveldb::WriteBatch batch;
+ for (std::set<int64>::const_iterator itr = ids.begin();
+ itr != ids.end(); ++itr) {
+ batch.Delete(CreateResourceIdKey(id_key_prefix, *itr));
+ }
+ return WriteBatch(&batch);
+}
+
bool ServiceWorkerDatabase::ReadDatabaseVersion(int64* db_version) {
std::string value;
leveldb::Status status =

Powered by Google App Engine
This is Rietveld 408576698