Chromium Code Reviews| 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 #include <set> | |
| 10 #include <vector> | |
| 9 | 11 |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/sequence_checker.h" | 13 #include "base/sequence_checker.h" |
| 12 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 13 | 15 |
| 14 class GURL; | 16 class GURL; |
| 15 | 17 |
| 16 namespace leveldb { | 18 namespace leveldb { |
| 17 class DB; | 19 class DB; |
| 18 class Env; | 20 class Env; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 Status Open(bool create_if_missing); | 62 Status Open(bool create_if_missing); |
| 61 | 63 |
| 62 // Reads the notification data for the notification identified by | 64 // Reads the notification data for the notification identified by |
| 63 // |notification_id| and belonging to |origin| from the database, and stores | 65 // |notification_id| and belonging to |origin| from the database, and stores |
| 64 // it in |notification_database_data|. Returns the status code. | 66 // it in |notification_database_data|. Returns the status code. |
| 65 Status ReadNotificationData( | 67 Status ReadNotificationData( |
| 66 int64_t notification_id, | 68 int64_t notification_id, |
| 67 const GURL& origin, | 69 const GURL& origin, |
| 68 NotificationDatabaseData* notification_database_data) const; | 70 NotificationDatabaseData* notification_database_data) const; |
| 69 | 71 |
| 72 // Reads all notification data for all origins from the database, and appends | |
| 73 // the data to |notification_data_vector|. Returns the status code of the | |
| 74 // reading operation. | |
| 75 Status ReadAllNotificationData( | |
| 76 std::vector<NotificationDatabaseData>* notification_data_vector) const; | |
| 77 | |
| 78 // Reads all notification data associated with |origin| from the database, and | |
| 79 // appends the data to |notification_data_vector|. Returns the status code of | |
| 80 // the reading operation. | |
| 81 Status ReadAllNotificationDataForOrigin( | |
| 82 const GURL& origin, | |
| 83 std::vector<NotificationDatabaseData>* notification_data_vector) const; | |
| 84 | |
| 85 // Reads all notification data associated to |service_worker_registration_id| | |
| 86 // belonging to |origin| from the database, and appends the data to the | |
| 87 // |notification_data_vector|. Returns the status code. | |
| 88 Status ReadAllNotificationDataForServiceWorkerRegistration( | |
| 89 const GURL& origin, | |
| 90 int64_t service_worker_registration_id, | |
| 91 std::vector<NotificationDatabaseData>* notification_data_vector) const; | |
| 92 | |
| 70 // Writes the |notification_database_data| for a new notification belonging to | 93 // Writes the |notification_database_data| for a new notification belonging to |
| 71 // |origin| to the database, and returns the status code of the writing | 94 // |origin| to the database, and returns the status code of the writing |
| 72 // operation. The id of the new notification will be set in |notification_id|. | 95 // operation. The id of the new notification will be set in |notification_id|. |
| 73 Status WriteNotificationData( | 96 Status WriteNotificationData( |
| 74 const GURL& origin, | 97 const GURL& origin, |
| 75 const NotificationDatabaseData& notification_database_data, | 98 const NotificationDatabaseData& notification_database_data, |
| 76 int64_t* notification_id); | 99 int64_t* notification_id); |
| 77 | 100 |
| 78 // Deletes all data associated with the notification identified by | 101 // Deletes all data associated with the notification identified by |
| 79 // |notification_id| belonging to |origin| from the database. Returns the | 102 // |notification_id| belonging to |origin| from the database. Returns the |
| 80 // status code of the deletion operation. Note that it is not considered a | 103 // status code of the deletion operation. Note that it is not considered a |
| 81 // failure if the to-be-deleted notification does not exist. | 104 // failure if the to-be-deleted notification does not exist. |
| 82 Status DeleteNotificationData(int64_t notification_id, const GURL& origin); | 105 Status DeleteNotificationData(int64_t notification_id, const GURL& origin); |
| 83 | 106 |
| 107 // Deletes all data associated with |origin| from the database, and appends | |
| 108 // the deleted notification ids to |deleted_notification_ids|. Returns the | |
| 109 // status code of the deletion operation. | |
| 110 Status DeleteAllNotificationDataForOrigin( | |
| 111 const GURL& origin, | |
| 112 std::set<int64_t>* deleted_notification_ids); | |
| 113 | |
| 114 // Deletes all data associated with the |service_worker_registration_id| | |
| 115 // belonging to |origin| from the database, and appends the deleted | |
| 116 // notification ids to |deleted_notification_ids|. Returns the status code | |
| 117 // of the deletion operation. | |
| 118 Status DeleteAllNotificationDataForServiceWorkerRegistration( | |
| 119 const GURL& origin, | |
| 120 int64_t service_worker_registration_id, | |
| 121 std::set<int64_t>* deleted_notification_ids); | |
| 122 | |
| 84 // Completely destroys the contents of this database. | 123 // Completely destroys the contents of this database. |
| 85 Status Destroy(); | 124 Status Destroy(); |
| 86 | 125 |
| 87 private: | 126 private: |
| 88 friend class NotificationDatabaseTest; | 127 friend class NotificationDatabaseTest; |
| 89 | 128 |
| 90 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | 129 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. |
| 91 // See https://crbug.com/463869. | 130 // See https://crbug.com/463869. |
| 92 enum State { | 131 enum State { |
| 93 STATE_UNINITIALIZED, | 132 STATE_UNINITIALIZED, |
| 94 STATE_INITIALIZED, | 133 STATE_INITIALIZED, |
| 95 STATE_DISABLED, | 134 STATE_DISABLED, |
| 96 }; | 135 }; |
| 97 | 136 |
| 98 // Reads the next available notification id from the database and returns | 137 // Reads the next available notification id from the database and returns |
| 99 // the status code of the reading operation. The value will be stored in | 138 // the status code of the reading operation. The value will be stored in |
| 100 // the |next_notification_id_| member. | 139 // the |next_notification_id_| member. |
| 101 Status ReadNextNotificationId(); | 140 Status ReadNextNotificationId(); |
| 102 | 141 |
| 142 // Reads all notification data with the given constraints. |origin| may be | |
| 143 // empty to read all notification data from all origins. If |origin| is | |
| 144 // set, but |service_worker_registration_id| is invalid, then all notification | |
| 145 // data for |origin| will be read. If both are set, then all notification data | |
| 146 // for the given |service_worker_registration_id| will be read. | |
| 147 Status ReadAllNotificationDataInternal( | |
| 148 const GURL& origin, | |
| 149 int64_t service_worker_registration_id, | |
| 150 std::vector<NotificationDatabaseData>* notification_data_vector) const; | |
| 151 | |
| 152 // Delets all notification data with the given constraints. |origin| must | |
|
cmumford
2015/03/18 22:41:21
s/Delets/Deletes/
Peter Beverloo
2015/03/19 14:35:52
Done.
| |
| 153 // always be set - use Destroy() when the goal is to empty the database. If | |
| 154 // |service_worker_registration_id| is invalid, all notification data for the | |
| 155 // |origin| will be deleted. | |
| 156 // All deleted notification ids will be written to |deleted_notification_ids|. | |
| 157 Status DeleteAllNotificationDataInternal( | |
| 158 const GURL& origin, | |
| 159 int64_t service_worker_registration_id, | |
| 160 std::set<int64_t>* deleted_notification_ids); | |
| 161 | |
| 103 // Returns whether the database has been opened. | 162 // Returns whether the database has been opened. |
| 104 bool IsOpen() const { return db_ != nullptr; } | 163 bool IsOpen() const { return db_ != nullptr; } |
| 105 | 164 |
| 106 // Returns whether the database should only exist in memory. | 165 // Returns whether the database should only exist in memory. |
| 107 bool IsInMemoryDatabase() const { return path_.empty(); } | 166 bool IsInMemoryDatabase() const { return path_.empty(); } |
| 108 | 167 |
| 109 // Exposes the LevelDB database used to back this notification database. | 168 // Exposes the LevelDB database used to back this notification database. |
| 110 // Should only be used for testing purposes. | 169 // Should only be used for testing purposes. |
| 111 leveldb::DB* GetDBForTesting() const { return db_.get(); } | 170 leveldb::DB* GetDBForTesting() const { return db_.get(); } |
| 112 | 171 |
| 113 base::FilePath path_; | 172 base::FilePath path_; |
| 114 | 173 |
| 115 int64_t next_notification_id_ = 0; | 174 int64_t next_notification_id_ = 0; |
| 116 | 175 |
| 117 // The declaration order for these members matters, as |db_| depends on |env_| | 176 // The declaration order for these members matters, as |db_| depends on |env_| |
| 118 // and thus has to be destructed first. | 177 // and thus has to be destructed first. |
| 119 scoped_ptr<leveldb::Env> env_; | 178 scoped_ptr<leveldb::Env> env_; |
| 120 scoped_ptr<leveldb::DB> db_; | 179 scoped_ptr<leveldb::DB> db_; |
| 121 | 180 |
| 122 State state_ = STATE_UNINITIALIZED; | 181 State state_ = STATE_UNINITIALIZED; |
| 123 | 182 |
| 124 base::SequenceChecker sequence_checker_; | 183 base::SequenceChecker sequence_checker_; |
| 125 | 184 |
| 126 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 185 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
| 127 }; | 186 }; |
| 128 | 187 |
| 129 } // namespace content | 188 } // namespace content |
| 130 | 189 |
| 131 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 190 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
| OLD | NEW |