| 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 <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "content/browser/service_worker/service_worker_database.h" | 19 #include "content/browser/service_worker/service_worker_database.h" |
| 20 #include "content/browser/service_worker/service_worker_database_task_manager.h" | 20 #include "content/browser/service_worker/service_worker_database_task_manager.h" |
| 21 #include "content/browser/service_worker/service_worker_metrics.h" |
| 21 #include "content/browser/service_worker/service_worker_version.h" | 22 #include "content/browser/service_worker/service_worker_version.h" |
| 22 #include "content/common/content_export.h" | 23 #include "content/common/content_export.h" |
| 23 #include "content/common/service_worker/service_worker_status_code.h" | 24 #include "content/common/service_worker/service_worker_status_code.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 class SequencedTaskRunner; | 28 class SequencedTaskRunner; |
| 28 class SingleThreadTaskRunner; | 29 class SingleThreadTaskRunner; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace storage { | 32 namespace storage { |
| 32 class QuotaManagerProxy; | 33 class QuotaManagerProxy; |
| 33 class SpecialStoragePolicy; | 34 class SpecialStoragePolicy; |
| 34 } | 35 } |
| 35 | 36 |
| 36 namespace content { | 37 namespace content { |
| 37 | 38 |
| 38 class ServiceWorkerContextCore; | 39 class ServiceWorkerContextCore; |
| 39 class ServiceWorkerDiskCache; | 40 class ServiceWorkerDiskCache; |
| 41 class ServiceWorkerDiskCacheMigrator; |
| 40 class ServiceWorkerRegistration; | 42 class ServiceWorkerRegistration; |
| 41 class ServiceWorkerResponseMetadataWriter; | 43 class ServiceWorkerResponseMetadataWriter; |
| 42 class ServiceWorkerResponseReader; | 44 class ServiceWorkerResponseReader; |
| 43 class ServiceWorkerResponseWriter; | 45 class ServiceWorkerResponseWriter; |
| 44 struct ServiceWorkerRegistrationInfo; | 46 struct ServiceWorkerRegistrationInfo; |
| 45 | 47 |
| 46 // This class provides an interface to store and retrieve ServiceWorker | 48 // This class provides an interface to store and retrieve ServiceWorker |
| 47 // registration data. | 49 // registration data. |
| 48 class CONTENT_EXPORT ServiceWorkerStorage | 50 class CONTENT_EXPORT ServiceWorkerStorage |
| 49 : NON_EXPORTED_BASE(public ServiceWorkerVersion::Listener) { | 51 : NON_EXPORTED_BASE(public ServiceWorkerVersion::Listener) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 208 |
| 207 // |resources| must already be on the purgeable list. | 209 // |resources| must already be on the purgeable list. |
| 208 void PurgeResources(const ResourceList& resources); | 210 void PurgeResources(const ResourceList& resources); |
| 209 | 211 |
| 210 private: | 212 private: |
| 211 friend class ServiceWorkerHandleTest; | 213 friend class ServiceWorkerHandleTest; |
| 212 friend class ServiceWorkerStorageTest; | 214 friend class ServiceWorkerStorageTest; |
| 213 friend class ServiceWorkerResourceStorageTest; | 215 friend class ServiceWorkerResourceStorageTest; |
| 214 friend class ServiceWorkerControlleeRequestHandlerTest; | 216 friend class ServiceWorkerControlleeRequestHandlerTest; |
| 215 friend class ServiceWorkerContextRequestHandlerTest; | 217 friend class ServiceWorkerContextRequestHandlerTest; |
| 218 friend class ServiceWorkerDiskCacheMigratorTest; |
| 216 friend class ServiceWorkerRequestHandlerTest; | 219 friend class ServiceWorkerRequestHandlerTest; |
| 217 friend class ServiceWorkerURLRequestJobTest; | 220 friend class ServiceWorkerURLRequestJobTest; |
| 218 friend class ServiceWorkerVersionBrowserTest; | 221 friend class ServiceWorkerVersionBrowserTest; |
| 219 friend class ServiceWorkerVersionTest; | 222 friend class ServiceWorkerVersionTest; |
| 220 friend class ServiceWorkerWriteToCacheJobTest; | 223 friend class ServiceWorkerWriteToCacheJobTest; |
| 224 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, |
| 225 MigrateOnDiskCacheAccess); |
| 226 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, |
| 227 NotMigrateOnDatabaseAccess); |
| 228 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, |
| 229 NotMigrateForEmptyDatabase); |
| 221 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDispatcherHostTest, | 230 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDispatcherHostTest, |
| 222 CleanupOnRendererCrash); | 231 CleanupOnRendererCrash); |
| 223 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, | 232 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, |
| 224 DeleteRegistration_NoLiveVersion); | 233 DeleteRegistration_NoLiveVersion); |
| 225 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, | 234 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, |
| 226 DeleteRegistration_WaitingVersion); | 235 DeleteRegistration_WaitingVersion); |
| 227 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, | 236 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, |
| 228 DeleteRegistration_ActiveVersion); | 237 DeleteRegistration_ActiveVersion); |
| 229 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, | 238 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageTest, |
| 230 UpdateRegistration); | 239 UpdateRegistration); |
| 231 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageDiskTest, | 240 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageDiskTest, |
| 232 CleanupOnRestart); | 241 CleanupOnRestart); |
| 233 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageDiskTest, | 242 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerResourceStorageDiskTest, |
| 234 ClearOnExit); | 243 ClearOnExit); |
| 235 | 244 |
| 236 struct InitialData { | 245 struct InitialData { |
| 237 int64 next_registration_id; | 246 int64 next_registration_id; |
| 238 int64 next_version_id; | 247 int64 next_version_id; |
| 239 int64 next_resource_id; | 248 int64 next_resource_id; |
| 240 std::set<GURL> origins; | 249 std::set<GURL> origins; |
| 250 bool disk_cache_migration_needed; |
| 251 bool old_disk_cache_deletion_needed; |
| 241 | 252 |
| 242 InitialData(); | 253 InitialData(); |
| 243 ~InitialData(); | 254 ~InitialData(); |
| 244 }; | 255 }; |
| 245 | 256 |
| 246 // Because there are too many params for base::Bind to wrap a closure around. | 257 // Because there are too many params for base::Bind to wrap a closure around. |
| 247 struct DidDeleteRegistrationParams { | 258 struct DidDeleteRegistrationParams { |
| 248 int64 registration_id; | 259 int64 registration_id; |
| 249 GURL origin; | 260 GURL origin; |
| 250 StatusCallback callback; | 261 StatusCallback callback; |
| 251 | 262 |
| 252 DidDeleteRegistrationParams(); | 263 DidDeleteRegistrationParams(); |
| 253 ~DidDeleteRegistrationParams(); | 264 ~DidDeleteRegistrationParams(); |
| 254 }; | 265 }; |
| 255 | 266 |
| 256 typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList; | 267 typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList; |
| 257 typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> > | 268 typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> > |
| 258 RegistrationRefsById; | 269 RegistrationRefsById; |
| 259 typedef base::Callback<void( | 270 typedef base::Callback<void(InitialData* data, |
| 260 InitialData* data, | 271 ServiceWorkerDatabase::Status status)> |
| 261 ServiceWorkerDatabase::Status status)> InitializeCallback; | 272 InitializeCallback; |
| 273 typedef base::Callback<void(ServiceWorkerDatabase::Status status)> |
| 274 DatabaseStatusCallback; |
| 262 typedef base::Callback<void( | 275 typedef base::Callback<void( |
| 263 const GURL& origin, | 276 const GURL& origin, |
| 264 const ServiceWorkerDatabase::RegistrationData& deleted_version_data, | 277 const ServiceWorkerDatabase::RegistrationData& deleted_version_data, |
| 265 const std::vector<int64>& newly_purgeable_resources, | 278 const std::vector<int64>& newly_purgeable_resources, |
| 266 ServiceWorkerDatabase::Status status)> WriteRegistrationCallback; | 279 ServiceWorkerDatabase::Status status)> WriteRegistrationCallback; |
| 267 typedef base::Callback<void( | 280 typedef base::Callback<void( |
| 268 bool origin_is_deletable, | 281 bool origin_is_deletable, |
| 269 const ServiceWorkerDatabase::RegistrationData& deleted_version_data, | 282 const ServiceWorkerDatabase::RegistrationData& deleted_version_data, |
| 270 const std::vector<int64>& newly_purgeable_resources, | 283 const std::vector<int64>& newly_purgeable_resources, |
| 271 ServiceWorkerDatabase::Status status)> DeleteRegistrationCallback; | 284 ServiceWorkerDatabase::Status status)> DeleteRegistrationCallback; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 288 const base::FilePath& path, | 301 const base::FilePath& path, |
| 289 base::WeakPtr<ServiceWorkerContextCore> context, | 302 base::WeakPtr<ServiceWorkerContextCore> context, |
| 290 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, | 303 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, |
| 291 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, | 304 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, |
| 292 storage::QuotaManagerProxy* quota_manager_proxy, | 305 storage::QuotaManagerProxy* quota_manager_proxy, |
| 293 storage::SpecialStoragePolicy* special_storage_policy); | 306 storage::SpecialStoragePolicy* special_storage_policy); |
| 294 | 307 |
| 295 base::FilePath GetDatabasePath(); | 308 base::FilePath GetDatabasePath(); |
| 296 base::FilePath GetDiskCachePath(); | 309 base::FilePath GetDiskCachePath(); |
| 297 | 310 |
| 298 // Loads the registration data from backend storage. This must be called | 311 // Returns a path to an old diskcache backed with BlockFile. This is used for |
| 299 // before any method that requires registration data. | 312 // the diskcache migration (see service_worker_disk_cache_migrator.h). |
| 313 // TODO(nhiroki): Remove this after several milestones pass |
| 314 // (http://crbug.com/487482) |
| 315 base::FilePath GetOldDiskCachePath(); |
| 316 |
| 300 bool LazyInitialize( | 317 bool LazyInitialize( |
| 301 const base::Closure& callback); | 318 const base::Closure& callback); |
| 302 void DidReadInitialData( | 319 void DidReadInitialData(InitialData* data, |
| 303 InitialData* data, | 320 ServiceWorkerDatabase::Status status); |
| 304 ServiceWorkerDatabase::Status status); | |
| 305 void DidFindRegistrationForDocument( | 321 void DidFindRegistrationForDocument( |
| 306 const GURL& document_url, | 322 const GURL& document_url, |
| 307 const FindRegistrationCallback& callback, | 323 const FindRegistrationCallback& callback, |
| 308 int64 callback_id, | 324 int64 callback_id, |
| 309 const ServiceWorkerDatabase::RegistrationData& data, | 325 const ServiceWorkerDatabase::RegistrationData& data, |
| 310 const ResourceList& resources, | 326 const ResourceList& resources, |
| 311 ServiceWorkerDatabase::Status status); | 327 ServiceWorkerDatabase::Status status); |
| 312 void DidFindRegistrationForPattern( | 328 void DidFindRegistrationForPattern( |
| 313 const GURL& scope, | 329 const GURL& scope, |
| 314 const FindRegistrationCallback& callback, | 330 const FindRegistrationCallback& callback, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 const ResourceList& resources); | 385 const ResourceList& resources); |
| 370 ServiceWorkerRegistration* FindInstallingRegistrationForDocument( | 386 ServiceWorkerRegistration* FindInstallingRegistrationForDocument( |
| 371 const GURL& document_url); | 387 const GURL& document_url); |
| 372 ServiceWorkerRegistration* FindInstallingRegistrationForPattern( | 388 ServiceWorkerRegistration* FindInstallingRegistrationForPattern( |
| 373 const GURL& scope); | 389 const GURL& scope); |
| 374 ServiceWorkerRegistration* FindInstallingRegistrationForId( | 390 ServiceWorkerRegistration* FindInstallingRegistrationForId( |
| 375 int64 registration_id); | 391 int64 registration_id); |
| 376 | 392 |
| 377 // Lazy disk_cache getter. | 393 // Lazy disk_cache getter. |
| 378 ServiceWorkerDiskCache* disk_cache(); | 394 ServiceWorkerDiskCache* disk_cache(); |
| 395 void MigrateDiskCache(); |
| 396 void DidMigrateDiskCache(ServiceWorkerStatusCode status); |
| 397 void DidSetDiskCacheMigrationNotNeeded(ServiceWorkerDatabase::Status status); |
| 398 void OnDiskCacheMigrationFailed(); |
| 399 void InitializeDiskCache(); |
| 379 void OnDiskCacheInitialized(int rv); | 400 void OnDiskCacheInitialized(int rv); |
| 380 | 401 |
| 381 void StartPurgingResources(const std::vector<int64>& ids); | 402 void StartPurgingResources(const std::vector<int64>& ids); |
| 382 void StartPurgingResources(const ResourceList& resources); | 403 void StartPurgingResources(const ResourceList& resources); |
| 383 void ContinuePurgingResources(); | 404 void ContinuePurgingResources(); |
| 384 void PurgeResource(int64 id); | 405 void PurgeResource(int64 id); |
| 385 void OnResourcePurged(int64 id, int rv); | 406 void OnResourcePurged(int64 id, int rv); |
| 386 | 407 |
| 387 // Deletes purgeable and uncommitted resources left over from the previous | 408 // Deletes purgeable and uncommitted resources left over from the previous |
| 388 // browser session. This must be called once per session before any database | 409 // browser session. This must be called once per session before any database |
| 389 // operation that may mutate the purgeable or uncommitted resource lists. | 410 // operation that may mutate the purgeable or uncommitted resource lists. |
| 390 void DeleteStaleResources(); | 411 void DeleteStaleResources(); |
| 391 void DidCollectStaleResources(const std::vector<int64>& stale_resource_ids, | 412 void DidCollectStaleResources(const std::vector<int64>& stale_resource_ids, |
| 392 ServiceWorkerDatabase::Status status); | 413 ServiceWorkerDatabase::Status status); |
| 393 | 414 |
| 394 void ClearSessionOnlyOrigins(); | 415 void ClearSessionOnlyOrigins(); |
| 395 | 416 |
| 396 // Static cross-thread helpers. | 417 // Static cross-thread helpers. |
| 397 static void CollectStaleResourcesFromDB( | 418 static void CollectStaleResourcesFromDB( |
| 398 ServiceWorkerDatabase* database, | 419 ServiceWorkerDatabase* database, |
| 399 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 420 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 400 const GetResourcesCallback& callback); | 421 const GetResourcesCallback& callback); |
| 401 static void ReadInitialDataFromDB( | 422 static void ReadInitialDataFromDB( |
| 402 ServiceWorkerDatabase* database, | 423 ServiceWorkerDatabase* database, |
| 403 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 424 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 404 const InitializeCallback& callback); | 425 const InitializeCallback& callback); |
| 426 static void DeleteOldDiskCacheInDB(ServiceWorkerDatabase* database, |
| 427 const base::FilePath& disk_cache_path); |
| 405 static void DeleteRegistrationFromDB( | 428 static void DeleteRegistrationFromDB( |
| 406 ServiceWorkerDatabase* database, | 429 ServiceWorkerDatabase* database, |
| 407 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 430 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 408 int64 registration_id, | 431 int64 registration_id, |
| 409 const GURL& origin, | 432 const GURL& origin, |
| 410 const DeleteRegistrationCallback& callback); | 433 const DeleteRegistrationCallback& callback); |
| 411 static void WriteRegistrationInDB( | 434 static void WriteRegistrationInDB( |
| 412 ServiceWorkerDatabase* database, | 435 ServiceWorkerDatabase* database, |
| 413 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 436 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 414 const ServiceWorkerDatabase::RegistrationData& registration, | 437 const ServiceWorkerDatabase::RegistrationData& registration, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 base::FilePath path_; | 506 base::FilePath path_; |
| 484 base::WeakPtr<ServiceWorkerContextCore> context_; | 507 base::WeakPtr<ServiceWorkerContextCore> context_; |
| 485 | 508 |
| 486 // Only accessed using |database_task_manager_|. | 509 // Only accessed using |database_task_manager_|. |
| 487 scoped_ptr<ServiceWorkerDatabase> database_; | 510 scoped_ptr<ServiceWorkerDatabase> database_; |
| 488 | 511 |
| 489 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager_; | 512 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager_; |
| 490 scoped_refptr<base::SingleThreadTaskRunner> disk_cache_thread_; | 513 scoped_refptr<base::SingleThreadTaskRunner> disk_cache_thread_; |
| 491 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 514 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 492 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; | 515 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; |
| 516 |
| 493 scoped_ptr<ServiceWorkerDiskCache> disk_cache_; | 517 scoped_ptr<ServiceWorkerDiskCache> disk_cache_; |
| 518 scoped_ptr<ServiceWorkerDiskCacheMigrator> disk_cache_migrator_; |
| 519 bool disk_cache_migration_needed_; |
| 520 bool old_disk_cache_deletion_needed_; |
| 521 |
| 494 std::deque<int64> purgeable_resource_ids_; | 522 std::deque<int64> purgeable_resource_ids_; |
| 495 bool is_purge_pending_; | 523 bool is_purge_pending_; |
| 496 bool has_checked_for_stale_resources_; | 524 bool has_checked_for_stale_resources_; |
| 497 std::set<int64> pending_deletions_; | 525 std::set<int64> pending_deletions_; |
| 498 | 526 |
| 499 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; | 527 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; |
| 500 | 528 |
| 501 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); | 529 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); |
| 502 }; | 530 }; |
| 503 | 531 |
| 504 } // namespace content | 532 } // namespace content |
| 505 | 533 |
| 506 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ | 534 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| OLD | NEW |