1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_METRICS_EVENTS_METRICS_MANAGER_H_
6 #define CC_METRICS_EVENTS_METRICS_MANAGER_H_
11 #include "base/callback.h"
12 #include "cc/cc_export.h"
13 #include "cc/metrics/event_metrics.h"
17 // Manages a list of active EventMetrics objects. Each thread (main or impl) has
18 // its own instance of this class to help it determine which events have led to
20 class CC_EXPORT EventsMetricsManager {
22 // This interface is used to denote the scope of an event handling. The scope
23 // is started as soon as an instance is constructed and ended when the
24 // instance is destructed. EventsMetricsManager uses this to determine whether
25 // a frame update has happened due to handling of a specific event or not.
26 // Since it is possible to have nested event loops, scoped monitors can be
30 // Type of the callback function that is called after a scoped monitor goes
31 // out of scope. `handled` specifies whether the corresponding event was
32 // handled in which case the function should return its corresponding
33 // `EventMetrics` object for reporting purposes. Since calling this callback
34 // denotes the end of event processing, clients can use this to set relevant
35 // timestamps to the metrics object, before potentially returning it.
37 base::OnceCallback<std::unique_ptr<EventMetrics>(bool handled)>;
40 virtual ~ScopedMonitor();
42 virtual void SetSaveMetrics() = 0;
44 ScopedMonitor(const ScopedMonitor&) = delete;
45 ScopedMonitor& operator=(const ScopedMonitor&) = delete;
48 EventsMetricsManager();
49 ~EventsMetricsManager();
51 EventsMetricsManager(const EventsMetricsManager&) = delete;
52 EventsMetricsManager& operator=(const EventsMetricsManager&) = delete;
54 // Called by clients when they start handling an event. Destruction of the
55 // scoped monitor indicates the end of event handling which ends in calling
56 // `done_callback`. `done_callback` is allowed to be a null callback, which
57 // means the client is not interested in reporting metrics for the event being
58 // handled in this specific scope.
59 std::unique_ptr<ScopedMonitor> GetScopedMonitor(
60 ScopedMonitor::DoneCallback done_callback);
62 // Called by clients when a frame needs to be produced. If any scoped monitor
63 // is active at this time, its corresponding event metrics would be marked to
64 // be saved when it goes out of scope.
65 void SaveActiveEventMetrics();
67 // Empties the list of saved EventMetrics objects, returning them to the
69 EventMetrics::List TakeSavedEventsMetrics();
71 size_t saved_events_metrics_count_for_testing() const {
72 return saved_events_.size();
76 class ScopedMonitorImpl;
78 // Called when the most nested scoped monitor is destroyed. If the monitored
79 // metrics need to be saved it will be passed in as `metrics`.
80 void OnScopedMonitorEnded(std::unique_ptr<EventMetrics> metrics);
82 // Stack of active, potentially nested, scoped monitors.
83 std::vector<ScopedMonitorImpl*> active_scoped_monitors_;
85 // List of event metrics saved for reporting.
86 EventMetrics::List saved_events_;
91 #endif // CC_METRICS_EVENTS_METRICS_MANAGER_H_