Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/metrics_service.h" | 5 #include "chrome/browser/metrics/android_metrics_provider_android.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
| 11 #include "base/values.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" | 12 #include "chrome/common/pref_names.h" |
| 15 | 13 |
| 16 namespace { | 14 namespace { |
| 17 | 15 |
| 18 // Increments a particular entry in the ListValue. | 16 // Increments a particular entry in the ListValue. |
| 19 void IncrementListValue(base::ListValue* counts, int index) { | 17 void IncrementListValue(base::ListValue* counts, int index) { |
| 20 int current_count = 0; | 18 int current_count = 0; |
| 21 counts->GetInteger(index, ¤t_count); | 19 counts->GetInteger(index, ¤t_count); |
| 22 counts->Set(index, new base::FundamentalValue(current_count + 1)); | 20 counts->Set(index, new base::FundamentalValue(current_count + 1)); |
| 23 } | 21 } |
| 24 | 22 |
| 25 // Takes an int corresponding to a Type and returns the corresponding flag. | 23 // Takes an int corresponding to a Type and returns the corresponding flag. |
| 26 int GetActivityFlag(int type_id) { | 24 int GetActivityFlag(int type_id) { |
| 27 ActivityTypeIds::Type type = ActivityTypeIds::GetActivityType(type_id); | 25 ActivityTypeIds::Type type = ActivityTypeIds::GetActivityType(type_id); |
| 28 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE); | 26 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 29 return (1 << type); | 27 return (1 << type); |
| 30 } | 28 } |
| 31 | 29 |
| 32 } // namespace | 30 } // namespace |
| 33 | 31 |
| 34 // static | 32 AndroidMetricsProvider::AndroidMetricsProvider(PrefService* local_state) |
| 35 void MetricsService::RegisterPrefsAndroid(PrefRegistrySimple* registry) { | 33 : local_state_(local_state) { |
| 36 registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType, | 34 DCHECK(local_state_); |
|
Ilya Sherman
2014/05/21 09:15:28
nit: This seems unnecessary.
Alexei Svitkine (slow)
2014/05/21 09:48:47
Done.
| |
| 37 ActivityTypeIds::ACTIVITY_NONE); | 35 LogAndroidStabilityToPrefs(); |
| 38 registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0); | |
| 39 registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts); | |
| 40 registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts); | |
| 41 } | 36 } |
| 42 | 37 |
| 43 void MetricsService::LogAndroidStabilityToPrefs(PrefService* pref) { | 38 AndroidMetricsProvider::~AndroidMetricsProvider() { |
| 39 } | |
| 40 | |
| 41 | |
| 42 void AndroidMetricsProvider::ProvideStabilityMetrics( | |
| 43 metrics::SystemProfileProto_Stability* stability_proto) { | |
| 44 ConvertAndroidStabilityPrefsToHistograms(); | |
| 45 } | |
| 46 | |
| 47 void AndroidMetricsProvider::LogAndroidStabilityToPrefs() { | |
| 44 // Track which Activities were launched by the user. | 48 // Track which Activities were launched by the user. |
| 45 // A 'launch' is defined as starting the Activity at least once during a | 49 // 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 | 50 // 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). | 51 // for users to hop between Activities (e.g. entering and leaving Settings). |
| 48 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); | 52 const int launched = |
| 49 ListPrefUpdate update_launches(pref, prefs::kStabilityLaunchedActivityCounts); | 53 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags); |
| 54 ListPrefUpdate update_launches(local_state_, | |
| 55 prefs::kStabilityLaunchedActivityCounts); | |
| 50 base::ListValue* launch_counts = update_launches.Get(); | 56 base::ListValue* launch_counts = update_launches.Get(); |
| 51 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; | 57 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; |
| 52 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; | 58 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; |
| 53 ++activity_type) { | 59 ++activity_type) { |
| 54 if (launched & GetActivityFlag(activity_type)) | 60 if (launched & GetActivityFlag(activity_type)) |
| 55 IncrementListValue(launch_counts, activity_type); | 61 IncrementListValue(launch_counts, activity_type); |
| 56 } | 62 } |
| 57 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); | 63 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0); |
| 58 | 64 |
| 59 // Track any Activities that were in the foreground when Chrome died. | 65 // Track any Activities that were in the foreground when Chrome died. |
| 60 // These Activities failed to be recorded as leaving the foreground, so Chrome | 66 // 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. | 67 // couldn't have ended the UMA session cleanly. Record them as crashing. |
| 62 int foreground = pref->GetInteger(prefs::kStabilityForegroundActivityType); | 68 const int foreground = |
| 69 local_state_->GetInteger(prefs::kStabilityForegroundActivityType); | |
| 63 if (foreground != ActivityTypeIds::ACTIVITY_NONE) { | 70 if (foreground != ActivityTypeIds::ACTIVITY_NONE) { |
| 64 ListPrefUpdate update_crashes(pref, prefs::kStabilityCrashedActivityCounts); | 71 ListPrefUpdate update_crashes(local_state_, |
| 72 prefs::kStabilityCrashedActivityCounts); | |
| 65 base::ListValue* crash_counts = update_crashes.Get(); | 73 base::ListValue* crash_counts = update_crashes.Get(); |
| 66 IncrementListValue(crash_counts, foreground); | 74 IncrementListValue(crash_counts, foreground); |
| 67 pref->SetInteger(prefs::kStabilityForegroundActivityType, | 75 local_state_->SetInteger(prefs::kStabilityForegroundActivityType, |
| 68 ActivityTypeIds::ACTIVITY_NONE); | 76 ActivityTypeIds::ACTIVITY_NONE); |
| 69 } | 77 } |
| 70 | 78 |
| 71 pref->CommitPendingWrite(); | 79 local_state_->CommitPendingWrite(); |
| 72 } | 80 } |
| 73 | 81 |
| 74 void MetricsService::ConvertAndroidStabilityPrefsToHistograms( | 82 void AndroidMetricsProvider::ConvertAndroidStabilityPrefsToHistograms(f) { |
|
Ilya Sherman
2014/05/21 09:15:28
nit: "(f)" -> "()"
Alexei Svitkine (slow)
2014/05/21 09:48:47
Done.
| |
| 75 PrefService* pref) { | 83 ListPrefUpdate launch_updater(local_state_, |
| 76 ListPrefUpdate launch_updater(pref, prefs::kStabilityLaunchedActivityCounts); | 84 prefs::kStabilityLaunchedActivityCounts); |
| 77 ListPrefUpdate crash_updater(pref, prefs::kStabilityCrashedActivityCounts); | 85 ListPrefUpdate crash_updater(local_state_, |
| 86 prefs::kStabilityCrashedActivityCounts); | |
| 78 | 87 |
| 79 base::ListValue* launch_counts = launch_updater.Get(); | 88 base::ListValue* launch_counts = launch_updater.Get(); |
| 80 base::ListValue* crash_counts = crash_updater.Get(); | 89 base::ListValue* crash_counts = crash_updater.Get(); |
| 81 | 90 |
| 82 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; | 91 for (int activity_type = ActivityTypeIds::ACTIVITY_NONE; |
| 83 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; | 92 activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE; |
| 84 ++activity_type) { | 93 ++activity_type) { |
| 85 int launch_count = 0; | 94 int launch_count = 0; |
| 86 int crash_count = 0; | 95 int crash_count = 0; |
| 87 | 96 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 99 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts", | 108 UMA_STABILITY_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts", |
| 100 activity_type, | 109 activity_type, |
| 101 ActivityTypeIds::ACTIVITY_MAX_VALUE); | 110 ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 102 } | 111 } |
| 103 } | 112 } |
| 104 | 113 |
| 105 launch_counts->Clear(); | 114 launch_counts->Clear(); |
| 106 crash_counts->Clear(); | 115 crash_counts->Clear(); |
| 107 } | 116 } |
| 108 | 117 |
| 109 void MetricsService::OnForegroundActivityChanged(PrefService* pref, | 118 void AndroidMetricsProvider::OnForegroundActivityChanged( |
| 110 ActivityTypeIds::Type type) { | 119 ActivityTypeIds::Type type) { |
| 111 DCHECK(type < ActivityTypeIds::ACTIVITY_MAX_VALUE); | 120 DCHECK_LT(type, ActivityTypeIds::ACTIVITY_MAX_VALUE); |
| 112 | 121 |
| 113 int activity_type = pref->GetInteger(prefs::kStabilityForegroundActivityType); | 122 if (type == local_state_->GetInteger(prefs::kStabilityForegroundActivityType)) |
| 114 if (activity_type == type) | |
| 115 return; | 123 return; |
| 116 | 124 |
| 117 // Record that the Activity is now in the foreground. | 125 // Record that the Activity is now in the foreground. |
| 118 pref->SetInteger(prefs::kStabilityForegroundActivityType, type); | 126 local_state_->SetInteger(prefs::kStabilityForegroundActivityType, type); |
| 119 | 127 |
| 120 // Record that the Activity was launched this sesaion. | 128 // Record that the Activity was launched this sesaion. |
| 121 // The pref stores a set of flags ORed together, where each set flag | 129 // The pref stores a set of flags ORed together, where each set flag |
| 122 // corresponds to a launched Activity type. | 130 // corresponds to a launched Activity type. |
| 123 int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags); | 131 int launched = |
| 132 local_state_->GetInteger(prefs::kStabilityLaunchedActivityFlags); | |
| 124 if (type != ActivityTypeIds::ACTIVITY_NONE) { | 133 if (type != ActivityTypeIds::ACTIVITY_NONE) { |
| 125 launched |= GetActivityFlag(type); | 134 launched |= GetActivityFlag(type); |
| 126 pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched); | 135 local_state_->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched); |
| 127 } | 136 } |
| 128 | 137 |
| 129 pref->CommitPendingWrite(); | 138 local_state_->CommitPendingWrite(); |
| 130 } | 139 } |
| 140 | |
| 141 // static | |
| 142 void AndroidMetricsProvider::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 143 registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType, | |
| 144 ActivityTypeIds::ACTIVITY_NONE); | |
| 145 registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0); | |
| 146 registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts); | |
| 147 registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts); | |
| 148 } | |
| OLD | NEW |