blob: 9511f66672ac1efcd4b1794afe07ec73d5b7e9d0 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2014 The Chromium Authors
[email protected]e6e30ac2014-01-13 21:24:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/metrics/user_metrics.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
[email protected]e6e30ac2014-01-13 21:24:399#include <vector>
10
beaudoind5c435e2016-04-22 14:17:1011#include "base/bind.h"
[email protected]e6e30ac2014-01-13 21:24:3912#include "base/lazy_instance.h"
beaudoind5c435e2016-04-22 14:17:1013#include "base/location.h"
[email protected]abca0982014-04-10 01:12:3614#include "base/threading/thread_checker.h"
bttkc8a31b702020-02-25 02:55:1715#include "base/time/time.h"
Eric Secklerf6c544f2020-06-02 10:49:2116#include "base/trace_event/base_tracing.h"
[email protected]e6e30ac2014-01-13 21:24:3917
18namespace base {
19namespace {
20
scottmg5e65e3a2017-03-08 08:48:4621LazyInstance<std::vector<ActionCallback>>::DestructorAtExit g_callbacks =
beaudoind5c435e2016-04-22 14:17:1022 LAZY_INSTANCE_INITIALIZER;
scottmg5e65e3a2017-03-08 08:48:4623LazyInstance<scoped_refptr<SingleThreadTaskRunner>>::DestructorAtExit
24 g_task_runner = LAZY_INSTANCE_INITIALIZER;
[email protected]e6e30ac2014-01-13 21:24:3925
26} // namespace
27
28void RecordAction(const UserMetricsAction& action) {
beaudoind5c435e2016-04-22 14:17:1029 RecordComputedAction(action.str_);
[email protected]e6e30ac2014-01-13 21:24:3930}
31
32void RecordComputedAction(const std::string& action) {
bttkc8a31b702020-02-25 02:55:1733 RecordComputedActionAt(action, TimeTicks::Now());
34}
35
bttk211b8512020-03-18 04:24:0136void RecordComputedActionSince(const std::string& action,
37 TimeDelta time_since) {
38 RecordComputedActionAt(action, TimeTicks::Now() - time_since);
39}
40
bttkc8a31b702020-02-25 02:55:1741void RecordComputedActionAt(const std::string& action, TimeTicks action_time) {
bttkc8a31b702020-02-25 02:55:1742 TRACE_EVENT_INSTANT1("ui", "UserEvent", TRACE_EVENT_SCOPE_GLOBAL, "action",
43 action);
beaudoind5c435e2016-04-22 14:17:1044 if (!g_task_runner.Get()) {
45 DCHECK(g_callbacks.Get().empty());
46 return;
47 }
48
49 if (!g_task_runner.Get()->BelongsToCurrentThread()) {
bttkc8a31b702020-02-25 02:55:1750 g_task_runner.Get()->PostTask(
51 FROM_HERE, BindOnce(&RecordComputedActionAt, action, action_time));
beaudoind5c435e2016-04-22 14:17:1052 return;
53 }
54
55 for (const ActionCallback& callback : g_callbacks.Get()) {
bttkc8a31b702020-02-25 02:55:1756 callback.Run(action, action_time);
beaudoind5c435e2016-04-22 14:17:1057 }
[email protected]e6e30ac2014-01-13 21:24:3958}
59
60void AddActionCallback(const ActionCallback& callback) {
beaudoind5c435e2016-04-22 14:17:1061 // Only allow adding a callback if the task runner is set.
62 DCHECK(g_task_runner.Get());
63 DCHECK(g_task_runner.Get()->BelongsToCurrentThread());
64 g_callbacks.Get().push_back(callback);
[email protected]e6e30ac2014-01-13 21:24:3965}
66
67void RemoveActionCallback(const ActionCallback& callback) {
beaudoind5c435e2016-04-22 14:17:1068 DCHECK(g_task_runner.Get());
69 DCHECK(g_task_runner.Get()->BelongsToCurrentThread());
70 std::vector<ActionCallback>* callbacks = g_callbacks.Pointer();
Peter Kastingfc94f5062022-06-08 16:41:4571 const auto i = std::find(callbacks->begin(), callbacks->end(), callback);
72 if (i != callbacks->end())
73 callbacks->erase(i);
beaudoind5c435e2016-04-22 14:17:1074}
[email protected]abca0982014-04-10 01:12:3675
beaudoind5c435e2016-04-22 14:17:1076void SetRecordActionTaskRunner(
77 scoped_refptr<SingleThreadTaskRunner> task_runner) {
78 DCHECK(task_runner->BelongsToCurrentThread());
79 DCHECK(!g_task_runner.Get() || g_task_runner.Get()->BelongsToCurrentThread());
80 g_task_runner.Get() = task_runner;
[email protected]e6e30ac2014-01-13 21:24:3981}
82
ssid70aefba42020-02-04 19:34:0383scoped_refptr<SingleThreadTaskRunner> GetRecordActionTaskRunner() {
84 if (g_task_runner.IsCreated())
85 return g_task_runner.Get();
86 return nullptr;
87}
88
[email protected]e6e30ac2014-01-13 21:24:3989} // namespace base