| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 RegistrationData(); | 61 RegistrationData(); |
| 62 ~RegistrationData(); | 62 ~RegistrationData(); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 struct ResourceRecord { | 65 struct ResourceRecord { |
| 66 int64 resource_id; | 66 int64 resource_id; |
| 67 GURL url; | 67 GURL url; |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // For use during initialization. | 70 // Reads next available ids from the database. Returns OK if they are |
| 71 // successfully read. Fills the arguments with an initial value and returns |
| 72 // OK if they are not found in the database. Otherwise, returns an error. |
| 71 ServiceWorkerStatusCode GetNextAvailableIds( | 73 ServiceWorkerStatusCode GetNextAvailableIds( |
| 72 int64* next_avail_registration_id, | 74 int64* next_avail_registration_id, |
| 73 int64* next_avail_version_id, | 75 int64* next_avail_version_id, |
| 74 int64* next_avail_resource_id); | 76 int64* next_avail_resource_id); |
| 77 |
| 78 // Reads origins that have one or more than one registration from the |
| 79 // database. Returns OK if they are successfully read or not found. |
| 80 // Otherwise, returns an error. |
| 75 ServiceWorkerStatusCode GetOriginsWithRegistrations( | 81 ServiceWorkerStatusCode GetOriginsWithRegistrations( |
| 76 std::set<GURL>* origins); | 82 std::set<GURL>* origins); |
| 77 | 83 |
| 78 // For use when first handling a request in an origin with registrations. | 84 // Reads registrations for |origin| from the database. Returns OK if they are |
| 79 bool GetRegistrationsForOrigin(const GURL& origin, | 85 // successfully read or not found. Otherwise, returns an error. |
| 80 std::vector<RegistrationData>* registrations); | 86 ServiceWorkerStatusCode GetRegistrationsForOrigin( |
| 87 const GURL& origin, |
| 88 std::vector<RegistrationData>* registrations); |
| 81 | 89 |
| 82 bool GetAllRegistrations(std::vector<RegistrationData>* registrations); | 90 // Reads all registrations from the database. Returns OK if successfully read |
| 91 // or not found. Otherwise, returns an error. |
| 92 ServiceWorkerStatusCode GetAllRegistrations( |
| 93 std::vector<RegistrationData>* registrations); |
| 83 | 94 |
| 84 // Saving, retrieving, and updating registration data. | 95 // Saving, retrieving, and updating registration data. |
| 85 // (will bump next_avail_xxxx_ids as needed) | 96 // (will bump next_avail_xxxx_ids as needed) |
| 86 // (resource ids will be added/removed from the uncommitted/purgeable | 97 // (resource ids will be added/removed from the uncommitted/purgeable |
| 87 // lists as needed) | 98 // lists as needed) |
| 88 | 99 |
| 89 bool ReadRegistration(int64 registration_id, | 100 bool ReadRegistration(int64 registration_id, |
| 90 const GURL& origin, | 101 const GURL& origin, |
| 91 RegistrationData* registration, | 102 RegistrationData* registration, |
| 92 std::vector<ResourceRecord>* resources); | 103 std::vector<ResourceRecord>* resources); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 145 |
| 135 // Delete all data for |origin|, namely, unique origin, registrations and | 146 // Delete all data for |origin|, namely, unique origin, registrations and |
| 136 // resource records. Resources are moved to the purgeable list. | 147 // resource records. Resources are moved to the purgeable list. |
| 137 bool DeleteAllDataForOrigin(const GURL& origin); | 148 bool DeleteAllDataForOrigin(const GURL& origin); |
| 138 | 149 |
| 139 bool is_disabled() const { return is_disabled_; } | 150 bool is_disabled() const { return is_disabled_; } |
| 140 bool was_corruption_detected() const { return was_corruption_detected_; } | 151 bool was_corruption_detected() const { return was_corruption_detected_; } |
| 141 | 152 |
| 142 private: | 153 private: |
| 143 // 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 |
| 144 // 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. |
| 145 // 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 |
| 146 // does not exist and |create_if_needed| is false. | 157 // false. Otherwise, returns an error. |
| 147 bool LazyOpen(bool create_if_needed); | 158 ServiceWorkerStatusCode LazyOpen(bool create_if_missing); |
| 148 | 159 |
| 160 // Reads the next available id for |id_key|. Returns OK if it's successfully |
| 161 // read. Fills |next_avail_id| with an initial value and returns OK if it's |
| 162 // not found in the database. Otherwise, returns an error. |
| 149 ServiceWorkerStatusCode ReadNextAvailableId( | 163 ServiceWorkerStatusCode ReadNextAvailableId( |
| 150 const char* id_key, | 164 const char* id_key, |
| 151 int64* next_avail_id); | 165 int64* next_avail_id); |
| 166 |
| 152 bool ReadRegistrationData(int64 registration_id, | 167 bool ReadRegistrationData(int64 registration_id, |
| 153 const GURL& origin, | 168 const GURL& origin, |
| 154 RegistrationData* registration); | 169 RegistrationData* registration); |
| 155 bool ReadResourceRecords(int64 version_id, | 170 bool ReadResourceRecords(int64 version_id, |
| 156 std::vector<ResourceRecord>* resources); | 171 std::vector<ResourceRecord>* resources); |
| 157 bool DeleteResourceRecords(int64 version_id, | 172 bool DeleteResourceRecords(int64 version_id, |
| 158 leveldb::WriteBatch* batch); | 173 leveldb::WriteBatch* batch); |
| 159 bool ReadResourceIds(const char* id_key_prefix, | 174 bool ReadResourceIds(const char* id_key_prefix, |
| 160 std::set<int64>* ids); | 175 std::set<int64>* ids); |
| 161 bool WriteResourceIds(const char* id_key_prefix, | 176 bool WriteResourceIds(const char* id_key_prefix, |
| 162 const std::set<int64>& ids); | 177 const std::set<int64>& ids); |
| 163 bool DeleteResourceIds(const char* id_key_prefix, | 178 bool DeleteResourceIds(const char* id_key_prefix, |
| 164 const std::set<int64>& ids); | 179 const std::set<int64>& ids); |
| 165 | 180 |
| 166 // Reads the current schema version from the database. If the database hasn't | 181 // Reads the current schema version from the database. If the database hasn't |
| 167 // been written anything yet, sets |db_version| to 0 and returns true. | 182 // been written anything yet, sets |db_version| to 0 and returns OK. |
| 168 bool ReadDatabaseVersion(int64* db_version); | 183 ServiceWorkerStatusCode ReadDatabaseVersion(int64* db_version); |
| 169 | 184 |
| 170 // Write a batch into the database. | 185 // Write a batch into the database. |
| 171 // NOTE: You must call this when you want to put something into the database | 186 // NOTE: You must call this when you want to put something into the database |
| 172 // because this initializes the database if needed. | 187 // because this initializes the database if needed. |
| 173 bool WriteBatch(leveldb::WriteBatch* batch); | 188 bool WriteBatch(leveldb::WriteBatch* batch); |
| 174 | 189 |
| 175 // Bumps the next available id if |used_id| is greater than or equal to the | 190 // Bumps the next available id if |used_id| is greater than or equal to the |
| 176 // cached one. | 191 // cached one. |
| 177 void BumpNextRegistrationIdIfNeeded(int64 used_id, | 192 void BumpNextRegistrationIdIfNeeded(int64 used_id, |
| 178 leveldb::WriteBatch* batch); | 193 leveldb::WriteBatch* batch); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 202 // True if a database was initialized, that is, the schema version was written | 217 // True if a database was initialized, that is, the schema version was written |
| 203 // in the database. | 218 // in the database. |
| 204 bool is_initialized_; | 219 bool is_initialized_; |
| 205 | 220 |
| 206 base::SequenceChecker sequence_checker_; | 221 base::SequenceChecker sequence_checker_; |
| 207 | 222 |
| 208 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 223 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
| 209 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 224 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
| 210 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); | 225 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, DatabaseVersion); |
| 211 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 226 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
| 212 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Basic); | |
| 213 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Overwrite); | |
| 214 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, Registration_Multiple); | |
| 215 | 227 |
| 216 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 228 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
| 217 }; | 229 }; |
| 218 | 230 |
| 219 } // namespace content | 231 } // namespace content |
| 220 | 232 |
| 221 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 233 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
| OLD | NEW |