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

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

Issue 1014543003: Add an IPC method for Blink to call RapporService::RecordSample() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed to ETLD_PLUS_ONE Created 5 years, 9 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 <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 #include "base/threading/thread_checker.h"
11 11
12 namespace base { 12 namespace base {
13 namespace { 13 namespace {
14 14
15 // A helper class for tracking callbacks and ensuring thread-safety. 15 // A helper class for tracking callbacks and ensuring thread-safety.
16 class Callbacks { 16 class Callbacks {
17 public: 17 public:
18 Callbacks() {} 18 Callbacks() {}
19 19
20 // Records the |action|. 20 // Records the |action|.
21 void Record(const std::string& action) { 21 void Record(const std::string& action) {
22 DCHECK(thread_checker_.CalledOnValidThread()); 22 DCHECK(thread_checker_.CalledOnValidThread());
23 for (size_t i = 0; i < callbacks_.size(); ++i) { 23 for (size_t i = 0; i < callbacks_.size(); ++i) {
24 callbacks_[i].Run(action); 24 callbacks_[i].Run(action);
25 } 25 }
26 } 26 }
27 27
28 void Record(const std::string& metric, const std::string& sample) {
29 DCHECK(thread_checker_.CalledOnValidThread());
30 if (!rappor_callback_.is_null())
31 rappor_callback_.Run(metric, sample);
32 }
33
28 // Adds |callback| to the list of |callbacks_|. 34 // Adds |callback| to the list of |callbacks_|.
29 void AddCallback(const ActionCallback& callback) { 35 void AddCallback(const ActionCallback& callback) {
30 DCHECK(thread_checker_.CalledOnValidThread()); 36 DCHECK(thread_checker_.CalledOnValidThread());
31 callbacks_.push_back(callback); 37 callbacks_.push_back(callback);
32 } 38 }
33 39
34 // Removes the first instance of |callback| from the list of |callbacks_|, if 40 // Removes the first instance of |callback| from the list of |callbacks_|, if
35 // there is one. 41 // there is one.
36 void RemoveCallback(const ActionCallback& callback) { 42 void RemoveCallback(const ActionCallback& callback) {
37 DCHECK(thread_checker_.CalledOnValidThread()); 43 DCHECK(thread_checker_.CalledOnValidThread());
38 for (size_t i = 0; i < callbacks_.size(); ++i) { 44 for (size_t i = 0; i < callbacks_.size(); ++i) {
39 if (callbacks_[i].Equals(callback)) { 45 if (callbacks_[i].Equals(callback)) {
40 callbacks_.erase(callbacks_.begin() + i); 46 callbacks_.erase(callbacks_.begin() + i);
41 return; 47 return;
42 } 48 }
43 } 49 }
44 } 50 }
45 51
52 void AddCallback(const RapporCallback& callback) {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 DCHECK(rappor_callback_.is_null());
55 rappor_callback_ = callback;
56 }
57
46 private: 58 private:
47 base::ThreadChecker thread_checker_; 59 base::ThreadChecker thread_checker_;
48 std::vector<ActionCallback> callbacks_; 60 std::vector<ActionCallback> callbacks_;
61 RapporCallback rappor_callback_;
49 62
50 DISALLOW_COPY_AND_ASSIGN(Callbacks); 63 DISALLOW_COPY_AND_ASSIGN(Callbacks);
51 }; 64 };
52 65
53 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER; 66 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER;
54 67
55 } // namespace 68 } // namespace
56 69
57 void RecordAction(const UserMetricsAction& action) { 70 void RecordAction(const UserMetricsAction& action) {
58 g_callbacks.Get().Record(action.str_); 71 g_callbacks.Get().Record(action.str_);
59 } 72 }
60 73
61 void RecordComputedAction(const std::string& action) { 74 void RecordComputedAction(const std::string& action) {
62 g_callbacks.Get().Record(action); 75 g_callbacks.Get().Record(action);
63 } 76 }
64 77
78 void RecordRappor(const std::string& metric, const std::string& sample) {
79 g_callbacks.Get().Record(metric, sample);
80 }
81
65 void AddActionCallback(const ActionCallback& callback) { 82 void AddActionCallback(const ActionCallback& callback) {
66 g_callbacks.Get().AddCallback(callback); 83 g_callbacks.Get().AddCallback(callback);
67 } 84 }
68 85
69 void RemoveActionCallback(const ActionCallback& callback) { 86 void RemoveActionCallback(const ActionCallback& callback) {
70 g_callbacks.Get().RemoveCallback(callback); 87 g_callbacks.Get().RemoveCallback(callback);
88 }
71 89
90 void AddRapporCallback(const RapporCallback& callback) {
91 g_callbacks.Get().AddCallback(callback);
72 } 92 }
73 93
74 } // namespace base 94 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698