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 #include "chrome/browser/chromeos/login/error_screens_histogram_helper.h"
7 #include "base/metrics/histogram.h"
13 static const char kOobeErrorScreensCounterPrefix
[] = "OOBE.NetworkErrorShown.";
14 static const char kOobeTimeSpentOnErrorScreensPrefix
[] =
15 "OOBE.ErrorScreensTime.";
17 const base::TimeDelta time_min
= base::TimeDelta::FromMilliseconds(10);
18 const base::TimeDelta time_max
= base::TimeDelta::FromMinutes(3);
19 const int time_bucket_count
= 50;
21 std::string
ErrorToString(NetworkError::ErrorState error
) {
23 case NetworkError::ERROR_STATE_PORTAL
:
25 case NetworkError::ERROR_STATE_OFFLINE
:
27 case NetworkError::ERROR_STATE_PROXY
:
29 case NetworkError::ERROR_STATE_AUTH_EXT_TIMEOUT
:
30 return ".AuthExtTimeout";
32 NOTREACHED() << "Invalid ErrorState " << error
;
37 void StoreErrorScreenToHistogram(const std::string
& screen_name
,
38 NetworkError::ErrorState error
) {
39 if (error
<= NetworkError::ERROR_STATE_UNKNOWN
||
40 error
> NetworkError::ERROR_STATE_NONE
)
42 std::string histogram_name
= kOobeErrorScreensCounterPrefix
+ screen_name
;
43 int boundary
= NetworkError::ERROR_STATE_NONE
+ 1;
44 // This comes from UMA_HISTOGRAM_ENUMERATION macros. Can't use it because of
45 // non const histogram name.
46 base::HistogramBase
* histogram
= base::LinearHistogram::FactoryGet(
51 base::HistogramBase::kUmaTargetedHistogramFlag
);
52 histogram
->Add(error
);
55 void StoreTimeOnErrorScreenToHistogram(const std::string
& screen_name
,
56 NetworkError::ErrorState error
,
57 const base::TimeDelta
& time_delta
) {
58 if (error
<= NetworkError::ERROR_STATE_UNKNOWN
||
59 error
> NetworkError::ERROR_STATE_NONE
)
61 std::string histogram_name
=
62 kOobeTimeSpentOnErrorScreensPrefix
+ screen_name
+ ErrorToString(error
);
64 // This comes from UMA_HISTOGRAM_MEDIUM_TIMES macros. Can't use it because of
65 // non const histogram name.
66 base::HistogramBase
* histogram
= base::Histogram::FactoryTimeGet(
71 base::HistogramBase::kUmaTargetedHistogramFlag
);
73 histogram
->AddTime(time_delta
);
78 ErrorScreensHistogramHelper::ErrorScreensHistogramHelper(
79 const std::string
& screen_name
)
80 : screen_name_(screen_name
),
82 last_error_shown_(NetworkError::ERROR_STATE_NONE
) {
85 void ErrorScreensHistogramHelper::OnScreenShow() {
89 void ErrorScreensHistogramHelper::OnErrorShow(NetworkError::ErrorState error
) {
90 OnErrorShowTime(error
, base::Time::Now());
93 void ErrorScreensHistogramHelper::OnErrorShowTime(
94 NetworkError::ErrorState error
,
96 last_error_shown_
= error
;
97 if (error_screen_start_time_
.is_null())
98 error_screen_start_time_
= now
;
99 StoreErrorScreenToHistogram(screen_name_
, error
);
102 void ErrorScreensHistogramHelper::OnErrorHide() {
103 OnErrorHideTime(base::Time::Now());
106 void ErrorScreensHistogramHelper::OnErrorHideTime(base::Time now
) {
107 if (error_screen_start_time_
.is_null())
109 time_on_error_screens_
+= now
- error_screen_start_time_
;
110 error_screen_start_time_
= base::Time();
113 ErrorScreensHistogramHelper::~ErrorScreensHistogramHelper() {
115 if (last_error_shown_
== NetworkError::ERROR_STATE_NONE
) {
116 StoreErrorScreenToHistogram(screen_name_
, NetworkError::ERROR_STATE_NONE
);
118 if (!error_screen_start_time_
.is_null()) {
119 time_on_error_screens_
+= base::Time::Now() - error_screen_start_time_
;
120 error_screen_start_time_
= base::Time();
122 StoreTimeOnErrorScreenToHistogram(
123 screen_name_
, last_error_shown_
, time_on_error_screens_
);
128 } // namespace chromeos