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 // Reads the notification data for the notification identified by |
56 // stores the id in |notification_id| if the read was successful. | 60 // |notification_id| and belonging to |origin| from the database, and stores |
57 Status GetNextNotificationId(int64_t* notification_id) const; | 61 // it in |notification_database_data|. Returns the status code. |
| 62 Status ReadNotificationData( |
| 63 int64_t notification_id, |
| 64 const GURL& origin, |
| 65 NotificationDatabaseData* notification_database_data) const; |
| 66 |
| 67 // Writes the |notification_database_data| for a new notification belonging to |
| 68 // |origin| to the database, and returns the status code of the writing |
| 69 // operation. The id of the new notification will be set in |notification_id|. |
| 70 Status WriteNotificationData( |
| 71 const GURL& origin, |
| 72 const NotificationDatabaseData& notification_database_data, |
| 73 int64_t* notification_id); |
| 74 |
| 75 // Deletes all data associated with the notification identified by |
| 76 // |notification_id| belonging to |origin| from the database. Returns the |
| 77 // status code of the deletion operation. |
| 78 Status DeleteNotificationData(int64_t notification_id, const GURL& origin); |
58 | 79 |
59 // Completely destroys the contents of this database. | 80 // Completely destroys the contents of this database. |
60 Status Destroy(); | 81 Status Destroy(); |
61 | 82 |
62 private: | 83 private: |
63 friend class NotificationDatabaseTest; | 84 friend class NotificationDatabaseTest; |
64 | 85 |
65 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | 86 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. |
66 // See https://crbug.com/463869. | 87 // See https://crbug.com/463869. |
67 enum State { | 88 enum State { |
68 STATE_UNINITIALIZED, | 89 STATE_UNINITIALIZED, |
69 STATE_INITIALIZED, | 90 STATE_INITIALIZED, |
70 STATE_DISABLED, | 91 STATE_DISABLED, |
71 }; | 92 }; |
72 | 93 |
73 // Writes the next available notification id as a put operation to |batch|. | 94 // Returns whether the next available notification id could be read, and |
74 void WriteNextNotificationId(leveldb::WriteBatch* batch, | 95 // stores the id in |notification_id| if the read was successful. |
75 int64_t next_notification_id) const; | 96 Status GetNextNotificationId(int64_t* notification_id) const; |
76 | 97 |
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 |
| 110 int64_t next_notification_id_; |
| 111 |
89 // The declaration order for these members matters, as |db_| depends on |env_| | 112 // The declaration order for these members matters, as |db_| depends on |env_| |
90 // and thus has to be destructed first. | 113 // and thus has to be destructed first. |
91 scoped_ptr<leveldb::Env> env_; | 114 scoped_ptr<leveldb::Env> env_; |
92 scoped_ptr<leveldb::DB> db_; | 115 scoped_ptr<leveldb::DB> db_; |
93 | 116 |
94 State state_; | 117 State state_; |
95 | 118 |
96 base::SequenceChecker sequence_checker_; | 119 base::SequenceChecker sequence_checker_; |
97 | 120 |
98 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 121 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
99 }; | 122 }; |
100 | 123 |
101 } // namespace content | 124 } // namespace content |
102 | 125 |
103 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 126 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
OLD | NEW |