Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | |
| 6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/sequence_checker.h" | |
| 11 #include "content/common/content_export.h" | |
| 12 | |
| 13 namespace leveldb { | |
| 14 class DB; | |
| 15 class Env; | |
| 16 } | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 // Implementation of the persistent notification database. | |
| 21 // | |
| 22 // This class can be constructed on any thread, but method calls must only be | |
| 23 // made on a thread or sequenced task runner that allows file I/O. The same | |
| 24 // thread or task runner must be used for all method calls. | |
| 25 class CONTENT_EXPORT NotificationDatabase { | |
| 26 public: | |
| 27 // Result status codes for interations with the database. Will be used for | |
| 28 // UMA, so the assigned ids must remain stable. | |
| 29 enum Status { | |
| 30 STATUS_OK = 0, | |
| 31 | |
| 32 // The database, or the key associated with the operation, could not be | |
| 33 // found. | |
| 34 STATUS_ERROR_NOT_FOUND = 1, | |
| 35 | |
| 36 // The database, or data in the database, could not be parsed as valid data. | |
| 37 STATUS_ERROR_CORRUPTED = 2, | |
| 38 | |
| 39 // General failure code. More specific failures should be used if available. | |
| 40 STATUS_ERROR_FAILED = 3, | |
| 41 }; | |
| 42 | |
| 43 explicit NotificationDatabase(const base::FilePath& path); | |
| 44 ~NotificationDatabase(); | |
| 45 | |
| 46 // Opens the database. If |path| is non-empty, it will be created on the given | |
| 47 // directory on the filesystem. If |path| is empty, the database will be | |
| 48 // created in memory instead, and its lifetime will be tied to this instance. | |
| 49 // |create_if_missing| determines whether to create the database if necessary. | |
| 50 Status Open(bool create_if_missing); | |
| 51 | |
| 52 // Completely destroys the contents of this database. | |
| 53 Status Destroy(); | |
| 54 | |
| 55 private: | |
| 56 friend class NotificationDatabaseTest; | |
| 57 | |
| 58 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | |
| 59 // See https://crbug.com/463869. | |
| 60 enum State { | |
| 61 STATE_UNINITIALIZED, | |
| 62 STATE_INITIALIZED, | |
| 63 STATE_DISABLED, | |
| 64 }; | |
| 65 | |
| 66 // Returns whether the database has been opened. | |
| 67 bool IsOpen() const { return db_ != nullptr; } | |
| 68 | |
| 69 // Returns whether the database should only exist in memory. | |
| 70 bool IsInMemoryDatabase() const { return path_.empty(); } | |
| 71 | |
| 72 base::FilePath path_; | |
| 73 | |
| 74 // These members depend on the declaration order for destruction. | |
|
Bernhard Bauer
2015/03/11 21:35:11
I would actually be explicit about it: |db_| depen
Peter Beverloo
2015/03/11 22:11:53
Done.
| |
| 75 scoped_ptr<leveldb::Env> env_; | |
| 76 scoped_ptr<leveldb::DB> db_; | |
| 77 | |
| 78 State state_; | |
| 79 | |
| 80 base::SequenceChecker sequence_checker_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | |
| 83 }; | |
| 84 | |
| 85 } // namespace content | |
| 86 | |
| 87 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | |
| OLD | NEW |