| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
| 6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/sequence_checker.h" | 12 #include "base/sequence_checker.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 | 14 |
| 15 class GURL; |
| 16 |
| 15 namespace leveldb { | 17 namespace leveldb { |
| 16 class DB; | 18 class DB; |
| 17 class Env; | 19 class Env; |
| 18 class WriteBatch; | 20 class WriteBatch; |
| 19 } | 21 } |
| 20 | 22 |
| 21 namespace content { | 23 namespace content { |
| 22 | 24 |
| 25 struct NotificationDatabaseData; |
| 26 |
| 23 // Implementation of the persistent notification database. | 27 // Implementation of the persistent notification database. |
| 24 // | 28 // |
| 25 // This class can be constructed on any thread, but method calls must only be | 29 // This class can be constructed on any thread, but method calls must only be |
| 26 // made on a thread or sequenced task runner that allows file I/O. The same | 30 // made on a thread or sequenced task runner that allows file I/O. The same |
| 27 // thread or task runner must be used for all method calls. | 31 // thread or task runner must be used for all method calls. |
| 28 class CONTENT_EXPORT NotificationDatabase { | 32 class CONTENT_EXPORT NotificationDatabase { |
| 29 public: | 33 public: |
| 30 // Result status codes for interations with the database. Will be used for | 34 // Result status codes for interations with the database. Will be used for |
| 31 // UMA, so the assigned ids must remain stable. | 35 // UMA, so the assigned ids must remain stable. |
| 32 enum Status { | 36 enum Status { |
| 33 STATUS_OK = 0, | 37 STATUS_OK = 0, |
| 34 | 38 |
| 35 // The database, or the key associated with the operation, could not be | 39 // The database, a notification, or a LevelDB key associated with the |
| 36 // found. | 40 // operation could not be found. |
| 37 STATUS_ERROR_NOT_FOUND = 1, | 41 STATUS_ERROR_NOT_FOUND = 1, |
| 38 | 42 |
| 39 // The database, or data in the database, could not be parsed as valid data. | 43 // The database, or data in the database, could not be parsed as valid data. |
| 40 STATUS_ERROR_CORRUPTED = 2, | 44 STATUS_ERROR_CORRUPTED = 2, |
| 41 | 45 |
| 42 // General failure code. More specific failures should be used if available. | 46 // General failure code. More specific failures should be used if available. |
| 43 STATUS_ERROR_FAILED = 3, | 47 STATUS_ERROR_FAILED = 3, |
| 44 }; | 48 }; |
| 45 | 49 |
| 46 explicit NotificationDatabase(const base::FilePath& path); | 50 explicit NotificationDatabase(const base::FilePath& path); |
| 47 ~NotificationDatabase(); | 51 ~NotificationDatabase(); |
| 48 | 52 |
| 49 // Opens the database. If |path| is non-empty, it will be created on the given | 53 // Opens the database. If |path| is non-empty, it will be created on the given |
| 50 // directory on the filesystem. If |path| is empty, the database will be | 54 // directory on the filesystem. If |path| is empty, the database will be |
| 51 // created in memory instead, and its lifetime will be tied to this instance. | 55 // created in memory instead, and its lifetime will be tied to this instance. |
| 52 // |create_if_missing| determines whether to create the database if necessary. | 56 // |create_if_missing| determines whether to create the database if necessary. |
| 53 Status Open(bool create_if_missing); | 57 Status Open(bool create_if_missing); |
| 54 | 58 |
| 55 // Returns whether the next available notification id could be read, and | 59 // Returns whether the next available notification id could be read, and |
| 56 // stores the id in |notification_id| if the read was successful. | 60 // stores the id in |notification_id| if the read was successful. |
| 57 Status GetNextNotificationId(int64_t* notification_id) const; | 61 Status GetNextNotificationId(int64_t* notification_id) const; |
| 58 | 62 |
| 63 // Reads the notification data for the notification identified by |
| 64 // |notification_id| and belonging to |origin| from the database, and stores |
| 65 // it in |notification_database_data|. Returns the status code. |
| 66 Status ReadNotificationData( |
| 67 int64_t notification_id, |
| 68 const GURL& origin, |
| 69 NotificationDatabaseData* notification_database_data) const; |
| 70 |
| 71 // Writes the |notification_database_data| for the notification identified by |
| 72 // |notification_id| and belonging to |origin| to the database, and returns |
| 73 // the status code of the writing operation. |
| 74 Status WriteNotificationData( |
| 75 int64_t notification_id, |
| 76 const GURL& origin, |
| 77 const NotificationDatabaseData& notification_database_data); |
| 78 |
| 79 // Deletes all data associated with the notification identified by |
| 80 // |notification_id| belonging to |origin| from the database. Returns the |
| 81 // status code of the deletion operation. |
| 82 Status DeleteNotificationData(int64_t notification_id, const GURL& origin); |
| 83 |
| 59 // Completely destroys the contents of this database. | 84 // Completely destroys the contents of this database. |
| 60 Status Destroy(); | 85 Status Destroy(); |
| 61 | 86 |
| 62 private: | 87 private: |
| 63 friend class NotificationDatabaseTest; | 88 friend class NotificationDatabaseTest; |
| 64 | 89 |
| 65 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | 90 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. |
| 66 // See https://crbug.com/463869. | 91 // See https://crbug.com/463869. |
| 67 enum State { | 92 enum State { |
| 68 STATE_UNINITIALIZED, | 93 STATE_UNINITIALIZED, |
| 69 STATE_INITIALIZED, | 94 STATE_INITIALIZED, |
| 70 STATE_DISABLED, | 95 STATE_DISABLED, |
| 71 }; | 96 }; |
| 72 | 97 |
| 73 // Writes the next available notification id as a put operation to |batch|. | |
| 74 void WriteNextNotificationId(leveldb::WriteBatch* batch, | |
| 75 int64_t next_notification_id) const; | |
| 76 | |
| 77 // Returns whether the database has been opened. | 98 // Returns whether the database has been opened. |
| 78 bool IsOpen() const { return db_ != nullptr; } | 99 bool IsOpen() const { return db_ != nullptr; } |
| 79 | 100 |
| 80 // Returns whether the database should only exist in memory. | 101 // Returns whether the database should only exist in memory. |
| 81 bool IsInMemoryDatabase() const { return path_.empty(); } | 102 bool IsInMemoryDatabase() const { return path_.empty(); } |
| 82 | 103 |
| 83 // Exposes the LevelDB database used to back this notification database. | 104 // Exposes the LevelDB database used to back this notification database. |
| 84 // Should only be used for testing purposes. | 105 // Should only be used for testing purposes. |
| 85 leveldb::DB* GetDBForTesting() const { return db_.get(); } | 106 leveldb::DB* GetDBForTesting() const { return db_.get(); } |
| 86 | 107 |
| 87 base::FilePath path_; | 108 base::FilePath path_; |
| 88 | 109 |
| 89 // The declaration order for these members matters, as |db_| depends on |env_| | 110 // The declaration order for these members matters, as |db_| depends on |env_| |
| 90 // and thus has to be destructed first. | 111 // and thus has to be destructed first. |
| 91 scoped_ptr<leveldb::Env> env_; | 112 scoped_ptr<leveldb::Env> env_; |
| 92 scoped_ptr<leveldb::DB> db_; | 113 scoped_ptr<leveldb::DB> db_; |
| 93 | 114 |
| 94 State state_; | 115 State state_; |
| 95 | 116 |
| 96 base::SequenceChecker sequence_checker_; | 117 base::SequenceChecker sequence_checker_; |
| 97 | 118 |
| 98 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 119 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
| 99 }; | 120 }; |
| 100 | 121 |
| 101 } // namespace content | 122 } // namespace content |
| 102 | 123 |
| 103 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 124 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
| OLD | NEW |