| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_DISK_CACHE_MIGRATOR_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISK_CACHE_MIGRATOR_H_ |
| 7 |
| 5 #include "content/browser/service_worker/service_worker_disk_cache.h" | 8 #include "content/browser/service_worker/service_worker_disk_cache.h" |
| 6 | 9 |
| 7 #include "base/id_map.h" | 10 #include "base/id_map.h" |
| 8 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/browser/service_worker/service_worker_database.h" | 12 #include "content/browser/service_worker/service_worker_database.h" |
| 10 #include "content/common/service_worker/service_worker_status_code.h" | 13 #include "content/common/service_worker/service_worker_status_code.h" |
| 11 | 14 |
| 12 namespace content { | 15 namespace content { |
| 13 | 16 |
| 14 // This is used for migrating the ServiceWorkerDiskCache from the BlockFile | 17 // This is used for migrating the ServiceWorkerDiskCache from the BlockFile |
| 15 // backend to the Simple backend. The migrator iterates over resources in the | 18 // backend to the Simple backend. The migrator iterates over resources in the |
| 16 // src DiskCache and copies them into the dest DiskCache one by one. This does | 19 // src DiskCache and copies them into the dest DiskCache one by one. This does |
| 17 // not delete the resources in the src DiskCache. | 20 // not delete the resources in the src DiskCache. |
| 18 // | 21 // |
| 19 // TODO(nhiroki): Remove this migrator after several milestones pass | 22 // TODO(nhiroki): Remove this migrator after several milestones pass |
| 20 // (http://crbug.com/487482) | 23 // (http://crbug.com/487482) |
| 21 class CONTENT_EXPORT ServiceWorkerDiskCacheMigrator { | 24 class CONTENT_EXPORT ServiceWorkerDiskCacheMigrator { |
| 22 public: | 25 public: |
| 23 using StatusCallback = base::Callback<void(ServiceWorkerStatusCode)>; | 26 using StatusCallback = base::Callback<void(ServiceWorkerStatusCode)>; |
| 24 | 27 |
| 25 ServiceWorkerDiskCacheMigrator(ServiceWorkerDiskCache* src, | 28 ServiceWorkerDiskCacheMigrator(ServiceWorkerDiskCache* src, |
| 26 ServiceWorkerDiskCache* dest); | 29 ServiceWorkerDiskCache* dest); |
| 27 ~ServiceWorkerDiskCacheMigrator(); | 30 ~ServiceWorkerDiskCacheMigrator(); |
| 28 | 31 |
| 29 // Returns SERVICE_WORKER_OK if all resources are successfully migrated. The | 32 // Returns SERVICE_WORKER_OK if all resources are successfully migrated. The |
| 30 // caller should delete the src DiskCache after migration. | 33 // caller should delete the src DiskCache after migration. |
| 31 void Start(const StatusCallback& callback); | 34 void Start(const StatusCallback& callback); |
| 32 | 35 |
| 33 private: | 36 private: |
| 34 friend class ServiceWorkerDiskCacheMigratorTest; | 37 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDiskCacheMigratorTest, |
| 38 ThrottleInflightTasks); |
| 39 |
| 35 class Task; | 40 class Task; |
| 36 class WrappedEntry; | 41 class WrappedEntry; |
| 37 | 42 |
| 38 using InflightTaskMap = IDMap<Task, IDMapOwnPointer>; | 43 using InflightTaskMap = IDMap<Task, IDMapOwnPointer>; |
| 39 | 44 |
| 40 void OpenNextEntry(); | 45 void OpenNextEntry(); |
| 41 void OnNextEntryOpened(scoped_ptr<WrappedEntry> entry, int result); | 46 void OnNextEntryOpened(scoped_ptr<WrappedEntry> entry, int result); |
| 42 void RunPendingTask(); | 47 void RunPendingTask(); |
| 43 void OnEntryMigrated(InflightTaskMap::KeyType task_id, | 48 void OnEntryMigrated(InflightTaskMap::KeyType task_id, |
| 44 ServiceWorkerStatusCode status); | 49 ServiceWorkerStatusCode status); |
| 45 void Complete(ServiceWorkerStatusCode status); | 50 void Complete(ServiceWorkerStatusCode status); |
| 46 | 51 |
| 47 void set_max_number_of_inflight_tasks(size_t max_number) { | 52 void set_max_number_of_inflight_tasks(size_t max_number) { |
| 48 max_number_of_inflight_tasks_ = max_number; | 53 max_number_of_inflight_tasks_ = max_number; |
| 49 } | 54 } |
| 50 | 55 |
| 51 scoped_ptr<disk_cache::Backend::Iterator> iterator_; | 56 scoped_ptr<disk_cache::Backend::Iterator> iterator_; |
| 52 bool is_iterating_ = false; | 57 bool is_iterating_ = false; |
| 53 | 58 |
| 54 ServiceWorkerDiskCache* src_; | 59 ServiceWorkerDiskCache* src_; |
| 55 ServiceWorkerDiskCache* dest_; | 60 ServiceWorkerDiskCache* dest_; |
| 56 | 61 |
| 57 InflightTaskMap::KeyType next_task_id_ = 0; | 62 InflightTaskMap::KeyType next_task_id_ = 0; |
| 58 InflightTaskMap inflight_tasks_; | 63 InflightTaskMap inflight_tasks_; |
| 59 scoped_ptr<Task> pending_task_; | 64 scoped_ptr<Task> pending_task_; |
| 60 size_t max_number_of_inflight_tasks_ = 10; | 65 size_t max_number_of_inflight_tasks_ = 10; |
| 61 | 66 |
| 67 base::TimeTicks start_time_; |
| 68 size_t number_of_migrated_resources_ = 0; |
| 69 |
| 62 StatusCallback callback_; | 70 StatusCallback callback_; |
| 63 | 71 |
| 64 base::WeakPtrFactory<ServiceWorkerDiskCacheMigrator> weak_factory_; | 72 base::WeakPtrFactory<ServiceWorkerDiskCacheMigrator> weak_factory_; |
| 65 | 73 |
| 66 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDiskCacheMigrator); | 74 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDiskCacheMigrator); |
| 67 }; | 75 }; |
| 68 | 76 |
| 69 } // namespace content | 77 } // namespace content |
| 78 |
| 79 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISK_CACHE_MIGRATOR_H_ |
| OLD | NEW |