Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / fake_chrome_user_manager.cc
blob7191f0c10b7bc0106f7e5bdc304d78bdae1ae85f
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
7 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
8 #include "chrome/browser/chromeos/login/users/fake_supervised_user_manager.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/browser/chromeos/settings/cros_settings.h"
11 #include "chrome/grit/theme_resources.h"
12 #include "components/user_manager/user_image/user_image.h"
13 #include "components/user_manager/user_type.h"
14 #include "ui/base/resource/resource_bundle.h"
16 namespace chromeos {
18 class FakeSupervisedUserManager;
20 FakeChromeUserManager::FakeChromeUserManager()
21 : supervised_user_manager_(new FakeSupervisedUserManager),
22 bootstrap_manager_(NULL),
23 multi_profile_user_controller_(NULL) {
26 FakeChromeUserManager::~FakeChromeUserManager() {
29 const user_manager::User* FakeChromeUserManager::AddUser(
30 const std::string& email) {
31 return AddUserWithAffiliation(email, false);
34 const user_manager::User* FakeChromeUserManager::AddUserWithAffiliation(
35 const std::string& email,
36 bool is_affiliated) {
37 user_manager::User* user = user_manager::User::CreateRegularUser(email);
38 user->set_affiliation(is_affiliated);
39 user->set_username_hash(
40 ProfileHelper::GetUserIdHashByUserIdForTesting(email));
41 user->SetStubImage(user_manager::UserImage(
42 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
43 IDR_PROFILE_PICTURE_LOADING)),
44 user_manager::User::USER_IMAGE_PROFILE, false);
45 users_.push_back(user);
46 return user;
49 const user_manager::User* FakeChromeUserManager::AddPublicAccountUser(
50 const std::string& email) {
51 user_manager::User* user = user_manager::User::CreatePublicAccountUser(email);
52 user->set_username_hash(
53 ProfileHelper::GetUserIdHashByUserIdForTesting(email));
54 user->SetStubImage(user_manager::UserImage(
55 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
56 IDR_PROFILE_PICTURE_LOADING)),
57 user_manager::User::USER_IMAGE_PROFILE, false);
58 users_.push_back(user);
59 return user;
62 void FakeChromeUserManager::AddKioskAppUser(
63 const std::string& kiosk_app_username) {
64 user_manager::User* user =
65 user_manager::User::CreateKioskAppUser(kiosk_app_username);
66 user->set_username_hash(
67 ProfileHelper::GetUserIdHashByUserIdForTesting(kiosk_app_username));
68 users_.push_back(user);
71 void FakeChromeUserManager::LoginUser(const std::string& email) {
72 UserLoggedIn(email, ProfileHelper::GetUserIdHashByUserIdForTesting(email),
73 false /* browser_restart */);
76 BootstrapManager* FakeChromeUserManager::GetBootstrapManager() {
77 return bootstrap_manager_;
80 MultiProfileUserController*
81 FakeChromeUserManager::GetMultiProfileUserController() {
82 return multi_profile_user_controller_;
85 SupervisedUserManager* FakeChromeUserManager::GetSupervisedUserManager() {
86 return supervised_user_manager_.get();
89 UserImageManager* FakeChromeUserManager::GetUserImageManager(
90 const std::string& /* user_id */) {
91 return nullptr;
94 void FakeChromeUserManager::SetUserFlow(const std::string& email,
95 UserFlow* flow) {
96 ResetUserFlow(email);
97 specific_flows_[email] = flow;
100 UserFlow* FakeChromeUserManager::GetCurrentUserFlow() const {
101 if (!IsUserLoggedIn())
102 return GetDefaultUserFlow();
103 return GetUserFlow(GetLoggedInUser()->email());
106 UserFlow* FakeChromeUserManager::GetUserFlow(const std::string& email) const {
107 FlowMap::const_iterator it = specific_flows_.find(email);
108 if (it != specific_flows_.end())
109 return it->second;
110 return GetDefaultUserFlow();
113 void FakeChromeUserManager::ResetUserFlow(const std::string& email) {
114 FlowMap::iterator it = specific_flows_.find(email);
115 if (it != specific_flows_.end()) {
116 delete it->second;
117 specific_flows_.erase(it);
121 void FakeChromeUserManager::SwitchActiveUser(const std::string& email) {
122 active_user_id_ = email;
123 ProfileHelper::Get()->ActiveUserHashChanged(
124 ProfileHelper::GetUserIdHashByUserIdForTesting(email));
125 if (!users_.empty() && !active_user_id_.empty()) {
126 for (user_manager::User* user : users_)
127 user->set_is_active(user->email() == active_user_id_);
131 const std::string& FakeChromeUserManager::GetOwnerEmail() const {
132 return owner_email_;
135 void FakeChromeUserManager::SessionStarted() {
138 void FakeChromeUserManager::RemoveUser(
139 const std::string& email,
140 user_manager::RemoveUserDelegate* delegate) {
143 user_manager::UserList
144 FakeChromeUserManager::GetUsersAllowedForSupervisedUsersCreation() const {
145 CrosSettings* cros_settings = CrosSettings::Get();
146 bool allow_new_user = true;
147 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
148 bool supervised_users_allowed = AreSupervisedUsersAllowed();
150 // Restricted either by policy or by owner.
151 if (!allow_new_user || !supervised_users_allowed)
152 return user_manager::UserList();
154 return ChromeUserManager::GetUsersAllowedAsSupervisedUserManagers(GetUsers());
157 user_manager::UserList FakeChromeUserManager::GetUsersAllowedForMultiProfile()
158 const {
159 // Supervised users are not allowed to use multi-profiles.
160 if (GetLoggedInUsers().size() == 1 &&
161 GetPrimaryUser()->GetType() != user_manager::USER_TYPE_REGULAR) {
162 return user_manager::UserList();
165 user_manager::UserList result;
166 const user_manager::UserList& users = GetUsers();
167 for (user_manager::User* user : users) {
168 if (user->GetType() == user_manager::USER_TYPE_REGULAR &&
169 !user->is_logged_in()) {
170 result.push_back(user);
174 return result;
177 UserFlow* FakeChromeUserManager::GetDefaultUserFlow() const {
178 if (!default_flow_.get())
179 default_flow_.reset(new DefaultUserFlow());
180 return default_flow_.get();
183 bool FakeChromeUserManager::FindKnownUserPrefs(
184 const user_manager::UserID& user_id,
185 const base::DictionaryValue** out_value) {
186 return false;
189 void FakeChromeUserManager::UpdateKnownUserPrefs(
190 const user_manager::UserID& user_id,
191 const base::DictionaryValue& values,
192 bool clear) {
195 } // namespace chromeos