This source file includes following definitions.
- RegisterProfilePrefs
- SignedIn
- Shutdown
- clock_
- UpdateLimit
- ForceOnlineLogin
#include "chrome/browser/chromeos/login/saml/saml_offline_signin_limiter.h"
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
namespace chromeos {
namespace {
const int kDefaultSAMLOfflineSigninTimeLimit = 14 * 24 * 60 * 60;
}
void SAMLOfflineSigninLimiter::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(
prefs::kSAMLOfflineSigninTimeLimit,
kDefaultSAMLOfflineSigninTimeLimit,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterInt64Pref(
prefs::kSAMLLastGAIASignInTime,
0,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
void SAMLOfflineSigninLimiter::SignedIn(UserContext::AuthFlow auth_flow) {
PrefService* prefs = profile_->GetPrefs();
const User* user = UserManager::Get()->GetUserByProfile(profile_);
if (!user) {
NOTREACHED();
return;
}
const std::string& user_id = user->email();
if (auth_flow == UserContext::AUTH_FLOW_GAIA_WITHOUT_SAML) {
prefs->ClearPref(prefs::kSAMLLastGAIASignInTime);
UserManager::Get()->SaveForceOnlineSignin(user_id, false);
return;
}
if (auth_flow == UserContext::AUTH_FLOW_GAIA_WITH_SAML) {
UserManager::Get()->SaveForceOnlineSignin(user_id, false);
prefs->SetInt64(prefs::kSAMLLastGAIASignInTime,
clock_->Now().ToInternalValue());
}
pref_change_registrar_.Init(prefs);
pref_change_registrar_.Add(prefs::kSAMLOfflineSigninTimeLimit,
base::Bind(&SAMLOfflineSigninLimiter::UpdateLimit,
base::Unretained(this)));
UpdateLimit();
}
void SAMLOfflineSigninLimiter::Shutdown() {
pref_change_registrar_.RemoveAll();
offline_signin_limit_timer_.reset();
}
SAMLOfflineSigninLimiter::SAMLOfflineSigninLimiter(Profile* profile,
base::Clock* clock)
: profile_(profile),
clock_(clock ? clock : &default_clock_) {
}
SAMLOfflineSigninLimiter::~SAMLOfflineSigninLimiter() {
}
void SAMLOfflineSigninLimiter::UpdateLimit() {
offline_signin_limit_timer_.reset();
PrefService* prefs = pref_change_registrar_.prefs();
const base::TimeDelta offline_signin_time_limit =
base::TimeDelta::FromSeconds(
prefs->GetInteger(prefs::kSAMLOfflineSigninTimeLimit));
base::Time last_gaia_signin_time = base::Time::FromInternalValue(
prefs->GetInt64(prefs::kSAMLLastGAIASignInTime));
if (offline_signin_time_limit < base::TimeDelta() ||
last_gaia_signin_time.is_null()) {
return;
}
const base::Time now = clock_->Now();
if (last_gaia_signin_time > now) {
NOTREACHED();
last_gaia_signin_time = now;
prefs->SetInt64(prefs::kSAMLLastGAIASignInTime, now.ToInternalValue());
}
const base::TimeDelta time_since_last_gaia_signin =
now - last_gaia_signin_time;
if (time_since_last_gaia_signin >= offline_signin_time_limit) {
ForceOnlineLogin();
return;
}
offline_signin_limit_timer_.reset(
new base::OneShotTimer<SAMLOfflineSigninLimiter>);
offline_signin_limit_timer_->Start(
FROM_HERE,
offline_signin_time_limit - time_since_last_gaia_signin,
this,
&SAMLOfflineSigninLimiter::ForceOnlineLogin);
}
void SAMLOfflineSigninLimiter::ForceOnlineLogin() {
User* user = UserManager::Get()->GetUserByProfile(profile_);
if (!user) {
NOTREACHED();
return;
}
UserManager::Get()->SaveForceOnlineSignin(user->email(), true);
offline_signin_limit_timer_.reset();
}
}