OLD | NEW |
---|---|
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 // Adds |callback| to the list of |callbacks_|. | |
19 void AddCallback(const ActionCallback& callback) { | |
20 DCHECK(thread_checker_.CalledOnValidThread()); | |
21 callbacks_.push_back(callback); | |
22 } | |
16 | 23 |
17 void Record(const char *action) { | 24 // Removes the first instance of |callback| from the list of |callbacks_|, if |
18 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) | 25 // there is one. |
19 g_action_callbacks.Get()[i].Run(action); | 26 void RemoveCallback(const ActionCallback& callback) { |
20 } | 27 DCHECK(thread_checker_.CalledOnValidThread()); |
28 for (size_t i = 0; i < callbacks_.size(); ++i) { | |
29 if (callbacks_[i].Equals(callback)) { | |
30 callbacks_.erase(callbacks_.begin() + i); | |
31 return; | |
32 } | |
33 } | |
34 } | |
35 | |
36 // Records the |action|. | |
37 void Record(const std::string& action) { | |
Alexei Svitkine (slow)
2014/04/09 15:03:44
Nit: Match order of the corresponding functions be
Ilya Sherman
2014/04/09 22:42:39
Done.
| |
38 DCHECK(thread_checker_.CalledOnValidThread()); | |
39 for (size_t i = 0; i < callbacks_.size(); ++i) { | |
40 callbacks_[i].Run(action); | |
41 } | |
42 } | |
43 | |
Alexei Svitkine (slow)
2014/04/09 15:03:44
Nit: Remove extra line.
Ilya Sherman
2014/04/09 22:42:39
Done.
| |
44 | |
45 private: | |
46 std::vector<ActionCallback> callbacks_; | |
47 base::ThreadChecker thread_checker_; | |
Alexei Svitkine (slow)
2014/04/09 15:03:44
Nit: Put this above callbacks to correspond to the
Ilya Sherman
2014/04/09 22:42:39
Done.
| |
48 }; | |
Alexei Svitkine (slow)
2014/04/09 15:03:44
Nit: DISALLOW_COPY_AND_ASSIGN()?
Ilya Sherman
2014/04/09 22:42:39
Done.
| |
49 | |
50 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER; | |
21 | 51 |
22 } // namespace | 52 } // namespace |
23 | 53 |
24 void RecordAction(const UserMetricsAction& action) { | 54 void RecordAction(const UserMetricsAction& action) { |
25 Record(action.str_); | 55 g_callbacks.Get().Record(action.str_); |
26 } | 56 } |
27 | 57 |
28 void RecordComputedAction(const std::string& action) { | 58 void RecordComputedAction(const std::string& action) { |
29 Record(action.c_str()); | 59 g_callbacks.Get().Record(action); |
30 } | 60 } |
31 | 61 |
32 void AddActionCallback(const ActionCallback& callback) { | 62 void AddActionCallback(const ActionCallback& callback) { |
33 g_action_callbacks.Get().push_back(callback); | 63 g_callbacks.Get().AddCallback(callback); |
34 } | 64 } |
35 | 65 |
36 void RemoveActionCallback(const ActionCallback& callback) { | 66 void RemoveActionCallback(const ActionCallback& callback) { |
37 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) { | 67 g_callbacks.Get().RemoveCallback(callback); |
38 if (g_action_callbacks.Get()[i].Equals(callback)) { | 68 |
39 g_action_callbacks.Get().erase(g_action_callbacks.Get().begin() + i); | |
40 return; | |
41 } | |
42 } | |
43 } | 69 } |
44 | 70 |
45 } // namespace base | 71 } // namespace base |
OLD | NEW |