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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 #include "base/metrics/user_metrics.h" 5 #include "base/metrics/user_metrics.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h"
11 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/location.h"
12 #include "base/macros.h" 14 #include "base/macros.h"
13 #include "base/threading/thread_checker.h"
14 15
15 namespace base { 16 namespace base {
16 namespace { 17 namespace {
17 18
18 // A helper class for tracking callbacks and ensuring thread-safety. 19 // A helper class for tracking callbacks and ensuring thread-safety.
19 class Callbacks { 20 class Callbacks {
20 public: 21 public:
21 Callbacks() {} 22 Callbacks() {}
22 23
23 // Records the |action|. 24 // Records the |action|.
24 void Record(const std::string& action) { 25 void Record(const std::string& action) {
25 DCHECK(thread_checker_.CalledOnValidThread());
26 for (size_t i = 0; i < callbacks_.size(); ++i) { 26 for (size_t i = 0; i < callbacks_.size(); ++i) {
27 callbacks_[i].Run(action); 27 callbacks_[i].Run(action);
28 } 28 }
29 } 29 }
30 30
31 // Adds |callback| to the list of |callbacks_|. 31 // Adds |callback| to the list of |callbacks_|.
32 void AddCallback(const ActionCallback& callback) { 32 void AddCallback(const ActionCallback& callback) {
33 DCHECK(thread_checker_.CalledOnValidThread());
34 callbacks_.push_back(callback); 33 callbacks_.push_back(callback);
35 } 34 }
36 35
37 // Removes the first instance of |callback| from the list of |callbacks_|, if 36 // Removes the first instance of |callback| from the list of |callbacks_|, if
38 // there is one. 37 // there is one.
39 void RemoveCallback(const ActionCallback& callback) { 38 void RemoveCallback(const ActionCallback& callback) {
40 DCHECK(thread_checker_.CalledOnValidThread());
41 for (size_t i = 0; i < callbacks_.size(); ++i) { 39 for (size_t i = 0; i < callbacks_.size(); ++i) {
42 if (callbacks_[i].Equals(callback)) { 40 if (callbacks_[i].Equals(callback)) {
43 callbacks_.erase(callbacks_.begin() + i); 41 callbacks_.erase(callbacks_.begin() + i);
44 return; 42 return;
45 } 43 }
46 } 44 }
47 } 45 }
48 46
49 private: 47 private:
50 base::ThreadChecker thread_checker_;
51 std::vector<ActionCallback> callbacks_; 48 std::vector<ActionCallback> callbacks_;
52 49
53 DISALLOW_COPY_AND_ASSIGN(Callbacks); 50 DISALLOW_COPY_AND_ASSIGN(Callbacks);
54 }; 51 };
Ilya Sherman 2016/04/06 02:36:28 I don't think that it's appropriate, or necessary,
beaudoin 2016/04/06 15:06:13 Ok. I've removed the thread hop for Add/Remove and
Ilya Sherman 2016/04/06 21:48:48 Yeah, I'd be fine with the DCHECKs you described,
beaudoin 2016/04/07 18:12:34 Done.
55 52
56 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER; 53 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER;
54 base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner>> g_task_runner =
55 LAZY_INSTANCE_INITIALIZER;
57 56
58 } // namespace 57 } // namespace
59 58
60 void RecordAction(const UserMetricsAction& action) { 59 void RecordAction(const UserMetricsAction& action) {
61 g_callbacks.Get().Record(action.str_); 60 RecordComputedAction(action.str_);
62 } 61 }
63 62
64 void RecordComputedAction(const std::string& action) { 63 void RecordComputedAction(const std::string& action) {
64 DCHECK(g_task_runner.Get());
65 if (!g_task_runner.Get()->BelongsToCurrentThread()) {
66 g_task_runner.Get()->PostTask(FROM_HERE,
67 base::Bind(&RecordComputedAction, action));
68 return;
69 }
65 g_callbacks.Get().Record(action); 70 g_callbacks.Get().Record(action);
66 } 71 }
67 72
68 void AddActionCallback(const ActionCallback& callback) { 73 void AddActionCallback(const ActionCallback& callback) {
74 DCHECK(g_task_runner.Get());
75 if (!g_task_runner.Get()->BelongsToCurrentThread()) {
76 g_task_runner.Get()->PostTask(FROM_HERE,
77 base::Bind(&AddActionCallback, callback));
78 return;
79 }
69 g_callbacks.Get().AddCallback(callback); 80 g_callbacks.Get().AddCallback(callback);
70 } 81 }
71 82
72 void RemoveActionCallback(const ActionCallback& callback) { 83 void RemoveActionCallback(const ActionCallback& callback) {
84 DCHECK(g_task_runner.Get());
85 if (!g_task_runner.Get()->BelongsToCurrentThread()) {
86 g_task_runner.Get()->PostTask(FROM_HERE,
87 base::Bind(&RemoveActionCallback, callback));
88 return;
89 }
73 g_callbacks.Get().RemoveCallback(callback); 90 g_callbacks.Get().RemoveCallback(callback);
91 }
74 92
93 void SetRecordActionTaskRunner(
94 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
95 if (g_task_runner.Get() && !g_task_runner.Get()->BelongsToCurrentThread()) {
96 g_task_runner.Get()->PostTask(FROM_HERE,
97 base::Bind(&SetRecordActionTaskRunner, task_runner));
Ilya Sherman 2016/04/06 02:36:28 The task runner should only be set once, IMO, so I
beaudoin 2016/04/06 15:06:13 Done.
98 return;
99 }
100 g_task_runner.Get() = task_runner;
75 } 101 }
76 102
77 } // namespace base 103 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698