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 // Reads all registrations from the database. Returns OK if successfully read | 90 // Reads all registrations from the database. Returns OK if successfully read |
91 // or not found. Otherwise, returns an error. | 91 // or not found. Otherwise, returns an error. |
92 ServiceWorkerStatusCode GetAllRegistrations( | 92 ServiceWorkerStatusCode GetAllRegistrations( |
93 std::vector<RegistrationData>* registrations); | 93 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 ServiceWorkerStatusCode 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 ServiceWorkerStatusCode 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 ServiceWorkerStatusCode 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 ServiceWorkerStatusCode 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 ServiceWorkerStatusCode 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 true on success. |
123 // Otherwise clears |ids| and returns false. | 147 // Otherwise clears |ids| and returns false. |
(...skipping 12 matching lines...) Expand all Loading... |
136 bool GetPurgeableResourceIds(std::set<int64>* ids); | 160 bool 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 true on |
139 // success. Otherwise writes nothing and returns false. | 163 // success. Otherwise writes nothing and returns false. |
140 bool WritePurgeableResourceIds(const std::set<int64>& ids); | 164 bool 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 true on success. Otherwise deletes nothing and returns false. |
144 bool ClearPurgeableResourceIds(const std::set<int64>& ids); | 168 bool 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 ServiceWorkerStatusCode 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 ServiceWorkerStatusCode LazyOpen(bool create_if_missing); | 184 ServiceWorkerStatusCode LazyOpen(bool create_if_missing); |
159 | 185 |
160 // Reads the next available id for |id_key|. Returns OK if it's successfully | 186 // Reads the next available id for |id_key|. Returns OK if it's successfully |
161 // read. Fills |next_avail_id| with an initial value and returns OK if it's | 187 // read. Fills |next_avail_id| with an initial value and returns OK if it's |
162 // not found in the database. Otherwise, returns an error. | 188 // not found in the database. Otherwise, returns an error. |
163 ServiceWorkerStatusCode ReadNextAvailableId( | 189 ServiceWorkerStatusCode ReadNextAvailableId( |
164 const char* id_key, | 190 const char* id_key, |
165 int64* next_avail_id); | 191 int64* next_avail_id); |
166 | 192 |
167 bool ReadRegistrationData(int64 registration_id, | 193 // Reads registration data for |registration_id| from the database. Returns OK |
168 const GURL& origin, | 194 // if successfully reads. Otherwise, returns an error. |
169 RegistrationData* registration); | 195 ServiceWorkerStatusCode ReadRegistrationData( |
170 bool ReadResourceRecords(int64 version_id, | 196 int64 registration_id, |
171 std::vector<ResourceRecord>* resources); | 197 const GURL& origin, |
172 bool DeleteResourceRecords(int64 version_id, | 198 RegistrationData* registration); |
173 leveldb::WriteBatch* batch); | 199 |
| 200 // Reads resource records for |version_id| from the database. Returns OK if |
| 201 // it's successfully read or not found in the database. Otherwise, returns an |
| 202 // error. |
| 203 ServiceWorkerStatusCode ReadResourceRecords( |
| 204 int64 version_id, |
| 205 std::vector<ResourceRecord>* resources); |
| 206 |
| 207 // Deletes resource records for |version_id| from the database. Returns OK if |
| 208 // they are successfully deleted or not found in the database. Otherwise, |
| 209 // returns an error. |
| 210 ServiceWorkerStatusCode DeleteResourceRecords( |
| 211 int64 version_id, |
| 212 leveldb::WriteBatch* batch); |
| 213 |
174 bool ReadResourceIds(const char* id_key_prefix, | 214 bool ReadResourceIds(const char* id_key_prefix, |
175 std::set<int64>* ids); | 215 std::set<int64>* ids); |
176 bool WriteResourceIds(const char* id_key_prefix, | 216 bool WriteResourceIds(const char* id_key_prefix, |
177 const std::set<int64>& ids); | 217 const std::set<int64>& ids); |
178 bool DeleteResourceIds(const char* id_key_prefix, | 218 bool DeleteResourceIds(const char* id_key_prefix, |
179 const std::set<int64>& ids); | 219 const std::set<int64>& ids); |
180 | 220 |
181 // Reads the current schema version from the database. If the database hasn't | 221 // Reads the current schema version from the database. If the database hasn't |
182 // been written anything yet, sets |db_version| to 0 and returns OK. | 222 // been written anything yet, sets |db_version| to 0 and returns OK. |
183 ServiceWorkerStatusCode ReadDatabaseVersion(int64* db_version); | 223 ServiceWorkerStatusCode ReadDatabaseVersion(int64* db_version); |
184 | 224 |
185 // Write a batch into the database. | 225 // Writes a batch into the database. |
186 // NOTE: You must call this when you want to put something into the database | 226 // NOTE: You must call this when you want to put something into the database |
187 // because this initializes the database if needed. | 227 // because this initializes the database if needed. |
188 bool WriteBatch(leveldb::WriteBatch* batch); | 228 ServiceWorkerStatusCode WriteBatch(leveldb::WriteBatch* batch); |
189 | 229 |
190 // Bumps the next available id if |used_id| is greater than or equal to the | 230 // Bumps the next available id if |used_id| is greater than or equal to the |
191 // cached one. | 231 // cached one. |
192 void BumpNextRegistrationIdIfNeeded(int64 used_id, | 232 void BumpNextRegistrationIdIfNeeded(int64 used_id, |
193 leveldb::WriteBatch* batch); | 233 leveldb::WriteBatch* batch); |
194 void BumpNextVersionIdIfNeeded(int64 used_id, | 234 void BumpNextVersionIdIfNeeded(int64 used_id, |
195 leveldb::WriteBatch* batch); | 235 leveldb::WriteBatch* batch); |
196 | 236 |
197 bool IsOpen(); | 237 bool IsOpen(); |
198 | 238 |
(...skipping 25 matching lines...) Expand all Loading... |
224 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 264 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
225 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); | 265 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); |
226 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 266 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
227 | 267 |
228 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 268 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
229 }; | 269 }; |
230 | 270 |
231 } // namespace content | 271 } // namespace content |
232 | 272 |
233 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 273 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |