Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_database.h
bloba2336dbc1fa3c33e67d9e11d96a87e4583029eaf
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_
8 #include <map>
9 #include <set>
10 #include <vector>
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"
19 #include "url/gurl.h"
21 namespace leveldb {
22 class DB;
23 class Env;
24 class Status;
25 class WriteBatch;
28 namespace content {
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 {
36 public:
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;
44 GURL scope;
45 GURL script;
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.
50 int64 version_id;
51 bool is_active;
52 bool has_fetch_handler;
53 base::Time last_update_check;
55 ServiceWorkerVersion::Status GetVersionStatus() const {
56 if (is_active)
57 return ServiceWorkerVersion::ACTIVE;
58 return ServiceWorkerVersion::INSTALLED;
61 RegistrationData();
62 ~RegistrationData();
65 struct ResourceRecord {
66 int64 resource_id;
67 GURL url;
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 // Saving, retrieving, and updating registration data.
81 // (will bump next_avail_xxxx_ids as needed)
82 // (resource ids will be added/removed from the uncommitted/purgeable
83 // lists as needed)
85 bool ReadRegistration(int64 registration_id,
86 const GURL& origin,
87 RegistrationData* registration,
88 std::vector<ResourceRecord>* resources);
89 bool WriteRegistration(const RegistrationData& registration,
90 const std::vector<ResourceRecord>& resources);
92 bool UpdateVersionToActive(int64 registration_id,
93 const GURL& origin);
94 bool UpdateLastCheckTime(int64 registration_id,
95 const GURL& origin,
96 const base::Time& time);
97 bool DeleteRegistration(int64 registration_id,
98 const GURL& origin);
100 // As new resources are put into the diskcache, they go into an uncommitted
101 // list. When a registration is saved that refers to those ids, they're
102 // removed from that list. When a resource no longer has any registrations or
103 // caches referring to it, it's added to the purgeable list. Periodically,
104 // the purgeable list can be purged from the diskcache. At system startup, all
105 // uncommitted ids are moved to the purgeable list.
107 // Reads uncommitted resource ids from the database. Returns true on success.
108 // Otherwise clears |ids| and returns false.
109 bool GetUncommittedResourceIds(std::set<int64>* ids);
111 // Writes |ids| into the database as uncommitted resources. Returns true on
112 // success. Otherwise writes nothing and returns false.
113 bool WriteUncommittedResourceIds(const std::set<int64>& ids);
115 // Deletes uncommitted resource ids specified by |ids| from the database.
116 // Returns true on success. Otherwise deletes nothing and returns false.
117 bool ClearUncommittedResourceIds(const std::set<int64>& ids);
119 // Reads purgeable resource ids from the database. Returns true on success.
120 // Otherwise clears |ids| and returns false.
121 bool GetPurgeableResourceIds(std::set<int64>* ids);
123 // Writes |ids| into the database as purgeable resources. Returns true on
124 // success. Otherwise writes nothing and returns false.
125 bool WritePurgeableResourceIds(const std::set<int64>& ids);
127 // Deletes purgeable resource ids specified by |ids| from the database.
128 // Returns true on success. Otherwise deletes nothing and returns false.
129 bool ClearPurgeableResourceIds(const std::set<int64>& ids);
131 bool is_disabled() const { return is_disabled_; }
132 bool was_corruption_detected() const { return was_corruption_detected_; }
134 private:
135 // Opens the database at the |path_|. This is lazily called when the first
136 // database API is called. Returns true if the database was opened. Returns
137 // false if the opening failed or was not neccessary, that is, the database
138 // does not exist and |create_if_needed| is false.
139 bool LazyOpen(bool create_if_needed);
141 bool ReadNextAvailableId(const char* id_key,
142 int64* next_avail_id);
143 bool ReadRegistrationData(int64 registration_id,
144 const GURL& origin,
145 RegistrationData* registration);
146 bool ReadResourceRecords(int64 version_id,
147 std::vector<ResourceRecord>* resources);
148 bool DeleteResourceRecords(int64 version_id,
149 leveldb::WriteBatch* batch);
150 bool ReadResourceIds(const char* id_key_prefix,
151 std::set<int64>* ids);
152 bool WriteResourceIds(const char* id_key_prefix,
153 const std::set<int64>& ids);
154 bool DeleteResourceIds(const char* id_key_prefix,
155 const std::set<int64>& ids);
157 // Reads the current schema version from the database. If the database hasn't
158 // been written anything yet, sets |db_version| to 0 and returns true.
159 bool ReadDatabaseVersion(int64* db_version);
161 // Write a batch into the database.
162 // NOTE: You must call this when you want to put something into the database
163 // because this initializes the database if needed.
164 bool WriteBatch(leveldb::WriteBatch* batch);
166 // Bumps the next available id if |used_id| is greater than or equal to the
167 // cached one.
168 void BumpNextRegistrationIdIfNeeded(int64 used_id,
169 leveldb::WriteBatch* batch);
170 void BumpNextVersionIdIfNeeded(int64 used_id,
171 leveldb::WriteBatch* batch);
173 bool IsOpen();
175 void HandleError(const tracked_objects::Location& from_here,
176 const leveldb::Status& status);
178 base::FilePath path_;
179 scoped_ptr<leveldb::Env> env_;
180 scoped_ptr<leveldb::DB> db_;
182 int64 next_avail_registration_id_;
183 int64 next_avail_resource_id_;
184 int64 next_avail_version_id_;
186 // True if a database error has occurred (e.g. cannot read data).
187 // If true, all database accesses will fail.
188 bool is_disabled_;
190 // True if a database corruption was detected.
191 bool was_corruption_detected_;
193 // True if a database was initialized, that is, the schema version was written
194 // in the database.
195 bool is_initialized_;
197 base::SequenceChecker sequence_checker_;
199 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase);
200 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory);
201 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion);
202 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds);
203 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Basic);
204 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Overwrite);
205 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Multiple);
207 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase);
210 } // namespace content
212 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_