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_
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/sequence_checker.h"
16 #include "base/time/time.h"
17 #include "content/browser/service_worker/service_worker_version.h"
18 #include "content/common/content_export.h"
30 // Class to persist serviceworker registration data in a database.
31 // Should NOT be used on the IO thread since this does blocking
32 // file io. The ServiceWorkerStorage class owns this class and
33 // is responsible for only calling it serially on background
34 // non-IO threads (ala SequencedWorkerPool).
35 class CONTENT_EXPORT ServiceWorkerDatabase {
37 // We do leveldb stuff in |path| or in memory if |path| is empty.
38 explicit ServiceWorkerDatabase(const base::FilePath& path);
39 ~ServiceWorkerDatabase();
41 struct CONTENT_EXPORT RegistrationData {
42 // These values are immutable for the life of a registration.
43 int64 registration_id;
47 // Versions are first stored once they successfully install and become
48 // the waiting version. Then transition to the active version. The stored
49 // version may be in the ACTIVE state or in the INSTALLED state.
52 bool has_fetch_handler;
53 base::Time last_update_check;
55 ServiceWorkerVersion::Status GetVersionStatus() const {
57 return ServiceWorkerVersion::ACTIVE;
58 return ServiceWorkerVersion::INSTALLED;
65 struct ResourceRecord {
70 // For use during initialization.
71 bool GetNextAvailableIds(int64* next_avail_registration_id,
72 int64* next_avail_version_id,
73 int64* next_avail_resource_id);
74 bool GetOriginsWithRegistrations(std::set<GURL>* origins);
76 // For use when first handling a request in an origin with registrations.
77 bool GetRegistrationsForOrigin(const GURL& origin,
78 std::vector<RegistrationData>* registrations);
80 bool GetAllRegistrations(std::vector<RegistrationData>* registrations);
82 // Saving, retrieving, and updating registration data.
83 // (will bump next_avail_xxxx_ids as needed)
84 // (resource ids will be added/removed from the uncommitted/purgeable
87 bool ReadRegistration(int64 registration_id,
89 RegistrationData* registration,
90 std::vector<ResourceRecord>* resources);
91 bool WriteRegistration(const RegistrationData& registration,
92 const std::vector<ResourceRecord>& resources);
94 bool UpdateVersionToActive(int64 registration_id,
96 bool UpdateLastCheckTime(int64 registration_id,
98 const base::Time& time);
99 bool DeleteRegistration(int64 registration_id,
102 // As new resources are put into the diskcache, they go into an uncommitted
103 // list. When a registration is saved that refers to those ids, they're
104 // removed from that list. When a resource no longer has any registrations or
105 // caches referring to it, it's added to the purgeable list. Periodically,
106 // the purgeable list can be purged from the diskcache. At system startup, all
107 // uncommitted ids are moved to the purgeable list.
109 // Reads uncommitted resource ids from the database. Returns true on success.
110 // Otherwise clears |ids| and returns false.
111 bool GetUncommittedResourceIds(std::set<int64>* ids);
113 // Writes |ids| into the database as uncommitted resources. Returns true on
114 // success. Otherwise writes nothing and returns false.
115 bool WriteUncommittedResourceIds(const std::set<int64>& ids);
117 // Deletes uncommitted resource ids specified by |ids| from the database.
118 // Returns true on success. Otherwise deletes nothing and returns false.
119 bool ClearUncommittedResourceIds(const std::set<int64>& ids);
121 // Reads purgeable resource ids from the database. Returns true on success.
122 // Otherwise clears |ids| and returns false.
123 bool GetPurgeableResourceIds(std::set<int64>* ids);
125 // Writes |ids| into the database as purgeable resources. Returns true on
126 // success. Otherwise writes nothing and returns false.
127 bool WritePurgeableResourceIds(const std::set<int64>& ids);
129 // Deletes purgeable resource ids specified by |ids| from the database.
130 // Returns true on success. Otherwise deletes nothing and returns false.
131 bool ClearPurgeableResourceIds(const std::set<int64>& ids);
133 // Delete all data for |origin|, namely, unique origin, registrations and
134 // resource records. Resources are moved to the purgeable list.
135 bool DeleteAllDataForOrigin(const GURL& origin);
137 bool is_disabled() const { return is_disabled_; }
138 bool was_corruption_detected() const { return was_corruption_detected_; }
141 // Opens the database at the |path_|. This is lazily called when the first
142 // database API is called. Returns true if the database was opened. Returns
143 // false if the opening failed or was not neccessary, that is, the database
144 // does not exist and |create_if_needed| is false.
145 bool LazyOpen(bool create_if_needed);
147 bool ReadNextAvailableId(const char* id_key,
148 int64* next_avail_id);
149 bool ReadRegistrationData(int64 registration_id,
151 RegistrationData* registration);
152 bool ReadResourceRecords(int64 version_id,
153 std::vector<ResourceRecord>* resources);
154 bool DeleteResourceRecords(int64 version_id,
155 leveldb::WriteBatch* batch);
156 bool ReadResourceIds(const char* id_key_prefix,
157 std::set<int64>* ids);
158 bool WriteResourceIds(const char* id_key_prefix,
159 const std::set<int64>& ids);
160 bool DeleteResourceIds(const char* id_key_prefix,
161 const std::set<int64>& ids);
163 // Reads the current schema version from the database. If the database hasn't
164 // been written anything yet, sets |db_version| to 0 and returns true.
165 bool ReadDatabaseVersion(int64* db_version);
167 // Write a batch into the database.
168 // NOTE: You must call this when you want to put something into the database
169 // because this initializes the database if needed.
170 bool WriteBatch(leveldb::WriteBatch* batch);
172 // Bumps the next available id if |used_id| is greater than or equal to the
174 void BumpNextRegistrationIdIfNeeded(int64 used_id,
175 leveldb::WriteBatch* batch);
176 void BumpNextVersionIdIfNeeded(int64 used_id,
177 leveldb::WriteBatch* batch);
181 void HandleError(const tracked_objects::Location& from_here,
182 const leveldb::Status& status);
184 base::FilePath path_;
185 scoped_ptr<leveldb::Env> env_;
186 scoped_ptr<leveldb::DB> db_;
188 int64 next_avail_registration_id_;
189 int64 next_avail_resource_id_;
190 int64 next_avail_version_id_;
192 // True if a database error has occurred (e.g. cannot read data).
193 // If true, all database accesses will fail.
196 // True if a database corruption was detected.
197 bool was_corruption_detected_;
199 // True if a database was initialized, that is, the schema version was written
201 bool is_initialized_;
203 base::SequenceChecker sequence_checker_;
205 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase);
206 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory);
207 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion);
208 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds);
209 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Basic);
210 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Overwrite);
211 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Multiple);
213 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase);
216 } // namespace content
218 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_