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 <memory> | 10 #include <memory> |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 | 71 |
72 explicit NotificationDatabase(const base::FilePath& path); | 72 explicit NotificationDatabase(const base::FilePath& path); |
73 ~NotificationDatabase(); | 73 ~NotificationDatabase(); |
74 | 74 |
75 // Opens the database. If |path| is non-empty, it will be created on the given | 75 // Opens the database. If |path| is non-empty, it will be created on the given |
76 // directory on the filesystem. If |path| is empty, the database will be | 76 // directory on the filesystem. If |path| is empty, the database will be |
77 // created in memory instead, and its lifetime will be tied to this instance. | 77 // created in memory instead, and its lifetime will be tied to this instance. |
78 // |create_if_missing| determines whether to create the database if necessary. | 78 // |create_if_missing| determines whether to create the database if necessary. |
79 Status Open(bool create_if_missing); | 79 Status Open(bool create_if_missing); |
80 | 80 |
81 // Gets the next assignable notification ID. Subsequent calls to this method | |
johnme
2016/09/07 17:13:14
persistent notification ID
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
82 // will yield unique identifiers for the same database. The last used ID will | |
83 // be written to the database when a notification is created. | |
84 int64_t GetNextNotificationId(); | |
johnme
2016/09/07 17:13:14
GetNextPersistentNotificationId
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
85 | |
81 // Reads the notification data for the notification identified by | 86 // Reads the notification data for the notification identified by |
82 // |notification_id| and belonging to |origin| from the database, and stores | 87 // |notification_id| and belonging to |origin| from the database, and stores |
83 // it in |notification_database_data|. Returns the status code. | 88 // it in |*notification_database_data|. Returns the status code. |
84 Status ReadNotificationData( | 89 Status ReadNotificationData( |
85 int64_t notification_id, | 90 const std::string& notification_id, |
86 const GURL& origin, | 91 const GURL& origin, |
87 NotificationDatabaseData* notification_database_data) const; | 92 NotificationDatabaseData* notification_database_data) const; |
johnme
2016/09/07 17:13:14
Nit: notification_data since you renamed it in Wri
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
88 | 93 |
89 // Reads all notification data for all origins from the database, and appends | 94 // Reads all notification data for all origins from the database, and appends |
90 // the data to |notification_data_vector|. Returns the status code. | 95 // the data to |notification_data_vector|. Returns the status code. |
91 Status ReadAllNotificationData( | 96 Status ReadAllNotificationData( |
92 std::vector<NotificationDatabaseData>* notification_data_vector) const; | 97 std::vector<NotificationDatabaseData>* notification_data_vector) const; |
93 | 98 |
94 // Reads all notification data associated with |origin| from the database, and | 99 // Reads all notification data associated with |origin| from the database, and |
95 // appends the data to |notification_data_vector|. Returns the status code. | 100 // appends the data to |notification_data_vector|. Returns the status code. |
96 Status ReadAllNotificationDataForOrigin( | 101 Status ReadAllNotificationDataForOrigin( |
97 const GURL& origin, | 102 const GURL& origin, |
98 std::vector<NotificationDatabaseData>* notification_data_vector) const; | 103 std::vector<NotificationDatabaseData>* notification_data_vector) const; |
99 | 104 |
100 // Reads all notification data associated to |service_worker_registration_id| | 105 // Reads all notification data associated to |service_worker_registration_id| |
101 // belonging to |origin| from the database, and appends the data to the | 106 // belonging to |origin| from the database, and appends the data to the |
102 // |notification_data_vector|. Returns the status code. | 107 // |notification_data_vector|. Returns the status code. |
103 Status ReadAllNotificationDataForServiceWorkerRegistration( | 108 Status ReadAllNotificationDataForServiceWorkerRegistration( |
104 const GURL& origin, | 109 const GURL& origin, |
105 int64_t service_worker_registration_id, | 110 int64_t service_worker_registration_id, |
106 std::vector<NotificationDatabaseData>* notification_data_vector) const; | 111 std::vector<NotificationDatabaseData>* notification_data_vector) const; |
107 | 112 |
108 // Writes the |notification_database_data| for a new notification belonging to | 113 // Writes the |notification_data| for a new notification belonging to |origin| |
109 // |origin| to the database, and returns the status code of the writing | 114 // to the database, and returns the status code of the writing operation. The |
110 // operation. The id of the new notification will be set in |notification_id|. | 115 // notification's ID must have been set in the |notification_data|. |
111 Status WriteNotificationData( | 116 Status WriteNotificationData( |
112 const GURL& origin, | 117 const GURL& origin, |
113 const NotificationDatabaseData& notification_database_data, | 118 const NotificationDatabaseData& notification_data); |
114 int64_t* notification_id); | |
115 | 119 |
116 // Deletes all data associated with the notification identified by | 120 // Deletes all data associated with the notification identified by |
117 // |notification_id| belonging to |origin| from the database. Returns the | 121 // |notification_id| belonging to |origin| from the database. Returns the |
118 // status code of the deletion operation. Note that it is not considered a | 122 // status code of the deletion operation. Note that it is not considered a |
119 // failure if the to-be-deleted notification does not exist. | 123 // failure if the to-be-deleted notification does not exist. |
120 Status DeleteNotificationData(int64_t notification_id, const GURL& origin); | 124 Status DeleteNotificationData(const std::string& notification_id, |
125 const GURL& origin); | |
121 | 126 |
122 // Deletes all data associated with |origin| from the database, optionally | 127 // Deletes all data associated with |origin| from the database, optionally |
123 // filtered by the |tag|, and appends the deleted notification ids to | 128 // filtered by the |tag|, and appends the deleted notification ids to |
124 // |deleted_notification_set|. Returns the status code of the deletion | 129 // |deleted_notification_ids|. Returns the status code of the deletion |
125 // operation. | 130 // operation. |
126 Status DeleteAllNotificationDataForOrigin( | 131 Status DeleteAllNotificationDataForOrigin( |
127 const GURL& origin, | 132 const GURL& origin, |
128 const std::string& tag, | 133 const std::string& tag, |
129 std::set<int64_t>* deleted_notification_set); | 134 std::set<std::string>* deleted_notification_ids); |
130 | 135 |
131 // Deletes all data associated with the |service_worker_registration_id| | 136 // Deletes all data associated with the |service_worker_registration_id| |
132 // belonging to |origin| from the database, and appends the deleted | 137 // belonging to |origin| from the database, and appends the deleted |
133 // notification ids to |deleted_notification_set|. Returns the status code | 138 // notification ids to |deleted_notification_set|. Returns the status code |
134 // of the deletion operation. | 139 // of the deletion operation. |
135 Status DeleteAllNotificationDataForServiceWorkerRegistration( | 140 Status DeleteAllNotificationDataForServiceWorkerRegistration( |
136 const GURL& origin, | 141 const GURL& origin, |
137 int64_t service_worker_registration_id, | 142 int64_t service_worker_registration_id, |
138 std::set<int64_t>* deleted_notification_set); | 143 std::set<std::string>* deleted_notification_ids); |
139 | 144 |
140 // Completely destroys the contents of this database. | 145 // Completely destroys the contents of this database. |
141 Status Destroy(); | 146 Status Destroy(); |
142 | 147 |
143 private: | 148 private: |
144 friend class NotificationDatabaseTest; | 149 friend class NotificationDatabaseTest; |
145 | 150 |
146 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | 151 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. |
147 // See https://crbug.com/463869. | 152 // See https://crbug.com/463869. |
148 enum State { | 153 enum State { |
149 STATE_UNINITIALIZED, | 154 STATE_UNINITIALIZED, |
150 STATE_INITIALIZED, | 155 STATE_INITIALIZED, |
151 STATE_DISABLED, | 156 STATE_DISABLED, |
152 }; | 157 }; |
153 | 158 |
154 // Reads the next available notification id from the database and returns | 159 // Reads the next available notification id from the database and returns |
johnme
2016/09/07 17:13:14
persistent notification id
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
155 // the status code of the reading operation. The value will be stored in | 160 // the status code of the reading operation. The value will be stored in |
156 // the |next_notification_id_| member. | 161 // the |next_notification_id_| member. |
johnme
2016/09/07 17:13:14
next_persistent_notification_id
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
157 Status ReadNextNotificationId(); | 162 Status ReadNextNotificationId(); |
johnme
2016/09/07 17:13:14
ReadNextPersistentNotificationId
Peter Beverloo
2016/09/08 13:18:56
Done.
| |
158 | 163 |
159 // Reads all notification data with the given constraints. |origin| may be | 164 // Reads all notification data with the given constraints. |origin| may be |
160 // empty to read all notification data from all origins. If |origin| is | 165 // empty to read all notification data from all origins. If |origin| is |
161 // set, but |service_worker_registration_id| is invalid, then all notification | 166 // set, but |service_worker_registration_id| is invalid, then all notification |
162 // data for |origin| will be read. If both are set, then all notification data | 167 // data for |origin| will be read. If both are set, then all notification data |
163 // for the given |service_worker_registration_id| will be read. | 168 // for the given |service_worker_registration_id| will be read. |
164 Status ReadAllNotificationDataInternal( | 169 Status ReadAllNotificationDataInternal( |
165 const GURL& origin, | 170 const GURL& origin, |
166 int64_t service_worker_registration_id, | 171 int64_t service_worker_registration_id, |
167 std::vector<NotificationDatabaseData>* notification_data_vector) const; | 172 std::vector<NotificationDatabaseData>* notification_data_vector) const; |
168 | 173 |
169 // Deletes all notification data with the given constraints. |origin| must | 174 // Deletes all notification data with the given constraints. |origin| must |
170 // always be set - use Destroy() when the goal is to empty the database. If | 175 // always be set - use Destroy() when the goal is to empty the database. If |
171 // |service_worker_registration_id| is invalid, all notification data for the | 176 // |service_worker_registration_id| is invalid, all notification data for the |
172 // |origin| will be deleted, optionally filtered by the |tag| when non-empty. | 177 // |origin| will be deleted, optionally filtered by the |tag| when non-empty. |
173 // All deleted notification ids will be written to |deleted_notification_set|. | 178 // All deleted notification ids will be written to |deleted_notification_ids|. |
174 Status DeleteAllNotificationDataInternal( | 179 Status DeleteAllNotificationDataInternal( |
175 const GURL& origin, | 180 const GURL& origin, |
176 const std::string& tag, | 181 const std::string& tag, |
177 int64_t service_worker_registration_id, | 182 int64_t service_worker_registration_id, |
178 std::set<int64_t>* deleted_notification_set); | 183 std::set<std::string>* deleted_notification_ids); |
179 | 184 |
180 // Returns whether the database has been opened. | 185 // Returns whether the database has been opened. |
181 bool IsOpen() const { return db_ != nullptr; } | 186 bool IsOpen() const { return db_ != nullptr; } |
182 | 187 |
183 // Returns whether the database should only exist in memory. | 188 // Returns whether the database should only exist in memory. |
184 bool IsInMemoryDatabase() const { return path_.empty(); } | 189 bool IsInMemoryDatabase() const { return path_.empty(); } |
185 | 190 |
186 // Exposes the LevelDB database used to back this notification database. | 191 // Exposes the LevelDB database used to back this notification database. |
187 // Should only be used for testing purposes. | 192 // Should only be used for testing purposes. |
188 leveldb::DB* GetDBForTesting() const { return db_.get(); } | 193 leveldb::DB* GetDBForTesting() const { return db_.get(); } |
189 | 194 |
190 base::FilePath path_; | 195 base::FilePath path_; |
191 | 196 |
192 int64_t next_notification_id_ = 0; | 197 int64_t next_persistent_notification_id_ = 0; |
198 int64_t written_persistent_notification_id_ = 0; | |
193 | 199 |
194 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; | 200 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; |
195 | 201 |
196 // The declaration order for these members matters, as |db_| depends on |env_| | 202 // The declaration order for these members matters, as |db_| depends on |env_| |
197 // and thus has to be destructed first. | 203 // and thus has to be destructed first. |
198 std::unique_ptr<leveldb::Env> env_; | 204 std::unique_ptr<leveldb::Env> env_; |
199 std::unique_ptr<leveldb::DB> db_; | 205 std::unique_ptr<leveldb::DB> db_; |
200 | 206 |
201 State state_ = STATE_UNINITIALIZED; | 207 State state_ = STATE_UNINITIALIZED; |
202 | 208 |
203 base::SequenceChecker sequence_checker_; | 209 base::SequenceChecker sequence_checker_; |
204 | 210 |
205 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 211 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
206 }; | 212 }; |
207 | 213 |
208 } // namespace content | 214 } // namespace content |
209 | 215 |
210 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 216 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
OLD | NEW |