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

Unified Diff: content/browser/notifications/notification_database.cc

Issue 2300093002: Make //content responsible for generating notification Ids (Closed)
Patch Set: Make //content responsible for generating notification Ids Created 4 years, 3 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/notifications/notification_database.cc
diff --git a/content/browser/notifications/notification_database.cc b/content/browser/notifications/notification_database.cc
index e7ac09b3f0ac78d7acb13e237c4a2ed43616317a..6e5d0a238e99865a3748516688fea39f2cbde5ec 100644
--- a/content/browser/notifications/notification_database.cc
+++ b/content/browser/notifications/notification_database.cc
@@ -29,6 +29,10 @@
// value: String containing the NotificationDatabaseDataProto protocol buffer
// in serialized form.
//
+// key: "ASSOC:" <notification id>
+// value: String containing the associated persistent notification id as an
+// int64_t in textual representation.
+//
// key: "NEXT_NOTIFICATION_ID"
// value: Decimal string which fits into an int64_t.
@@ -38,6 +42,7 @@ namespace {
// Keys of the fields defined in the database.
const char kNextNotificationIdKey[] = "NEXT_NOTIFICATION_ID";
const char kDataKeyPrefix[] = "DATA:";
+const char kAssociatedKeyPrefix[] = "ASSOC:";
// Separates the components of compound keys.
const char kKeySeparator = '\x00';
@@ -75,9 +80,18 @@ std::string CreateDataPrefix(const GURL& origin) {
}
// Creates the compound data key in which notification data is stored.
-std::string CreateDataKey(const GURL& origin, int64_t notification_id) {
+std::string CreateDataKey(const GURL& origin,
+ int64_t persistent_notification_id) {
DCHECK(origin.is_valid());
- return CreateDataPrefix(origin) + base::Int64ToString(notification_id);
+ return CreateDataPrefix(origin) +
+ base::Int64ToString(persistent_notification_id);
+}
+
+// Creates the compound key for mapping the given |notification_id| to the
+// latest persistent notification id in the database.
+std::string CreateAssociatedKey(const std::string& notification_id) {
+ return base::StringPrintf("%s%s", kAssociatedKeyPrefix,
+ notification_id.c_str());
}
// Deserializes data in |serialized_data| to |notification_database_data|.
@@ -141,16 +155,16 @@ NotificationDatabase::Status NotificationDatabase::Open(
}
NotificationDatabase::Status NotificationDatabase::ReadNotificationData(
- int64_t notification_id,
+ int64_t persistent_notification_id,
const GURL& origin,
NotificationDatabaseData* notification_database_data) const {
DCHECK(sequence_checker_.CalledOnValidSequence());
DCHECK_EQ(STATE_INITIALIZED, state_);
- DCHECK_GE(notification_id, kFirstNotificationId);
+ DCHECK_GE(persistent_notification_id, kFirstNotificationId);
DCHECK(origin.is_valid());
DCHECK(notification_database_data);
- std::string key = CreateDataKey(origin, notification_id);
+ std::string key = CreateDataKey(origin, persistent_notification_id);
std::string serialized_data;
Status status = LevelDBStatusToStatus(
@@ -189,10 +203,10 @@ NotificationDatabase::ReadAllNotificationDataForServiceWorkerRegistration(
NotificationDatabase::Status NotificationDatabase::WriteNotificationData(
const GURL& origin,
const NotificationDatabaseData& notification_database_data,
- int64_t* notification_id) {
+ int64_t* persistent_notification_id) {
DCHECK(sequence_checker_.CalledOnValidSequence());
DCHECK_EQ(STATE_INITIALIZED, state_);
- DCHECK(notification_id);
+ DCHECK(persistent_notification_id);
DCHECK(origin.is_valid());
DCHECK_GE(next_notification_id_, kFirstNotificationId);
@@ -217,19 +231,19 @@ NotificationDatabase::Status NotificationDatabase::WriteNotificationData(
if (status != STATUS_OK)
return status;
- *notification_id = next_notification_id_++;
+ *persistent_notification_id = next_notification_id_++;
return STATUS_OK;
}
NotificationDatabase::Status NotificationDatabase::DeleteNotificationData(
- int64_t notification_id,
+ int64_t persistent_notification_id,
const GURL& origin) {
DCHECK(sequence_checker_.CalledOnValidSequence());
DCHECK_EQ(STATE_INITIALIZED, state_);
- DCHECK_GE(notification_id, kFirstNotificationId);
+ DCHECK_GE(persistent_notification_id, kFirstNotificationId);
DCHECK(origin.is_valid());
- std::string key = CreateDataKey(origin, notification_id);
+ std::string key = CreateDataKey(origin, persistent_notification_id);
return LevelDBStatusToStatus(db_->Delete(leveldb::WriteOptions(), key));
}
@@ -253,6 +267,52 @@ NotificationDatabase::DeleteAllNotificationDataForServiceWorkerRegistration(
deleted_notification_set);
}
+NotificationDatabase::Status NotificationDatabase::StoreIdAssociation(
+ const std::string& notification_id,
+ int64_t persistent_notification_id) {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+
+ std::string key = CreateAssociatedKey(notification_id);
+ std::string serialized_persistent_notification_id =
+ base::Int64ToString(persistent_notification_id);
+
+ Status status = LevelDBStatusToStatus(db_->Put(
+ leveldb::WriteOptions(), key, serialized_persistent_notification_id));
+
+ return status;
+}
+
+NotificationDatabase::Status NotificationDatabase::ReadIdAssociation(
+ const std::string& notification_id,
+ int64_t* persistent_notification_id) {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+
+ std::string key = CreateAssociatedKey(notification_id);
+ std::string serialized_persistent_notification_id;
+
+ Status status = LevelDBStatusToStatus(db_->Get(
+ leveldb::ReadOptions(), key, &serialized_persistent_notification_id));
+
+ if (status != STATUS_OK)
+ return status;
+
+ if (!base::StringToInt64(serialized_persistent_notification_id,
+ persistent_notification_id)) {
+ return STATUS_ERROR_CORRUPTED;
+ }
+
+ return status;
+}
+
+NotificationDatabase::Status NotificationDatabase::DeleteIdAssociation(
+ const std::string& notification_id) {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+
+ std::string key = CreateAssociatedKey(notification_id);
+
+ return LevelDBStatusToStatus(db_->Delete(leveldb::WriteOptions(), key));
+}
+
NotificationDatabase::Status NotificationDatabase::Destroy() {
DCHECK(sequence_checker_.CalledOnValidSequence());
@@ -375,13 +435,13 @@ NotificationDatabase::DeleteAllNotificationDataInternal(
leveldb::Slice notification_id_slice = iter->key();
notification_id_slice.remove_prefix(prefix_slice.size());
- int64_t notification_id = 0;
+ int64_t persistent_notification_id = 0;
if (!base::StringToInt64(notification_id_slice.ToString(),
- &notification_id)) {
+ &persistent_notification_id)) {
return STATUS_ERROR_CORRUPTED;
}
- deleted_notification_set->insert(notification_id);
+ deleted_notification_set->insert(persistent_notification_id);
batch.Delete(iter->key());
}

Powered by Google App Engine
This is Rietveld 408576698