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

Unified Diff: chrome/browser/safe_browsing/delayed_callback_runner.cc

Issue 441453002: Support for process-wide incidents in the safe browsing incident reporting service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: new test plus other review comments Created 6 years, 4 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: chrome/browser/safe_browsing/delayed_callback_runner.cc
diff --git a/chrome/browser/safe_browsing/delayed_callback_runner.cc b/chrome/browser/safe_browsing/delayed_callback_runner.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f3444597c85faaf81c938c201b82a9dc267d66cf
--- /dev/null
+++ b/chrome/browser/safe_browsing/delayed_callback_runner.cc
@@ -0,0 +1,51 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/safe_browsing/delayed_callback_runner.h"
+
+#include "base/location.h"
+
+namespace safe_browsing {
+
+DelayedCallbackRunner::DelayedCallbackRunner(
+ base::TimeDelta delay,
+ const scoped_refptr<base::TaskRunner>& task_runner)
+ : task_runner_(task_runner),
+ next_callback_(callbacks_.end()),
+ timer_(FROM_HERE, delay, this, &DelayedCallbackRunner::OnTimer) {
+}
+
+DelayedCallbackRunner::~DelayedCallbackRunner() {
+}
+
+void DelayedCallbackRunner::RegisterCallback(const base::Closure& callback) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ callbacks_.push_back(callback);
+}
+
+void DelayedCallbackRunner::Start() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ // Nothing to do if the runner is already running or nothing has been added.
+ if (next_callback_ != callbacks_.end() || callbacks_.empty())
+ return;
+
+ // Prime the system with the first callback.
+ next_callback_ = callbacks_.begin();
+
+ // Point the starter pistol in the air and pull the trigger.
+ timer_.Reset();
+}
+
+void DelayedCallbackRunner::OnTimer() {
+ // Run the next callback on the task runner.
+ task_runner_->PostTask(FROM_HERE, *next_callback_);
+
+ // Remove this callback and get ready for the next if there is one.
+ next_callback_ = callbacks_.erase(next_callback_);
+ if (next_callback_ != callbacks_.end())
+ timer_.Reset();
+}
+
+} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698