Index: content/browser/cache_storage/cache_storage_scheduler.h |
diff --git a/content/browser/cache_storage/cache_storage_scheduler.h b/content/browser/cache_storage/cache_storage_scheduler.h |
index d5a48778dacd97878d2fdab1318722b4ec400f32..40829a3beffbe2ebc8fdfa7e33f50018fa4c3b85 100644 |
--- a/content/browser/cache_storage/cache_storage_scheduler.h |
+++ b/content/browser/cache_storage/cache_storage_scheduler.h |
@@ -7,8 +7,10 @@ |
#include <list> |
+#include "base/bind.h" |
#include "base/callback.h" |
#include "base/macros.h" |
+#include "base/memory/weak_ptr.h" |
#include "content/common/content_export.h" |
namespace content { |
@@ -37,12 +39,34 @@ class CONTENT_EXPORT CacheStorageScheduler { |
// Returns true if there are any running or pending operations. |
bool ScheduledOperations() const; |
+ // Wraps |callback| to first call CompleteOperationAndRunNext before running. |
michaeln
2016/07/20 18:42:16
Comment is a little misleading, maybe you meant to
jkarlin
2016/07/20 19:07:31
Done.
|
+ template <typename... Args> |
+ base::Callback<void(Args...)> WrapCallbackToRunNext( |
+ const base::Callback<void(Args...)>& callback) { |
+ return base::Bind(&CacheStorageScheduler::RunNextContinuation<Args...>, |
+ weak_ptr_factory_.GetWeakPtr(), callback); |
+ } |
+ |
private: |
void RunOperationIfIdle(); |
+ template <typename... Args> |
+ void RunNextContinuation(const base::Callback<void(Args...)>& callback, |
+ Args... args) { |
+ // Grab a weak ptr to guard against the scheduler being deleted during the |
+ // callback. |
+ base::WeakPtr<CacheStorageScheduler> scheduler = |
+ weak_ptr_factory_.GetWeakPtr(); |
+ |
+ callback.Run(std::forward<Args>(args)...); |
+ if (scheduler) |
+ CompleteOperationAndRunNext(); |
+ } |
+ |
// The list of operations waiting on initialization. |
std::list<base::Closure> pending_operations_; |
bool operation_running_; |
+ base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_; |
DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); |
}; |