| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| 7 | 7 |
| 8 #include <deque> |
| 8 #include <map> | 9 #include <map> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "content/browser/service_worker/service_worker_database.h" | 18 #include "content/browser/service_worker/service_worker_database.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 46 typedef base::Callback<void(ServiceWorkerStatusCode status, | 47 typedef base::Callback<void(ServiceWorkerStatusCode status, |
| 47 const scoped_refptr<ServiceWorkerRegistration>& | 48 const scoped_refptr<ServiceWorkerRegistration>& |
| 48 registration)> FindRegistrationCallback; | 49 registration)> FindRegistrationCallback; |
| 49 typedef base::Callback< | 50 typedef base::Callback< |
| 50 void(const std::vector<ServiceWorkerRegistrationInfo>& registrations)> | 51 void(const std::vector<ServiceWorkerRegistrationInfo>& registrations)> |
| 51 GetAllRegistrationInfosCallback; | 52 GetAllRegistrationInfosCallback; |
| 52 typedef base::Callback< | 53 typedef base::Callback< |
| 53 void(ServiceWorkerStatusCode status, int result)> | 54 void(ServiceWorkerStatusCode status, int result)> |
| 54 CompareCallback; | 55 CompareCallback; |
| 55 | 56 |
| 56 struct InitialData { | |
| 57 int64 next_registration_id; | |
| 58 int64 next_version_id; | |
| 59 int64 next_resource_id; | |
| 60 std::set<GURL> origins; | |
| 61 | |
| 62 InitialData(); | |
| 63 ~InitialData(); | |
| 64 }; | |
| 65 | |
| 66 ServiceWorkerStorage(const base::FilePath& path, | 57 ServiceWorkerStorage(const base::FilePath& path, |
| 67 base::WeakPtr<ServiceWorkerContextCore> context, | 58 base::WeakPtr<ServiceWorkerContextCore> context, |
| 68 base::SequencedTaskRunner* database_task_runner, | 59 base::SequencedTaskRunner* database_task_runner, |
| 69 base::MessageLoopProxy* disk_cache_thread, | 60 base::MessageLoopProxy* disk_cache_thread, |
| 70 quota::QuotaManagerProxy* quota_manager_proxy); | 61 quota::QuotaManagerProxy* quota_manager_proxy); |
| 71 ~ServiceWorkerStorage(); | 62 ~ServiceWorkerStorage(); |
| 72 | 63 |
| 73 // Finds registration for |document_url| or |pattern| or |registration_id|. | 64 // Finds registration for |document_url| or |pattern| or |registration_id|. |
| 74 // The Find methods will find stored and initially installing registrations. | 65 // The Find methods will find stored and initially installing registrations. |
| 75 // Returns SERVICE_WORKER_OK with non-null registration if registration | 66 // Returns SERVICE_WORKER_OK with non-null registration if registration |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 int64 NewResourceId(); | 114 int64 NewResourceId(); |
| 124 | 115 |
| 125 // Intended for use only by ServiceWorkerRegisterJob. | 116 // Intended for use only by ServiceWorkerRegisterJob. |
| 126 void NotifyInstallingRegistration( | 117 void NotifyInstallingRegistration( |
| 127 ServiceWorkerRegistration* registration); | 118 ServiceWorkerRegistration* registration); |
| 128 void NotifyDoneInstallingRegistration( | 119 void NotifyDoneInstallingRegistration( |
| 129 ServiceWorkerRegistration* registration); | 120 ServiceWorkerRegistration* registration); |
| 130 | 121 |
| 131 private: | 122 private: |
| 132 friend class ServiceWorkerStorageTest; | 123 friend class ServiceWorkerStorageTest; |
| 124 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStorageTest, |
| 125 ResourceIdsAreStoredAndPurged); |
| 126 |
| 127 struct InitialData { |
| 128 int64 next_registration_id; |
| 129 int64 next_version_id; |
| 130 int64 next_resource_id; |
| 131 std::set<GURL> origins; |
| 132 |
| 133 InitialData(); |
| 134 ~InitialData(); |
| 135 }; |
| 133 | 136 |
| 134 typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList; | 137 typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList; |
| 135 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> ResourceList; | 138 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> ResourceList; |
| 139 typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> > |
| 140 RegistrationRefsById; |
| 141 typedef base::Callback<void( |
| 142 InitialData* data, |
| 143 ServiceWorkerDatabase::Status status)> InitializeCallback; |
| 144 typedef base::Callback<void( |
| 145 const GURL& origin, |
| 146 const std::vector<int64>& newly_purgeable_resources, |
| 147 ServiceWorkerDatabase::Status status)> WriteRegistrationCallback; |
| 148 typedef base::Callback<void( |
| 149 bool origin_is_deletable, |
| 150 const std::vector<int64>& newly_purgeable_resources, |
| 151 ServiceWorkerDatabase::Status status)> DeleteRegistrationCallback; |
| 152 typedef base::Callback<void( |
| 153 const ServiceWorkerDatabase::RegistrationData& data, |
| 154 const ResourceList& resources, |
| 155 ServiceWorkerDatabase::Status status)> FindInDBCallback; |
| 156 |
| 157 base::FilePath GetDatabasePath(); |
| 158 base::FilePath GetDiskCachePath(); |
| 136 | 159 |
| 137 bool LazyInitialize( | 160 bool LazyInitialize( |
| 138 const base::Closure& callback); | 161 const base::Closure& callback); |
| 139 void DidReadInitialData( | 162 void DidReadInitialData( |
| 140 InitialData* data, | 163 InitialData* data, |
| 141 ServiceWorkerDatabase::Status status); | 164 ServiceWorkerDatabase::Status status); |
| 142 void DidGetRegistrationsForPattern( | 165 void DidFindRegistrationForDocument( |
| 166 const GURL& document_url, |
| 167 const FindRegistrationCallback& callback, |
| 168 const ServiceWorkerDatabase::RegistrationData& data, |
| 169 const ResourceList& resources, |
| 170 ServiceWorkerDatabase::Status status); |
| 171 void DidFindRegistrationForPattern( |
| 143 const GURL& scope, | 172 const GURL& scope, |
| 144 const FindRegistrationCallback& callback, | 173 const FindRegistrationCallback& callback, |
| 145 RegistrationList* registrations, | 174 const ServiceWorkerDatabase::RegistrationData& data, |
| 175 const ResourceList& resources, |
| 146 ServiceWorkerDatabase::Status status); | 176 ServiceWorkerDatabase::Status status); |
| 147 void DidGetRegistrationsForDocument( | 177 void DidFindRegistrationForId( |
| 148 const GURL& scope, | |
| 149 const FindRegistrationCallback& callback, | 178 const FindRegistrationCallback& callback, |
| 150 RegistrationList* registrations, | 179 const ServiceWorkerDatabase::RegistrationData& data, |
| 151 ServiceWorkerDatabase::Status status); | 180 const ResourceList& resources, |
| 152 void DidReadRegistrationForId( | |
| 153 const FindRegistrationCallback& callback, | |
| 154 ServiceWorkerDatabase::RegistrationData* registration, | |
| 155 ResourceList* resources, | |
| 156 ServiceWorkerDatabase::Status status); | 181 ServiceWorkerDatabase::Status status); |
| 157 void DidGetAllRegistrations( | 182 void DidGetAllRegistrations( |
| 158 const GetAllRegistrationInfosCallback& callback, | 183 const GetAllRegistrationInfosCallback& callback, |
| 159 RegistrationList* registrations, | 184 RegistrationList* registrations, |
| 160 ServiceWorkerDatabase::Status status); | 185 ServiceWorkerDatabase::Status status); |
| 161 void DidStoreRegistration( | 186 void DidStoreRegistration( |
| 187 const StatusCallback& callback, |
| 162 const GURL& origin, | 188 const GURL& origin, |
| 163 const StatusCallback& callback, | 189 const std::vector<int64>& newly_purgeable_resources, |
| 164 ServiceWorkerDatabase::Status status); | 190 ServiceWorkerDatabase::Status status); |
| 165 void DidUpdateToActiveState( | 191 void DidUpdateToActiveState( |
| 166 const StatusCallback& callback, | 192 const StatusCallback& callback, |
| 167 ServiceWorkerDatabase::Status status); | 193 ServiceWorkerDatabase::Status status); |
| 168 void DidDeleteRegistration( | 194 void DidDeleteRegistration( |
| 169 const GURL& origin, | 195 const GURL& origin, |
| 170 const StatusCallback& callback, | 196 const StatusCallback& callback, |
| 171 bool origin_is_deletable, | 197 bool origin_is_deletable, |
| 198 const std::vector<int64>& newly_purgeable_resources, |
| 172 ServiceWorkerDatabase::Status status); | 199 ServiceWorkerDatabase::Status status); |
| 173 | 200 |
| 174 scoped_refptr<ServiceWorkerRegistration> CreateRegistration( | 201 scoped_refptr<ServiceWorkerRegistration> GetOrCreateRegistration( |
| 175 const ServiceWorkerDatabase::RegistrationData& data); | 202 const ServiceWorkerDatabase::RegistrationData& data, |
| 203 const ResourceList& resources); |
| 176 ServiceWorkerRegistration* FindInstallingRegistrationForDocument( | 204 ServiceWorkerRegistration* FindInstallingRegistrationForDocument( |
| 177 const GURL& document_url); | 205 const GURL& document_url); |
| 178 ServiceWorkerRegistration* FindInstallingRegistrationForPattern( | 206 ServiceWorkerRegistration* FindInstallingRegistrationForPattern( |
| 179 const GURL& scope); | 207 const GURL& scope); |
| 180 ServiceWorkerRegistration* FindInstallingRegistrationForId( | 208 ServiceWorkerRegistration* FindInstallingRegistrationForId( |
| 181 int64 registration_id); | 209 int64 registration_id); |
| 182 | 210 |
| 183 // For finding registrations being installed. | |
| 184 typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> > | |
| 185 RegistrationRefsById; | |
| 186 RegistrationRefsById installing_registrations_; | |
| 187 | |
| 188 // Lazy disk_cache getter. | 211 // Lazy disk_cache getter. |
| 189 ServiceWorkerDiskCache* disk_cache(); | 212 ServiceWorkerDiskCache* disk_cache(); |
| 213 void OnDiskCacheInitialized(int rv); |
| 214 |
| 215 void StartPurgingResources(const std::vector<int64>& ids); |
| 216 void PurgeResource(int64 id); |
| 217 void OnResourcePurged(int64 id, int rv); |
| 218 |
| 219 // Static cross-thread helpers. |
| 220 static void ReadInitialDataFromDB( |
| 221 ServiceWorkerDatabase* database, |
| 222 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 223 const InitializeCallback& callback); |
| 224 static void DeleteRegistrationFromDB( |
| 225 ServiceWorkerDatabase* database, |
| 226 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 227 int64 registration_id, |
| 228 const GURL& origin, |
| 229 const DeleteRegistrationCallback& callback); |
| 230 static void WriteRegistrationInDB( |
| 231 ServiceWorkerDatabase* database, |
| 232 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 233 const ServiceWorkerDatabase::RegistrationData& registration, |
| 234 const ResourceList& resources, |
| 235 const WriteRegistrationCallback& callback); |
| 236 static void FindForDocumentInDB( |
| 237 ServiceWorkerDatabase* database, |
| 238 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 239 const GURL& document_url, |
| 240 const FindInDBCallback& callback); |
| 241 static void FindForPatternInDB( |
| 242 ServiceWorkerDatabase* database, |
| 243 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 244 const GURL& scope, |
| 245 const FindInDBCallback& callback); |
| 246 static void FindForIdInDB( |
| 247 ServiceWorkerDatabase* database, |
| 248 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 249 int64 registration_id, |
| 250 const GURL& origin, |
| 251 const FindInDBCallback& callback); |
| 252 |
| 253 // For finding registrations being installed. |
| 254 RegistrationRefsById installing_registrations_; |
| 190 | 255 |
| 191 // Origins having registations. | 256 // Origins having registations. |
| 192 std::set<GURL> registered_origins_; | 257 std::set<GURL> registered_origins_; |
| 193 | 258 |
| 194 // Pending database tasks waiting for initialization. | 259 // Pending database tasks waiting for initialization. |
| 195 std::vector<base::Closure> pending_tasks_; | 260 std::vector<base::Closure> pending_tasks_; |
| 196 | 261 |
| 197 int64 next_registration_id_; | 262 int64 next_registration_id_; |
| 198 int64 next_version_id_; | 263 int64 next_version_id_; |
| 199 int64 next_resource_id_; | 264 int64 next_resource_id_; |
| 200 | 265 |
| 201 enum State { | 266 enum State { |
| 202 UNINITIALIZED, | 267 UNINITIALIZED, |
| 203 INITIALIZING, | 268 INITIALIZING, |
| 204 INITIALIZED, | 269 INITIALIZED, |
| 205 DISABLED, | 270 DISABLED, |
| 206 }; | 271 }; |
| 207 State state_; | 272 State state_; |
| 208 | 273 |
| 209 base::FilePath path_; | 274 base::FilePath path_; |
| 210 base::WeakPtr<ServiceWorkerContextCore> context_; | 275 base::WeakPtr<ServiceWorkerContextCore> context_; |
| 211 | 276 |
| 212 // Only accessed on |database_task_runner_|. | 277 // Only accessed on |database_task_runner_|. |
| 213 scoped_ptr<ServiceWorkerDatabase> database_; | 278 scoped_ptr<ServiceWorkerDatabase> database_; |
| 214 | 279 |
| 215 scoped_refptr<base::SequencedTaskRunner> database_task_runner_; | 280 scoped_refptr<base::SequencedTaskRunner> database_task_runner_; |
| 216 scoped_refptr<base::MessageLoopProxy> disk_cache_thread_; | 281 scoped_refptr<base::MessageLoopProxy> disk_cache_thread_; |
| 217 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; | 282 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; |
| 218 scoped_ptr<ServiceWorkerDiskCache> disk_cache_; | 283 scoped_ptr<ServiceWorkerDiskCache> disk_cache_; |
| 284 std::deque<int64> purgeable_reource_ids_; |
| 285 bool is_purge_pending_; |
| 219 | 286 |
| 220 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; | 287 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; |
| 221 | 288 |
| 222 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); | 289 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); |
| 223 }; | 290 }; |
| 224 | 291 |
| 225 } // namespace content | 292 } // namespace content |
| 226 | 293 |
| 227 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ | 294 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| OLD | NEW |