ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / error_screens_histogram_helper.cc
blob94a7ff6722455271d471c0c6eb93124b377d41c3
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"
9 namespace chromeos {
11 namespace {
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) {
22 switch (error) {
23 case NetworkError::ERROR_STATE_PORTAL:
24 return ".Portal";
25 case NetworkError::ERROR_STATE_OFFLINE:
26 return ".Offline";
27 case NetworkError::ERROR_STATE_PROXY:
28 return ".Proxy";
29 case NetworkError::ERROR_STATE_AUTH_EXT_TIMEOUT:
30 return ".AuthExtTimeout";
31 default:
32 NOTREACHED() << "Invalid ErrorState " << error;
33 return std::string();
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)
41 return;
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(
47 histogram_name,
49 boundary,
50 boundary + 1,
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)
60 return;
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(
67 histogram_name,
68 time_min,
69 time_max,
70 time_bucket_count,
71 base::HistogramBase::kUmaTargetedHistogramFlag);
73 histogram->AddTime(time_delta);
76 } // namespace
78 ErrorScreensHistogramHelper::ErrorScreensHistogramHelper(
79 const std::string& screen_name)
80 : screen_name_(screen_name),
81 was_shown_(false),
82 last_error_shown_(NetworkError::ERROR_STATE_NONE) {
85 void ErrorScreensHistogramHelper::OnScreenShow() {
86 was_shown_ = true;
89 void ErrorScreensHistogramHelper::OnErrorShow(NetworkError::ErrorState error) {
90 OnErrorShowTime(error, base::Time::Now());
93 void ErrorScreensHistogramHelper::OnErrorShowTime(
94 NetworkError::ErrorState error,
95 base::Time now) {
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())
108 return;
109 time_on_error_screens_ += now - error_screen_start_time_;
110 error_screen_start_time_ = base::Time();
113 ErrorScreensHistogramHelper::~ErrorScreensHistogramHelper() {
114 if (was_shown_) {
115 if (last_error_shown_ == NetworkError::ERROR_STATE_NONE) {
116 StoreErrorScreenToHistogram(screen_name_, NetworkError::ERROR_STATE_NONE);
117 } else {
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