OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 // Reads all registrations from the database. Returns OK if successfully read | 91 // Reads all registrations from the database. Returns OK if successfully read |
92 // or not found. Otherwise, returns an error. | 92 // or not found. Otherwise, returns an error. |
93 Status GetAllRegistrations(std::vector<RegistrationData>* registrations); | 93 Status GetAllRegistrations(std::vector<RegistrationData>* registrations); |
94 | 94 |
95 // Saving, retrieving, and updating registration data. | 95 // Saving, retrieving, and updating registration data. |
96 // (will bump next_avail_xxxx_ids as needed) | 96 // (will bump next_avail_xxxx_ids as needed) |
97 // (resource ids will be added/removed from the uncommitted/purgeable | 97 // (resource ids will be added/removed from the uncommitted/purgeable |
98 // lists as needed) | 98 // lists as needed) |
99 | 99 |
100 bool ReadRegistration(int64 registration_id, | 100 // Reads a registration for |registration_id| and resource records associated |
101 const GURL& origin, | 101 // with it from the database. Returns OK if they are successfully read. |
102 RegistrationData* registration, | 102 // Otherwise, returns an error. |
103 std::vector<ResourceRecord>* resources); | 103 Status ReadRegistration( |
104 bool WriteRegistration(const RegistrationData& registration, | 104 int64 registration_id, |
105 const std::vector<ResourceRecord>& resources); | 105 const GURL& origin, |
| 106 RegistrationData* registration, |
| 107 std::vector<ResourceRecord>* resources); |
106 | 108 |
107 bool UpdateVersionToActive(int64 registration_id, | 109 // Writes |registration| and |resources| into the database and does following |
108 const GURL& origin); | 110 // things: |
109 bool UpdateLastCheckTime(int64 registration_id, | 111 // - Deletes an old version of the registration if exists. |
110 const GURL& origin, | 112 // - Bumps the next registration id and the next version id if needed. |
111 const base::Time& time); | 113 // - Removes |resources| from the uncommitted list if exist. |
112 bool DeleteRegistration(int64 registration_id, | 114 // Returns OK they are successfully written. Otherwise, returns an error. |
113 const GURL& origin); | 115 Status WriteRegistration( |
| 116 const RegistrationData& registration, |
| 117 const std::vector<ResourceRecord>& resources); |
| 118 |
| 119 // Updates a registration for |registration_id| to an active state. Returns OK |
| 120 // if it's successfully updated. Otherwise, returns an error. |
| 121 Status UpdateVersionToActive( |
| 122 int64 registration_id, |
| 123 const GURL& origin); |
| 124 |
| 125 // Updates last check time of a registration for |registration_id| by |time|. |
| 126 // Returns OK if it's successfully updated. Otherwise, returns an error. |
| 127 Status UpdateLastCheckTime( |
| 128 int64 registration_id, |
| 129 const GURL& origin, |
| 130 const base::Time& time); |
| 131 |
| 132 // Deletes a registration for |registration_id| and moves resource records |
| 133 // associated with it into the purgeable list. Returns OK if it's successfully |
| 134 // deleted or not found in the database. Otherwise, returns an error. |
| 135 Status DeleteRegistration( |
| 136 int64 registration_id, |
| 137 const GURL& origin); |
114 | 138 |
115 // As new resources are put into the diskcache, they go into an uncommitted | 139 // As new resources are put into the diskcache, they go into an uncommitted |
116 // list. When a registration is saved that refers to those ids, they're | 140 // list. When a registration is saved that refers to those ids, they're |
117 // removed from that list. When a resource no longer has any registrations or | 141 // removed from that list. When a resource no longer has any registrations or |
118 // caches referring to it, it's added to the purgeable list. Periodically, | 142 // caches referring to it, it's added to the purgeable list. Periodically, |
119 // the purgeable list can be purged from the diskcache. At system startup, all | 143 // the purgeable list can be purged from the diskcache. At system startup, all |
120 // uncommitted ids are moved to the purgeable list. | 144 // uncommitted ids are moved to the purgeable list. |
121 | 145 |
122 // Reads uncommitted resource ids from the database. Returns true on success. | 146 // Reads uncommitted resource ids from the database. Returns OK on success. |
123 // Otherwise clears |ids| and returns false. | 147 // Otherwise clears |ids| and returns an error. |
124 bool GetUncommittedResourceIds(std::set<int64>* ids); | 148 Status GetUncommittedResourceIds(std::set<int64>* ids); |
125 | 149 |
126 // Writes |ids| into the database as uncommitted resources. Returns true on | 150 // Writes |ids| into the database as uncommitted resources. Returns OK on |
127 // success. Otherwise writes nothing and returns false. | 151 // success. Otherwise writes nothing and returns an error. |
128 bool WriteUncommittedResourceIds(const std::set<int64>& ids); | 152 Status WriteUncommittedResourceIds(const std::set<int64>& ids); |
129 | 153 |
130 // Deletes uncommitted resource ids specified by |ids| from the database. | 154 // Deletes uncommitted resource ids specified by |ids| from the database. |
131 // Returns true on success. Otherwise deletes nothing and returns false. | 155 // Returns OK on success. Otherwise deletes nothing and returns an error. |
132 bool ClearUncommittedResourceIds(const std::set<int64>& ids); | 156 Status ClearUncommittedResourceIds(const std::set<int64>& ids); |
133 | 157 |
134 // Reads purgeable resource ids from the database. Returns true on success. | 158 // Reads purgeable resource ids from the database. Returns OK on success. |
135 // Otherwise clears |ids| and returns false. | 159 // Otherwise clears |ids| and returns an error. |
136 bool GetPurgeableResourceIds(std::set<int64>* ids); | 160 Status GetPurgeableResourceIds(std::set<int64>* ids); |
137 | 161 |
138 // Writes |ids| into the database as purgeable resources. Returns true on | 162 // Writes |ids| into the database as purgeable resources. Returns OK on |
139 // success. Otherwise writes nothing and returns false. | 163 // success. Otherwise writes nothing and returns an error. |
140 bool WritePurgeableResourceIds(const std::set<int64>& ids); | 164 Status WritePurgeableResourceIds(const std::set<int64>& ids); |
141 | 165 |
142 // Deletes purgeable resource ids specified by |ids| from the database. | 166 // Deletes purgeable resource ids specified by |ids| from the database. |
143 // Returns true on success. Otherwise deletes nothing and returns false. | 167 // Returns OK on success. Otherwise deletes nothing and returns an error. |
144 bool ClearPurgeableResourceIds(const std::set<int64>& ids); | 168 Status ClearPurgeableResourceIds(const std::set<int64>& ids); |
145 | 169 |
146 // Delete all data for |origin|, namely, unique origin, registrations and | 170 // Deletes all data for |origin|, namely, unique origin, registrations and |
147 // resource records. Resources are moved to the purgeable list. | 171 // resource records. Resources are moved to the purgeable list. Returns OK if |
148 bool DeleteAllDataForOrigin(const GURL& origin); | 172 // they are successfully deleted or not found in the database. Otherwise, |
| 173 // returns an error. |
| 174 Status DeleteAllDataForOrigin(const GURL& origin); |
149 | 175 |
150 bool is_disabled() const { return is_disabled_; } | 176 bool is_disabled() const { return is_disabled_; } |
151 bool was_corruption_detected() const { return was_corruption_detected_; } | 177 bool was_corruption_detected() const { return was_corruption_detected_; } |
152 | 178 |
153 private: | 179 private: |
154 // Opens the database at the |path_|. This is lazily called when the first | 180 // Opens the database at the |path_|. This is lazily called when the first |
155 // database API is called. Returns OK if the database is successfully opened. | 181 // database API is called. Returns OK if the database is successfully opened. |
156 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is | 182 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is |
157 // false. Otherwise, returns an error. | 183 // false. Otherwise, returns an error. |
158 Status LazyOpen(bool create_if_missing); | 184 Status LazyOpen(bool create_if_missing); |
159 | 185 |
160 // Helper for LazyOpen(). |status| must be the return value from LazyOpen() | 186 // Helper for LazyOpen(). |status| must be the return value from LazyOpen() |
161 // and this must be called just after LazyOpen() is called. Returns true if | 187 // and this must be called just after LazyOpen() is called. Returns true if |
162 // the database is new or nonexistent, that is, it has never been used. | 188 // the database is new or nonexistent, that is, it has never been used. |
163 bool IsNewOrNonexistentDatabase(Status status); | 189 bool IsNewOrNonexistentDatabase(Status status); |
164 | 190 |
165 // Reads the next available id for |id_key|. Returns OK if it's successfully | 191 // Reads the next available id for |id_key|. Returns OK if it's successfully |
166 // read. Fills |next_avail_id| with an initial value and returns OK if it's | 192 // read. Fills |next_avail_id| with an initial value and returns OK if it's |
167 // not found in the database. Otherwise, returns an error. | 193 // not found in the database. Otherwise, returns an error. |
168 Status ReadNextAvailableId( | 194 Status ReadNextAvailableId( |
169 const char* id_key, | 195 const char* id_key, |
170 int64* next_avail_id); | 196 int64* next_avail_id); |
171 | 197 |
172 bool ReadRegistrationData(int64 registration_id, | 198 // Reads registration data for |registration_id| from the database. Returns OK |
173 const GURL& origin, | 199 // if successfully reads. Otherwise, returns an error. |
174 RegistrationData* registration); | 200 Status ReadRegistrationData( |
175 bool ReadResourceRecords(int64 version_id, | 201 int64 registration_id, |
176 std::vector<ResourceRecord>* resources); | 202 const GURL& origin, |
177 bool DeleteResourceRecords(int64 version_id, | 203 RegistrationData* registration); |
178 leveldb::WriteBatch* batch); | 204 |
179 bool ReadResourceIds(const char* id_key_prefix, | 205 // Reads resource records for |version_id| from the database. Returns OK if |
180 std::set<int64>* ids); | 206 // it's successfully read or not found in the database. Otherwise, returns an |
181 bool WriteResourceIds(const char* id_key_prefix, | 207 // error. |
182 const std::set<int64>& ids); | 208 Status ReadResourceRecords( |
183 bool DeleteResourceIds(const char* id_key_prefix, | 209 int64 version_id, |
184 const std::set<int64>& ids); | 210 std::vector<ResourceRecord>* resources); |
| 211 |
| 212 // Deletes resource records for |version_id| from the database. Returns OK if |
| 213 // they are successfully deleted or not found in the database. Otherwise, |
| 214 // returns an error. |
| 215 Status DeleteResourceRecords( |
| 216 int64 version_id, |
| 217 leveldb::WriteBatch* batch); |
| 218 |
| 219 // Reads resource ids for |id_key_prefix| from the database. Returns OK if |
| 220 // it's successfully read or not found in the database. Otherwise, returns an |
| 221 // error. |
| 222 Status ReadResourceIds( |
| 223 const char* id_key_prefix, |
| 224 std::set<int64>* ids); |
| 225 |
| 226 // Write resource ids for |id_key_prefix| into the database. Returns OK on |
| 227 // success. Otherwise, returns writes nothing and returns an error. |
| 228 Status WriteResourceIds( |
| 229 const char* id_key_prefix, |
| 230 const std::set<int64>& ids); |
| 231 |
| 232 // Deletes resource ids for |id_key_prefix| from the database. Returns OK if |
| 233 // it's successfully deleted or not found in the database. Otherwise, returns |
| 234 // an error. |
| 235 Status DeleteResourceIds( |
| 236 const char* id_key_prefix, |
| 237 const std::set<int64>& ids); |
185 | 238 |
186 // Reads the current schema version from the database. If the database hasn't | 239 // Reads the current schema version from the database. If the database hasn't |
187 // been written anything yet, sets |db_version| to 0 and returns OK. | 240 // been written anything yet, sets |db_version| to 0 and returns OK. |
188 Status ReadDatabaseVersion(int64* db_version); | 241 Status ReadDatabaseVersion(int64* db_version); |
189 | 242 |
190 // Write a batch into the database. | 243 // Writes a batch into the database. |
191 // NOTE: You must call this when you want to put something into the database | 244 // NOTE: You must call this when you want to put something into the database |
192 // because this initializes the database if needed. | 245 // because this initializes the database if needed. |
193 bool WriteBatch(leveldb::WriteBatch* batch); | 246 Status WriteBatch(leveldb::WriteBatch* batch); |
194 | 247 |
195 // Bumps the next available id if |used_id| is greater than or equal to the | 248 // Bumps the next available id if |used_id| is greater than or equal to the |
196 // cached one. | 249 // cached one. |
197 void BumpNextRegistrationIdIfNeeded(int64 used_id, | 250 void BumpNextRegistrationIdIfNeeded( |
198 leveldb::WriteBatch* batch); | 251 int64 used_id, |
199 void BumpNextVersionIdIfNeeded(int64 used_id, | 252 leveldb::WriteBatch* batch); |
200 leveldb::WriteBatch* batch); | 253 void BumpNextVersionIdIfNeeded( |
| 254 int64 used_id, |
| 255 leveldb::WriteBatch* batch); |
201 | 256 |
202 bool IsOpen(); | 257 bool IsOpen(); |
203 | 258 |
204 void HandleError(const tracked_objects::Location& from_here, | 259 void HandleError( |
205 const leveldb::Status& status); | 260 const tracked_objects::Location& from_here, |
| 261 const leveldb::Status& status); |
206 | 262 |
207 base::FilePath path_; | 263 base::FilePath path_; |
208 scoped_ptr<leveldb::Env> env_; | 264 scoped_ptr<leveldb::Env> env_; |
209 scoped_ptr<leveldb::DB> db_; | 265 scoped_ptr<leveldb::DB> db_; |
210 | 266 |
211 int64 next_avail_registration_id_; | 267 int64 next_avail_registration_id_; |
212 int64 next_avail_resource_id_; | 268 int64 next_avail_resource_id_; |
213 int64 next_avail_version_id_; | 269 int64 next_avail_version_id_; |
214 | 270 |
215 // True if a database error has occurred (e.g. cannot read data). | 271 // True if a database error has occurred (e.g. cannot read data). |
(...skipping 13 matching lines...) Expand all Loading... |
229 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 285 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
230 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); | 286 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); |
231 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 287 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
232 | 288 |
233 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 289 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
234 }; | 290 }; |
235 | 291 |
236 } // namespace content | 292 } // namespace content |
237 | 293 |
238 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 294 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |