| 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_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ |
| 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" |
| 15 #include "content/browser/cache_storage/cache_storage_scheduler_client.h" |
| 14 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 15 | 17 |
| 16 namespace content { | 18 namespace content { |
| 17 | 19 |
| 20 class CacheStorageOperation; |
| 21 |
| 18 // TODO(jkarlin): Support readers and writers so operations can run in parallel. | 22 // TODO(jkarlin): Support readers and writers so operations can run in parallel. |
| 19 // TODO(jkarlin): Support operation identification so that ops can be checked in | 23 // TODO(jkarlin): Support operation identification so that ops can be checked in |
| 20 // DCHECKs. | 24 // DCHECKs. |
| 21 | 25 |
| 22 // CacheStorageScheduler runs the scheduled callbacks sequentially. Add an | 26 // CacheStorageScheduler runs the scheduled callbacks sequentially. Add an |
| 23 // operation by calling ScheduleOperation() with your callback. Once your | 27 // operation by calling ScheduleOperation() with your callback. Once your |
| 24 // operation is done be sure to call CompleteOperationAndRunNext() to schedule | 28 // operation is done be sure to call CompleteOperationAndRunNext() to schedule |
| 25 // the next operation. | 29 // the next operation. |
| 26 class CONTENT_EXPORT CacheStorageScheduler { | 30 class CONTENT_EXPORT CacheStorageScheduler { |
| 27 public: | 31 public: |
| 28 CacheStorageScheduler(); | 32 explicit CacheStorageScheduler(CacheStorageSchedulerClient client_type); |
| 29 virtual ~CacheStorageScheduler(); | 33 virtual ~CacheStorageScheduler(); |
| 30 | 34 |
| 31 // Adds the operation to the tail of the queue and starts it if the scheduler | 35 // Adds the operation to the tail of the queue and starts it if the scheduler |
| 32 // is idle. | 36 // is idle. |
| 33 void ScheduleOperation(const base::Closure& closure); | 37 void ScheduleOperation(const base::Closure& closure); |
| 34 | 38 |
| 35 // Call this after each operation completes. It cleans up the current | 39 // Call this after each operation completes. It cleans up the current |
| 36 // operation and starts the next. | 40 // operation and starts the next. |
| 37 void CompleteOperationAndRunNext(); | 41 void CompleteOperationAndRunNext(); |
| 38 | 42 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 // Grab a weak ptr to guard against the scheduler being deleted during the | 60 // Grab a weak ptr to guard against the scheduler being deleted during the |
| 57 // callback. | 61 // callback. |
| 58 base::WeakPtr<CacheStorageScheduler> scheduler = | 62 base::WeakPtr<CacheStorageScheduler> scheduler = |
| 59 weak_ptr_factory_.GetWeakPtr(); | 63 weak_ptr_factory_.GetWeakPtr(); |
| 60 | 64 |
| 61 callback.Run(std::forward<Args>(args)...); | 65 callback.Run(std::forward<Args>(args)...); |
| 62 if (scheduler) | 66 if (scheduler) |
| 63 CompleteOperationAndRunNext(); | 67 CompleteOperationAndRunNext(); |
| 64 } | 68 } |
| 65 | 69 |
| 66 // The list of operations waiting on initialization. | 70 std::list<std::unique_ptr<CacheStorageOperation>> pending_operations_; |
| 67 std::list<base::Closure> pending_operations_; | 71 std::unique_ptr<CacheStorageOperation> running_operation_; |
| 68 bool operation_running_; | 72 CacheStorageSchedulerClient client_type_; |
| 73 |
| 69 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; | 74 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; |
| 70 | 75 |
| 71 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); | 76 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); |
| 72 }; | 77 }; |
| 73 | 78 |
| 74 } // namespace content | 79 } // namespace content |
| 75 | 80 |
| 76 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ | 81 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ |
| OLD | NEW |