Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Unified Diff: chrome/browser/metrics/metrics_service_android.cc

Issue 103943006: Let MetricsService know about some Android Activities (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding more activities, addressing comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/metrics_service_android.cc
diff --git a/chrome/browser/metrics/metrics_service_android.cc b/chrome/browser/metrics/metrics_service_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..825760499971cd90cc2fd49581a426a17ac6acec
--- /dev/null
+++ b/chrome/browser/metrics/metrics_service_android.cc
@@ -0,0 +1,144 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/metrics/metrics_service.h"
+
+#include "base/metrics/histogram.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+#include "base/prefs/scoped_user_pref_update.h"
+#include "base/values.h"
+#include "chrome/browser/android/activity_type_ids.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/common/pref_names.h"
+
+namespace {
+
+// Increments a particular entry in the ListValue.
+void IncrementListValue(ListValue* counts, int index) {
+ int current_count = 0;
+ counts->GetInteger(index, &current_count);
+ counts->Set(index, new base::FundamentalValue(current_count + 1));
+}
+
+// Takes an int corresponding to a Type and returns the corresponding flag.
+int GetActivityFlag(int type_id) {
+ ActivityTypeIds::Type type = ActivityTypeIds::GetActivityType(type_id);
+ CHECK(type < ActivityTypeIds::ACTIVITY_MAX_VALUE);
+ return (1 << type);
+}
+
+} // namespace
+
+// static
+void MetricsService::RegisterPrefsAndroid(PrefRegistrySimple* registry) {
+ registry->RegisterIntegerPref(prefs::kStabilityForegroundActivityType,
+ ActivityTypeIds::ACTIVITY_NONE);
+ registry->RegisterIntegerPref(prefs::kStabilityLaunchedActivityFlags, 0);
+ registry->RegisterListPref(prefs::kStabilityLaunchedActivityCounts);
+ registry->RegisterListPref(prefs::kStabilityCrashedActivityCounts);
+}
+
+void MetricsService::RecordAndroidStabilityPrefs() {
+ 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.
+
+ // Track which Activities were launched by the user.
+ // A 'launch' is defined as starting the Activity at least once during a
+ // UMA session. Multiple launches are counted only once since it is possible
+ // for users to hop between Activities (e.g. entering and leaving Settings).
+ int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags);
+ ListPrefUpdate update_launches(pref, prefs::kStabilityLaunchedActivityCounts);
+ ListValue* launch_counts = update_launches.Get();
+ 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.
+ activity_type < ActivityTypeIds::ACTIVITY_MAX_VALUE;
+ ++activity_type) {
+ if (launched & GetActivityFlag(activity_type))
+ IncrementListValue(launch_counts, activity_type);
+ }
+ pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0);
+
+ // Track any Activities that were in the foreground when Chrome died.
+ // These Activities failed to be recorded as leaving the foreground, so Chrome
+ // couldn't have ended the UMA session cleanly. Record them as crashing.
+ int foreground = pref->GetInteger(prefs::kStabilityForegroundActivityType);
+ if (foreground != ActivityTypeIds::ACTIVITY_NONE) {
+ ListPrefUpdate update_crashes(pref, prefs::kStabilityCrashedActivityCounts);
+ ListValue* crash_counts = update_crashes.Get();
+ IncrementListValue(crash_counts, foreground);
+ pref->SetInteger(prefs::kStabilityForegroundActivityType,
+ ActivityTypeIds::ACTIVITY_NONE);
+ }
+
+ pref->CommitPendingWrite();
+}
+
+void MetricsService::RecordAndroidStabilityHistograms() {
+ PrefService* pref = g_browser_process->local_state();
+
+ ListPrefUpdate launch_updater(pref, prefs::kStabilityLaunchedActivityCounts);
+ ListPrefUpdate crash_updater(pref, prefs::kStabilityCrashedActivityCounts);
+
+ ListValue* launch_counts = launch_updater.Get();
+ ListValue* crash_counts = crash_updater.Get();
+
+ for (int activity = ActivityTypeIds::ACTIVITY_NONE;
+ activity < ActivityTypeIds::ACTIVITY_MAX_VALUE;
+ ++activity) {
+ int launch_count = 0;
+ int crash_count = 0;
+
+ launch_counts->GetInteger(activity, &launch_count);
+ crash_counts->GetInteger(activity, &crash_count);
+
+ for (int count = 0; count < launch_count; ++count) {
+ UMA_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.LaunchCounts",
+ activity,
+ ActivityTypeIds::ACTIVITY_MAX_VALUE);
+ }
+
+ for (int count = 0; count < crash_count; ++count) {
+ UMA_HISTOGRAM_ENUMERATION("Chrome.Android.Activity.CrashCounts",
+ activity,
+ ActivityTypeIds::ACTIVITY_MAX_VALUE);
+ }
+ }
+
+ launch_counts->Clear();
+ crash_counts->Clear();
+}
+
+void MetricsService::OnForegroundActivityChanged(ActivityTypeIds::Type type) {
+ 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.
+
+ PrefService* pref = g_browser_process->local_state();
+ int activity = pref->GetInteger(prefs::kStabilityForegroundActivityType);
+ 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.
+
+ // Record that the Activity is now in the foreground.
+ pref->SetInteger(prefs::kStabilityForegroundActivityType, type);
+
+ // Record that the Activity was launched this sesaion.
+ // The pref stores a set of flags ORed together, where each set flag
+ // corresponds to a launched Activity type.
+ int launched = pref->GetInteger(prefs::kStabilityLaunchedActivityFlags);
+ if (type != ActivityTypeIds::ACTIVITY_NONE) {
+ launched |= GetActivityFlag(type);
+ pref->SetInteger(prefs::kStabilityLaunchedActivityFlags, launched);
+ }
+
+ pref->CommitPendingWrite();
+}
+
+// static
+void MetricsService::DiscardOldStabilityStatsAndroid(PrefService* local_state) {
+ local_state->SetInteger(prefs::kStabilityForegroundActivityType,
+ ActivityTypeIds::ACTIVITY_NONE);
+ local_state->SetInteger(prefs::kStabilityLaunchedActivityFlags, 0);
+
+ ListPrefUpdate launches(local_state, prefs::kStabilityLaunchedActivityCounts);
+ launches.Get()->Clear();
+
+ ListPrefUpdate crashes(local_state, prefs::kStabilityCrashedActivityCounts);
+ crashes.Get()->Clear();
+}

Powered by Google App Engine
This is Rietveld 408576698