| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #include "chrome/browser/metrics/metrics_service.h" |
| 6 |
| 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/activity_type_ids.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 |
| 16 namespace { |
| 17 |
| 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, ¤t_count); |
| 22 counts->Set(index, new base::FundamentalValue(current_count + 1)); |
| 23 } |
| 24 |
| 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 CHECK(type < ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 29 return (1 << type); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 // static |
| 35 void MetricsService::RegisterPrefsAndroid(PrefRegistrySimple* registry) { |
| 36 registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType, |
| 37 ActivityTypeIds::ACTIVITY_NONE); |
| 38 registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0); |
| 39 registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts); |
| 40 registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts); |
| 41 } |
| 42 |
| 43 void MetricsService::RecordAndroidStabilityPrefs(PrefService* pref) { |
| 44 // Track which Activities were launched by the user. |
| 45 // A 'launch' is defined as starting the Activity at least once during a |
| 46 // UMA session. Multiple launches are counted only once since it is possible |
| 47 // for users to hop between Activities (e.g. entering and leaving Settings). |
| 48 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); |
| 49 ListPrefUpdate update_launches(pref, prefs::kStabilityLaunchedActivityCounts); |
| 50 base::ListValue* launch_counts = update_launches.Get(); |
| 51 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; |
| 52 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; |
| 53 ++activity_type) { |
| 54 if (launched & GetActivityFlag(activity_type)) |
| 55 IncrementListValue(launch_counts, activity_type); |
| 56 } |
| 57 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); |
| 58 |
| 59 // Track any Activities that were in the foreground when Chrome died. |
| 60 // These Activities failed to be recorded as leaving the foreground, so Chrome |
| 61 // couldn't have ended the UMA session cleanly. Record them as crashing. |
| 62 int foreground = pref->GetInteger(prefs::kStabilityForegroundActivityType); |
| 63 if (foreground != ActivityTypeIds::ACTIVITY_NONE) { |
| 64 ListPrefUpdate update_crashes(pref, prefs::kStabilityCrashedActivityCounts); |
| 65 base::ListValue* crash_counts = update_crashes.Get(); |
| 66 IncrementListValue(crash_counts, foreground); |
| 67 pref->SetInteger(prefs::kStabilityForegroundActivityType, |
| 68 ActivityTypeIds::ACTIVITY_NONE); |
| 69 } |
| 70 |
| 71 pref->CommitPendingWrite(); |
| 72 } |
| 73 |
| 74 void MetricsService::RecordAndroidStabilityHistograms(PrefService* pref) { |
| 75 ListPrefUpdate launch_updater(pref, prefs::kStabilityLaunchedActivityCounts); |
| 76 ListPrefUpdate crash_updater(pref, prefs::kStabilityCrashedActivityCounts); |
| 77 |
| 78 base::ListValue* launch_counts = launch_updater.Get(); |
| 79 base::ListValue* crash_counts = crash_updater.Get(); |
| 80 |
| 81 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; |
| 82 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; |
| 83 ++activity_type) { |
| 84 int launch_count = 0; |
| 85 int crash_count = 0; |
| 86 |
| 87 launch_counts->GetInteger(activity_type, &launch_count); |
| 88 crash_counts->GetInteger(activity_type, &crash_count); |
| 89 |
| 90 for (int count = 0; count < launch_count; ++count) { |
| 91 UMA_STABILITY_HISTOGRAM_ENUMERATION( |
| 92 "Chrome.Android.Activity.LaunchCounts", |
| 93 activity_type, |
| 94 ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 95 } |
| 96 |
| 97 for (int count = 0; count < crash_count; ++count) { |
| 98 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts", |
| 99 activity_type, |
| 100 ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 101 } |
| 102 } |
| 103 |
| 104 launch_counts->Clear(); |
| 105 crash_counts->Clear(); |
| 106 } |
| 107 |
| 108 void MetricsService::OnForegroundActivityChanged(PrefService* pref, |
| 109 ActivityTypeIds::Type type) { |
| 110 DCHECK(type < ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 111 |
| 112 int activity_type = pref->GetInteger(prefs::kStabilityForegroundActivityType); |
| 113 if (activity_type == type) |
| 114 return; |
| 115 |
| 116 // Record that the Activity is now in the foreground. |
| 117 pref->SetInteger(prefs::kStabilityForegroundActivityType, type); |
| 118 |
| 119 // Record that the Activity was launched this sesaion. |
| 120 // The pref stores a set of flags ORed together, where each set flag |
| 121 // corresponds to a launched Activity type. |
| 122 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); |
| 123 if (type != ActivityTypeIds::ACTIVITY_NONE) { |
| 124 launched |= GetActivityFlag(type); |
| 125 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched); |
| 126 } |
| 127 |
| 128 pref->CommitPendingWrite(); |
| 129 } |
| 130 |
| 131 // static |
| 132 void MetricsService::DiscardOldStabilityStatsAndroid(PrefService* local_state) { |
| 133 local_state->SetInteger(prefs::kStabilityForegroundActivityType, |
| 134 ActivityTypeIds::ACTIVITY_NONE); |
| 135 local_state->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); |
| 136 |
| 137 ListPrefUpdate launches(local_state, prefs::kStabilityLaunchedActivityCounts); |
| 138 launches.Get()->Clear(); |
| 139 |
| 140 ListPrefUpdate crashes(local_state, prefs::kStabilityCrashedActivityCounts); |
| 141 crashes.Get()->Clear(); |
| 142 } |
| OLD | NEW |