ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / android / metrics / uma_session_stats.cc
blobf4b29811e7aeb8da5680cb1cd7c526c1f0dfd6bc
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.
5 #include "chrome/browser/android/metrics/uma_session_stats.h"
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"
26 using base::android::ConvertJavaStringToUTF8;
27 using base::UserMetricsAction;
29 namespace {
30 UmaSessionStats* g_uma_session_stats = NULL;
31 } // namespace
33 UmaSessionStats::UmaSessionStats()
34 : active_session_count_(0) {
37 UmaSessionStats::~UmaSessionStats() {
40 void UmaSessionStats::UmaResumeSession(JNIEnv* env, jobject obj) {
41 DCHECK(g_browser_process);
43 if (active_session_count_ == 0) {
44 session_start_time_ = base::TimeTicks::Now();
46 // Tell the metrics service that the application resumes.
47 metrics::MetricsService* metrics = g_browser_process->metrics_service();
48 if (metrics) {
49 metrics->OnAppEnterForeground();
52 ++active_session_count_;
55 void UmaSessionStats::UmaEndSession(JNIEnv* env, jobject obj) {
56 --active_session_count_;
57 DCHECK(active_session_count_ >= 0);
59 if (active_session_count_ == 0) {
60 base::TimeDelta duration = base::TimeTicks::Now() - session_start_time_;
61 UMA_HISTOGRAM_LONG_TIMES("Session.TotalDuration", duration);
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();
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);
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);
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);
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));
110 g_browser_process->GetMetricsServicesManager()->UpdatePermissions(
111 may_record, may_upload);
114 // Renderer process crashed in the foreground.
115 static void LogRendererCrash(JNIEnv* env, jclass clazz, jboolean is_paused) {
116 DCHECK(g_browser_process);
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);
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"));
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);
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));
144 UmaSessionStats::RegisterSyntheticFieldTrialWithNameHash(
145 static_cast<uint32_t>(study_id), group_name_utf8);
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);
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 */);
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);
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"));
185 static void RecordPageLoadedWithKeyboard(JNIEnv*, jclass) {
186 content::RecordAction(UserMetricsAction("MobilePageLoadedWithKeyboard"));
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);
196 // Register native methods
197 bool RegisterUmaSessionStats(JNIEnv* env) {
198 return RegisterNativesImpl(env);