| 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 29 matching lines...) Expand all Loading... |
| 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::OnceCallback<void(Args...)> WrapCallbackToRunNext( | 48 base::OnceCallback<void(Args...)> WrapCallbackToRunNext( |
| 49 base::OnceCallback<void(Args...)> callback) { | 49 base::OnceCallback<void(Args...)> callback) { |
| 50 return base::BindOnce( | 50 return base::BindOnce(&CacheStorageScheduler::RunNextContinuation<Args...>, |
| 51 &CacheStorageScheduler::RunNextOnceContinuation<Args...>, | 51 weak_ptr_factory_.GetWeakPtr(), std::move(callback)); |
| 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); | |
| 60 } | 52 } |
| 61 | 53 |
| 62 private: | 54 private: |
| 63 void RunOperationIfIdle(); | 55 void RunOperationIfIdle(); |
| 64 | 56 |
| 65 template <typename... Args> | 57 template <typename... Args> |
| 66 void RunNextOnceContinuation(base::OnceCallback<void(Args...)> callback, | 58 void RunNextContinuation(base::OnceCallback<void(Args...)> callback, |
| 67 Args... args) { | 59 Args... args) { |
| 68 // 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 |
| 69 // callback. | 61 // callback. |
| 70 base::WeakPtr<CacheStorageScheduler> scheduler = | 62 base::WeakPtr<CacheStorageScheduler> scheduler = |
| 71 weak_ptr_factory_.GetWeakPtr(); | 63 weak_ptr_factory_.GetWeakPtr(); |
| 72 | 64 |
| 73 std::move(callback).Run(std::forward<Args>(args)...); | 65 std::move(callback).Run(std::forward<Args>(args)...); |
| 74 if (scheduler) | 66 if (scheduler) |
| 75 CompleteOperationAndRunNext(); | 67 CompleteOperationAndRunNext(); |
| 76 } | 68 } |
| 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 = | |
| 84 weak_ptr_factory_.GetWeakPtr(); | |
| 85 | |
| 86 callback.Run(std::forward<Args>(args)...); | |
| 87 if (scheduler) | |
| 88 CompleteOperationAndRunNext(); | |
| 89 } | |
| 90 | 69 |
| 91 std::list<std::unique_ptr<CacheStorageOperation>> pending_operations_; | 70 std::list<std::unique_ptr<CacheStorageOperation>> pending_operations_; |
| 92 std::unique_ptr<CacheStorageOperation> running_operation_; | 71 std::unique_ptr<CacheStorageOperation> running_operation_; |
| 93 CacheStorageSchedulerClient client_type_; | 72 CacheStorageSchedulerClient client_type_; |
| 94 | 73 |
| 95 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; | 74 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; |
| 96 | 75 |
| 97 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); | 76 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); |
| 98 }; | 77 }; |
| 99 | 78 |
| 100 } // namespace content | 79 } // namespace content |
| 101 | 80 |
| 102 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ | 81 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ |
| OLD | NEW |