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