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"
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
, ¤t_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
);
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
);
54 void AndroidMetricsProvider::OnForegroundActivityChanged(
55 ActivityTypeIds::Type type
) {
56 DCHECK_LT(type
, ActivityTypeIds::ACTIVITY_MAX_VALUE
);
58 if (type
== local_state_
->GetInteger(prefs::kStabilityForegroundActivityType
))
61 // Record that the Activity is now in the foreground.
62 local_state_
->SetInteger(prefs::kStabilityForegroundActivityType
, type
);
64 // Record that the Activity was launched this sesaion.
65 // The pref stores a set of flags ORed together, where each set flag
66 // corresponds to a launched Activity type.
68 local_state_
->GetInteger(prefs::kStabilityLaunchedActivityFlags
);
69 if (type
!= ActivityTypeIds::ACTIVITY_NONE
) {
70 launched
|= GetActivityFlag(type
);
71 local_state_
->SetInteger(prefs::kStabilityLaunchedActivityFlags
, launched
);
74 local_state_
->CommitPendingWrite();
78 void AndroidMetricsProvider::RegisterPrefs(PrefRegistrySimple
* registry
) {
79 registry
->RegisterIntegerPref(prefs::kStabilityForegroundActivityType
,
80 ActivityTypeIds::ACTIVITY_NONE
);
81 registry
->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags
, 0);
82 registry
->RegisterListPref(prefs::kStabilityLaunchedActivityCounts
);
83 registry
->RegisterListPref(prefs::kStabilityCrashedActivityCounts
);
86 void AndroidMetricsProvider::LogStabilityToPrefs() {
87 // Track which Activities were launched by the user.
88 // A 'launch' is defined as starting the Activity at least once during a
89 // UMA session. Multiple launches are counted only once since it is possible
90 // for users to hop between Activities (e.g. entering and leaving Settings).
92 local_state_
->GetInteger(prefs::kStabilityLaunchedActivityFlags
);
93 ListPrefUpdate
update_launches(local_state_
,
94 prefs::kStabilityLaunchedActivityCounts
);
95 base::ListValue
* launch_counts
= update_launches
.Get();
96 for (int activity_type
= ActivityTypeIds::ACTIVITY_NONE
;
97 activity_type
< ActivityTypeIds::ACTIVITY_MAX_VALUE
;
99 if (launched
& GetActivityFlag(activity_type
))
100 IncrementListValue(launch_counts
, activity_type
);
102 local_state_
->SetInteger(prefs::kStabilityLaunchedActivityFlags
, 0);
104 // Track any Activities that were in the foreground when Chrome died.
105 // These Activities failed to be recorded as leaving the foreground, so Chrome
106 // couldn't have ended the UMA session cleanly. Record them as crashing.
107 const int foreground
=
108 local_state_
->GetInteger(prefs::kStabilityForegroundActivityType
);
109 if (foreground
!= ActivityTypeIds::ACTIVITY_NONE
) {
110 ListPrefUpdate
update_crashes(local_state_
,
111 prefs::kStabilityCrashedActivityCounts
);
112 base::ListValue
* crash_counts
= update_crashes
.Get();
113 IncrementListValue(crash_counts
, foreground
);
114 local_state_
->SetInteger(prefs::kStabilityForegroundActivityType
,
115 ActivityTypeIds::ACTIVITY_NONE
);
118 local_state_
->CommitPendingWrite();
121 void AndroidMetricsProvider::ConvertStabilityPrefsToHistograms() {
122 ListPrefUpdate
launch_updater(local_state_
,
123 prefs::kStabilityLaunchedActivityCounts
);
124 ListPrefUpdate
crash_updater(local_state_
,
125 prefs::kStabilityCrashedActivityCounts
);
127 base::ListValue
* launch_counts
= launch_updater
.Get();
128 base::ListValue
* crash_counts
= crash_updater
.Get();
130 for (int activity_type
= ActivityTypeIds::ACTIVITY_NONE
;
131 activity_type
< ActivityTypeIds::ACTIVITY_MAX_VALUE
;
133 int launch_count
= 0;
136 launch_counts
->GetInteger(activity_type
, &launch_count
);
137 crash_counts
->GetInteger(activity_type
, &crash_count
);
139 for (int count
= 0; count
< launch_count
; ++count
) {
140 UMA_STABILITY_HISTOGRAM_ENUMERATION(
141 "Chrome.Android.Activity.LaunchCounts",
143 ActivityTypeIds::ACTIVITY_MAX_VALUE
);
146 for (int count
= 0; count
< crash_count
; ++count
) {
147 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts",
149 ActivityTypeIds::ACTIVITY_MAX_VALUE
);
153 launch_counts
->Clear();
154 crash_counts
->Clear();