Componentize HistoryURLProvider/ScoredHistoryMatch.
[chromium-blink-merge.git] / chrome / browser / metrics / android_metrics_provider.cc
blob25400e80c14a73ea55e912a585ce60d3d15fb80a
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/values.h"
12 #include "chrome/browser/android/feature_utilities.h"
13 #include "chrome/common/pref_names.h"
15 namespace {
17 // Increments a particular entry in the ListValue.
18 void IncrementListValue(base::ListValue* counts, int index) {
19 int current_count = 0;
20 counts->GetInteger(index, &current_count);
21 counts->Set(index, new base::FundamentalValue(current_count + 1));
24 // Takes an int corresponding to a Type and returns the corresponding flag.
25 int GetActivityFlag(int type_id) {
26 ActivityTypeIds::Type type = ActivityTypeIds::GetActivityType(type_id);
27 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE);
28 return (1 << type);
31 } // namespace
33 AndroidMetricsProvider::AndroidMetricsProvider(PrefService* local_state)
34 : local_state_(local_state) {
35 LogStabilityToPrefs();
38 AndroidMetricsProvider::~AndroidMetricsProvider() {
41 void AndroidMetricsProvider::ProvideStabilityMetrics(
42 metrics::SystemProfileProto* system_profile_proto) {
43 ConvertStabilityPrefsToHistograms();
46 void AndroidMetricsProvider::ProvideGeneralMetrics(
47 metrics::ChromeUserMetricsExtension* uma_proto) {
48 UMA_HISTOGRAM_ENUMERATION(
49 "DocumentActivity.Enabled",
50 chrome::android::GetDocumentModeValue(),
51 chrome::android::RUNNING_MODE_MAX);
52 UMA_HISTOGRAM_ENUMERATION(
53 "CustomTabs.Visible",
54 chrome::android::GetCustomTabsVisibleValue(),
55 chrome::android::CUSTOM_TABS_VISIBILITY_MAX);
58 void AndroidMetricsProvider::OnForegroundActivityChanged(
59 ActivityTypeIds::Type type) {
60 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE);
62 if (type == local_state_->GetInteger(prefs::kStabilityForegroundActivityType))
63 return;
65 // Record that the Activity is now in the foreground.
66 local_state_->SetInteger(prefs::kStabilityForegroundActivityType, type);
68 // Record that the Activity was launched this sesaion.
69 // The pref stores a set of flags ORed together, where each set flag
70 // corresponds to a launched Activity type.
71 int launched =
72 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags);
73 if (type != ActivityTypeIds::ACTIVITY_NONE) {
74 launched |= GetActivityFlag(type);
75 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched);
78 local_state_->CommitPendingWrite();
81 // static
82 void AndroidMetricsProvider::RegisterPrefs(PrefRegistrySimple* registry) {
83 registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType,
84 ActivityTypeIds::ACTIVITY_NONE);
85 registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0);
86 registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts);
87 registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts);
90 void AndroidMetricsProvider::LogStabilityToPrefs() {
91 // Track which Activities were launched by the user.
92 // A 'launch' is defined as starting the Activity at least once during a
93 // UMA session. Multiple launches are counted only once since it is possible
94 // for users to hop between Activities (e.g. entering and leaving Settings).
95 const int launched =
96 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags);
97 ListPrefUpdate update_launches(local_state_,
98 prefs::kStabilityLaunchedActivityCounts);
99 base::ListValue* launch_counts = update_launches.Get();
100 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE;
101 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE;
102 ++activity_type) {
103 if (launched & GetActivityFlag(activity_type))
104 IncrementListValue(launch_counts, activity_type);
106 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0);
108 // Track any Activities that were in the foreground when Chrome died.
109 // These Activities failed to be recorded as leaving the foreground, so Chrome
110 // couldn't have ended the UMA session cleanly. Record them as crashing.
111 const int foreground =
112 local_state_->GetInteger(prefs::kStabilityForegroundActivityType);
113 if (foreground != ActivityTypeIds::ACTIVITY_NONE) {
114 ListPrefUpdate update_crashes(local_state_,
115 prefs::kStabilityCrashedActivityCounts);
116 base::ListValue* crash_counts = update_crashes.Get();
117 IncrementListValue(crash_counts, foreground);
118 local_state_->SetInteger(prefs::kStabilityForegroundActivityType,
119 ActivityTypeIds::ACTIVITY_NONE);
122 local_state_->CommitPendingWrite();
125 void AndroidMetricsProvider::ConvertStabilityPrefsToHistograms() {
126 ListPrefUpdate launch_updater(local_state_,
127 prefs::kStabilityLaunchedActivityCounts);
128 ListPrefUpdate crash_updater(local_state_,
129 prefs::kStabilityCrashedActivityCounts);
131 base::ListValue* launch_counts = launch_updater.Get();
132 base::ListValue* crash_counts = crash_updater.Get();
134 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE;
135 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE;
136 ++activity_type) {
137 int launch_count = 0;
138 int crash_count = 0;
140 launch_counts->GetInteger(activity_type, &launch_count);
141 crash_counts->GetInteger(activity_type, &crash_count);
143 for (int count = 0; count < launch_count; ++count) {
144 UMA_STABILITY_HISTOGRAM_ENUMERATION(
145 "Chrome.Android.Activity.LaunchCounts",
146 activity_type,
147 ActivityTypeIds::ACTIVITY_MAX_VALUE);
150 for (int count = 0; count < crash_count; ++count) {
151 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts",
152 activity_type,
153 ActivityTypeIds::ACTIVITY_MAX_VALUE);
157 launch_counts->Clear();
158 crash_counts->Clear();