| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // operation by calling ScheduleOperation() with your callback. Once your | 27 // operation by calling ScheduleOperation() with your callback. Once your |
| 28 // operation is done be sure to call CompleteOperationAndRunNext() to schedule | 28 // operation is done be sure to call CompleteOperationAndRunNext() to schedule |
| 29 // the next operation. | 29 // the next operation. |
| 30 class CONTENT_EXPORT CacheStorageScheduler { | 30 class CONTENT_EXPORT CacheStorageScheduler { |
| 31 public: | 31 public: |
| 32 explicit CacheStorageScheduler(CacheStorageSchedulerClient client_type); | 32 explicit CacheStorageScheduler(CacheStorageSchedulerClient client_type); |
| 33 virtual ~CacheStorageScheduler(); | 33 virtual ~CacheStorageScheduler(); |
| 34 | 34 |
| 35 // 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 |
| 36 // is idle. | 36 // is idle. |
| 37 void ScheduleOperation(const base::Closure& closure); | 37 void ScheduleOperation(base::OnceClosure closure); |
| 38 | 38 |
| 39 // Call this after each operation completes. It cleans up the current | 39 // Call this after each operation completes. It cleans up the current |
| 40 // operation and starts the next. | 40 // operation and starts the next. |
| 41 void CompleteOperationAndRunNext(); | 41 void CompleteOperationAndRunNext(); |
| 42 | 42 |
| 43 // Returns true if there are any running or pending operations. | 43 // Returns true if there are any running or pending operations. |
| 44 bool ScheduledOperations() const; | 44 bool ScheduledOperations() const; |
| 45 | 45 |
| 46 // Wraps |callback| to also call CompleteOperationAndRunNext. | 46 // Wraps |callback| to also call CompleteOperationAndRunNext. |
| 47 template <typename... Args> | 47 template <typename... Args> |
| 48 base::Callback<void(Args...)> WrapCallbackToRunNext( | 48 base::OnceCallback<void(Args...)> WrapCallbackToRunNext( |
| 49 const base::Callback<void(Args...)>& callback) { | 49 base::OnceCallback<void(Args...)> callback) { |
| 50 return base::Bind(&CacheStorageScheduler::RunNextContinuation<Args...>, | 50 return base::BindOnce( |
| 51 weak_ptr_factory_.GetWeakPtr(), callback); | 51 &CacheStorageScheduler::RunNextOnceContinuation<Args...>, |
| 52 weak_ptr_factory_.GetWeakPtr(), std::move(callback)); |
| 53 } |
| 54 template <typename... Args> |
| 55 base::RepeatingCallback<void(Args...)> WrapCallbackToRunNext( |
| 56 const base::RepeatingCallback<void(Args...)>& callback) { |
| 57 return base::BindRepeating( |
| 58 &CacheStorageScheduler::RunNextRepeatingContinuation<Args...>, |
| 59 weak_ptr_factory_.GetWeakPtr(), callback); |
| 52 } | 60 } |
| 53 | 61 |
| 54 private: | 62 private: |
| 55 void RunOperationIfIdle(); | 63 void RunOperationIfIdle(); |
| 56 | 64 |
| 57 template <typename... Args> | 65 template <typename... Args> |
| 58 void RunNextContinuation(const base::Callback<void(Args...)>& callback, | 66 void RunNextOnceContinuation(base::OnceCallback<void(Args...)> callback, |
| 59 Args... args) { | 67 Args... args) { |
| 60 // Grab a weak ptr to guard against the scheduler being deleted during the | 68 // Grab a weak ptr to guard against the scheduler being deleted during the |
| 61 // callback. | 69 // callback. |
| 62 base::WeakPtr<CacheStorageScheduler> scheduler = | 70 base::WeakPtr<CacheStorageScheduler> scheduler = |
| 71 weak_ptr_factory_.GetWeakPtr(); |
| 72 |
| 73 std::move(callback).Run(std::forward<Args>(args)...); |
| 74 if (scheduler) |
| 75 CompleteOperationAndRunNext(); |
| 76 } |
| 77 template <typename... Args> |
| 78 void RunNextRepeatingContinuation( |
| 79 const base::RepeatingCallback<void(Args...)>& callback, |
| 80 Args... args) { |
| 81 // Grab a weak ptr to guard against the scheduler being deleted during the |
| 82 // callback. |
| 83 base::WeakPtr<CacheStorageScheduler> scheduler = |
| 63 weak_ptr_factory_.GetWeakPtr(); | 84 weak_ptr_factory_.GetWeakPtr(); |
| 64 | 85 |
| 65 callback.Run(std::forward<Args>(args)...); | 86 callback.Run(std::forward<Args>(args)...); |
| 66 if (scheduler) | 87 if (scheduler) |
| 67 CompleteOperationAndRunNext(); | 88 CompleteOperationAndRunNext(); |
| 68 } | 89 } |
| 69 | 90 |
| 70 std::list<std::unique_ptr<CacheStorageOperation>> pending_operations_; | 91 std::list<std::unique_ptr<CacheStorageOperation>> pending_operations_; |
| 71 std::unique_ptr<CacheStorageOperation> running_operation_; | 92 std::unique_ptr<CacheStorageOperation> running_operation_; |
| 72 CacheStorageSchedulerClient client_type_; | 93 CacheStorageSchedulerClient client_type_; |
| 73 | 94 |
| 74 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; | 95 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; |
| 75 | 96 |
| 76 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); | 97 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); |
| 77 }; | 98 }; |
| 78 | 99 |
| 79 } // namespace content | 100 } // namespace content |
| 80 | 101 |
| 81 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ | 102 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ |
| OLD | NEW |