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

Unified Diff: base/metrics/user_metrics.cc

Issue 1859213002: Move the thread hop for UMA user actions from content:: to base::. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Answered Alexei and Ilya. Created 4 years, 8 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
« no previous file with comments | « base/metrics/user_metrics.h ('k') | base/test/user_action_tester.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/user_metrics.cc
diff --git a/base/metrics/user_metrics.cc b/base/metrics/user_metrics.cc
index 55467e66eebba0f4a84f99444a08be935020e99b..13e76f6097e13dc39200248d6fbbfaeea14611ad 100644
--- a/base/metrics/user_metrics.cc
+++ b/base/metrics/user_metrics.cc
@@ -8,70 +8,67 @@
#include <vector>
+#include "base/bind.h"
#include "base/lazy_instance.h"
+#include "base/location.h"
#include "base/macros.h"
#include "base/threading/thread_checker.h"
namespace base {
namespace {
-// A helper class for tracking callbacks and ensuring thread-safety.
-class Callbacks {
- public:
- Callbacks() {}
-
- // Records the |action|.
- void Record(const std::string& action) {
- DCHECK(thread_checker_.CalledOnValidThread());
- for (size_t i = 0; i < callbacks_.size(); ++i) {
- callbacks_[i].Run(action);
- }
- }
-
- // Adds |callback| to the list of |callbacks_|.
- void AddCallback(const ActionCallback& callback) {
- DCHECK(thread_checker_.CalledOnValidThread());
- callbacks_.push_back(callback);
- }
-
- // Removes the first instance of |callback| from the list of |callbacks_|, if
- // there is one.
- void RemoveCallback(const ActionCallback& callback) {
- DCHECK(thread_checker_.CalledOnValidThread());
- for (size_t i = 0; i < callbacks_.size(); ++i) {
- if (callbacks_[i].Equals(callback)) {
- callbacks_.erase(callbacks_.begin() + i);
- return;
- }
- }
- }
-
- private:
- base::ThreadChecker thread_checker_;
- std::vector<ActionCallback> callbacks_;
-
- DISALLOW_COPY_AND_ASSIGN(Callbacks);
-};
-
-base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<std::vector<ActionCallback>> g_callbacks =
+ LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner>> g_task_runner =
+ LAZY_INSTANCE_INITIALIZER;
} // namespace
void RecordAction(const UserMetricsAction& action) {
- g_callbacks.Get().Record(action.str_);
+ RecordComputedAction(action.str_);
}
void RecordComputedAction(const std::string& action) {
- g_callbacks.Get().Record(action);
+ if (!g_task_runner.Get()) {
+ DCHECK(g_callbacks.Get().empty());
+ return;
+ }
+
+ if (!g_task_runner.Get()->BelongsToCurrentThread()) {
+ g_task_runner.Get()->PostTask(FROM_HERE,
+ base::Bind(&RecordComputedAction, action));
+ return;
+ }
+
+ for (const ActionCallback& callback : g_callbacks.Get()) {
+ callback.Run(action);
+ }
}
void AddActionCallback(const ActionCallback& callback) {
- g_callbacks.Get().AddCallback(callback);
+ // Only allow adding a callback if the task runner is set.
+ DCHECK(g_task_runner.Get());
+ DCHECK(g_task_runner.Get()->BelongsToCurrentThread());
+ g_callbacks.Get().push_back(callback);
}
void RemoveActionCallback(const ActionCallback& callback) {
- g_callbacks.Get().RemoveCallback(callback);
+ DCHECK(g_task_runner.Get());
+ DCHECK(g_task_runner.Get()->BelongsToCurrentThread());
+ std::vector<ActionCallback>* callbacks = &g_callbacks.Get();
+ for (size_t i = 0; i < callbacks->size(); ++i) {
+ if (callbacks->at(i).Equals(callback)) {
Ilya Sherman 2016/04/13 19:23:15 nit: Please don't use at(), as it can throw an exc
beaudoin 2016/04/15 18:45:56 Done.
+ callbacks->erase(callbacks->begin() + i);
+ return;
+ }
+ }
+}
+void SetRecordActionTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
+ DCHECK(!g_task_runner.Get() || g_task_runner.Get()->BelongsToCurrentThread());
+ DCHECK(task_runner->BelongsToCurrentThread());
Ilya Sherman 2016/04/13 19:23:15 Optional nit: I'd swap the order of these DCHECKs
beaudoin 2016/04/15 18:45:56 Done.
+ g_task_runner.Get() = task_runner;
}
} // namespace base
« no previous file with comments | « base/metrics/user_metrics.h ('k') | base/test/user_action_tester.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698