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

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

Issue 988163003: Introduce the NotificationDatabase class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments 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
new file mode 100644
index 0000000000000000000000000000000000000000..f348b7f4fca11f5049fb4c7543b004d9091fc4d5
--- /dev/null
+++ b/content/browser/notifications/notification_database.cc
@@ -0,0 +1,91 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/notifications/notification_database.h"
+
+#include "base/files/file_util.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"
+
+namespace content {
+namespace {
+
+// Converts the LevelDB |status| to one of the notification database's values.
+NotificationDatabase::Status LevelDBStatusToStatus(
+ const leveldb::Status& status) {
+ if (status.ok())
+ return NotificationDatabase::STATUS_OK;
+ else if (status.IsNotFound())
+ return NotificationDatabase::STATUS_ERROR_NOT_FOUND;
+
+ return NotificationDatabase::STATUS_ERROR_FAILED;
+}
+
+} // namespace
+
+NotificationDatabase::NotificationDatabase(const base::FilePath& path)
+ : path_(path),
+ state_(STATE_UNINITIALIZED) {
+ sequence_checker_.DetachFromSequence();
+}
+
+NotificationDatabase::~NotificationDatabase() {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ db_.reset();
+}
+
+NotificationDatabase::Status NotificationDatabase::Open(
+ bool create_if_missing) {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+ DCHECK_EQ(STATE_UNINITIALIZED, state_);
+
+ if (!create_if_missing) {
+ if (IsInMemoryDatabase() ||
+ !base::PathExists(path_) ||
+ base::IsDirectoryEmpty(path_)) {
+ return NotificationDatabase::STATUS_ERROR_NOT_FOUND;
+ }
+ }
+
+ leveldb::Options options;
+ options.create_if_missing = create_if_missing;
+ options.paranoid_checks = true;
+ if (IsInMemoryDatabase()) {
+ env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
+ options.env = env_.get();
+ }
+
+ leveldb::DB* db = nullptr;
+ Status status = LevelDBStatusToStatus(
+ leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db));
+ if (status != STATUS_OK)
+ return status;
+
+ state_ = STATE_INITIALIZED;
+ db_.reset(db);
+
+ return STATUS_OK;
+}
+
+NotificationDatabase::Status NotificationDatabase::Destroy() {
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread());
+
+ leveldb::Options options;
+ if (IsInMemoryDatabase()) {
+ if (!env_)
+ return STATUS_OK; // The database has not been initialized.
+
+ options.env = env_.get();
+ }
+
+ state_ = STATE_DISABLED;
+ db_.reset();
+
+ return LevelDBStatusToStatus(
+ leveldb::DestroyDB(path_.AsUTF8Unsafe(), options));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698