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

Side by Side Diff: base/metrics/user_metrics.cc

Issue 229873002: [UMA] Fix a crash on logging user actions posted from a non-UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: De-nitting Created 6 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 | Annotate | Revision Log
« no previous file with comments | « base/metrics/user_metrics.h ('k') | content/browser/plugin_loader_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <vector> 7 #include <vector>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/threading/thread_checker.h"
10 11
11 namespace base { 12 namespace base {
12 namespace { 13 namespace {
13 14
14 base::LazyInstance<std::vector<ActionCallback> > g_action_callbacks = 15 // A helper class for tracking callbacks and ensuring thread-safety.
15 LAZY_INSTANCE_INITIALIZER; 16 class Callbacks {
17 public:
18 Callbacks() {}
16 19
17 void Record(const char *action) { 20 // Records the |action|.
18 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) 21 void Record(const std::string& action) {
19 g_action_callbacks.Get()[i].Run(action); 22 DCHECK(thread_checker_.CalledOnValidThread());
20 } 23 for (size_t i = 0; i < callbacks_.size(); ++i) {
24 callbacks_[i].Run(action);
25 }
26 }
27
28 // Adds |callback| to the list of |callbacks_|.
29 void AddCallback(const ActionCallback& callback) {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 callbacks_.push_back(callback);
32 }
33
34 // Removes the first instance of |callback| from the list of |callbacks_|, if
35 // there is one.
36 void RemoveCallback(const ActionCallback& callback) {
37 DCHECK(thread_checker_.CalledOnValidThread());
38 for (size_t i = 0; i < callbacks_.size(); ++i) {
39 if (callbacks_[i].Equals(callback)) {
40 callbacks_.erase(callbacks_.begin() + i);
41 return;
42 }
43 }
44 }
45
46 private:
47 base::ThreadChecker thread_checker_;
48 std::vector<ActionCallback> callbacks_;
49
50 DISALLOW_COPY_AND_ASSIGN(Callbacks);
51 };
52
53 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER;
21 54
22 } // namespace 55 } // namespace
23 56
24 void RecordAction(const UserMetricsAction& action) { 57 void RecordAction(const UserMetricsAction& action) {
25 Record(action.str_); 58 g_callbacks.Get().Record(action.str_);
26 } 59 }
27 60
28 void RecordComputedAction(const std::string& action) { 61 void RecordComputedAction(const std::string& action) {
29 Record(action.c_str()); 62 g_callbacks.Get().Record(action);
30 } 63 }
31 64
32 void AddActionCallback(const ActionCallback& callback) { 65 void AddActionCallback(const ActionCallback& callback) {
33 g_action_callbacks.Get().push_back(callback); 66 g_callbacks.Get().AddCallback(callback);
34 } 67 }
35 68
36 void RemoveActionCallback(const ActionCallback& callback) { 69 void RemoveActionCallback(const ActionCallback& callback) {
37 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) { 70 g_callbacks.Get().RemoveCallback(callback);
38 if (g_action_callbacks.Get()[i].Equals(callback)) { 71
39 g_action_callbacks.Get().erase(g_action_callbacks.Get().begin() + i);
40 return;
41 }
42 }
43 } 72 }
44 73
45 } // namespace base 74 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/user_metrics.h ('k') | content/browser/plugin_loader_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698