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

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: 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..474e9ff4df7c796db527915a7aedd6c1a9600b18 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: Decimal string which fits into an int64_t.
+//
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) {
@@ -71,6 +88,34 @@ NotificationDatabase::Status NotificationDatabase::Open(
return STATUS_OK;
}
+NotificationDatabase::Status NotificationDatabase::GetNextNotificationId(
+ int64_t* notification_id) const {
+ 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;
+
+ int64_t next_notification_id;
+ if (!base::StringToInt64(value, &next_notification_id) ||
+ next_notification_id <= 0) {
Bernhard Bauer 2015/03/12 16:38:55 `next_notification_id < kFirstNotificationId`?
Peter Beverloo 2015/03/12 17:05:52 Done.
+ 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::WriteNextNotificationId(
+ leveldb::WriteBatch* batch,
+ int64_t next_notification_id) const {
+ DCHECK_GT(next_notification_id, 0);
Bernhard Bauer 2015/03/12 16:38:55 DCHECK_GE(next_notification_id, kFirstNotification
Peter Beverloo 2015/03/12 17:05:52 I like it. Done :)
+ DCHECK(batch);
+
+ batch->Put(kNextNotificationIdKey, base::Int64ToString(next_notification_id));
+}
+
} // namespace content
« no previous file with comments | « content/browser/notifications/notification_database.h ('k') | content/browser/notifications/notification_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698