Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 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 | |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 8 | #include <stdint.h> |
Daniel Cheng | 556abd1 | 2016-04-02 01:13:08 | [diff] [blame] | 9 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 10 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 11 | #include <optional> |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 12 | #include <set> |
| 13 | #include <vector> |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 14 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 16 | #include "base/sequence_checker.h" |
| 17 | #include "content/common/content_export.h" |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 18 | #include "content/public/browser/platform_notification_context.h" |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 19 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 20 | class GURL; |
| 21 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 22 | namespace blink { |
| 23 | struct NotificationResources; |
| 24 | } // namespace blink |
| 25 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 26 | namespace leveldb { |
| 27 | class DB; |
| 28 | class Env; |
peter | c1978f94 | 2015-04-01 17:38:18 | [diff] [blame] | 29 | class FilterPolicy; |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 30 | } // namespace leveldb |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 31 | |
| 32 | namespace content { |
| 33 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 34 | struct NotificationDatabaseData; |
| 35 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 36 | // Implementation of the persistent notification database. |
| 37 | // |
peter | 8f761268 | 2015-03-16 20:11:15 | [diff] [blame] | 38 | // The database is built on top of a LevelDB database, either in memory or on |
| 39 | // the filesystem depending on the path passed to the constructor. When writing |
| 40 | // a new notification, it will be assigned an id guaranteed to be unique for the |
| 41 | // lifetime of the database. |
| 42 | // |
| 43 | // This class must only be used on a thread or sequenced task runner that allows |
| 44 | // file I/O. The same thread or task runner must be used for all method calls. |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 45 | class CONTENT_EXPORT NotificationDatabase { |
| 46 | public: |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 47 | using UkmCallback = |
| 48 | base::RepeatingCallback<void(const NotificationDatabaseData&)>; |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 49 | using ReadAllNotificationsCallback = |
| 50 | base::RepeatingCallback<void(const NotificationDatabaseData&)>; |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 51 | |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 52 | // Result status codes for interactions with the database. Will be used for |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 53 | // UMA, so the assigned ids must remain stable. |
| 54 | enum Status { |
| 55 | STATUS_OK = 0, |
| 56 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 57 | // The database, a notification, or a LevelDB key associated with the |
| 58 | // operation could not be found. |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 59 | STATUS_ERROR_NOT_FOUND = 1, |
| 60 | |
| 61 | // The database, or data in the database, could not be parsed as valid data. |
| 62 | STATUS_ERROR_CORRUPTED = 2, |
| 63 | |
| 64 | // General failure code. More specific failures should be used if available. |
| 65 | STATUS_ERROR_FAILED = 3, |
peter | 9684384a | 2015-04-01 17:23:53 | [diff] [blame] | 66 | |
cmumford | 71054ce | 2015-10-20 17:15:41 | [diff] [blame] | 67 | // leveldb failed due to I/O error (read-only, full disk, etc.). |
| 68 | STATUS_IO_ERROR = 4, |
| 69 | |
| 70 | // leveldb operation not supported |
| 71 | STATUS_NOT_SUPPORTED = 5, |
| 72 | |
harkness | e564632 | 2016-06-22 08:56:22 | [diff] [blame] | 73 | // Invalid database ID or snapshot ID provided. |
| 74 | STATUS_INVALID_ARGUMENT = 6, |
| 75 | |
peter | 9684384a | 2015-04-01 17:23:53 | [diff] [blame] | 76 | // Number of entries in the status enumeration. Used by UMA. Must always be |
| 77 | // one higher than the otherwise highest value in this enumeration. |
harkness | e564632 | 2016-06-22 08:56:22 | [diff] [blame] | 78 | STATUS_COUNT = 7 |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 79 | }; |
| 80 | |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 81 | NotificationDatabase(const base::FilePath& path, UkmCallback callback); |
| 82 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 83 | NotificationDatabase(const NotificationDatabase&) = delete; |
| 84 | NotificationDatabase& operator=(const NotificationDatabase&) = delete; |
| 85 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 86 | ~NotificationDatabase(); |
| 87 | |
| 88 | // Opens the database. If |path| is non-empty, it will be created on the given |
| 89 | // directory on the filesystem. If |path| is empty, the database will be |
| 90 | // created in memory instead, and its lifetime will be tied to this instance. |
| 91 | // |create_if_missing| determines whether to create the database if necessary. |
| 92 | Status Open(bool create_if_missing); |
| 93 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 94 | // Gets the next assignable persistent notification ID. Subsequent calls to |
| 95 | // this method will yield unique identifiers for the same database. The last |
| 96 | // used ID will be written to the database when a notification is created. |
| 97 | int64_t GetNextPersistentNotificationId(); |
| 98 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 99 | // Reads the notification data for the notification identified by |
| 100 | // |notification_id| and belonging to |origin| from the database, and stores |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 101 | // it in |*notification_data|. Returns the status code. |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 102 | Status ReadNotificationData( |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 103 | const std::string& notification_id, |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 104 | const GURL& origin, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 105 | NotificationDatabaseData* notification_data) const; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 106 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 107 | // Reads the notification resources for the notification identified by |
| 108 | // |notification_id| and belonging to |origin| from the database, and stores |
| 109 | // it in |*notification_resources|. Returns the status code. |
| 110 | Status ReadNotificationResources( |
| 111 | const std::string& notification_id, |
| 112 | const GURL& origin, |
| 113 | blink::NotificationResources* notification_resources) const; |
| 114 | |
Sharon Yang | d96cd4b | 2018-05-31 11:43:43 | [diff] [blame] | 115 | // This function is identical to ReadNotificationData above, but also records |
| 116 | // an interaction with that notification in the database for UKM logging |
| 117 | // purposes. |
| 118 | Status ReadNotificationDataAndRecordInteraction( |
| 119 | const std::string& notification_id, |
| 120 | const GURL& origin, |
| 121 | PlatformNotificationContext::Interaction interaction, |
| 122 | NotificationDatabaseData* notification_data); |
| 123 | |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 124 | // Iterates over all notification data for all origins from the database, and |
| 125 | // calls |callback| with each notification data. Returns the status code. |
| 126 | Status ForEachNotificationData(ReadAllNotificationsCallback callback) const; |
| 127 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 128 | // Iterates over all notification data for |service_worker_registration_id| |
| 129 | // belonging to |origin| from the database, and calls |callback| with each |
| 130 | // notification data. Returns the status code. |
| 131 | Status ForEachNotificationDataForServiceWorkerRegistration( |
| 132 | const GURL& origin, |
| 133 | int64_t service_worker_registration_id, |
| 134 | ReadAllNotificationsCallback callback) const; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 135 | |
| 136 | // Reads all notification data associated with |origin| from the database, and |
| 137 | // appends the data to |notification_data_vector|. Returns the status code. |
| 138 | Status ReadAllNotificationDataForOrigin( |
| 139 | const GURL& origin, |
| 140 | std::vector<NotificationDatabaseData>* notification_data_vector) const; |
| 141 | |
| 142 | // Reads all notification data associated to |service_worker_registration_id| |
| 143 | // belonging to |origin| from the database, and appends the data to the |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 144 | // |notification_data_vector|. Optionally filtered by |is_shown_by_browser|. |
| 145 | // Returns the status code. |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 146 | Status ReadAllNotificationDataForServiceWorkerRegistration( |
| 147 | const GURL& origin, |
| 148 | int64_t service_worker_registration_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 149 | std::optional<bool> is_shown_by_browser, |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 150 | std::vector<NotificationDatabaseData>* notification_data_vector) const; |
| 151 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 152 | // Writes the |notification_data| for a new notification belonging to |origin| |
| 153 | // to the database, and returns the status code of the writing operation. The |
| 154 | // notification's ID must have been set in the |notification_data|. |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 155 | Status WriteNotificationData( |
| 156 | const GURL& origin, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 157 | const NotificationDatabaseData& notification_data); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 158 | |
| 159 | // Deletes all data associated with the notification identified by |
| 160 | // |notification_id| belonging to |origin| from the database. Returns the |
| 161 | // status code of the deletion operation. Note that it is not considered a |
| 162 | // failure if the to-be-deleted notification does not exist. |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 163 | Status DeleteNotificationData(const std::string& notification_id, |
| 164 | const GURL& origin); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 165 | |
Richard Knoll | acd400ea | 2019-05-31 11:53:07 | [diff] [blame] | 166 | // Deletes resources associated with the notification identified by |
| 167 | // |notification_id| belonging to |origin| from the database. Returns the |
| 168 | // status code of the deletion operation. Note that it is not considered a |
| 169 | // failure if the to-be-deleted resources do not exist. |
| 170 | Status DeleteNotificationResources(const std::string& notification_id, |
| 171 | const GURL& origin); |
| 172 | |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 173 | // Deletes all data associated with |origin| from the database, optionally |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 174 | // filtered by |tag| and |is_shown_by_browser|, and appends the deleted |
| 175 | // notification ids to |deleted_notification_ids|. Returns the status code of |
| 176 | // the deletion operation. |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 177 | Status DeleteAllNotificationDataForOrigin( |
| 178 | const GURL& origin, |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 179 | const std::string& tag, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 180 | std::optional<bool> is_shown_by_browser, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 181 | std::set<std::string>* deleted_notification_ids); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 182 | |
| 183 | // Deletes all data associated with the |service_worker_registration_id| |
| 184 | // belonging to |origin| from the database, and appends the deleted |
| 185 | // notification ids to |deleted_notification_set|. Returns the status code |
| 186 | // of the deletion operation. |
| 187 | Status DeleteAllNotificationDataForServiceWorkerRegistration( |
| 188 | const GURL& origin, |
| 189 | int64_t service_worker_registration_id, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 190 | std::set<std::string>* deleted_notification_ids); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 191 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 192 | // Completely destroys the contents of this database. |
| 193 | Status Destroy(); |
| 194 | |
| 195 | private: |
| 196 | friend class NotificationDatabaseTest; |
| 197 | |
Sharon Yang | e2decea | 2018-06-28 11:19:02 | [diff] [blame] | 198 | enum class State { |
| 199 | UNINITIALIZED, |
| 200 | INITIALIZED, |
| 201 | DISABLED, |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 202 | }; |
| 203 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 204 | // Reads the next available persistent notification id from the database and |
| 205 | // returns the status code of the reading operation. The value will be stored |
| 206 | // in the |next_persistent_notification_id_| member. |
| 207 | Status ReadNextPersistentNotificationId(); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 208 | |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 209 | // Iterates over all notifications and pushes matching ones onto |
| 210 | // |notification_data_vector|. See ForEachNotificationDataInternal for deails. |
| 211 | Status ReadAllNotificationDataInternal( |
| 212 | const GURL& origin, |
| 213 | int64_t service_worker_registration_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 214 | std::optional<bool> is_shown_by_browser, |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 215 | std::vector<NotificationDatabaseData>* notification_data_vector) const; |
| 216 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 217 | // Reads all notification data with the given constraints. |origin| may be |
| 218 | // empty to read all notification data from all origins. If |origin| is |
| 219 | // set, but |service_worker_registration_id| is invalid, then all notification |
| 220 | // data for |origin| will be read. If both are set, then all notification data |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 221 | // for the given |service_worker_registration_id| will be read. If |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 222 | // |is_shown_by_browser| is not std::nullopt, only notification data with |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 223 | // matching |is_shown_by_browser| flags will be read. |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 224 | Status ForEachNotificationDataInternal( |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 225 | const GURL& origin, |
| 226 | int64_t service_worker_registration_id, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 227 | std::optional<bool> is_shown_by_browser, |
Richard Knoll | b07f26b | 2019-03-07 09:55:33 | [diff] [blame] | 228 | ReadAllNotificationsCallback callback) const; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 229 | |
| 230 | // Deletes all notification data with the given constraints. |origin| must |
| 231 | // always be set - use Destroy() when the goal is to empty the database. If |
| 232 | // |service_worker_registration_id| is invalid, all notification data for the |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 233 | // |origin| will be deleted, optionally filtered by |tag| and |
| 234 | // |is_shown_by_browser| when non-empty. All deleted notification ids will be |
| 235 | // written to |deleted_notification_ids|. |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 236 | Status DeleteAllNotificationDataInternal( |
| 237 | const GURL& origin, |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 238 | const std::string& tag, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 239 | std::optional<bool> is_shown_by_browser, |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 240 | int64_t service_worker_registration_id, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 241 | std::set<std::string>* deleted_notification_ids); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 242 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 243 | // Returns whether the database has been opened. |
| 244 | bool IsOpen() const { return db_ != nullptr; } |
| 245 | |
| 246 | // Returns whether the database should only exist in memory. |
| 247 | bool IsInMemoryDatabase() const { return path_.empty(); } |
| 248 | |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 249 | // Exposes the LevelDB database used to back this notification database. |
| 250 | // Should only be used for testing purposes. |
| 251 | leveldb::DB* GetDBForTesting() const { return db_.get(); } |
| 252 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 253 | base::FilePath path_; |
| 254 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 255 | std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; |
peter | c1978f94 | 2015-04-01 17:38:18 | [diff] [blame] | 256 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 257 | // The declaration order for these members matters, as |db_| depends on |env_| |
| 258 | // and thus has to be destructed first. |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 259 | std::unique_ptr<leveldb::Env> env_; |
| 260 | std::unique_ptr<leveldb::DB> db_; |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 261 | |
Sharon Yang | e2decea | 2018-06-28 11:19:02 | [diff] [blame] | 262 | State state_ = State::UNINITIALIZED; |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 263 | |
Christian Flach | a80f17b | 2022-09-12 19:05:59 | [diff] [blame] | 264 | SEQUENCE_CHECKER(sequence_checker_); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 265 | |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 266 | // Callback to use for recording UKM metrics. Must be posted to the UI thread. |
| 267 | UkmCallback record_notification_to_ukm_callback_; |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | } // namespace content |
| 271 | |
| 272 | #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |