user_manager_base.cc 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. // Copyright 2014 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. #include "components/user_manager/user_manager_base.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <set>
  8. #include <utility>
  9. #include "base/bind.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/command_line.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/format_macros.h"
  14. #include "base/location.h"
  15. #include "base/logging.h"
  16. #include "base/metrics/histogram_functions.h"
  17. #include "base/metrics/histogram_macros.h"
  18. #include "base/strings/string_util.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "base/task/single_thread_task_runner.h"
  21. #include "base/values.h"
  22. #include "components/crash/core/common/crash_key.h"
  23. #include "components/prefs/pref_registry_simple.h"
  24. #include "components/prefs/pref_service.h"
  25. #include "components/prefs/scoped_user_pref_update.h"
  26. #include "components/user_manager/known_user.h"
  27. #include "components/user_manager/remove_user_delegate.h"
  28. #include "components/user_manager/user_directory_integrity_manager.h"
  29. #include "components/user_manager/user_type.h"
  30. #include "google_apis/gaia/gaia_auth_util.h"
  31. #include "third_party/abseil-cpp/absl/types/optional.h"
  32. namespace user_manager {
  33. namespace {
  34. // A dictionary that maps user IDs to the displayed name.
  35. const char kUserDisplayName[] = "UserDisplayName";
  36. // A dictionary that maps user IDs to the user's given name.
  37. const char kUserGivenName[] = "UserGivenName";
  38. // A dictionary that maps user IDs to the displayed (non-canonical) emails.
  39. const char kUserDisplayEmail[] = "UserDisplayEmail";
  40. // A dictionary that maps user IDs to OAuth token presence flag.
  41. const char kUserOAuthTokenStatus[] = "OAuthTokenStatus";
  42. // A dictionary that maps user IDs to a flag indicating whether online
  43. // authentication against GAIA should be enforced during the next sign-in.
  44. const char kUserForceOnlineSignin[] = "UserForceOnlineSignin";
  45. // A dictionary that maps user ID to the user type.
  46. const char kUserType[] = "UserType";
  47. // A string pref containing the ID of the last user who logged in if it was
  48. // a user with gaia account (regular) or an empty string if it was another type
  49. // of user (guest, kiosk, public account, etc.).
  50. const char kLastLoggedInGaiaUser[] = "LastLoggedInRegularUser";
  51. // A string pref containing the ID of the last active user.
  52. // In case of browser crash, this pref will be used to set active user after
  53. // session restore.
  54. const char kLastActiveUser[] = "LastActiveUser";
  55. // Upper bound for a histogram metric reporting the amount of time between
  56. // one regular user logging out and a different regular user logging in.
  57. const int kLogoutToLoginDelayMaxSec = 1800;
  58. // This reads integer value from kUserType Local State preference and
  59. // interprets it as UserType. It is used in initial users load.
  60. UserType GetStoredUserType(const base::Value::Dict& prefs_user_types,
  61. const AccountId& account_id) {
  62. const base::Value* stored_user_type = prefs_user_types.Find(
  63. account_id.HasAccountIdKey() ? account_id.GetAccountIdKey()
  64. : account_id.GetUserEmail());
  65. if (!stored_user_type || !stored_user_type->is_int())
  66. return USER_TYPE_REGULAR;
  67. int int_user_type = stored_user_type->GetInt();
  68. if (int_user_type < 0 || int_user_type >= NUM_USER_TYPES ||
  69. int_user_type == 2) {
  70. LOG(ERROR) << "Bad user type " << int_user_type;
  71. return USER_TYPE_REGULAR;
  72. }
  73. return static_cast<UserType>(int_user_type);
  74. }
  75. std::string UserTypeToString(UserType user_type) {
  76. switch (user_type) {
  77. case USER_TYPE_REGULAR:
  78. return "regular";
  79. case USER_TYPE_CHILD:
  80. return "child";
  81. case USER_TYPE_GUEST:
  82. return "guest";
  83. case USER_TYPE_PUBLIC_ACCOUNT:
  84. return "managed-guest-session";
  85. case USER_TYPE_KIOSK_APP:
  86. return "chrome-app-kiosk";
  87. case USER_TYPE_ARC_KIOSK_APP:
  88. return "arc-kiosk";
  89. case USER_TYPE_WEB_KIOSK_APP:
  90. return "web-kiosk";
  91. case USER_TYPE_ACTIVE_DIRECTORY:
  92. return "active-directory";
  93. case NUM_USER_TYPES:
  94. NOTREACHED();
  95. return "";
  96. }
  97. }
  98. } // namespace
  99. // static
  100. const char UserManagerBase::kLegacySupervisedUsersHistogramName[] =
  101. "ChromeOS.LegacySupervisedUsers.HiddenFromLoginScreen";
  102. // static
  103. const base::Feature UserManagerBase::kRemoveLegacySupervisedUsersOnStartup{
  104. "RemoveLegacySupervisedUsersOnStartup", base::FEATURE_ENABLED_BY_DEFAULT};
  105. // static
  106. void UserManagerBase::RegisterPrefs(PrefRegistrySimple* registry) {
  107. registry->RegisterListPref(kRegularUsersPref);
  108. registry->RegisterStringPref(kLastLoggedInGaiaUser, std::string());
  109. registry->RegisterDictionaryPref(kUserDisplayName);
  110. registry->RegisterDictionaryPref(kUserGivenName);
  111. registry->RegisterDictionaryPref(kUserDisplayEmail);
  112. registry->RegisterDictionaryPref(kUserOAuthTokenStatus);
  113. registry->RegisterDictionaryPref(kUserForceOnlineSignin);
  114. registry->RegisterDictionaryPref(kUserType);
  115. registry->RegisterStringPref(kLastActiveUser, std::string());
  116. UserDirectoryIntegrityManager::RegisterLocalStatePrefs(registry);
  117. KnownUser::RegisterPrefs(registry);
  118. }
  119. UserManagerBase::UserManagerBase(
  120. scoped_refptr<base::SingleThreadTaskRunner> task_runner)
  121. : task_runner_(std::move(task_runner)) {}
  122. UserManagerBase::~UserManagerBase() {
  123. // Can't use STLDeleteElements because of the private destructor of User.
  124. for (UserList::iterator it = users_.begin(); it != users_.end();
  125. it = users_.erase(it)) {
  126. DeleteUser(*it);
  127. }
  128. // These are pointers to the same User instances that were in users_ list.
  129. logged_in_users_.clear();
  130. lru_logged_in_users_.clear();
  131. DeleteUser(active_user_);
  132. }
  133. void UserManagerBase::Shutdown() {
  134. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  135. }
  136. const UserList& UserManagerBase::GetUsers() const {
  137. const_cast<UserManagerBase*>(this)->EnsureUsersLoaded();
  138. return users_;
  139. }
  140. const UserList& UserManagerBase::GetLoggedInUsers() const {
  141. return logged_in_users_;
  142. }
  143. const UserList& UserManagerBase::GetLRULoggedInUsers() const {
  144. return lru_logged_in_users_;
  145. }
  146. const AccountId& UserManagerBase::GetOwnerAccountId() const {
  147. return owner_account_id_;
  148. }
  149. const AccountId& UserManagerBase::GetLastSessionActiveAccountId() const {
  150. return last_session_active_account_id_;
  151. }
  152. void UserManagerBase::UserLoggedIn(const AccountId& account_id,
  153. const std::string& username_hash,
  154. bool browser_restart,
  155. bool is_child) {
  156. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  157. if (!last_session_active_account_id_initialized_) {
  158. last_session_active_account_id_ =
  159. AccountId::FromUserEmail(GetLocalState()->GetString(kLastActiveUser));
  160. last_session_active_account_id_initialized_ = true;
  161. }
  162. User* user = FindUserInListAndModify(account_id);
  163. const UserType user_type =
  164. CalculateUserType(account_id, user, browser_restart, is_child);
  165. if (active_user_ && user) {
  166. user->set_is_logged_in(true);
  167. user->set_username_hash(username_hash);
  168. logged_in_users_.push_back(user);
  169. lru_logged_in_users_.push_back(user);
  170. // Reset the new user flag if the user already exists.
  171. SetIsCurrentUserNew(false);
  172. NotifyUserAddedToSession(user, true /* user switch pending */);
  173. return;
  174. }
  175. switch (user_type) {
  176. case USER_TYPE_REGULAR: // fallthrough
  177. case USER_TYPE_CHILD: // fallthrough
  178. case USER_TYPE_ACTIVE_DIRECTORY:
  179. if (account_id != GetOwnerAccountId() && !user &&
  180. (AreEphemeralUsersEnabled() || browser_restart)) {
  181. RegularUserLoggedInAsEphemeral(account_id, user_type);
  182. } else {
  183. RegularUserLoggedIn(account_id, user_type);
  184. }
  185. break;
  186. case USER_TYPE_GUEST:
  187. GuestUserLoggedIn();
  188. break;
  189. case USER_TYPE_PUBLIC_ACCOUNT:
  190. PublicAccountUserLoggedIn(
  191. user ? user : User::CreatePublicAccountUser(account_id));
  192. break;
  193. case USER_TYPE_KIOSK_APP:
  194. case USER_TYPE_ARC_KIOSK_APP:
  195. case USER_TYPE_WEB_KIOSK_APP:
  196. KioskAppLoggedIn(user);
  197. break;
  198. default:
  199. NOTREACHED() << "Unhandled usert type " << user_type;
  200. }
  201. DCHECK(active_user_);
  202. active_user_->set_is_logged_in(true);
  203. active_user_->set_is_active(true);
  204. active_user_->set_username_hash(username_hash);
  205. logged_in_users_.push_back(active_user_);
  206. SetLRUUser(active_user_);
  207. if (!primary_user_) {
  208. primary_user_ = active_user_;
  209. if (primary_user_->HasGaiaAccount())
  210. SendGaiaUserLoginMetrics(account_id);
  211. } else if (primary_user_ != active_user_) {
  212. // This is only needed for tests where a new user session is created
  213. // for non-existent user. The new user is created and automatically set
  214. // to active and there will be no pending user switch in such case.
  215. SetIsCurrentUserNew(true);
  216. NotifyUserAddedToSession(active_user_, false /* user switch pending */);
  217. }
  218. UMA_HISTOGRAM_ENUMERATION(
  219. "UserManager.LoginUserType", active_user_->GetType(), NUM_USER_TYPES);
  220. static crash_reporter::CrashKeyString<32> session_type("session-type");
  221. session_type.Set(UserTypeToString(active_user_->GetType()));
  222. GetLocalState()->SetString(
  223. kLastLoggedInGaiaUser,
  224. active_user_->HasGaiaAccount() ? account_id.GetUserEmail() : "");
  225. NotifyOnLogin();
  226. PerformPostUserLoggedInActions(browser_restart);
  227. }
  228. void UserManagerBase::SwitchActiveUser(const AccountId& account_id) {
  229. User* user = FindUserAndModify(account_id);
  230. if (!user) {
  231. NOTREACHED() << "Switching to a non-existing user";
  232. return;
  233. }
  234. if (user == active_user_) {
  235. NOTREACHED() << "Switching to a user who is already active";
  236. return;
  237. }
  238. if (!user->is_logged_in()) {
  239. NOTREACHED() << "Switching to a user that is not logged in";
  240. return;
  241. }
  242. if (!user->HasGaiaAccount()) {
  243. NOTREACHED() <<
  244. "Switching to a user without gaia account (non-regular one)";
  245. return;
  246. }
  247. if (user->username_hash().empty()) {
  248. NOTREACHED() << "Switching to a user that doesn't have username_hash set";
  249. return;
  250. }
  251. DCHECK(active_user_);
  252. active_user_->set_is_active(false);
  253. user->set_is_active(true);
  254. active_user_ = user;
  255. // Move the user to the front.
  256. SetLRUUser(active_user_);
  257. NotifyActiveUserHashChanged(active_user_->username_hash());
  258. NotifyActiveUserChanged(active_user_);
  259. CallUpdateLoginState();
  260. }
  261. void UserManagerBase::SwitchToLastActiveUser() {
  262. if (!last_session_active_account_id_.is_valid())
  263. return;
  264. if (AccountId::FromUserEmail(
  265. GetActiveUser()->GetAccountId().GetUserEmail()) !=
  266. last_session_active_account_id_)
  267. SwitchActiveUser(last_session_active_account_id_);
  268. // Make sure that this function gets run only once.
  269. last_session_active_account_id_.clear();
  270. }
  271. void UserManagerBase::OnSessionStarted() {
  272. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  273. CallUpdateLoginState();
  274. GetLocalState()->CommitPendingWrite();
  275. }
  276. void UserManagerBase::RemoveUser(const AccountId& account_id,
  277. UserRemovalReason reason,
  278. RemoveUserDelegate* delegate) {
  279. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  280. if (!CanUserBeRemoved(FindUser(account_id)))
  281. return;
  282. RemoveUserInternal(account_id, reason, delegate);
  283. }
  284. void UserManagerBase::RemoveUserInternal(const AccountId& account_id,
  285. UserRemovalReason reason,
  286. RemoveUserDelegate* delegate) {
  287. RemoveNonOwnerUserInternal(account_id, reason, delegate);
  288. }
  289. void UserManagerBase::RemoveNonOwnerUserInternal(AccountId account_id,
  290. UserRemovalReason reason,
  291. RemoveUserDelegate* delegate) {
  292. if (delegate)
  293. delegate->OnBeforeUserRemoved(account_id);
  294. NotifyUserToBeRemoved(account_id);
  295. AsyncRemoveCryptohome(account_id);
  296. RemoveUserFromList(account_id);
  297. // |account_id| cannot be used after the |RemoveUserFromList| call, use
  298. // |account_id_copy| instead if needed.
  299. NotifyUserRemoved(account_id, reason);
  300. if (delegate)
  301. delegate->OnUserRemoved(account_id);
  302. }
  303. void UserManagerBase::RemoveUserFromList(const AccountId& account_id) {
  304. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  305. RemoveNonCryptohomeData(account_id);
  306. KnownUser(GetLocalState()).RemovePrefs(account_id);
  307. if (user_loading_stage_ == STAGE_LOADED) {
  308. // After the User object is deleted from memory in DeleteUser() here,
  309. // the account_id reference will be invalid if the reference points
  310. // to the account_id in the User object.
  311. DeleteUser(
  312. RemoveRegularOrSupervisedUserFromList(account_id, true /* notify */));
  313. } else {
  314. NOTREACHED() << "Users are not loaded yet.";
  315. return;
  316. }
  317. // Make sure that new data is persisted to Local State.
  318. GetLocalState()->CommitPendingWrite();
  319. }
  320. bool UserManagerBase::IsKnownUser(const AccountId& account_id) const {
  321. return FindUser(account_id) != nullptr;
  322. }
  323. const User* UserManagerBase::FindUser(const AccountId& account_id) const {
  324. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  325. if (active_user_ && active_user_->GetAccountId() == account_id)
  326. return active_user_;
  327. return FindUserInList(account_id);
  328. }
  329. User* UserManagerBase::FindUserAndModify(const AccountId& account_id) {
  330. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  331. if (active_user_ && active_user_->GetAccountId() == account_id)
  332. return active_user_;
  333. return FindUserInListAndModify(account_id);
  334. }
  335. const User* UserManagerBase::GetActiveUser() const {
  336. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  337. return active_user_;
  338. }
  339. User* UserManagerBase::GetActiveUser() {
  340. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  341. return active_user_;
  342. }
  343. const User* UserManagerBase::GetPrimaryUser() const {
  344. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  345. return primary_user_;
  346. }
  347. void UserManagerBase::SaveUserOAuthStatus(
  348. const AccountId& account_id,
  349. User::OAuthTokenStatus oauth_token_status) {
  350. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  351. DVLOG(1) << "Saving user OAuth token status in Local State";
  352. User* user = FindUserAndModify(account_id);
  353. if (user)
  354. user->set_oauth_token_status(oauth_token_status);
  355. // Do not update local state if data stored or cached outside the user's
  356. // cryptohome is to be treated as ephemeral.
  357. if (IsUserNonCryptohomeDataEphemeral(account_id))
  358. return;
  359. {
  360. DictionaryPrefUpdate oauth_status_update(GetLocalState(),
  361. kUserOAuthTokenStatus);
  362. oauth_status_update->SetIntKey(account_id.GetUserEmail(),
  363. static_cast<int>(oauth_token_status));
  364. }
  365. GetLocalState()->CommitPendingWrite();
  366. }
  367. void UserManagerBase::SaveForceOnlineSignin(const AccountId& account_id,
  368. bool force_online_signin) {
  369. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  370. User* const user = FindUserAndModify(account_id);
  371. if (user)
  372. user->set_force_online_signin(force_online_signin);
  373. // Do not update local state if data stored or cached outside the user's
  374. // cryptohome is to be treated as ephemeral.
  375. if (IsUserNonCryptohomeDataEphemeral(account_id))
  376. return;
  377. {
  378. DictionaryPrefUpdate force_online_update(GetLocalState(),
  379. kUserForceOnlineSignin);
  380. force_online_update->SetBoolKey(account_id.GetUserEmail(),
  381. force_online_signin);
  382. }
  383. GetLocalState()->CommitPendingWrite();
  384. }
  385. void UserManagerBase::SaveUserDisplayName(const AccountId& account_id,
  386. const std::u16string& display_name) {
  387. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  388. if (User* user = FindUserAndModify(account_id)) {
  389. user->set_display_name(display_name);
  390. // Do not update local state if data stored or cached outside the user's
  391. // cryptohome is to be treated as ephemeral.
  392. if (!IsUserNonCryptohomeDataEphemeral(account_id)) {
  393. DictionaryPrefUpdate display_name_update(GetLocalState(),
  394. kUserDisplayName);
  395. display_name_update->SetStringKey(account_id.GetUserEmail(),
  396. display_name);
  397. }
  398. }
  399. }
  400. std::u16string UserManagerBase::GetUserDisplayName(
  401. const AccountId& account_id) const {
  402. const User* user = FindUser(account_id);
  403. return user ? user->display_name() : std::u16string();
  404. }
  405. void UserManagerBase::SaveUserDisplayEmail(const AccountId& account_id,
  406. const std::string& display_email) {
  407. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  408. User* user = FindUserAndModify(account_id);
  409. if (!user) {
  410. LOG(ERROR) << "User not found: " << account_id.GetUserEmail();
  411. return; // Ignore if there is no such user.
  412. }
  413. user->set_display_email(display_email);
  414. // Do not update local state if data stored or cached outside the user's
  415. // cryptohome is to be treated as ephemeral.
  416. if (IsUserNonCryptohomeDataEphemeral(account_id))
  417. return;
  418. DictionaryPrefUpdate display_email_update(GetLocalState(), kUserDisplayEmail);
  419. display_email_update->SetStringKey(account_id.GetUserEmail(), display_email);
  420. }
  421. UserType UserManagerBase::GetUserType(const AccountId& account_id) {
  422. const base::Value::Dict& prefs_user_types =
  423. GetLocalState()->GetValueDict(kUserType);
  424. return GetStoredUserType(prefs_user_types, account_id);
  425. }
  426. void UserManagerBase::SaveUserType(const User* user) {
  427. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  428. CHECK(user);
  429. // Do not update local state if data stored or cached outside the user's
  430. // cryptohome is to be treated as ephemeral.
  431. if (IsUserNonCryptohomeDataEphemeral(user->GetAccountId()))
  432. return;
  433. DictionaryPrefUpdate user_type_update(GetLocalState(), kUserType);
  434. user_type_update->SetIntKey(user->GetAccountId().GetAccountIdKey(),
  435. static_cast<int>(user->GetType()));
  436. GetLocalState()->CommitPendingWrite();
  437. }
  438. void UserManagerBase::UpdateUserAccountData(
  439. const AccountId& account_id,
  440. const UserAccountData& account_data) {
  441. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  442. SaveUserDisplayName(account_id, account_data.display_name());
  443. if (User* user = FindUserAndModify(account_id)) {
  444. std::u16string given_name = account_data.given_name();
  445. user->set_given_name(given_name);
  446. if (!IsUserNonCryptohomeDataEphemeral(account_id)) {
  447. DictionaryPrefUpdate given_name_update(GetLocalState(), kUserGivenName);
  448. given_name_update->SetStringKey(account_id.GetUserEmail(), given_name);
  449. }
  450. }
  451. UpdateUserAccountLocale(account_id, account_data.locale());
  452. }
  453. void UserManagerBase::ParseUserList(const base::Value::List& users_list,
  454. const std::set<AccountId>& existing_users,
  455. std::vector<AccountId>* users_vector,
  456. std::set<AccountId>* users_set) {
  457. users_vector->clear();
  458. users_set->clear();
  459. for (size_t i = 0; i < users_list.size(); ++i) {
  460. const std::string* email = users_list[i].GetIfString();
  461. if (!email || email->empty()) {
  462. LOG(ERROR) << "Corrupt entry in user list at index " << i << ".";
  463. continue;
  464. }
  465. const AccountId account_id = known_user::GetAccountId(
  466. *email, std::string() /* id */, AccountType::UNKNOWN);
  467. if (existing_users.find(account_id) != existing_users.end() ||
  468. !users_set->insert(account_id).second) {
  469. LOG(ERROR) << "Duplicate user: " << *email;
  470. continue;
  471. }
  472. users_vector->push_back(account_id);
  473. }
  474. }
  475. bool UserManagerBase::IsCurrentUserOwner() const {
  476. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  477. return !owner_account_id_.empty() && active_user_ &&
  478. active_user_->GetAccountId() == owner_account_id_;
  479. }
  480. bool UserManagerBase::IsCurrentUserNew() const {
  481. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  482. return is_current_user_new_;
  483. }
  484. bool UserManagerBase::IsCurrentUserNonCryptohomeDataEphemeral() const {
  485. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  486. return IsUserLoggedIn() &&
  487. IsUserNonCryptohomeDataEphemeral(GetActiveUser()->GetAccountId());
  488. }
  489. bool UserManagerBase::IsCurrentUserCryptohomeDataEphemeral() const {
  490. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  491. return IsUserLoggedIn() &&
  492. IsUserCryptohomeDataEphemeral(GetActiveUser()->GetAccountId());
  493. }
  494. bool UserManagerBase::CanCurrentUserLock() const {
  495. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  496. return IsUserLoggedIn() && active_user_->can_lock();
  497. }
  498. bool UserManagerBase::IsUserLoggedIn() const {
  499. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  500. return active_user_;
  501. }
  502. bool UserManagerBase::IsLoggedInAsUserWithGaiaAccount() const {
  503. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  504. return IsUserLoggedIn() && active_user_->HasGaiaAccount();
  505. }
  506. bool UserManagerBase::IsLoggedInAsChildUser() const {
  507. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  508. return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_CHILD;
  509. }
  510. bool UserManagerBase::IsLoggedInAsPublicAccount() const {
  511. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  512. return IsUserLoggedIn() &&
  513. active_user_->GetType() == USER_TYPE_PUBLIC_ACCOUNT;
  514. }
  515. bool UserManagerBase::IsLoggedInAsGuest() const {
  516. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  517. return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_GUEST;
  518. }
  519. bool UserManagerBase::IsLoggedInAsKioskApp() const {
  520. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  521. return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_KIOSK_APP;
  522. }
  523. bool UserManagerBase::IsLoggedInAsArcKioskApp() const {
  524. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  525. return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_ARC_KIOSK_APP;
  526. }
  527. bool UserManagerBase::IsLoggedInAsWebKioskApp() const {
  528. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  529. return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_WEB_KIOSK_APP;
  530. }
  531. bool UserManagerBase::IsLoggedInAsAnyKioskApp() const {
  532. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  533. return IsUserLoggedIn() && active_user_->IsKioskType();
  534. }
  535. bool UserManagerBase::IsLoggedInAsStub() const {
  536. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  537. return IsUserLoggedIn() && IsStubAccountId(active_user_->GetAccountId());
  538. }
  539. bool UserManagerBase::IsUserNonCryptohomeDataEphemeral(
  540. const AccountId& account_id) const {
  541. // Data belonging to the guest and stub users is always ephemeral.
  542. if (IsGuestAccountId(account_id) || IsStubAccountId(account_id))
  543. return true;
  544. // Data belonging to the owner, anyone found on the user list and obsolete
  545. // device local accounts whose data has not been removed yet is not ephemeral.
  546. if (account_id == GetOwnerAccountId() || UserExistsInList(account_id) ||
  547. IsDeviceLocalAccountMarkedForRemoval(account_id)) {
  548. return false;
  549. }
  550. // Data belonging to the currently logged-in user is ephemeral when:
  551. // a) The user logged into a regular gaia account while the ephemeral users
  552. // policy was enabled.
  553. // - or -
  554. // b) The user logged into any other account type.
  555. if (IsUserLoggedIn() && (account_id == GetActiveUser()->GetAccountId()) &&
  556. (is_current_user_ephemeral_regular_user_ ||
  557. !IsLoggedInAsUserWithGaiaAccount())) {
  558. return true;
  559. }
  560. // Data belonging to any other user is ephemeral when:
  561. // a) Going through the regular login flow and the ephemeral users policy is
  562. // enabled.
  563. // - or -
  564. // b) The browser is restarting after a crash.
  565. return AreEphemeralUsersEnabled() || HasBrowserRestarted();
  566. }
  567. bool UserManagerBase::IsUserCryptohomeDataEphemeral(
  568. const AccountId& account_id) const {
  569. // Don't consider stub users data as ephemeral.
  570. if (IsStubAccountId(account_id))
  571. return false;
  572. // Data belonging to the guest users is always ephemeral.
  573. if (IsGuestAccountId(account_id))
  574. return true;
  575. // Data belonging to the public accounts is always ephemeral.
  576. const User* user = FindUser(account_id);
  577. if (user && user->GetType() == USER_TYPE_PUBLIC_ACCOUNT)
  578. return true;
  579. // Ephemeral users.
  580. if (AreEphemeralUsersEnabled() && user &&
  581. user->GetType() == USER_TYPE_REGULAR &&
  582. FindUserInList(account_id) == nullptr) {
  583. return true;
  584. }
  585. return false;
  586. }
  587. void UserManagerBase::AddObserver(UserManager::Observer* obs) {
  588. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  589. observer_list_.AddObserver(obs);
  590. }
  591. void UserManagerBase::RemoveObserver(UserManager::Observer* obs) {
  592. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  593. observer_list_.RemoveObserver(obs);
  594. }
  595. void UserManagerBase::AddSessionStateObserver(
  596. UserManager::UserSessionStateObserver* obs) {
  597. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  598. session_state_observer_list_.AddObserver(obs);
  599. }
  600. void UserManagerBase::RemoveSessionStateObserver(
  601. UserManager::UserSessionStateObserver* obs) {
  602. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  603. session_state_observer_list_.RemoveObserver(obs);
  604. }
  605. void UserManagerBase::NotifyLocalStateChanged() {
  606. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  607. for (auto& observer : observer_list_)
  608. observer.LocalStateChanged(this);
  609. }
  610. void UserManagerBase::NotifyUserImageChanged(const User& user) {
  611. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  612. for (auto& observer : observer_list_)
  613. observer.OnUserImageChanged(user);
  614. }
  615. void UserManagerBase::NotifyUserImageIsEnterpriseManagedChanged(
  616. const User& user,
  617. bool is_enterprise_managed) {
  618. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  619. for (auto& observer : observer_list_) {
  620. observer.OnUserImageIsEnterpriseManagedChanged(user, is_enterprise_managed);
  621. }
  622. }
  623. void UserManagerBase::NotifyUserProfileImageUpdateFailed(const User& user) {
  624. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  625. for (auto& observer : observer_list_)
  626. observer.OnUserProfileImageUpdateFailed(user);
  627. }
  628. void UserManagerBase::NotifyUserProfileImageUpdated(
  629. const User& user,
  630. const gfx::ImageSkia& profile_image) {
  631. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  632. for (auto& observer : observer_list_)
  633. observer.OnUserProfileImageUpdated(user, profile_image);
  634. }
  635. void UserManagerBase::NotifyUsersSignInConstraintsChanged() {
  636. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  637. for (auto& observer : observer_list_)
  638. observer.OnUsersSignInConstraintsChanged();
  639. }
  640. void UserManagerBase::NotifyUserToBeRemoved(const AccountId& account_id) {
  641. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  642. for (auto& observer : observer_list_)
  643. observer.OnUserToBeRemoved(account_id);
  644. }
  645. void UserManagerBase::NotifyUserRemoved(const AccountId& account_id,
  646. UserRemovalReason reason) {
  647. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  648. for (auto& observer : observer_list_)
  649. observer.OnUserRemoved(account_id, reason);
  650. }
  651. bool UserManagerBase::CanUserBeRemoved(const User* user) const {
  652. // Only regular users are allowed to be manually removed.
  653. if (!user || !(user->HasGaiaAccount() || user->IsActiveDirectoryUser()))
  654. return false;
  655. // Sanity check: we must not remove single user unless it's an enterprise
  656. // device. This check may seem redundant at a first sight because
  657. // this single user must be an owner and we perform special check later
  658. // in order not to remove an owner. However due to non-instant nature of
  659. // ownership assignment this later check may sometimes fail.
  660. // See http://crosbug.com/12723
  661. if (users_.size() < 2 && !IsEnterpriseManaged())
  662. return false;
  663. // Sanity check: do not allow any of the the logged in users to be removed.
  664. for (UserList::const_iterator it = logged_in_users_.begin();
  665. it != logged_in_users_.end();
  666. ++it) {
  667. if ((*it)->GetAccountId() == user->GetAccountId())
  668. return false;
  669. }
  670. return true;
  671. }
  672. bool UserManagerBase::GetEphemeralUsersEnabled() const {
  673. return ephemeral_users_enabled_;
  674. }
  675. void UserManagerBase::SetEphemeralUsersEnabled(bool enabled) {
  676. ephemeral_users_enabled_ = enabled;
  677. }
  678. void UserManagerBase::SetIsCurrentUserNew(bool is_new) {
  679. is_current_user_new_ = is_new;
  680. }
  681. void UserManagerBase::SetOwnerId(const AccountId& owner_account_id) {
  682. owner_account_id_ = owner_account_id;
  683. CallUpdateLoginState();
  684. }
  685. const AccountId& UserManagerBase::GetPendingUserSwitchID() const {
  686. return pending_user_switch_;
  687. }
  688. void UserManagerBase::SetPendingUserSwitchId(const AccountId& account_id) {
  689. pending_user_switch_ = account_id;
  690. }
  691. void UserManagerBase::EnsureUsersLoaded() {
  692. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  693. if (!GetLocalState())
  694. return;
  695. if (user_loading_stage_ != STAGE_NOT_LOADED)
  696. return;
  697. user_loading_stage_ = STAGE_LOADING;
  698. PrefService* local_state = GetLocalState();
  699. const base::Value::List& prefs_regular_users =
  700. local_state->GetValueList(kRegularUsersPref);
  701. const base::Value::Dict& prefs_display_names =
  702. local_state->GetValueDict(kUserDisplayName);
  703. const base::Value::Dict& prefs_given_names =
  704. local_state->GetValueDict(kUserGivenName);
  705. const base::Value::Dict& prefs_display_emails =
  706. local_state->GetValueDict(kUserDisplayEmail);
  707. const base::Value::Dict& prefs_user_types =
  708. local_state->GetValueDict(kUserType);
  709. // Load public sessions first.
  710. std::set<AccountId> device_local_accounts_set;
  711. LoadDeviceLocalAccounts(&device_local_accounts_set);
  712. // Load regular users and supervised users.
  713. std::vector<AccountId> regular_users;
  714. std::set<AccountId> regular_users_set;
  715. ParseUserList(prefs_regular_users, device_local_accounts_set, &regular_users,
  716. &regular_users_set);
  717. for (std::vector<AccountId>::const_iterator it = regular_users.begin();
  718. it != regular_users.end(); ++it) {
  719. if (IsDeprecatedSupervisedAccountId(*it)) {
  720. RemoveLegacySupervisedUser(*it);
  721. // Hide legacy supervised users from the login screen if not removed.
  722. continue;
  723. }
  724. base::UmaHistogramEnumeration(
  725. kLegacySupervisedUsersHistogramName,
  726. LegacySupervisedUserStatus::kGaiaUserDisplayed);
  727. User* user =
  728. User::CreateRegularUser(*it, GetStoredUserType(prefs_user_types, *it));
  729. user->set_oauth_token_status(LoadUserOAuthStatus(*it));
  730. user->set_force_online_signin(LoadForceOnlineSignin(*it));
  731. KnownUser known_user(GetLocalState());
  732. user->set_using_saml(known_user.IsUsingSAML(*it));
  733. users_.push_back(user);
  734. }
  735. for (auto* user : users_) {
  736. auto& account_id = user->GetAccountId();
  737. const std::string* display_name =
  738. prefs_display_names.FindString(account_id.GetUserEmail());
  739. if (display_name) {
  740. user->set_display_name(base::UTF8ToUTF16(*display_name));
  741. }
  742. const std::string* given_name =
  743. prefs_given_names.FindString(account_id.GetUserEmail());
  744. if (given_name) {
  745. user->set_given_name(base::UTF8ToUTF16(*given_name));
  746. }
  747. const std::string* display_email =
  748. prefs_display_emails.FindString(account_id.GetUserEmail());
  749. if (display_email) {
  750. user->set_display_email(*display_email);
  751. }
  752. }
  753. user_loading_stage_ = STAGE_LOADED;
  754. PerformPostUserListLoadingActions();
  755. }
  756. UserList& UserManagerBase::GetUsersAndModify() {
  757. EnsureUsersLoaded();
  758. return users_;
  759. }
  760. const User* UserManagerBase::FindUserInList(const AccountId& account_id) const {
  761. const UserList& users = GetUsers();
  762. for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) {
  763. if ((*it)->GetAccountId() == account_id)
  764. return *it;
  765. }
  766. return nullptr;
  767. }
  768. bool UserManagerBase::UserExistsInList(const AccountId& account_id) const {
  769. const base::Value::List& user_list =
  770. GetLocalState()->GetValueList(kRegularUsersPref);
  771. for (const base::Value& i : user_list) {
  772. const std::string* email = i.GetIfString();
  773. if (email && (account_id.GetUserEmail() == *email))
  774. return true;
  775. }
  776. return false;
  777. }
  778. User* UserManagerBase::FindUserInListAndModify(const AccountId& account_id) {
  779. UserList& users = GetUsersAndModify();
  780. for (UserList::iterator it = users.begin(); it != users.end(); ++it) {
  781. if ((*it)->GetAccountId() == account_id)
  782. return *it;
  783. }
  784. return nullptr;
  785. }
  786. void UserManagerBase::GuestUserLoggedIn() {
  787. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  788. active_user_ = User::CreateGuestUser(GetGuestAccountId());
  789. }
  790. void UserManagerBase::AddUserRecord(User* user) {
  791. // Add the user to the front of the user list.
  792. ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsersPref);
  793. prefs_users_update->GetList().Insert(
  794. prefs_users_update->GetList().begin(),
  795. base::Value(user->GetAccountId().GetUserEmail()));
  796. users_.insert(users_.begin(), user);
  797. }
  798. void UserManagerBase::RegularUserLoggedIn(const AccountId& account_id,
  799. const UserType user_type) {
  800. // Remove the user from the user list.
  801. active_user_ =
  802. RemoveRegularOrSupervisedUserFromList(account_id, false /* notify */);
  803. KnownUser known_user(GetLocalState());
  804. if (active_user_ && active_user_->GetType() != user_type) {
  805. active_user_->UpdateType(user_type);
  806. // Clear information about profile policy requirements to enforce setting it
  807. // again for the new account type.
  808. known_user.ClearProfileRequiresPolicy(account_id);
  809. }
  810. // If the user was not found on the user list, create a new user.
  811. SetIsCurrentUserNew(!active_user_);
  812. if (IsCurrentUserNew()) {
  813. active_user_ = User::CreateRegularUser(account_id, user_type);
  814. SaveUserType(active_user_);
  815. active_user_->set_oauth_token_status(LoadUserOAuthStatus(account_id));
  816. SaveUserDisplayName(active_user_->GetAccountId(),
  817. base::UTF8ToUTF16(active_user_->GetAccountName(true)));
  818. } else {
  819. SaveUserType(active_user_);
  820. }
  821. AddUserRecord(active_user_);
  822. known_user.SetIsEphemeralUser(active_user_->GetAccountId(), false);
  823. // Make sure that new data is persisted to Local State.
  824. GetLocalState()->CommitPendingWrite();
  825. }
  826. void UserManagerBase::RegularUserLoggedInAsEphemeral(
  827. const AccountId& account_id,
  828. const UserType user_type) {
  829. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  830. SetIsCurrentUserNew(true);
  831. is_current_user_ephemeral_regular_user_ = true;
  832. active_user_ = User::CreateRegularUser(account_id, user_type);
  833. KnownUser(GetLocalState())
  834. .SetIsEphemeralUser(active_user_->GetAccountId(), true);
  835. }
  836. void UserManagerBase::NotifyActiveUserChanged(User* active_user) {
  837. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  838. for (auto& observer : session_state_observer_list_)
  839. observer.ActiveUserChanged(active_user);
  840. }
  841. void UserManagerBase::NotifyOnLogin() {
  842. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  843. NotifyActiveUserHashChanged(active_user_->username_hash());
  844. NotifyActiveUserChanged(active_user_);
  845. CallUpdateLoginState();
  846. }
  847. User::OAuthTokenStatus UserManagerBase::LoadUserOAuthStatus(
  848. const AccountId& account_id) const {
  849. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  850. const base::Value::Dict& prefs_oauth_status =
  851. GetLocalState()->GetValueDict(kUserOAuthTokenStatus);
  852. absl::optional<int> oauth_token_status =
  853. prefs_oauth_status.FindInt(account_id.GetUserEmail());
  854. if (!oauth_token_status.has_value())
  855. return User::OAUTH_TOKEN_STATUS_UNKNOWN;
  856. return static_cast<User::OAuthTokenStatus>(oauth_token_status.value());
  857. }
  858. bool UserManagerBase::LoadForceOnlineSignin(const AccountId& account_id) const {
  859. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  860. const base::Value::Dict& prefs_force_online =
  861. GetLocalState()->GetValueDict(kUserForceOnlineSignin);
  862. return prefs_force_online.FindBool(account_id.GetUserEmail()).value_or(false);
  863. }
  864. void UserManagerBase::RemoveNonCryptohomeData(const AccountId& account_id) {
  865. PrefService* prefs = GetLocalState();
  866. DictionaryPrefUpdate prefs_display_name_update(prefs, kUserDisplayName);
  867. prefs_display_name_update->RemoveKey(account_id.GetUserEmail());
  868. DictionaryPrefUpdate prefs_given_name_update(prefs, kUserGivenName);
  869. prefs_given_name_update->RemoveKey(account_id.GetUserEmail());
  870. DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail);
  871. prefs_display_email_update->RemoveKey(account_id.GetUserEmail());
  872. DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus);
  873. prefs_oauth_update->RemoveKey(account_id.GetUserEmail());
  874. DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin);
  875. prefs_force_online_update->RemoveKey(account_id.GetUserEmail());
  876. KnownUser(prefs).RemovePrefs(account_id);
  877. const AccountId last_active_user =
  878. AccountId::FromUserEmail(GetLocalState()->GetString(kLastActiveUser));
  879. if (account_id == last_active_user)
  880. GetLocalState()->SetString(kLastActiveUser, std::string());
  881. }
  882. User* UserManagerBase::RemoveRegularOrSupervisedUserFromList(
  883. const AccountId& account_id,
  884. bool notify) {
  885. ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsersPref);
  886. prefs_users_update->ClearList();
  887. User* user = nullptr;
  888. for (UserList::iterator it = users_.begin(); it != users_.end();) {
  889. if ((*it)->GetAccountId() == account_id) {
  890. user = *it;
  891. it = users_.erase(it);
  892. } else {
  893. if ((*it)->HasGaiaAccount() || (*it)->IsActiveDirectoryUser()) {
  894. const std::string user_email = (*it)->GetAccountId().GetUserEmail();
  895. prefs_users_update->Append(user_email);
  896. }
  897. ++it;
  898. }
  899. }
  900. if (notify) {
  901. OnUserRemoved(account_id);
  902. NotifyLocalStateChanged();
  903. }
  904. return user;
  905. }
  906. void UserManagerBase::NotifyUserAddedToSession(const User* added_user,
  907. bool user_switch_pending) {
  908. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  909. for (auto& observer : session_state_observer_list_)
  910. observer.UserAddedToSession(added_user);
  911. }
  912. void UserManagerBase::NotifyActiveUserHashChanged(const std::string& hash) {
  913. DCHECK(!task_runner_ || task_runner_->RunsTasksInCurrentSequence());
  914. for (auto& observer : session_state_observer_list_)
  915. observer.ActiveUserHashChanged(hash);
  916. }
  917. void UserManagerBase::Initialize() {
  918. UserManager::Initialize();
  919. if (!HasBrowserRestarted()) {
  920. PrefService* local_state = GetLocalState();
  921. // local_state may be null in unit tests.
  922. if (local_state) {
  923. KnownUser known_user(local_state);
  924. known_user.CleanEphemeralUsers();
  925. known_user.CleanObsoletePrefs();
  926. }
  927. }
  928. CallUpdateLoginState();
  929. }
  930. void UserManagerBase::CallUpdateLoginState() {
  931. UpdateLoginState(active_user_, primary_user_, IsCurrentUserOwner());
  932. }
  933. void UserManagerBase::SetLRUUser(User* user) {
  934. GetLocalState()->SetString(kLastActiveUser,
  935. user->GetAccountId().GetUserEmail());
  936. GetLocalState()->CommitPendingWrite();
  937. UserList::iterator it =
  938. std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user);
  939. if (it != lru_logged_in_users_.end())
  940. lru_logged_in_users_.erase(it);
  941. lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user);
  942. }
  943. void UserManagerBase::SendGaiaUserLoginMetrics(const AccountId& account_id) {
  944. // If this isn't the first time Chrome was run after the system booted,
  945. // assume that Chrome was restarted because a previous session ended.
  946. if (IsFirstExecAfterBoot())
  947. return;
  948. const std::string last_email =
  949. GetLocalState()->GetString(kLastLoggedInGaiaUser);
  950. const base::TimeDelta time_to_login =
  951. base::TimeTicks::Now() - manager_creation_time_;
  952. if (!last_email.empty() &&
  953. account_id != AccountId::FromUserEmail(last_email) &&
  954. time_to_login.InSeconds() <= kLogoutToLoginDelayMaxSec) {
  955. UMA_HISTOGRAM_CUSTOM_COUNTS("UserManager.LogoutToLoginDelay",
  956. time_to_login.InSeconds(), 1,
  957. kLogoutToLoginDelayMaxSec, 50);
  958. }
  959. }
  960. void UserManagerBase::UpdateUserAccountLocale(const AccountId& account_id,
  961. const std::string& locale) {
  962. std::unique_ptr<std::string> resolved_locale(new std::string());
  963. if (!locale.empty() && locale != GetApplicationLocale()) {
  964. // std::move will nullptr out |resolved_locale|, so cache the underlying
  965. // ptr.
  966. std::string* raw_resolved_locale = resolved_locale.get();
  967. ScheduleResolveLocale(
  968. locale,
  969. base::BindOnce(&UserManagerBase::DoUpdateAccountLocale,
  970. weak_factory_.GetWeakPtr(), account_id,
  971. std::move(resolved_locale)),
  972. raw_resolved_locale);
  973. } else {
  974. resolved_locale = std::make_unique<std::string>(locale);
  975. DoUpdateAccountLocale(account_id, std::move(resolved_locale));
  976. }
  977. }
  978. void UserManagerBase::DoUpdateAccountLocale(
  979. const AccountId& account_id,
  980. std::unique_ptr<std::string> resolved_locale) {
  981. User* user = FindUserAndModify(account_id);
  982. if (user && resolved_locale)
  983. user->SetAccountLocale(*resolved_locale);
  984. }
  985. void UserManagerBase::DeleteUser(User* user) {
  986. const bool is_active_user = (user == active_user_);
  987. delete user;
  988. if (is_active_user)
  989. active_user_ = nullptr;
  990. }
  991. // TODO(crbug/1189715): Remove dormant legacy supervised user cryptohomes. After
  992. // we have enough confidence that there are no more supervised users on devices
  993. // in the wild, remove this.
  994. void UserManagerBase::RemoveLegacySupervisedUser(const AccountId& account_id) {
  995. DCHECK(IsDeprecatedSupervisedAccountId(account_id));
  996. if (base::FeatureList::IsEnabled(kRemoveLegacySupervisedUsersOnStartup)) {
  997. // Since we skip adding legacy supervised users to the users list,
  998. // FindUser(account_id) returns nullptr and CanUserBeRemoved() returns
  999. // false. This is why we call RemoveUserInternal() directly instead of
  1000. // RemoveUser().
  1001. RemoveUserInternal(account_id, UserRemovalReason::UNKNOWN,
  1002. /*delegate=*/nullptr);
  1003. base::UmaHistogramEnumeration(kLegacySupervisedUsersHistogramName,
  1004. LegacySupervisedUserStatus::kLSUDeleted);
  1005. } else {
  1006. base::UmaHistogramEnumeration(kLegacySupervisedUsersHistogramName,
  1007. LegacySupervisedUserStatus::kLSUHidden);
  1008. }
  1009. }
  1010. } // namespace user_manager