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

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

Issue 1004493002: Store the next notification id in the notification database. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@n-db-NotificationDatabase
Patch Set: Bernhard's feedback Created 5 years, 9 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 756d01dc084f6bbf0c66318cf588e335f10a7a9e..f7297e6aadcc2ac1fe8f3e2d7a9d8f53e865de1a 100644
--- a/content/browser/notifications/notification_database.cc
+++ b/content/browser/notifications/notification_database.cc
@@ -4,15 +4,32 @@
#include "content/browser/notifications/notification_database.h"
+#include <string>
+
#include "base/files/file_util.h"
+#include "base/strings/string_number_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/env.h"
+#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
+
+// Notification LevelDB database schema (in alphabetized order)
+// =======================
+//
+// key: "NEXT_NOTIFICATION_ID"
+// value: <int64 'next_notification_id'>
+//
namespace content {
namespace {
+// Keys of the fields defined in the database.
+const char kNextNotificationIdKey[] = "NEXT_NOTIFICATION_ID";
+
+// The first notification id which to be handed out by the database.
+const int64_t kFirstNotificationId = 1;
+
// Converts the LevelDB |status| to one of the notification database's values.
NotificationDatabase::Status LevelDBStatusToStatus(
const leveldb::Status& status) {
@@ -30,6 +47,7 @@ NotificationDatabase::Status LevelDBStatusToStatus(
NotificationDatabase::NotificationDatabase(const base::FilePath& path)
: path_(path),
+ next_notification_id_(kFirstNotificationId),
state_(STATE_UNINITIALIZED) {
sequence_checker_.DetachFromSequence();
}
@@ -71,6 +89,33 @@ NotificationDatabase::Status NotificationDatabase::Open(
return STATUS_OK;
}
+NotificationDatabase::Status NotificationDatabase::GetNextNotificationId(
cmumford 2015/03/12 02:09:37 This doesn't modify the class so could be a const
Peter Beverloo 2015/03/12 02:13:56 It writes to |next_notification_id_| on line 110.
+ int64_t* notification_id) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ DCHECK_EQ(state_, STATE_INITIALIZED);
+ DCHECK(notification_id);
+
+ std::string value;
+ Status status = LevelDBStatusToStatus(
+ db_->Get(leveldb::ReadOptions(), kNextNotificationIdKey, &value));
+
+ if (status == STATUS_ERROR_NOT_FOUND) {
+ *notification_id = kFirstNotificationId;
+ return STATUS_OK;
+ }
+
+ if (status != STATUS_OK)
+ return status;
+
+ if (!base::StringToInt64(value, &next_notification_id_) ||
+ next_notification_id_ <= 0) {
+ return STATUS_ERROR_CORRUPTED;
+ }
+
+ *notification_id = next_notification_id_;
+ return STATUS_OK;
+}
+
NotificationDatabase::Status NotificationDatabase::Destroy() {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
@@ -89,4 +134,13 @@ NotificationDatabase::Status NotificationDatabase::Destroy() {
leveldb::DestroyDB(path_.AsUTF8Unsafe(), options));
}
+void NotificationDatabase::IncrementNextNotificationId(
+ leveldb::WriteBatch* batch) {
+ DCHECK(batch);
+
+ next_notification_id_++;
+ batch->Put(kNextNotificationIdKey,
+ base::Int64ToString(next_notification_id_));
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698