Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / chromecast / base / metrics / cast_metrics_helper.h
blobdd4f5f013df701f5be068886df1e204b0559b0c9
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
3 // found in the LICENSE file.
5 #ifndef CHROMECAST_BASE_METRICS_CAST_METRICS_HELPER_H_
6 #define CHROMECAST_BASE_METRICS_CAST_METRICS_HELPER_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
14 namespace base {
15 class SingleThreadTaskRunner;
18 namespace chromecast {
19 namespace metrics {
21 // Helper class for tracking complex metrics. This particularly includes
22 // playback metrics that span events across time, such as "time from app launch
23 // to video being rendered."
24 class CastMetricsHelper {
25 public:
26 enum BufferingType {
27 kInitialBuffering,
28 kBufferingAfterUnderrun,
29 kAbortedBuffering,
32 typedef base::Callback<void(const std::string&)> RecordActionCallback;
34 class MetricsSink {
35 public:
36 virtual ~MetricsSink() {}
38 virtual void OnAction(const std::string& action) = 0;
39 virtual void OnEnumerationEvent(const std::string& name,
40 int value, int num_buckets) = 0;
41 virtual void OnTimeEvent(const std::string& name,
42 const base::TimeDelta& value,
43 const base::TimeDelta& min,
44 const base::TimeDelta& max,
45 int num_buckets) = 0;
48 // Decodes action_name/app_id/session_id/sdk_version from metrics name.
49 // Return false if the metrics name is not generated from
50 // EncodeAppInfoIntoMetricsName() with correct format.
51 static bool DecodeAppInfoFromMetricsName(
52 const std::string& metrics_name,
53 std::string* action_name,
54 std::string* app_id,
55 std::string* session_id,
56 std::string* sdk_version);
58 static CastMetricsHelper* GetInstance();
60 explicit CastMetricsHelper(
61 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
62 virtual ~CastMetricsHelper();
64 // This function updates the info and stores the startup time of the current
65 // active application
66 virtual void UpdateCurrentAppInfo(const std::string& app_id,
67 const std::string& session_id);
68 // This function updates the sdk version of the current active application
69 virtual void UpdateSDKInfo(const std::string& sdk_version);
71 // Logs UMA record for media play/pause user actions.
72 virtual void LogMediaPlay();
73 virtual void LogMediaPause();
75 // Logs a simple UMA user action.
76 // This is used as an in-place replacement of content::RecordComputedAction().
77 virtual void RecordSimpleAction(const std::string& action);
79 // Logs UMA record of the time the app made its first paint.
80 virtual void LogTimeToFirstPaint();
82 // Logs UMA record of the elapsed time from the app launch
83 // to the time first video frame is displayed.
84 virtual void LogTimeToDisplayVideo();
86 // Logs UMA record of the time needed to re-buffer A/V.
87 virtual void LogTimeToBufferAv(BufferingType buffering_type,
88 base::TimeDelta time);
90 virtual void ResetVideoFrameSampling();
92 // Logs UMA statistics for video decoder and rendering data.
93 // Negative values are considered invalid and will not be logged.
94 virtual void LogFramesPer5Seconds(int displayed_frames, int dropped_frames,
95 int delayed_frames, int error_frames);
97 // Returns metrics name with app name between prefix and suffix.
98 virtual std::string GetMetricsNameWithAppName(
99 const std::string& prefix,
100 const std::string& suffix) const;
102 // Provides a MetricsSink instance to delegate UMA event logging.
103 // Once the delegate interface is set, CastMetricsHelper will not log UMA
104 // events internally unless SetMetricsSink(NULL) is called.
105 // CastMetricsHelper can only hold one MetricsSink instance.
106 // Caller retains ownership of MetricsSink.
107 virtual void SetMetricsSink(MetricsSink* delegate);
109 // Sets a default callback to record user action when MetricsSink is not set.
110 // This function could be called multiple times (in unittests), and
111 // CastMetricsHelper only honors the last one.
112 virtual void SetRecordActionCallback(const RecordActionCallback& callback);
114 protected:
115 // Creates a CastMetricsHelper instance with no MessageLoopProxy. This should
116 // only be used by tests, since invoking any non-overridden methods on this
117 // instance will cause a failure.
118 CastMetricsHelper();
120 private:
121 static std::string EncodeAppInfoIntoMetricsName(
122 const std::string& action_name,
123 const std::string& app_id,
124 const std::string& session_id,
125 const std::string& sdk_version);
127 void LogEnumerationHistogramEvent(const std::string& name,
128 int value, int num_buckets);
129 void LogTimeHistogramEvent(const std::string& name,
130 const base::TimeDelta& value,
131 const base::TimeDelta& min,
132 const base::TimeDelta& max,
133 int num_buckets);
134 void LogMediumTimeHistogramEvent(const std::string& name,
135 const base::TimeDelta& value);
137 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
139 // Start time of the most recent app.
140 base::TimeTicks app_start_time_;
142 // Currently running app id. Used to construct histogram name.
143 std::string app_id_;
144 std::string session_id_;
145 std::string sdk_version_;
147 // Whether a new app start time has been stored but not recorded.
148 // After the startup time has been used to generate an UMA event,
149 // this is set to false.
150 bool new_startup_time_;
152 base::TimeTicks previous_video_stat_sample_time_;
154 MetricsSink* metrics_sink_;
155 // Default RecordAction callback when metrics_sink_ is not set.
156 RecordActionCallback record_action_callback_;
158 DISALLOW_COPY_AND_ASSIGN(CastMetricsHelper);
161 } // namespace metrics
162 } // namespace chromecast
164 #endif // CHROMECAST_BASE_METRICS_CAST_METRICS_HELPER_H_