| 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 21 matching lines...) Expand all Loading... |
| 32 // Should NOT be used on the IO thread since this does blocking | 32 // Should NOT be used on the IO thread since this does blocking |
| 33 // file io. The ServiceWorkerStorage class owns this class and | 33 // file io. The ServiceWorkerStorage class owns this class and |
| 34 // is responsible for only calling it serially on background | 34 // is responsible for only calling it serially on background |
| 35 // non-IO threads (ala SequencedWorkerPool). | 35 // non-IO threads (ala SequencedWorkerPool). |
| 36 class CONTENT_EXPORT ServiceWorkerDatabase { | 36 class CONTENT_EXPORT ServiceWorkerDatabase { |
| 37 public: | 37 public: |
| 38 // We do leveldb stuff in |path| or in memory if |path| is empty. | 38 // We do leveldb stuff in |path| or in memory if |path| is empty. |
| 39 explicit ServiceWorkerDatabase(const base::FilePath& path); | 39 explicit ServiceWorkerDatabase(const base::FilePath& path); |
| 40 ~ServiceWorkerDatabase(); | 40 ~ServiceWorkerDatabase(); |
| 41 | 41 |
| 42 enum Status { |
| 43 STATUS_OK, |
| 44 STATUS_ERROR_NOT_FOUND, |
| 45 STATUS_ERROR_CORRUPTED, |
| 46 STATUS_ERROR_FAILED, |
| 47 }; |
| 48 |
| 42 struct CONTENT_EXPORT RegistrationData { | 49 struct CONTENT_EXPORT RegistrationData { |
| 43 // These values are immutable for the life of a registration. | 50 // These values are immutable for the life of a registration. |
| 44 int64 registration_id; | 51 int64 registration_id; |
| 45 GURL scope; | 52 GURL scope; |
| 46 GURL script; | 53 GURL script; |
| 47 | 54 |
| 48 // Versions are first stored once they successfully install and become | 55 // Versions are first stored once they successfully install and become |
| 49 // the waiting version. Then transition to the active version. The stored | 56 // the waiting version. Then transition to the active version. The stored |
| 50 // version may be in the ACTIVE state or in the INSTALLED state. | 57 // version may be in the ACTIVE state or in the INSTALLED state. |
| 51 int64 version_id; | 58 int64 version_id; |
| 52 bool is_active; | 59 bool is_active; |
| 53 bool has_fetch_handler; | 60 bool has_fetch_handler; |
| 54 base::Time last_update_check; | 61 base::Time last_update_check; |
| 55 | 62 |
| 56 RegistrationData(); | 63 RegistrationData(); |
| 57 ~RegistrationData(); | 64 ~RegistrationData(); |
| 58 }; | 65 }; |
| 59 | 66 |
| 60 struct ResourceRecord { | 67 struct ResourceRecord { |
| 61 int64 resource_id; | 68 int64 resource_id; |
| 62 GURL url; | 69 GURL url; |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 // For use during initialization. | 72 // Reads next available ids from the database. Returns OK if they are |
| 66 ServiceWorkerStatusCode GetNextAvailableIds( | 73 // 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. |
| 75 Status GetNextAvailableIds( |
| 67 int64* next_avail_registration_id, | 76 int64* next_avail_registration_id, |
| 68 int64* next_avail_version_id, | 77 int64* next_avail_version_id, |
| 69 int64* next_avail_resource_id); | 78 int64* next_avail_resource_id); |
| 70 ServiceWorkerStatusCode GetOriginsWithRegistrations( | |
| 71 std::set<GURL>* origins); | |
| 72 | 79 |
| 73 // For use when first handling a request in an origin with registrations. | 80 // Reads origins that have one or more than one registration from the |
| 74 bool GetRegistrationsForOrigin(const GURL& origin, | 81 // database. Returns OK if they are successfully read or not found. |
| 75 std::vector<RegistrationData>* registrations); | 82 // Otherwise, returns an error. |
| 83 Status GetOriginsWithRegistrations(std::set<GURL>* origins); |
| 76 | 84 |
| 77 bool GetAllRegistrations(std::vector<RegistrationData>* registrations); | 85 // Reads registrations for |origin| from the database. Returns OK if they are |
| 86 // successfully read or not found. Otherwise, returns an error. |
| 87 Status GetRegistrationsForOrigin( |
| 88 const GURL& origin, |
| 89 std::vector<RegistrationData>* registrations); |
| 90 |
| 91 // Reads all registrations from the database. Returns OK if successfully read |
| 92 // or not found. Otherwise, returns an error. |
| 93 Status GetAllRegistrations(std::vector<RegistrationData>* registrations); |
| 78 | 94 |
| 79 // Saving, retrieving, and updating registration data. | 95 // Saving, retrieving, and updating registration data. |
| 80 // (will bump next_avail_xxxx_ids as needed) | 96 // (will bump next_avail_xxxx_ids as needed) |
| 81 // (resource ids will be added/removed from the uncommitted/purgeable | 97 // (resource ids will be added/removed from the uncommitted/purgeable |
| 82 // lists as needed) | 98 // lists as needed) |
| 83 | 99 |
| 84 bool ReadRegistration(int64 registration_id, | 100 bool ReadRegistration(int64 registration_id, |
| 85 const GURL& origin, | 101 const GURL& origin, |
| 86 RegistrationData* registration, | 102 RegistrationData* registration, |
| 87 std::vector<ResourceRecord>* resources); | 103 std::vector<ResourceRecord>* resources); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 145 |
| 130 // Delete all data for |origin|, namely, unique origin, registrations and | 146 // Delete all data for |origin|, namely, unique origin, registrations and |
| 131 // resource records. Resources are moved to the purgeable list. | 147 // resource records. Resources are moved to the purgeable list. |
| 132 bool DeleteAllDataForOrigin(const GURL& origin); | 148 bool DeleteAllDataForOrigin(const GURL& origin); |
| 133 | 149 |
| 134 bool is_disabled() const { return is_disabled_; } | 150 bool is_disabled() const { return is_disabled_; } |
| 135 bool was_corruption_detected() const { return was_corruption_detected_; } | 151 bool was_corruption_detected() const { return was_corruption_detected_; } |
| 136 | 152 |
| 137 private: | 153 private: |
| 138 // Opens the database at the |path_|. This is lazily called when the first | 154 // Opens the database at the |path_|. This is lazily called when the first |
| 139 // database API is called. Returns true if the database was opened. Returns | 155 // database API is called. Returns OK if the database is successfully opened. |
| 140 // false if the opening failed or was not neccessary, that is, the database | 156 // Returns NOT_FOUND if the database does not exist and |create_if_missing| is |
| 141 // does not exist and |create_if_needed| is false. | 157 // false. Otherwise, returns an error. |
| 142 bool LazyOpen(bool create_if_needed); | 158 Status LazyOpen(bool create_if_missing); |
| 143 | 159 |
| 144 ServiceWorkerStatusCode ReadNextAvailableId( | 160 // Helper for LazyOpen(). |status| must be the return value from LazyOpen() |
| 161 // and this must be called just after LazyOpen() is called. Returns true if |
| 162 // the database is new or nonexistent, that is, it has never been used. |
| 163 bool IsNewOrNonexistentDatabase(Status status); |
| 164 |
| 165 // Reads the next available id for |id_key|. Returns OK if it's successfully |
| 166 // read. Fills |next_avail_id| with an initial value and returns OK if it's |
| 167 // not found in the database. Otherwise, returns an error. |
| 168 Status ReadNextAvailableId( |
| 145 const char* id_key, | 169 const char* id_key, |
| 146 int64* next_avail_id); | 170 int64* next_avail_id); |
| 171 |
| 147 bool ReadRegistrationData(int64 registration_id, | 172 bool ReadRegistrationData(int64 registration_id, |
| 148 const GURL& origin, | 173 const GURL& origin, |
| 149 RegistrationData* registration); | 174 RegistrationData* registration); |
| 150 bool ReadResourceRecords(int64 version_id, | 175 bool ReadResourceRecords(int64 version_id, |
| 151 std::vector<ResourceRecord>* resources); | 176 std::vector<ResourceRecord>* resources); |
| 152 bool DeleteResourceRecords(int64 version_id, | 177 bool DeleteResourceRecords(int64 version_id, |
| 153 leveldb::WriteBatch* batch); | 178 leveldb::WriteBatch* batch); |
| 154 bool ReadResourceIds(const char* id_key_prefix, | 179 bool ReadResourceIds(const char* id_key_prefix, |
| 155 std::set<int64>* ids); | 180 std::set<int64>* ids); |
| 156 bool WriteResourceIds(const char* id_key_prefix, | 181 bool WriteResourceIds(const char* id_key_prefix, |
| 157 const std::set<int64>& ids); | 182 const std::set<int64>& ids); |
| 158 bool DeleteResourceIds(const char* id_key_prefix, | 183 bool DeleteResourceIds(const char* id_key_prefix, |
| 159 const std::set<int64>& ids); | 184 const std::set<int64>& ids); |
| 160 | 185 |
| 161 // Reads the current schema version from the database. If the database hasn't | 186 // Reads the current schema version from the database. If the database hasn't |
| 162 // been written anything yet, sets |db_version| to 0 and returns true. | 187 // been written anything yet, sets |db_version| to 0 and returns OK. |
| 163 bool ReadDatabaseVersion(int64* db_version); | 188 Status ReadDatabaseVersion(int64* db_version); |
| 164 | 189 |
| 165 // Write a batch into the database. | 190 // Write a batch into the database. |
| 166 // NOTE: You must call this when you want to put something into the database | 191 // NOTE: You must call this when you want to put something into the database |
| 167 // because this initializes the database if needed. | 192 // because this initializes the database if needed. |
| 168 bool WriteBatch(leveldb::WriteBatch* batch); | 193 bool WriteBatch(leveldb::WriteBatch* batch); |
| 169 | 194 |
| 170 // Bumps the next available id if |used_id| is greater than or equal to the | 195 // Bumps the next available id if |used_id| is greater than or equal to the |
| 171 // cached one. | 196 // cached one. |
| 172 void BumpNextRegistrationIdIfNeeded(int64 used_id, | 197 void BumpNextRegistrationIdIfNeeded(int64 used_id, |
| 173 leveldb::WriteBatch* batch); | 198 leveldb::WriteBatch* batch); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 197 // True if a database was initialized, that is, the schema version was written | 222 // True if a database was initialized, that is, the schema version was written |
| 198 // in the database. | 223 // in the database. |
| 199 bool is_initialized_; | 224 bool is_initialized_; |
| 200 | 225 |
| 201 base::SequenceChecker sequence_checker_; | 226 base::SequenceChecker sequence_checker_; |
| 202 | 227 |
| 203 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 228 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
| 204 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 229 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
| 205 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); | 230 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); |
| 206 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 231 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
| 207 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Basic); | |
| 208 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Overwrite); | |
| 209 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Multiple); | |
| 210 | 232 |
| 211 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 233 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
| 212 }; | 234 }; |
| 213 | 235 |
| 214 } // namespace content | 236 } // namespace content |
| 215 | 237 |
| 216 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 238 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
| OLD | NEW |