Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/android/uma_session_stats.h" | |
|
Alexei Svitkine (slow)
2015/02/27 18:59:57
Could this be placed in chrome/browser/metrics/and
Yaron
2015/02/27 19:13:34
I think chrome/browser/android/metrics is a bit ea
| |
| 6 | |
| 7 #include "base/android/jni_string.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/prefs/pref_service.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | |
| 15 #include "chrome/browser/metrics/metrics_services_manager.h" | |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "chrome/installer/util/google_update_settings.h" | |
| 19 #include "components/metrics/metrics_service.h" | |
| 20 #include "components/variations/metrics_util.h" | |
| 21 #include "components/variations/variations_associated_data.h" | |
| 22 #include "content/public/browser/browser_thread.h" | |
| 23 #include "content/public/browser/user_metrics.h" | |
| 24 #include "jni/UmaSessionStats_jni.h" | |
| 25 | |
| 26 using base::android::ConvertJavaStringToUTF8; | |
| 27 using base::UserMetricsAction; | |
| 28 | |
| 29 namespace { | |
| 30 UmaSessionStats* g_uma_session_stats = NULL; | |
| 31 } // namespace | |
| 32 | |
| 33 UmaSessionStats::UmaSessionStats() | |
| 34 : active_session_count_(0) { | |
| 35 } | |
| 36 | |
| 37 UmaSessionStats::~UmaSessionStats() { | |
| 38 } | |
| 39 | |
| 40 void UmaSessionStats::UmaResumeSession(JNIEnv* env, jobject obj) { | |
| 41 DCHECK(g_browser_process); | |
| 42 | |
| 43 if (active_session_count_ == 0) { | |
| 44 session_start_time_ = base::TimeTicks::Now(); | |
| 45 | |
| 46 // Tell the metrics service that the application resumes. | |
| 47 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | |
| 48 if (metrics) { | |
| 49 metrics->OnAppEnterForeground(); | |
| 50 } | |
| 51 } | |
| 52 ++active_session_count_; | |
| 53 } | |
| 54 | |
| 55 void UmaSessionStats::UmaEndSession(JNIEnv* env, jobject obj) { | |
| 56 --active_session_count_; | |
| 57 DCHECK(active_session_count_ >= 0); | |
| 58 | |
| 59 if (active_session_count_ == 0) { | |
| 60 base::TimeDelta duration = base::TimeTicks::Now() - session_start_time_; | |
| 61 UMA_HISTOGRAM_LONG_TIMES("Session.TotalDuration", duration); | |
| 62 | |
| 63 DCHECK(g_browser_process); | |
| 64 // Tell the metrics service it was cleanly shutdown. | |
| 65 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | |
| 66 if (metrics) { | |
| 67 metrics->OnAppEnterBackground(); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 void UmaSessionStats::RegisterSyntheticFieldTrialWithNameHash( | |
| 74 uint32_t trial_name_hash, | |
| 75 const std::string& group_name) { | |
| 76 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrialWithNameHash( | |
| 77 trial_name_hash, group_name); | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 void UmaSessionStats::RegisterSyntheticFieldTrial( | |
| 82 const std::string& trial_name, | |
| 83 const std::string& group_name) { | |
| 84 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(trial_name, | |
| 85 group_name); | |
| 86 } | |
| 87 | |
| 88 // Starts/stops the MetricsService when permissions have changed. | |
| 89 // There are three possible states: | |
| 90 // * Logs are being recorded and being uploaded to the server. | |
| 91 // * Logs are being recorded, but not being uploaded to the server. | |
| 92 // This happens when we've got permission to upload on Wi-Fi but we're on a | |
| 93 // mobile connection (for example). | |
| 94 // * Logs are neither being recorded or uploaded. | |
| 95 static void UpdateMetricsServiceState(JNIEnv* env, jobject obj, | |
| 96 jboolean may_record, jboolean may_upload) { | |
| 97 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | |
| 98 DCHECK(metrics); | |
| 99 | |
| 100 if (metrics->recording_active() != may_record) { | |
| 101 // This function puts a consent file with the ClientID in the | |
| 102 // data directory. The ID is passed to the renderer for crash | |
| 103 // reporting when things go wrong. | |
| 104 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, | |
| 105 base::Bind( | |
| 106 base::IgnoreResult(GoogleUpdateSettings::SetCollectStatsConsent), | |
| 107 may_record)); | |
| 108 } | |
| 109 | |
| 110 g_browser_process->GetMetricsServicesManager()->UpdatePermissions( | |
| 111 may_record, may_upload); | |
| 112 } | |
| 113 | |
| 114 // Renderer process crashed in the foreground. | |
| 115 static void LogRendererCrash(JNIEnv* env, jclass clazz, jboolean is_paused) { | |
| 116 DCHECK(g_browser_process); | |
| 117 | |
| 118 if (!is_paused) { | |
| 119 // Increment the renderer crash count in stability metrics. | |
| 120 PrefService* pref = g_browser_process->local_state(); | |
| 121 DCHECK(pref); | |
| 122 int value = pref->GetInteger(prefs::kStabilityRendererCrashCount); | |
| 123 pref->SetInteger(prefs::kStabilityRendererCrashCount, value + 1); | |
| 124 } | |
| 125 | |
| 126 // Note: When we are paused, any UI metric we increment may not make it to | |
| 127 // the disk before we are killed. Treat the count below as a lower bound. | |
| 128 content::RecordAction(base::UserMetricsAction("MobileRendererCrashed")); | |
| 129 } | |
| 130 | |
| 131 static void RegisterExternalExperiment(JNIEnv* env, | |
| 132 jclass clazz, | |
| 133 jint study_id, | |
| 134 jint experiment_id) { | |
| 135 const std::string group_name_utf8 = base::IntToString(experiment_id); | |
| 136 | |
| 137 variations::ActiveGroupId active_group; | |
| 138 active_group.name = static_cast<uint32>(study_id); | |
| 139 active_group.group = metrics::HashName(group_name_utf8); | |
| 140 variations::AssociateGoogleVariationIDForceHashes( | |
| 141 variations::GOOGLE_WEB_PROPERTIES, active_group, | |
| 142 static_cast<variations::VariationID>(experiment_id)); | |
| 143 | |
| 144 UmaSessionStats::RegisterSyntheticFieldTrialWithNameHash( | |
| 145 static_cast<uint32_t>(study_id), group_name_utf8); | |
| 146 } | |
| 147 | |
| 148 static void RegisterSyntheticFieldTrial(JNIEnv* env, | |
| 149 jclass clazz, | |
| 150 jstring jtrial_name, | |
| 151 jstring jgroup_name) { | |
| 152 std::string trial_name(ConvertJavaStringToUTF8(env, jtrial_name)); | |
| 153 std::string group_name(ConvertJavaStringToUTF8(env, jgroup_name)); | |
| 154 UmaSessionStats::RegisterSyntheticFieldTrial(trial_name, group_name); | |
| 155 } | |
| 156 | |
| 157 static void RecordMultiWindowSession(JNIEnv*, jclass, | |
| 158 jint area_percent, | |
| 159 jint instance_count) { | |
| 160 UMA_HISTOGRAM_PERCENTAGE("MobileStartup.MobileMultiWindowSession", | |
| 161 area_percent); | |
| 162 // Make sure the bucket count is the same as the range. This currently | |
| 163 // expects no more than 10 simultaneous multi window instances. | |
| 164 UMA_HISTOGRAM_CUSTOM_COUNTS("MobileStartup.MobileMultiWindowInstances", | |
| 165 instance_count, | |
| 166 1 /* min */, | |
| 167 10 /* max */, | |
| 168 10 /* bucket count */); | |
| 169 } | |
| 170 | |
| 171 static void RecordTabCountPerLoad(JNIEnv*, jclass, jint num_tabs) { | |
| 172 // Record how many tabs total are open. | |
| 173 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", num_tabs, 1, 200, 50); | |
| 174 } | |
| 175 | |
| 176 static void RecordPageLoaded(JNIEnv*, jclass, jboolean is_desktop_user_agent) { | |
| 177 // Should be called whenever a page has been loaded. | |
| 178 content::RecordAction(UserMetricsAction("MobilePageLoaded")); | |
| 179 if (is_desktop_user_agent) { | |
| 180 content::RecordAction( | |
| 181 UserMetricsAction("MobilePageLoadedDesktopUserAgent")); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 static void RecordPageLoadedWithKeyboard(JNIEnv*, jclass) { | |
| 186 content::RecordAction(UserMetricsAction("MobilePageLoadedWithKeyboard")); | |
| 187 } | |
| 188 | |
| 189 static jlong Init(JNIEnv* env, jclass obj) { | |
| 190 // We should have only one UmaSessionStats instance. | |
| 191 DCHECK(!g_uma_session_stats); | |
| 192 g_uma_session_stats = new UmaSessionStats(); | |
| 193 return reinterpret_cast<intptr_t>(g_uma_session_stats); | |
| 194 } | |
| 195 | |
| 196 // Register native methods | |
| 197 bool RegisterUmaSessionStats(JNIEnv* env) { | |
| 198 return RegisterNativesImpl(env); | |
| 199 } | |
| OLD | NEW |