forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_metrics.cc
74 lines (56 loc) · 1.85 KB
/
user_metrics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/user_metrics.h"
#include <vector>
#include "base/lazy_instance.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;
} // namespace
void RecordAction(const UserMetricsAction& action) {
g_callbacks.Get().Record(action.str_);
}
void RecordComputedAction(const std::string& action) {
g_callbacks.Get().Record(action);
}
void AddActionCallback(const ActionCallback& callback) {
g_callbacks.Get().AddCallback(callback);
}
void RemoveActionCallback(const ActionCallback& callback) {
g_callbacks.Get().RemoveCallback(callback);
}
} // namespace base