Chromium Code Reviews| Index: chrome/browser/chromeos/login/enterprise_user_session_metrics.cc |
| diff --git a/chrome/browser/chromeos/login/enterprise_user_session_metrics.cc b/chrome/browser/chromeos/login/enterprise_user_session_metrics.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e48a1d214d5e65a65c27f410a33f976c5a94daa |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/enterprise_user_session_metrics.cc |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2017 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/chromeos/login/enterprise_user_session_metrics.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/logging.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/login/auth/user_context.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/pref_service.h" |
| + |
| +namespace { |
| + |
| +// Returns true if the device is enterprise managed, false otherwise. |
| +bool IsEnterpriseManaged() { |
| + return g_browser_process->platform_part() |
| + ->browser_policy_connector_chromeos() |
| + ->IsEnterpriseManaged(); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| +namespace enterprise_user_session_metrics { |
| + |
| +void RegisterPrefs(PrefRegistrySimple* registry) { |
| + registry->RegisterIntegerPref(prefs::kLastSessionType, 0); |
| + registry->RegisterInt64Pref(prefs::kLastSessionLength, 0); |
| +} |
| + |
| +void RecordSignInEvent(SignInEventType sign_in_event_type) { |
| + DCHECK(IsEnterpriseManaged()); |
| + |
| + UMA_HISTOGRAM_ENUMERATION( |
| + "Enterprise.UserSession.Logins", static_cast<int>(sign_in_event_type), |
| + static_cast<int>(SignInEventType::SIGN_IN_EVENT_COUNT)); |
| +} |
| + |
| +void RecordSignInEvent(const UserContext& user_context, bool is_auto_login) { |
| + DCHECK(IsEnterpriseManaged()); |
| + |
| + const user_manager::UserType session_type = user_context.GetUserType(); |
| + if (session_type == user_manager::USER_TYPE_REGULAR) { |
| + RecordSignInEvent(SignInEventType::REGULAR_USER); |
| + } else if (session_type == user_manager::USER_TYPE_PUBLIC_ACCOUNT) { |
| + RecordSignInEvent(is_auto_login ? SignInEventType::AUTOMATIC_PUBLIC_SESSSION |
| + : SignInEventType::MANUAL_PUBLIC_SESSION); |
| + } |
| + |
| + // Kiosk sign-ins are handled separately in AppLaunchController and other |
| + // session types are ignored for now. |
| +} |
| + |
| +void StoreSessionLength(user_manager::UserType session_type, |
| + const base::TimeDelta& session_length) { |
| + DCHECK(IsEnterpriseManaged()); |
| + |
| + if (session_type != user_manager::USER_TYPE_REGULAR && |
| + session_type == user_manager::USER_TYPE_PUBLIC_ACCOUNT) { |
| + // No session length metric for other session types. |
| + return; |
| + } |
| + |
| + PrefService* local_state = g_browser_process->local_state(); |
| + local_state->SetInteger(prefs::kLastSessionType, session_type); |
| + local_state->SetInt64(prefs::kLastSessionLength, |
| + session_length.ToInternalValue()); |
| + local_state->CommitPendingWrite(); |
| +} |
| + |
| +void RecordStoredSessionLength() { |
| + DCHECK(IsEnterpriseManaged()); |
| + |
| + PrefService* local_state = g_browser_process->local_state(); |
| + if (!local_state->HasPrefPath(prefs::kLastSessionType) || |
| + !local_state->HasPrefPath(prefs::kLastSessionLength)) { |
| + return; |
| + } |
| + |
| + const user_manager::UserType session_type = |
| + static_cast<user_manager::UserType>( |
| + local_state->GetInteger(prefs::kLastSessionType)); |
| + const base::TimeDelta session_length = base::TimeDelta::FromInternalValue( |
| + local_state->GetInt64(prefs::kLastSessionLength)); |
| + |
| + local_state->ClearPref(prefs::kLastSessionType); |
| + local_state->ClearPref(prefs::kLastSessionLength); |
| + |
| + if (session_length.is_zero()) |
| + return; |
| + |
| + std::string metric_name; |
| + if (session_type == user_manager::USER_TYPE_REGULAR) { |
| + metric_name = "Enterprise.RegularUserSession.SessionLength"; |
| + } else if (session_type == user_manager::USER_TYPE_PUBLIC_ACCOUNT) { |
| + metric_name = "Enterprise.PublicSession.SessionLength"; |
| + } else { |
| + // NOTREACHED() since session length for other session types should not |
| + // be recorded. |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + // Report session duration for the first 24 hours, split into 144 buckets |
| + // (i.e. every 10 minute). |
| + const int bucket = std::min(session_length.InMinutes(), |
| + base::TimeDelta::FromHours(24).InMinutes()) / |
| + 10; |
|
Ilya Sherman
2017/02/15 23:42:44
If you wanted to, you could actually then re-multi
xiyuan
2017/02/16 00:21:47
Done.
|
| + UMA_HISTOGRAM_SPARSE_SLOWLY(metric_name, bucket); |
| +} |
| + |
| +} // namespace enterprise_user_session_metrics |
| +} // namespace chromeos |