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

Side by Side 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: Comment clarification 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 unified diff | Download patch
OLDNEW
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/callback.h" 11 #include "base/callback.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
12 #include "content/common/content_export.h" 14 #include "content/common/content_export.h"
13 15
14 namespace content { 16 namespace content {
15 17
16 // TODO(jkarlin): Support readers and writers so operations can run in parallel. 18 // TODO(jkarlin): Support readers and writers so operations can run in parallel.
17 // TODO(jkarlin): Support operation identification so that ops can be checked in 19 // TODO(jkarlin): Support operation identification so that ops can be checked in
18 // DCHECKs. 20 // DCHECKs.
19 21
20 // CacheStorageScheduler runs the scheduled callbacks sequentially. Add an 22 // CacheStorageScheduler runs the scheduled callbacks sequentially. Add an
21 // operation by calling ScheduleOperation() with your callback. Once your 23 // operation by calling ScheduleOperation() with your callback. Once your
22 // operation is done be sure to call CompleteOperationAndRunNext() to schedule 24 // operation is done be sure to call CompleteOperationAndRunNext() to schedule
23 // the next operation. 25 // the next operation.
24 class CONTENT_EXPORT CacheStorageScheduler { 26 class CONTENT_EXPORT CacheStorageScheduler {
25 public: 27 public:
26 CacheStorageScheduler(); 28 CacheStorageScheduler();
27 virtual ~CacheStorageScheduler(); 29 virtual ~CacheStorageScheduler();
28 30
29 // Adds the operation to the tail of the queue and starts it if the scheduler 31 // Adds the operation to the tail of the queue and starts it if the scheduler
30 // is idle. 32 // is idle.
31 void ScheduleOperation(const base::Closure& closure); 33 void ScheduleOperation(const base::Closure& closure);
32 34
33 // Call this after each operation completes. It cleans up the current 35 // Call this after each operation completes. It cleans up the current
34 // operation and starts the next. 36 // operation and starts the next.
35 void CompleteOperationAndRunNext(); 37 void CompleteOperationAndRunNext();
36 38
37 // Returns true if there are any running or pending operations. 39 // Returns true if there are any running or pending operations.
38 bool ScheduledOperations() const; 40 bool ScheduledOperations() const;
39 41
42 // Wraps |callback| to also call CompleteOperationAndRunNext.
43 template <typename... Args>
44 base::Callback<void(Args...)> WrapCallbackToRunNext(
45 const base::Callback<void(Args...)>& callback) {
46 return base::Bind(&CacheStorageScheduler::RunNextContinuation<Args...>,
47 weak_ptr_factory_.GetWeakPtr(), callback);
48 }
49
40 private: 50 private:
41 void RunOperationIfIdle(); 51 void RunOperationIfIdle();
42 52
53 template <typename... Args>
54 void RunNextContinuation(const base::Callback<void(Args...)>& callback,
55 Args... args) {
56 // Grab a weak ptr to guard against the scheduler being deleted during the
57 // callback.
58 base::WeakPtr<CacheStorageScheduler> scheduler =
59 weak_ptr_factory_.GetWeakPtr();
60
61 callback.Run(std::forward<Args>(args)...);
62 if (scheduler)
63 CompleteOperationAndRunNext();
64 }
65
43 // The list of operations waiting on initialization. 66 // The list of operations waiting on initialization.
44 std::list<base::Closure> pending_operations_; 67 std::list<base::Closure> pending_operations_;
45 bool operation_running_; 68 bool operation_running_;
69 base::WeakPtrFactory<CacheStorageScheduler> weak_ptr_factory_;
46 70
47 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler); 71 DISALLOW_COPY_AND_ASSIGN(CacheStorageScheduler);
48 }; 72 };
49 73
50 } // namespace content 74 } // namespace content
51 75
52 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_ 76 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_SCHEDULER_H_
OLDNEW
« 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