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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 bool has_fetch_handler; | 60 bool has_fetch_handler; |
61 base::Time last_update_check; | 61 base::Time last_update_check; |
62 | 62 |
63 RegistrationData(); | 63 RegistrationData(); |
64 ~RegistrationData(); | 64 ~RegistrationData(); |
65 }; | 65 }; |
66 | 66 |
67 struct ResourceRecord { | 67 struct ResourceRecord { |
68 int64 resource_id; | 68 int64 resource_id; |
69 GURL url; | 69 GURL url; |
| 70 |
| 71 ResourceRecord() {} |
| 72 ResourceRecord(int64 id, GURL url) : resource_id(id), url(url) {} |
70 }; | 73 }; |
71 | 74 |
72 // Reads next available ids from the database. Returns OK if they are | 75 // Reads next available ids from the database. Returns OK if they are |
73 // successfully read. Fills the arguments with an initial value and returns | 76 // successfully read. Fills the arguments with an initial value and returns |
74 // OK if they are not found in the database. Otherwise, returns an error. | 77 // OK if they are not found in the database. Otherwise, returns an error. |
75 Status GetNextAvailableIds( | 78 Status GetNextAvailableIds( |
76 int64* next_avail_registration_id, | 79 int64* next_avail_registration_id, |
77 int64* next_avail_version_id, | 80 int64* next_avail_version_id, |
78 int64* next_avail_resource_id); | 81 int64* next_avail_resource_id); |
79 | 82 |
(...skipping 27 matching lines...) Expand all Loading... |
107 std::vector<ResourceRecord>* resources); | 110 std::vector<ResourceRecord>* resources); |
108 | 111 |
109 // Writes |registration| and |resources| into the database and does following | 112 // Writes |registration| and |resources| into the database and does following |
110 // things: | 113 // things: |
111 // - Deletes an old version of the registration if exists. | 114 // - Deletes an old version of the registration if exists. |
112 // - Bumps the next registration id and the next version id if needed. | 115 // - Bumps the next registration id and the next version id if needed. |
113 // - Removes |resources| from the uncommitted list if exist. | 116 // - Removes |resources| from the uncommitted list if exist. |
114 // Returns OK they are successfully written. Otherwise, returns an error. | 117 // Returns OK they are successfully written. Otherwise, returns an error. |
115 Status WriteRegistration( | 118 Status WriteRegistration( |
116 const RegistrationData& registration, | 119 const RegistrationData& registration, |
117 const std::vector<ResourceRecord>& resources); | 120 const std::vector<ResourceRecord>& resources, |
| 121 std::vector<int64>* newly_purgeable_resources); |
118 | 122 |
119 // Updates a registration for |registration_id| to an active state. Returns OK | 123 // Updates a registration for |registration_id| to an active state. Returns OK |
120 // if it's successfully updated. Otherwise, returns an error. | 124 // if it's successfully updated. Otherwise, returns an error. |
121 Status UpdateVersionToActive( | 125 Status UpdateVersionToActive( |
122 int64 registration_id, | 126 int64 registration_id, |
123 const GURL& origin); | 127 const GURL& origin); |
124 | 128 |
125 // Updates last check time of a registration for |registration_id| by |time|. | 129 // Updates last check time of a registration for |registration_id| by |time|. |
126 // Returns OK if it's successfully updated. Otherwise, returns an error. | 130 // Returns OK if it's successfully updated. Otherwise, returns an error. |
127 Status UpdateLastCheckTime( | 131 Status UpdateLastCheckTime( |
128 int64 registration_id, | 132 int64 registration_id, |
129 const GURL& origin, | 133 const GURL& origin, |
130 const base::Time& time); | 134 const base::Time& time); |
131 | 135 |
132 // Deletes a registration for |registration_id| and moves resource records | 136 // Deletes a registration for |registration_id| and moves resource records |
133 // associated with it into the purgeable list. Returns OK if it's successfully | 137 // 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. | 138 // deleted or not found in the database. Otherwise, returns an error. |
135 Status DeleteRegistration( | 139 Status DeleteRegistration( |
136 int64 registration_id, | 140 int64 registration_id, |
137 const GURL& origin); | 141 const GURL& origin, |
| 142 std::vector<int64>* newly_purgeable_resources); |
138 | 143 |
139 // As new resources are put into the diskcache, they go into an uncommitted | 144 // As new resources are put into the diskcache, they go into an uncommitted |
140 // list. When a registration is saved that refers to those ids, they're | 145 // list. When a registration is saved that refers to those ids, they're |
141 // removed from that list. When a resource no longer has any registrations or | 146 // removed from that list. When a resource no longer has any registrations or |
142 // caches referring to it, it's added to the purgeable list. Periodically, | 147 // caches referring to it, it's added to the purgeable list. Periodically, |
143 // the purgeable list can be purged from the diskcache. At system startup, all | 148 // the purgeable list can be purged from the diskcache. At system startup, all |
144 // uncommitted ids are moved to the purgeable list. | 149 // uncommitted ids are moved to the purgeable list. |
145 | 150 |
146 // Reads uncommitted resource ids from the database. Returns OK on success. | 151 // Reads uncommitted resource ids from the database. Returns OK on success. |
147 // Otherwise clears |ids| and returns an error. | 152 // Otherwise clears |ids| and returns an error. |
(...skipping 16 matching lines...) Expand all Loading... |
164 Status WritePurgeableResourceIds(const std::set<int64>& ids); | 169 Status WritePurgeableResourceIds(const std::set<int64>& ids); |
165 | 170 |
166 // Deletes purgeable resource ids specified by |ids| from the database. | 171 // Deletes purgeable resource ids specified by |ids| from the database. |
167 // Returns OK on success. Otherwise deletes nothing and returns an error. | 172 // Returns OK on success. Otherwise deletes nothing and returns an error. |
168 Status ClearPurgeableResourceIds(const std::set<int64>& ids); | 173 Status ClearPurgeableResourceIds(const std::set<int64>& ids); |
169 | 174 |
170 // Deletes all data for |origin|, namely, unique origin, registrations and | 175 // Deletes all data for |origin|, namely, unique origin, registrations and |
171 // resource records. Resources are moved to the purgeable list. Returns OK if | 176 // resource records. Resources are moved to the purgeable list. Returns OK if |
172 // they are successfully deleted or not found in the database. Otherwise, | 177 // they are successfully deleted or not found in the database. Otherwise, |
173 // returns an error. | 178 // returns an error. |
174 Status DeleteAllDataForOrigin(const GURL& origin); | 179 Status DeleteAllDataForOrigin( |
| 180 const GURL& origin, |
| 181 std::vector<int64>* newly_purgeable_resources); |
175 | 182 |
176 bool is_disabled() const { return is_disabled_; } | 183 bool is_disabled() const { return is_disabled_; } |
177 bool was_corruption_detected() const { return was_corruption_detected_; } | 184 bool was_corruption_detected() const { return was_corruption_detected_; } |
178 | 185 |
179 private: | 186 private: |
180 // Opens the database at the |path_|. This is lazily called when the first | 187 // Opens the database at the |path_|. This is lazily called when the first |
181 // database API is called. Returns OK if the database is successfully opened. | 188 // database API is called. Returns OK if the database is successfully opened. |
182 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is | 189 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is |
183 // false. Otherwise, returns an error. | 190 // false. Otherwise, returns an error. |
184 Status LazyOpen(bool create_if_missing); | 191 Status LazyOpen(bool create_if_missing); |
(...skipping 22 matching lines...) Expand all Loading... |
207 // error. | 214 // error. |
208 Status ReadResourceRecords( | 215 Status ReadResourceRecords( |
209 int64 version_id, | 216 int64 version_id, |
210 std::vector<ResourceRecord>* resources); | 217 std::vector<ResourceRecord>* resources); |
211 | 218 |
212 // Deletes resource records for |version_id| from the database. Returns OK if | 219 // Deletes resource records for |version_id| from the database. Returns OK if |
213 // they are successfully deleted or not found in the database. Otherwise, | 220 // they are successfully deleted or not found in the database. Otherwise, |
214 // returns an error. | 221 // returns an error. |
215 Status DeleteResourceRecords( | 222 Status DeleteResourceRecords( |
216 int64 version_id, | 223 int64 version_id, |
| 224 std::vector<int64>* newly_purgeable_resources, |
217 leveldb::WriteBatch* batch); | 225 leveldb::WriteBatch* batch); |
218 | 226 |
219 // Reads resource ids for |id_key_prefix| from the database. Returns OK if | 227 // 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 | 228 // it's successfully read or not found in the database. Otherwise, returns an |
221 // error. | 229 // error. |
222 Status ReadResourceIds( | 230 Status ReadResourceIds( |
223 const char* id_key_prefix, | 231 const char* id_key_prefix, |
224 std::set<int64>* ids); | 232 std::set<int64>* ids); |
225 | 233 |
226 // Write resource ids for |id_key_prefix| into the database. Returns OK on | 234 // Write resource ids for |id_key_prefix| into the database. Returns OK on |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 293 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
286 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); | 294 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); |
287 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 295 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
288 | 296 |
289 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 297 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
290 }; | 298 }; |
291 | 299 |
292 } // namespace content | 300 } // namespace content |
293 | 301 |
294 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 302 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |