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_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
15 class SingleThreadTaskRunner
;
18 namespace chromecast
{
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
{
28 kBufferingAfterUnderrun
,
32 typedef base::Callback
<void(const std::string
&)> RecordActionCallback
;
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
,
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
,
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
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 time needed to re-buffer A/V.
83 virtual void LogTimeToBufferAv(BufferingType buffering_type
,
84 base::TimeDelta time
);
86 // Returns metrics name with app name between prefix and suffix.
87 virtual std::string
GetMetricsNameWithAppName(
88 const std::string
& prefix
,
89 const std::string
& suffix
) const;
91 // Provides a MetricsSink instance to delegate UMA event logging.
92 // Once the delegate interface is set, CastMetricsHelper will not log UMA
93 // events internally unless SetMetricsSink(NULL) is called.
94 // CastMetricsHelper can only hold one MetricsSink instance.
95 // Caller retains ownership of MetricsSink.
96 virtual void SetMetricsSink(MetricsSink
* delegate
);
98 // Sets a default callback to record user action when MetricsSink is not set.
99 // This function could be called multiple times (in unittests), and
100 // CastMetricsHelper only honors the last one.
101 virtual void SetRecordActionCallback(const RecordActionCallback
& callback
);
104 // Creates a CastMetricsHelper instance with no task runner. This should only
105 // be used by tests, since invoking any non-overridden methods on this
106 // instance will cause a failure.
110 static std::string
EncodeAppInfoIntoMetricsName(
111 const std::string
& action_name
,
112 const std::string
& app_id
,
113 const std::string
& session_id
,
114 const std::string
& sdk_version
);
116 void LogEnumerationHistogramEvent(const std::string
& name
,
117 int value
, int num_buckets
);
118 void LogTimeHistogramEvent(const std::string
& name
,
119 const base::TimeDelta
& value
,
120 const base::TimeDelta
& min
,
121 const base::TimeDelta
& max
,
123 void LogMediumTimeHistogramEvent(const std::string
& name
,
124 const base::TimeDelta
& value
);
126 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
128 // Start time of the most recent app.
129 base::TimeTicks app_start_time_
;
131 // Currently running app id. Used to construct histogram name.
133 std::string session_id_
;
134 std::string sdk_version_
;
136 MetricsSink
* metrics_sink_
;
137 // Default RecordAction callback when metrics_sink_ is not set.
138 RecordActionCallback record_action_callback_
;
140 DISALLOW_COPY_AND_ASSIGN(CastMetricsHelper
);
143 } // namespace metrics
144 } // namespace chromecast
146 #endif // CHROMECAST_BASE_METRICS_CAST_METRICS_HELPER_H_