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..56516a1853fce128d1dcc3f1253a4ffb7a2130df 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. |
+ template <typename R, typename... Args> |
+ base::Callback<R(Args...)> WrapCallbackToRunNext( |
+ const base::Callback<R(Args...)>& callback) { |
+ return base::Bind(&CacheStorageScheduler::RunNextContinuation<R, Args...>, |
+ weak_ptr_factory_.GetWeakPtr(), callback); |
+ } |
+ |
private: |
void RunOperationIfIdle(); |
+ template <typename R, typename... Args> |
+ R RunNextContinuation(const base::Callback<R(Args...)>& callback, |
michaeln
2016/07/19 23:45:25
should this function return a value of type 'R'? h
jkarlin
2016/07/20 00:51:50
Good catch on specifying void. Done.
|
+ 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); |
}; |