Chromium Code Reviews| 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(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() { | |
| 44 PrefService* pref = g_browser_process->local_state(); | |
|
Alexei Svitkine (slow)
2014/01/09 20:20:12
Can this be passed as a param? Same for the functi
Kibeom Kim (inactive)
2014/01/16 00:18:16
Done.
| |
| 45 | |
| 46 // Track which Activities were launched by the user. | |
| 47 // A 'launch' is defined as starting the Activity at least once during a | |
| 48 // UMA session. Multiple launches are counted only once since it is possible | |
| 49 // for users to hop between Activities (e.g. entering and leaving Settings). | |
| 50 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); | |
| 51 ListPrefUpdate update_launches(pref, prefs::kStabilityLaunchedActivityCounts); | |
| 52 ListValue* launch_counts = update_launches.Get(); | |
| 53 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; | |
|
Alexei Svitkine (slow)
2014/01/09 20:20:12
Nit: You use activity_type here but activity in an
Kibeom Kim (inactive)
2014/01/16 00:18:16
Done.
| |
| 54 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; | |
| 55 ++activity_type) { | |
| 56 if (launched & GetActivityFlag(activity_type)) | |
| 57 IncrementListValue(launch_counts, activity_type); | |
| 58 } | |
| 59 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); | |
| 60 | |
| 61 // Track any Activities that were in the foreground when Chrome died. | |
| 62 // These Activities failed to be recorded as leaving the foreground, so Chrome | |
| 63 // couldn't have ended the UMA session cleanly. Record them as crashing. | |
| 64 int foreground = pref->GetInteger(prefs::kStabilityForegroundActivityType); | |
| 65 if (foreground != ActivityTypeIds::ACTIVITY_NONE) { | |
| 66 ListPrefUpdate update_crashes(pref, prefs::kStabilityCrashedActivityCounts); | |
| 67 ListValue* crash_counts = update_crashes.Get(); | |
| 68 IncrementListValue(crash_counts, foreground); | |
| 69 pref->SetInteger(prefs::kStabilityForegroundActivityType, | |
| 70 ActivityTypeIds::ACTIVITY_NONE); | |
| 71 } | |
| 72 | |
| 73 pref->CommitPendingWrite(); | |
| 74 } | |
| 75 | |
| 76 void MetricsService::RecordAndroidStabilityHistograms() { | |
| 77 PrefService* pref = g_browser_process->local_state(); | |
| 78 | |
| 79 ListPrefUpdate launch_updater(pref, prefs::kStabilityLaunchedActivityCounts); | |
| 80 ListPrefUpdate crash_updater(pref, prefs::kStabilityCrashedActivityCounts); | |
| 81 | |
| 82 ListValue* launch_counts = launch_updater.Get(); | |
| 83 ListValue* crash_counts = crash_updater.Get(); | |
| 84 | |
| 85 for (int activity = ActivityTypeIds::ACTIVITY_NONE; | |
| 86 activity < ActivityTypeIds::ACTIVITY_MAX_VALUE; | |
| 87 ++activity) { | |
| 88 int launch_count = 0; | |
| 89 int crash_count = 0; | |
| 90 | |
| 91 launch_counts->GetInteger(activity, &launch_count); | |
| 92 crash_counts->GetInteger(activity, &crash_count); | |
| 93 | |
| 94 for (int count = 0; count < launch_count; ++count) { | |
| 95 UMA_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.LaunchCounts", | |
| 96 activity, | |
| 97 ActivityTypeIds::ACTIVITY_MAX_VALUE); | |
| 98 } | |
| 99 | |
| 100 for (int count = 0; count < crash_count; ++count) { | |
| 101 UMA_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts", | |
| 102 activity, | |
| 103 ActivityTypeIds::ACTIVITY_MAX_VALUE); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 launch_counts->Clear(); | |
| 108 crash_counts->Clear(); | |
| 109 } | |
| 110 | |
| 111 void MetricsService::OnForegroundActivityChanged(ActivityTypeIds::Type type) { | |
| 112 CHECK(type < ActivityTypeIds::ACTIVITY_MAX_VALUE); | |
|
Alexei Svitkine (slow)
2014/01/09 20:20:12
I think that's why you have GetActivityType() to v
Kibeom Kim (inactive)
2014/01/16 00:18:16
Done.
| |
| 113 | |
| 114 PrefService* pref = g_browser_process->local_state(); | |
| 115 int activity = pref->GetInteger(prefs::kStabilityForegroundActivityType); | |
| 116 if (activity == type) return; | |
|
Alexei Svitkine (slow)
2014/01/09 20:20:12
Put return on next line.
Kibeom Kim (inactive)
2014/01/16 00:18:16
Done.
| |
| 117 | |
| 118 // Record that the Activity is now in the foreground. | |
| 119 pref->SetInteger(prefs::kStabilityForegroundActivityType, type); | |
| 120 | |
| 121 // Record that the Activity was launched this sesaion. | |
| 122 // The pref stores a set of flags ORed together, where each set flag | |
| 123 // corresponds to a launched Activity type. | |
| 124 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); | |
| 125 if (type != ActivityTypeIds::ACTIVITY_NONE) { | |
| 126 launched |= GetActivityFlag(type); | |
| 127 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched); | |
| 128 } | |
| 129 | |
| 130 pref->CommitPendingWrite(); | |
| 131 } | |
| 132 | |
| 133 // static | |
| 134 void MetricsService::DiscardOldStabilityStatsAndroid(PrefService* local_state) { | |
| 135 local_state->SetInteger(prefs::kStabilityForegroundActivityType, | |
| 136 ActivityTypeIds::ACTIVITY_NONE); | |
| 137 local_state->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); | |
| 138 | |
| 139 ListPrefUpdate launches(local_state, prefs::kStabilityLaunchedActivityCounts); | |
| 140 launches.Get()->Clear(); | |
| 141 | |
| 142 ListPrefUpdate crashes(local_state, prefs::kStabilityCrashedActivityCounts); | |
| 143 crashes.Get()->Clear(); | |
| 144 } | |
| OLD | NEW |