Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(771)

Unified Diff: content/browser/cache_storage/cache_storage_scheduler.h

Issue 2167483002: [CacheStorage] Simpler way to wrap callbacks to run pending operation completions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
};
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.cc ('k') | content/browser/cache_storage/cache_storage_scheduler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698