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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/sequence_checker.h"
18 #include "base/time/time.h"
19 #include "content/common/content_export.h"
20 #include "content/common/service_worker/service_worker_status_code.h"
32 // Class to persist serviceworker registration data in a database.
33 // Should NOT be used on the IO thread since this does blocking
34 // file io. The ServiceWorkerStorage class owns this class and
35 // is responsible for only calling it serially on background
36 // non-IO threads (ala SequencedWorkerPool).
37 class CONTENT_EXPORT ServiceWorkerDatabase
{
39 // We do leveldb stuff in |path| or in memory if |path| is empty.
40 explicit ServiceWorkerDatabase(const base::FilePath
& path
);
41 ~ServiceWorkerDatabase();
43 // Used in UMA. A new value must be appended only.
46 STATUS_ERROR_NOT_FOUND
,
47 STATUS_ERROR_IO_ERROR
,
48 STATUS_ERROR_CORRUPTED
,
52 static const char* StatusToString(Status status
);
54 struct CONTENT_EXPORT RegistrationData
{
55 // These values are immutable for the life of a registration.
56 int64 registration_id
;
59 // Versions are first stored once they successfully install and become
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.
65 bool has_fetch_handler
;
66 base::Time last_update_check
;
68 // Not populated until ServiceWorkerStorage::StoreRegistration is called.
69 int64_t resources_total_size_bytes
;
75 struct ResourceRecord
{
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.
82 ResourceRecord() : resource_id(-1), size_bytes(0) {}
83 ResourceRecord(int64 id
, GURL url
, int64 size_bytes
)
84 : resource_id(id
), url(url
), size_bytes(size_bytes
) {}
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
89 // OK if they are not found in the database. Otherwise, returns an error.
90 Status
GetNextAvailableIds(
91 int64
* next_avail_registration_id
,
92 int64
* next_avail_version_id
,
93 int64
* next_avail_resource_id
);
95 // Reads origins that have one or more than one registration from the
96 // database. Returns OK if they are successfully read or not found.
97 // Otherwise, returns an error.
98 Status
GetOriginsWithRegistrations(std::set
<GURL
>* origins
);
100 // Reads registrations for |origin| from the database. Returns OK if they are
101 // successfully read or not found. Otherwise, returns an error.
102 Status
GetRegistrationsForOrigin(
104 std::vector
<RegistrationData
>* registrations
);
106 // Reads all registrations from the database. Returns OK if successfully read
107 // or not found. Otherwise, returns an error.
108 Status
GetAllRegistrations(std::vector
<RegistrationData
>* registrations
);
110 // Saving, retrieving, and updating registration data.
111 // (will bump next_avail_xxxx_ids as needed)
112 // (resource ids will be added/removed from the uncommitted/purgeable
115 // Reads a registration for |registration_id| and resource records associated
116 // with it from the database. Returns OK if they are successfully read.
117 // Otherwise, returns an error.
118 Status
ReadRegistration(
119 int64 registration_id
,
121 RegistrationData
* registration
,
122 std::vector
<ResourceRecord
>* resources
);
124 // Writes |registration| and |resources| into the database and does following
126 // - If an old version of the registration exists, deletes it and sets
127 // |deleted_version| to the old version registration data object
128 // |newly_purgeable_resources| to its resources. Otherwise, sets
129 // |deleted_version->version_id| to -1.
130 // - Bumps the next registration id and the next version id if needed.
131 // - Removes |resources| from the uncommitted list if exist.
132 // Returns OK they are successfully written. Otherwise, returns an error.
133 Status
WriteRegistration(const RegistrationData
& registration
,
134 const std::vector
<ResourceRecord
>& resources
,
135 RegistrationData
* deleted_version
,
136 std::vector
<int64
>* newly_purgeable_resources
);
138 // Updates a registration for |registration_id| to an active state. Returns OK
139 // if it's successfully updated. Otherwise, returns an error.
140 Status
UpdateVersionToActive(
141 int64 registration_id
,
144 // Updates last check time of a registration for |registration_id| by |time|.
145 // Returns OK if it's successfully updated. Otherwise, returns an error.
146 Status
UpdateLastCheckTime(
147 int64 registration_id
,
149 const base::Time
& time
);
151 // Deletes a registration for |registration_id| and moves resource records
152 // associated with it into the purgeable list. If deletion occurred, sets
153 // |version_id| to the id of the version that was deleted and
154 // |newly_purgeable_resources| to its resources; otherwise, sets |version_id|
155 // to -1. Returns OK if it's successfully deleted or not found in the
156 // database. Otherwise, returns an error.
157 Status
DeleteRegistration(int64 registration_id
,
159 RegistrationData
* deleted_version
,
160 std::vector
<int64
>* newly_purgeable_resources
);
162 // Reads user data for |registration_id| and |user_data_name| from the
164 Status
ReadUserData(int64 registration_id
,
165 const std::string
& user_data_name
,
166 std::string
* user_data
);
168 // Writes |user_data| into the database. Returns NOT_FOUND if the registration
169 // specified by |registration_id| does not exist in the database.
170 Status
WriteUserData(int64 registration_id
,
172 const std::string
& user_data_name
,
173 const std::string
& user_data
);
175 // Deletes user data for |registration_id| and |user_data_name| from the
176 // database. Returns OK if it's successfully deleted or not found in the
178 Status
DeleteUserData(int64 registration_id
,
179 const std::string
& user_data_name
);
181 // As new resources are put into the diskcache, they go into an uncommitted
182 // list. When a registration is saved that refers to those ids, they're
183 // removed from that list. When a resource no longer has any registrations or
184 // caches referring to it, it's added to the purgeable list. Periodically,
185 // the purgeable list can be purged from the diskcache. At system startup, all
186 // uncommitted ids are moved to the purgeable list.
188 // Reads uncommitted resource ids from the database. Returns OK on success.
189 // Otherwise clears |ids| and returns an error.
190 Status
GetUncommittedResourceIds(std::set
<int64
>* ids
);
192 // Writes |ids| into the database as uncommitted resources. Returns OK on
193 // success. Otherwise writes nothing and returns an error.
194 Status
WriteUncommittedResourceIds(const std::set
<int64
>& ids
);
196 // Deletes uncommitted resource ids specified by |ids| from the database.
197 // Returns OK on success. Otherwise deletes nothing and returns an error.
198 Status
ClearUncommittedResourceIds(const std::set
<int64
>& ids
);
200 // Reads purgeable resource ids from the database. Returns OK on success.
201 // Otherwise clears |ids| and returns an error.
202 Status
GetPurgeableResourceIds(std::set
<int64
>* ids
);
204 // Writes |ids| into the database as purgeable resources. Returns OK on
205 // success. Otherwise writes nothing and returns an error.
206 Status
WritePurgeableResourceIds(const std::set
<int64
>& ids
);
208 // Deletes purgeable resource ids specified by |ids| from the database.
209 // Returns OK on success. Otherwise deletes nothing and returns an error.
210 Status
ClearPurgeableResourceIds(const std::set
<int64
>& ids
);
212 // Moves |ids| from the uncommitted list to the purgeable list.
213 // Returns OK on success. Otherwise deletes nothing and returns an error.
214 Status
PurgeUncommittedResourceIds(const std::set
<int64
>& ids
);
216 // Deletes all data for |origins|, namely, unique origin, registrations and
217 // resource records. Resources are moved to the purgeable list. Returns OK if
218 // they are successfully deleted or not found in the database. Otherwise,
220 Status
DeleteAllDataForOrigins(const std::set
<GURL
>& origins
,
221 std::vector
<int64
>* newly_purgeable_resources
);
223 // Completely deletes the contents of the database.
224 // Be careful using this function.
225 Status
DestroyDatabase();
228 // Opens the database at the |path_|. This is lazily called when the first
229 // database API is called. Returns OK if the database is successfully opened.
230 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is
231 // false. Otherwise, returns an error.
232 Status
LazyOpen(bool create_if_missing
);
234 // Helper for LazyOpen(). |status| must be the return value from LazyOpen()
235 // and this must be called just after LazyOpen() is called. Returns true if
236 // the database is new or nonexistent, that is, it has never been used.
237 bool IsNewOrNonexistentDatabase(Status status
);
239 // Reads the next available id for |id_key|. Returns OK if it's successfully
240 // read. Fills |next_avail_id| with an initial value and returns OK if it's
241 // not found in the database. Otherwise, returns an error.
242 Status
ReadNextAvailableId(
244 int64
* next_avail_id
);
246 // Reads registration data for |registration_id| from the database. Returns OK
247 // if successfully reads. Otherwise, returns an error.
248 Status
ReadRegistrationData(
249 int64 registration_id
,
251 RegistrationData
* registration
);
253 // Reads resource records for |version_id| from the database. Returns OK if
254 // it's successfully read or not found in the database. Otherwise, returns an
256 Status
ReadResourceRecords(
258 std::vector
<ResourceRecord
>* resources
);
260 // Deletes resource records for |version_id| from the database. Returns OK if
261 // they are successfully deleted or not found in the database. Otherwise,
263 Status
DeleteResourceRecords(
265 std::vector
<int64
>* newly_purgeable_resources
,
266 leveldb::WriteBatch
* batch
);
268 // Reads resource ids for |id_key_prefix| from the database. Returns OK if
269 // it's successfully read or not found in the database. Otherwise, returns an
271 Status
ReadResourceIds(
272 const char* id_key_prefix
,
273 std::set
<int64
>* ids
);
275 // Write resource ids for |id_key_prefix| into the database. Returns OK on
276 // success. Otherwise, returns writes nothing and returns an error.
277 Status
WriteResourceIds(
278 const char* id_key_prefix
,
279 const std::set
<int64
>& ids
);
280 Status
WriteResourceIdsInBatch(
281 const char* id_key_prefix
,
282 const std::set
<int64
>& ids
,
283 leveldb::WriteBatch
* batch
);
285 // Deletes resource ids for |id_key_prefix| from the database. Returns OK if
286 // it's successfully deleted or not found in the database. Otherwise, returns
288 Status
DeleteResourceIds(
289 const char* id_key_prefix
,
290 const std::set
<int64
>& ids
);
291 Status
DeleteResourceIdsInBatch(
292 const char* id_key_prefix
,
293 const std::set
<int64
>& ids
,
294 leveldb::WriteBatch
* batch
);
296 // Deletes all user data for |registration_id| from the database. Returns OK
297 // if they are successfully deleted or not found in the database.
298 Status
DeleteUserDataForRegistration(
299 int64 registration_id
,
300 leveldb::WriteBatch
* batch
);
302 // Reads the current schema version from the database. If the database hasn't
303 // been written anything yet, sets |db_version| to 0 and returns OK.
304 Status
ReadDatabaseVersion(int64
* db_version
);
306 // Writes a batch into the database.
307 // NOTE: You must call this when you want to put something into the database
308 // because this initializes the database if needed.
309 Status
WriteBatch(leveldb::WriteBatch
* batch
);
311 // Bumps the next available id if |used_id| is greater than or equal to the
313 void BumpNextRegistrationIdIfNeeded(
315 leveldb::WriteBatch
* batch
);
316 void BumpNextResourceIdIfNeeded(
318 leveldb::WriteBatch
* batch
);
319 void BumpNextVersionIdIfNeeded(
321 leveldb::WriteBatch
* batch
);
326 const tracked_objects::Location
& from_here
,
328 void HandleOpenResult(
329 const tracked_objects::Location
& from_here
,
331 void HandleReadResult(
332 const tracked_objects::Location
& from_here
,
334 void HandleWriteResult(
335 const tracked_objects::Location
& from_here
,
338 base::FilePath path_
;
339 scoped_ptr
<leveldb::Env
> env_
;
340 scoped_ptr
<leveldb::DB
> db_
;
342 int64 next_avail_registration_id_
;
343 int64 next_avail_resource_id_
;
344 int64 next_avail_version_id_
;
353 base::SequenceChecker sequence_checker_
;
355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest
, OpenDatabase
);
356 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest
, OpenDatabase_InMemory
);
357 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest
, DatabaseVersion
);
358 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest
, GetNextAvailableIds
);
359 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest
, DestroyDatabase
);
361 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase
);
364 } // namespace content
366 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_