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..cc2eacda1630964fca5957b4f335c6f03528dcb5 |
--- /dev/null |
+++ b/content/browser/notifications/notification_database.cc |
@@ -0,0 +1,88 @@ |
+// 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; |
cmumford
2015/03/11 17:13:24
You could call status.IsNotFound() and return STAT
Peter Beverloo
2015/03/11 18:01:25
Ah yes, good point, thanks!
I'm ramping up the er
|
+ |
+ 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(); |
Bernhard Bauer
2015/03/11 17:15:22
You don't need to explicitly reset the database he
Peter Beverloo
2015/03/11 18:01:26
I added this to clarify that db_ needs to be reset
Bernhard Bauer
2015/03/11 18:20:01
I'm all for being explicit about it, but I'd rathe
Peter Beverloo
2015/03/11 19:32:25
Ok. Done.
|
+} |
+ |
+NotificationDatabase::Status NotificationDatabase::Open( |
+ bool create_if_missing) { |
+ DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
+ DCHECK_EQ(state_, STATE_UNINITIALIZED); |
Bernhard Bauer
2015/03/11 17:15:22
Put the expected value first for nicer error messa
Peter Beverloo
2015/03/11 18:01:25
Done.
|
+ |
+ 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; |
cmumford
2015/03/11 17:13:24
I recommend paranoid_checks as they are cheap and
Peter Beverloo
2015/03/11 18:01:26
Done, thanks. It won't be a high-frequency databas
|
+ if (IsInMemoryDatabase()) { |
+ env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); |
+ options.env = env_.get(); |
+ } |
+ |
+ leveldb::DB* db; |
Bernhard Bauer
2015/03/11 17:15:22
Initialize with nullptr?
Peter Beverloo
2015/03/11 18:01:26
Done.
|
+ Status status = LevelDBStatusToStatus( |
+ leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); |
cmumford
2015/03/11 17:13:24
Will UMA stats arrive in a future CL?
Peter Beverloo
2015/03/11 18:01:25
Yes. I've clarified this in the header and added i
|
+ if (status != STATUS_OK) |
+ return status; |
cmumford
2015/03/11 17:13:24
FYI: leveldb supports the concept of a database re
Peter Beverloo
2015/03/11 18:01:26
That's a really good question actually - are we OK
cmumford
2015/03/11 18:24:33
If leveldb::DB::Open() returns an status.IsCorrupt
Peter Beverloo
2015/03/11 19:32:25
Right, thanks for the explanation!
This is logic
|
+ |
+ 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( |
cmumford
2015/03/11 17:13:24
If you destroy an in-memory db then won't you stil
Peter Beverloo
2015/03/11 18:01:25
The |path_| is a reference, so AsUTF8Unsafe() will
|
+ leveldb::DestroyDB(path_.AsUTF8Unsafe(), options)); |
cmumford
2015/03/11 17:13:24
FYI: DestroyDB does leave behind non leveldb files
Peter Beverloo
2015/03/11 18:01:25
How does that happen? Reading the code, it looks l
cmumford
2015/03/11 18:24:33
ChromiumEnv::DeleteDir calls base::DeleteFile(...,
Peter Beverloo
2015/03/11 19:32:25
I see. I think this is OK, since non-database file
|
+} |
+ |
+} // namespace content |