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 <string> | 10 #include <string> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 struct CONTENT_EXPORT RegistrationData { | 54 struct CONTENT_EXPORT RegistrationData { |
55 // These values are immutable for the life of a registration. | 55 // These values are immutable for the life of a registration. |
56 int64 registration_id; | 56 int64 registration_id; |
57 GURL scope; | 57 GURL scope; |
58 | 58 |
59 // Versions are first stored once they successfully install and become | 59 // Versions are first stored once they successfully install and become |
60 // the waiting version. Then transition to the active version. The stored | 60 // the waiting version. Then transition to the active version. The stored |
61 // version may be in the ACTIVATED state or in the INSTALLED state. | 61 // version may be in the ACTIVATED state or in the INSTALLED state. |
62 GURL script; | 62 GURL script; |
63 int64 version_id; | 63 std::string version_uuid; |
64 bool is_active; | 64 bool is_active; |
65 bool has_fetch_handler; | 65 bool has_fetch_handler; |
66 base::Time last_update_check; | 66 base::Time last_update_check; |
67 | 67 |
68 // Not populated until ServiceWorkerStorage::StoreRegistration is called. | 68 // Not populated until ServiceWorkerStorage::StoreRegistration is called. |
69 int64_t resources_total_size_bytes; | 69 int64_t resources_total_size_bytes; |
70 | 70 |
71 RegistrationData(); | 71 RegistrationData(); |
72 ~RegistrationData(); | 72 ~RegistrationData(); |
73 }; | 73 }; |
74 | 74 |
75 struct ResourceRecord { | 75 struct ResourceRecord { |
76 int64 resource_id; | 76 int64 resource_id; |
77 GURL url; | 77 GURL url; |
78 // Signed so we can store -1 to specify an unknown or error state. When | 78 // Signed so we can store -1 to specify an unknown or error state. When |
79 // stored to the database, this value should always be >= 0. | 79 // stored to the database, this value should always be >= 0. |
80 int64 size_bytes; | 80 int64 size_bytes; |
81 | 81 |
82 ResourceRecord() : resource_id(-1), size_bytes(0) {} | 82 ResourceRecord() : resource_id(-1), size_bytes(0) {} |
83 ResourceRecord(int64 id, GURL url, int64 size_bytes) | 83 ResourceRecord(int64 id, GURL url, int64 size_bytes) |
84 : resource_id(id), url(url), size_bytes(size_bytes) {} | 84 : resource_id(id), url(url), size_bytes(size_bytes) {} |
85 }; | 85 }; |
86 | 86 |
87 // Reads next available ids from the database. Returns OK if they are | 87 // Reads next available ids from the database. Returns OK if they are |
88 // successfully read. Fills the arguments with an initial value and returns | 88 // successfully read. Fills the arguments with an initial value and returns |
89 // OK if they are not found in the database. Otherwise, returns an error. | 89 // OK if they are not found in the database. Otherwise, returns an error. |
90 Status GetNextAvailableIds( | 90 Status GetNextAvailableIds( |
91 int64* next_avail_registration_id, | 91 int64* next_avail_registration_id, |
92 int64* next_avail_version_id, | |
93 int64* next_avail_resource_id); | 92 int64* next_avail_resource_id); |
94 | 93 |
95 // Used for diskcache migration (http://crbug.com/487482). Returns true if the | 94 // Used for diskcache migration (http://crbug.com/487482). Returns true if the |
96 // storage needs to migrate a disk cache. | 95 // storage needs to migrate a disk cache. |
97 Status IsDiskCacheMigrationNeeded(bool* migration_needed); | 96 Status IsDiskCacheMigrationNeeded(bool* migration_needed); |
98 Status SetDiskCacheMigrationNotNeeded(); | 97 Status SetDiskCacheMigrationNotNeeded(); |
99 | 98 |
100 // Used for diskcache migration (http://crbug.com/487482). Returns true if the | 99 // Used for diskcache migration (http://crbug.com/487482). Returns true if the |
101 // storage needs to delete an old disk cache. | 100 // storage needs to delete an old disk cache. |
102 Status IsOldDiskCacheDeletionNeeded(bool* deletion_needed); | 101 Status IsOldDiskCacheDeletionNeeded(bool* deletion_needed); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // Looks up the origin for the registration with |registration_id|. Returns OK | 134 // Looks up the origin for the registration with |registration_id|. Returns OK |
136 // if a registration was found and read successfully. Otherwise, returns an | 135 // if a registration was found and read successfully. Otherwise, returns an |
137 // error. | 136 // error. |
138 Status ReadRegistrationOrigin(int64 registration_id, GURL* origin); | 137 Status ReadRegistrationOrigin(int64 registration_id, GURL* origin); |
139 | 138 |
140 // Writes |registration| and |resources| into the database and does following | 139 // Writes |registration| and |resources| into the database and does following |
141 // things: | 140 // things: |
142 // - If an old version of the registration exists, deletes it and sets | 141 // - If an old version of the registration exists, deletes it and sets |
143 // |deleted_version| to the old version registration data object | 142 // |deleted_version| to the old version registration data object |
144 // |newly_purgeable_resources| to its resources. Otherwise, sets | 143 // |newly_purgeable_resources| to its resources. Otherwise, sets |
145 // |deleted_version->version_id| to -1. | 144 // |deleted_version->version_uuid| to -1. |
146 // - Bumps the next registration id and the next version id if needed. | 145 // - Bumps the next registration id and the next version id if needed. |
147 // - Removes |resources| from the uncommitted list if exist. | 146 // - Removes |resources| from the uncommitted list if exist. |
148 // Returns OK they are successfully written. Otherwise, returns an error. | 147 // Returns OK they are successfully written. Otherwise, returns an error. |
149 Status WriteRegistration(const RegistrationData& registration, | 148 Status WriteRegistration(const RegistrationData& registration, |
150 const std::vector<ResourceRecord>& resources, | 149 const std::vector<ResourceRecord>& resources, |
151 RegistrationData* deleted_version, | 150 RegistrationData* deleted_version, |
152 std::vector<int64>* newly_purgeable_resources); | 151 std::vector<int64>* newly_purgeable_resources); |
153 | 152 |
154 // Updates a registration for |registration_id| to an active state. Returns OK | 153 // Updates a registration for |registration_id| to an active state. Returns OK |
155 // if it's successfully updated. Otherwise, returns an error. | 154 // if it's successfully updated. Otherwise, returns an error. |
156 Status UpdateVersionToActive( | 155 Status UpdateVersionToActive( |
157 int64 registration_id, | 156 int64 registration_id, |
158 const GURL& origin); | 157 const GURL& origin); |
159 | 158 |
160 // Updates last check time of a registration for |registration_id| by |time|. | 159 // Updates last check time of a registration for |registration_id| by |time|. |
161 // Returns OK if it's successfully updated. Otherwise, returns an error. | 160 // Returns OK if it's successfully updated. Otherwise, returns an error. |
162 Status UpdateLastCheckTime( | 161 Status UpdateLastCheckTime( |
163 int64 registration_id, | 162 int64 registration_id, |
164 const GURL& origin, | 163 const GURL& origin, |
165 const base::Time& time); | 164 const base::Time& time); |
166 | 165 |
167 // Deletes a registration for |registration_id| and moves resource records | 166 // Deletes a registration for |registration_id| and moves resource records |
168 // associated with it into the purgeable list. If deletion occurred, sets | 167 // associated with it into the purgeable list. If deletion occurred, sets |
169 // |version_id| to the id of the version that was deleted and | 168 // |version_uuid| to the id of the version that was deleted and |
170 // |newly_purgeable_resources| to its resources; otherwise, sets |version_id| | 169 // |newly_purgeable_resources| to its resources; otherwise, sets |
| 170 // |version_uuid| |
171 // to -1. Returns OK if it's successfully deleted or not found in the | 171 // to -1. Returns OK if it's successfully deleted or not found in the |
172 // database. Otherwise, returns an error. | 172 // database. Otherwise, returns an error. |
173 Status DeleteRegistration(int64 registration_id, | 173 Status DeleteRegistration(int64 registration_id, |
174 const GURL& origin, | 174 const GURL& origin, |
175 RegistrationData* deleted_version, | 175 RegistrationData* deleted_version, |
176 std::vector<int64>* newly_purgeable_resources); | 176 std::vector<int64>* newly_purgeable_resources); |
177 | 177 |
178 // Reads user data for |registration_id| and |user_data_name| from the | 178 // Reads user data for |registration_id| and |user_data_name| from the |
179 // database. | 179 // database. |
180 Status ReadUserData(int64 registration_id, | 180 Status ReadUserData(int64 registration_id, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 const char* id_key, | 269 const char* id_key, |
270 int64* next_avail_id); | 270 int64* next_avail_id); |
271 | 271 |
272 // Reads registration data for |registration_id| from the database. Returns OK | 272 // Reads registration data for |registration_id| from the database. Returns OK |
273 // if successfully reads. Otherwise, returns an error. | 273 // if successfully reads. Otherwise, returns an error. |
274 Status ReadRegistrationData( | 274 Status ReadRegistrationData( |
275 int64 registration_id, | 275 int64 registration_id, |
276 const GURL& origin, | 276 const GURL& origin, |
277 RegistrationData* registration); | 277 RegistrationData* registration); |
278 | 278 |
279 // Reads resource records for |version_id| from the database. Returns OK if | 279 // Reads resource records for |version_uuid| from the database. Returns OK if |
280 // it's successfully read or not found in the database. Otherwise, returns an | 280 // it's successfully read or not found in the database. Otherwise, returns an |
281 // error. | 281 // error. |
282 Status ReadResourceRecords( | 282 Status ReadResourceRecords(std::string version_uuid, |
283 int64 version_id, | 283 std::vector<ResourceRecord>* resources); |
284 std::vector<ResourceRecord>* resources); | |
285 | 284 |
286 // Deletes resource records for |version_id| from the database. Returns OK if | 285 // Deletes resource records for |version_uuid| from the database. Returns OK |
| 286 // if |
287 // they are successfully deleted or not found in the database. Otherwise, | 287 // they are successfully deleted or not found in the database. Otherwise, |
288 // returns an error. | 288 // returns an error. |
289 Status DeleteResourceRecords( | 289 Status DeleteResourceRecords(std::string version_uuid, |
290 int64 version_id, | 290 std::vector<int64>* newly_purgeable_resources, |
291 std::vector<int64>* newly_purgeable_resources, | 291 leveldb::WriteBatch* batch); |
292 leveldb::WriteBatch* batch); | |
293 | 292 |
294 // Reads resource ids for |id_key_prefix| from the database. Returns OK if | 293 // Reads resource ids for |id_key_prefix| from the database. Returns OK if |
295 // it's successfully read or not found in the database. Otherwise, returns an | 294 // it's successfully read or not found in the database. Otherwise, returns an |
296 // error. | 295 // error. |
297 Status ReadResourceIds( | 296 Status ReadResourceIds( |
298 const char* id_key_prefix, | 297 const char* id_key_prefix, |
299 std::set<int64>* ids); | 298 std::set<int64>* ids); |
300 | 299 |
301 // Write resource ids for |id_key_prefix| into the database. Returns OK on | 300 // Write resource ids for |id_key_prefix| into the database. Returns OK on |
302 // success. Otherwise, returns writes nothing and returns an error. | 301 // success. Otherwise, returns writes nothing and returns an error. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 Status WriteBatch(leveldb::WriteBatch* batch); | 334 Status WriteBatch(leveldb::WriteBatch* batch); |
336 | 335 |
337 // Bumps the next available id if |used_id| is greater than or equal to the | 336 // Bumps the next available id if |used_id| is greater than or equal to the |
338 // cached one. | 337 // cached one. |
339 void BumpNextRegistrationIdIfNeeded( | 338 void BumpNextRegistrationIdIfNeeded( |
340 int64 used_id, | 339 int64 used_id, |
341 leveldb::WriteBatch* batch); | 340 leveldb::WriteBatch* batch); |
342 void BumpNextResourceIdIfNeeded( | 341 void BumpNextResourceIdIfNeeded( |
343 int64 used_id, | 342 int64 used_id, |
344 leveldb::WriteBatch* batch); | 343 leveldb::WriteBatch* batch); |
345 void BumpNextVersionIdIfNeeded( | |
346 int64 used_id, | |
347 leveldb::WriteBatch* batch); | |
348 | 344 |
349 bool IsOpen(); | 345 bool IsOpen(); |
350 | 346 |
351 void Disable( | 347 void Disable( |
352 const tracked_objects::Location& from_here, | 348 const tracked_objects::Location& from_here, |
353 Status status); | 349 Status status); |
354 void HandleOpenResult( | 350 void HandleOpenResult( |
355 const tracked_objects::Location& from_here, | 351 const tracked_objects::Location& from_here, |
356 Status status); | 352 Status status); |
357 void HandleReadResult( | 353 void HandleReadResult( |
358 const tracked_objects::Location& from_here, | 354 const tracked_objects::Location& from_here, |
359 Status status); | 355 Status status); |
360 void HandleWriteResult( | 356 void HandleWriteResult( |
361 const tracked_objects::Location& from_here, | 357 const tracked_objects::Location& from_here, |
362 Status status); | 358 Status status); |
363 | 359 |
364 const base::FilePath path_; | 360 const base::FilePath path_; |
365 scoped_ptr<leveldb::Env> env_; | 361 scoped_ptr<leveldb::Env> env_; |
366 scoped_ptr<leveldb::DB> db_; | 362 scoped_ptr<leveldb::DB> db_; |
367 | 363 |
368 int64 next_avail_registration_id_; | 364 int64 next_avail_registration_id_; |
369 int64 next_avail_resource_id_; | 365 int64 next_avail_resource_id_; |
370 int64 next_avail_version_id_; | |
371 | 366 |
372 enum State { | 367 enum State { |
373 UNINITIALIZED, | 368 UNINITIALIZED, |
374 INITIALIZED, | 369 INITIALIZED, |
375 DISABLED, | 370 DISABLED, |
376 }; | 371 }; |
377 State state_; | 372 State state_; |
378 | 373 |
379 bool IsDatabaseInMemory() const; | 374 bool IsDatabaseInMemory() const; |
380 | 375 |
(...skipping 19 matching lines...) Expand all Loading... |
400 MigrateOnDiskCacheAccess); | 395 MigrateOnDiskCacheAccess); |
401 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, | 396 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, |
402 NotMigrateOnDatabaseAccess); | 397 NotMigrateOnDatabaseAccess); |
403 | 398 |
404 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 399 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
405 }; | 400 }; |
406 | 401 |
407 } // namespace content | 402 } // namespace content |
408 | 403 |
409 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 404 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |