Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_DELAYED_CALLBACK_RUNNER_H_ | |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_DELAYED_CALLBACK_RUNNER_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/callback_forward.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/task_runner.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "base/timer/timer.h" | |
| 15 | |
| 16 namespace safe_browsing { | |
| 17 | |
| 18 // Runs callbacks on a given task runner, waiting a certain amount of time | |
| 19 // between each. The delay also applies to running the first callback (i.e., | |
| 20 // the first callback will be run some time after Start() is invoked. Callbacks | |
| 21 // are deleted after they are run. Start() is idempotent: calling it while the | |
| 22 // runner is doing it job has no effect. | |
|
robertshield
2014/08/04 01:35:12
its
grt (UTC plus 2)
2014/08/04 14:15:33
Done.
| |
| 23 class DelayedCallbackRunner { | |
|
robertshield
2014/08/04 01:35:12
May be worth mentioning in the class comment that
grt (UTC plus 2)
2014/08/04 14:15:33
Added enforcement via ThreadChecker.
| |
| 24 public: | |
| 25 // Constructs an instance that runs tasks on |callback_runner|, waiting for | |
| 26 // |delay| time to pass before and between each callback. | |
| 27 DelayedCallbackRunner(base::TimeDelta delay, | |
| 28 base::TaskRunner* task_runner); | |
| 29 ~DelayedCallbackRunner(); | |
| 30 | |
| 31 // Registers |callback| with the runner. | |
| 32 void RegisterCallback(const base::Closure& callback); | |
|
robertshield
2014/08/04 01:35:12
Is it worth mentioning that a copy of the Closure
grt (UTC plus 2)
2014/08/04 14:15:33
it should be held only until it is run. comment an
| |
| 33 | |
| 34 // Starts running the callbacks after the delay. | |
| 35 void Start(); | |
| 36 | |
| 37 private: | |
| 38 typedef std::list<base::Closure> CallbackList; | |
| 39 | |
| 40 // A callback invoked by the timer to run the next callback. The timer is | |
| 41 // restarted to process the next callback if there is one. | |
| 42 void OnTimer(); | |
| 43 | |
| 44 base::TaskRunner* task_runner_; | |
| 45 | |
| 46 CallbackList callbacks_; | |
| 47 | |
| 48 // callbacks_.end() when no work is being done. Any other value otherwise. | |
| 49 CallbackList::iterator next_callback_; | |
| 50 | |
| 51 // A timer upon the firing of which the next callback will be run. | |
| 52 base::DelayTimer<DelayedCallbackRunner> timer_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(DelayedCallbackRunner); | |
| 55 }; | |
| 56 | |
| 57 } // namespace safe_browsing | |
| 58 | |
| 59 #endif // CHROME_BROWSER_SAFE_BROWSING_DELAYED_CALLBACK_RUNNER_H_ | |
| OLD | NEW |