[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / notifications / notification_database.h
blob1404e63501a0317464eff7c66a1879520c9b858d
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.
5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_
6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_
8 #include <stdint.h>
9 #include <set>
10 #include <vector>
12 #include "base/files/file_path.h"
13 #include "base/sequence_checker.h"
14 #include "content/common/content_export.h"
16 class GURL;
18 namespace leveldb {
19 class DB;
20 class Env;
21 class FilterPolicy;
22 class WriteBatch;
25 namespace content {
27 struct NotificationDatabaseData;
29 // Implementation of the persistent notification database.
31 // The database is built on top of a LevelDB database, either in memory or on
32 // the filesystem depending on the path passed to the constructor. When writing
33 // a new notification, it will be assigned an id guaranteed to be unique for the
34 // lifetime of the database.
36 // This class must only be used on a thread or sequenced task runner that allows
37 // file I/O. The same thread or task runner must be used for all method calls.
38 class CONTENT_EXPORT NotificationDatabase {
39 public:
40 // Result status codes for interations with the database. Will be used for
41 // UMA, so the assigned ids must remain stable.
42 enum Status {
43 STATUS_OK = 0,
45 // The database, a notification, or a LevelDB key associated with the
46 // operation could not be found.
47 STATUS_ERROR_NOT_FOUND = 1,
49 // The database, or data in the database, could not be parsed as valid data.
50 STATUS_ERROR_CORRUPTED = 2,
52 // General failure code. More specific failures should be used if available.
53 STATUS_ERROR_FAILED = 3,
55 // Number of entries in the status enumeration. Used by UMA. Must always be
56 // one higher than the otherwise highest value in this enumeration.
57 STATUS_COUNT = 4
60 explicit NotificationDatabase(const base::FilePath& path);
61 ~NotificationDatabase();
63 // Opens the database. If |path| is non-empty, it will be created on the given
64 // directory on the filesystem. If |path| is empty, the database will be
65 // created in memory instead, and its lifetime will be tied to this instance.
66 // |create_if_missing| determines whether to create the database if necessary.
67 Status Open(bool create_if_missing);
69 // Reads the notification data for the notification identified by
70 // |notification_id| and belonging to |origin| from the database, and stores
71 // it in |notification_database_data|. Returns the status code.
72 Status ReadNotificationData(
73 int64_t notification_id,
74 const GURL& origin,
75 NotificationDatabaseData* notification_database_data) const;
77 // Reads all notification data for all origins from the database, and appends
78 // the data to |notification_data_vector|. Returns the status code.
79 Status ReadAllNotificationData(
80 std::vector<NotificationDatabaseData>* notification_data_vector) const;
82 // Reads all notification data associated with |origin| from the database, and
83 // appends the data to |notification_data_vector|. Returns the status code.
84 Status ReadAllNotificationDataForOrigin(
85 const GURL& origin,
86 std::vector<NotificationDatabaseData>* notification_data_vector) const;
88 // Reads all notification data associated to |service_worker_registration_id|
89 // belonging to |origin| from the database, and appends the data to the
90 // |notification_data_vector|. Returns the status code.
91 Status ReadAllNotificationDataForServiceWorkerRegistration(
92 const GURL& origin,
93 int64_t service_worker_registration_id,
94 std::vector<NotificationDatabaseData>* notification_data_vector) const;
96 // Writes the |notification_database_data| for a new notification belonging to
97 // |origin| to the database, and returns the status code of the writing
98 // operation. The id of the new notification will be set in |notification_id|.
99 Status WriteNotificationData(
100 const GURL& origin,
101 const NotificationDatabaseData& notification_database_data,
102 int64_t* notification_id);
104 // Deletes all data associated with the notification identified by
105 // |notification_id| belonging to |origin| from the database. Returns the
106 // status code of the deletion operation. Note that it is not considered a
107 // failure if the to-be-deleted notification does not exist.
108 Status DeleteNotificationData(int64_t notification_id, const GURL& origin);
110 // Deletes all data associated with |origin| from the database, and appends
111 // the deleted notification ids to |deleted_notification_set|. Returns the
112 // status code of the deletion operation.
113 Status DeleteAllNotificationDataForOrigin(
114 const GURL& origin,
115 std::set<int64_t>* deleted_notification_set);
117 // Deletes all data associated with the |service_worker_registration_id|
118 // belonging to |origin| from the database, and appends the deleted
119 // notification ids to |deleted_notification_set|. Returns the status code
120 // of the deletion operation.
121 Status DeleteAllNotificationDataForServiceWorkerRegistration(
122 const GURL& origin,
123 int64_t service_worker_registration_id,
124 std::set<int64_t>* deleted_notification_set);
126 // Completely destroys the contents of this database.
127 Status Destroy();
129 private:
130 friend class NotificationDatabaseTest;
132 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this.
133 // See https://crbug.com/463869.
134 enum State {
135 STATE_UNINITIALIZED,
136 STATE_INITIALIZED,
137 STATE_DISABLED,
140 // Reads the next available notification id from the database and returns
141 // the status code of the reading operation. The value will be stored in
142 // the |next_notification_id_| member.
143 Status ReadNextNotificationId();
145 // Reads all notification data with the given constraints. |origin| may be
146 // empty to read all notification data from all origins. If |origin| is
147 // set, but |service_worker_registration_id| is invalid, then all notification
148 // data for |origin| will be read. If both are set, then all notification data
149 // for the given |service_worker_registration_id| will be read.
150 Status ReadAllNotificationDataInternal(
151 const GURL& origin,
152 int64_t service_worker_registration_id,
153 std::vector<NotificationDatabaseData>* notification_data_vector) const;
155 // Deletes all notification data with the given constraints. |origin| must
156 // always be set - use Destroy() when the goal is to empty the database. If
157 // |service_worker_registration_id| is invalid, all notification data for the
158 // |origin| will be deleted.
159 // All deleted notification ids will be written to |deleted_notification_set|.
160 Status DeleteAllNotificationDataInternal(
161 const GURL& origin,
162 int64_t service_worker_registration_id,
163 std::set<int64_t>* deleted_notification_set);
165 // Returns whether the database has been opened.
166 bool IsOpen() const { return db_ != nullptr; }
168 // Returns whether the database should only exist in memory.
169 bool IsInMemoryDatabase() const { return path_.empty(); }
171 // Exposes the LevelDB database used to back this notification database.
172 // Should only be used for testing purposes.
173 leveldb::DB* GetDBForTesting() const { return db_.get(); }
175 base::FilePath path_;
177 int64_t next_notification_id_ = 0;
179 scoped_ptr<const leveldb::FilterPolicy> filter_policy_;
181 // The declaration order for these members matters, as |db_| depends on |env_|
182 // and thus has to be destructed first.
183 scoped_ptr<leveldb::Env> env_;
184 scoped_ptr<leveldb::DB> db_;
186 State state_ = STATE_UNINITIALIZED;
188 base::SequenceChecker sequence_checker_;
190 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase);
193 } // namespace content
195 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_