Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / metrics / android_metrics_provider.cc
blob94aadafe1893a62f379294d09702d2336bb3573c
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/metrics/android_metrics_provider.h"
7 #include "base/metrics/histogram.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/sys_info.h"
12 #include "base/values.h"
13 #include "chrome/browser/android/feature_utilities.h"
14 #include "chrome/common/pref_names.h"
16 namespace {
18 // Increments a particular entry in the ListValue.
19 void IncrementListValue(base::ListValue* counts, int index) {
20 int current_count = 0;
21 counts->GetInteger(index, &current_count);
22 counts->Set(index, new base::FundamentalValue(current_count + 1));
25 // Takes an int corresponding to a Type and returns the corresponding flag.
26 int GetActivityFlag(int type_id) {
27 ActivityTypeIds::Type type = ActivityTypeIds::GetActivityType(type_id);
28 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE);
29 return (1 << type);
32 } // namespace
34 AndroidMetricsProvider::AndroidMetricsProvider(PrefService* local_state)
35 : local_state_(local_state) {
36 LogStabilityToPrefs();
39 AndroidMetricsProvider::~AndroidMetricsProvider() {
42 void AndroidMetricsProvider::ProvideStabilityMetrics(
43 metrics::SystemProfileProto* system_profile_proto) {
44 ConvertStabilityPrefsToHistograms();
47 void AndroidMetricsProvider::ProvideGeneralMetrics(
48 metrics::ChromeUserMetricsExtension* uma_proto) {
49 UMA_HISTOGRAM_ENUMERATION(
50 "DocumentActivity.Enabled",
51 chrome::android::GetDocumentModeValue(),
52 chrome::android::RUNNING_MODE_MAX);
53 UMA_HISTOGRAM_ENUMERATION(
54 "CustomTabs.Visible",
55 chrome::android::GetCustomTabsVisibleValue(),
56 chrome::android::CUSTOM_TABS_VISIBILITY_MAX);
57 UMA_HISTOGRAM_BOOLEAN(
58 "MemoryAndroid.LowRamDevice",
59 base::SysInfo::IsLowEndDevice());
62 void AndroidMetricsProvider::OnForegroundActivityChanged(
63 ActivityTypeIds::Type type) {
64 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE);
66 if (type == local_state_->GetInteger(prefs::kStabilityForegroundActivityType))
67 return;
69 // Record that the Activity is now in the foreground.
70 local_state_->SetInteger(prefs::kStabilityForegroundActivityType, type);
72 // Record that the Activity was launched this sesaion.
73 // The pref stores a set of flags ORed together, where each set flag
74 // corresponds to a launched Activity type.
75 int launched =
76 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags);
77 if (type != ActivityTypeIds::ACTIVITY_NONE) {
78 launched |= GetActivityFlag(type);
79 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched);
82 local_state_->CommitPendingWrite();
85 // static
86 void AndroidMetricsProvider::RegisterPrefs(PrefRegistrySimple* registry) {
87 registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType,
88 ActivityTypeIds::ACTIVITY_NONE);
89 registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0);
90 registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts);
91 registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts);
94 void AndroidMetricsProvider::LogStabilityToPrefs() {
95 // Track which Activities were launched by the user.
96 // A 'launch' is defined as starting the Activity at least once during a
97 // UMA session. Multiple launches are counted only once since it is possible
98 // for users to hop between Activities (e.g. entering and leaving Settings).
99 const int launched =
100 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags);
101 ListPrefUpdate update_launches(local_state_,
102 prefs::kStabilityLaunchedActivityCounts);
103 base::ListValue* launch_counts = update_launches.Get();
104 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE;
105 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE;
106 ++activity_type) {
107 if (launched & GetActivityFlag(activity_type))
108 IncrementListValue(launch_counts, activity_type);
110 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0);
112 // Track any Activities that were in the foreground when Chrome died.
113 // These Activities failed to be recorded as leaving the foreground, so Chrome
114 // couldn't have ended the UMA session cleanly. Record them as crashing.
115 const int foreground =
116 local_state_->GetInteger(prefs::kStabilityForegroundActivityType);
117 if (foreground != ActivityTypeIds::ACTIVITY_NONE) {
118 ListPrefUpdate update_crashes(local_state_,
119 prefs::kStabilityCrashedActivityCounts);
120 base::ListValue* crash_counts = update_crashes.Get();
121 IncrementListValue(crash_counts, foreground);
122 local_state_->SetInteger(prefs::kStabilityForegroundActivityType,
123 ActivityTypeIds::ACTIVITY_NONE);
126 local_state_->CommitPendingWrite();
129 void AndroidMetricsProvider::ConvertStabilityPrefsToHistograms() {
130 ListPrefUpdate launch_updater(local_state_,
131 prefs::kStabilityLaunchedActivityCounts);
132 ListPrefUpdate crash_updater(local_state_,
133 prefs::kStabilityCrashedActivityCounts);
135 base::ListValue* launch_counts = launch_updater.Get();
136 base::ListValue* crash_counts = crash_updater.Get();
138 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE;
139 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE;
140 ++activity_type) {
141 int launch_count = 0;
142 int crash_count = 0;
144 launch_counts->GetInteger(activity_type, &launch_count);
145 crash_counts->GetInteger(activity_type, &crash_count);
147 for (int count = 0; count < launch_count; ++count) {
148 UMA_STABILITY_HISTOGRAM_ENUMERATION(
149 "Chrome.Android.Activity.LaunchCounts",
150 activity_type,
151 ActivityTypeIds::ACTIVITY_MAX_VALUE);
154 for (int count = 0; count < crash_count; ++count) {
155 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts",
156 activity_type,
157 ActivityTypeIds::ACTIVITY_MAX_VALUE);
161 launch_counts->Clear();
162 crash_counts->Clear();