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 5 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" 6 7 #include "chrome/browser/chromeos/login/users/fake_supervised_user_manager.h" 8 9 namespace { 10 11 // As defined in /chromeos/dbus/cryptohome_client.cc. 12 static const char kUserIdHashSuffix[] = "-hash"; 13 14 } // namespace 15 16 namespace chromeos { 17 18 FakeUserManager::FakeUserManager() 19 : supervised_user_manager_(new FakeSupervisedUserManager), 20 primary_user_(NULL), 21 multi_profile_user_controller_(NULL) {} 22 23 FakeUserManager::~FakeUserManager() { 24 // Can't use STLDeleteElements because of the private destructor of User. 25 for (UserList::iterator it = user_list_.begin(); it != user_list_.end(); 26 it = user_list_.erase(it)) { 27 delete *it; 28 } 29 } 30 31 const User* FakeUserManager::AddUser(const std::string& email) { 32 User* user = User::CreateRegularUser(email); 33 user->set_username_hash(email + kUserIdHashSuffix); 34 user->SetStubImage(User::kProfileImageIndex, false); 35 user_list_.push_back(user); 36 return user; 37 } 38 39 void FakeUserManager::AddKioskAppUser(const std::string& kiosk_app_username) { 40 User* user = User::CreateKioskAppUser(kiosk_app_username); 41 user->set_username_hash(kiosk_app_username + kUserIdHashSuffix); 42 user_list_.push_back(user); 43 } 44 45 void FakeUserManager::LoginUser(const std::string& email) { 46 UserLoggedIn(email, email + kUserIdHashSuffix, false); 47 } 48 49 void FakeUserManager::SetProfileForUser(const User* user, Profile* profile) { 50 user_to_profile_[user] = profile; 51 } 52 53 const UserList& FakeUserManager::GetUsers() const { 54 return user_list_; 55 } 56 57 UserList FakeUserManager::GetUsersAdmittedForMultiProfile() const { 58 UserList result; 59 for (UserList::const_iterator it = user_list_.begin(); 60 it != user_list_.end(); 61 ++it) { 62 if ((*it)->GetType() == User::USER_TYPE_REGULAR && !(*it)->is_logged_in()) 63 result.push_back(*it); 64 } 65 return result; 66 } 67 68 const UserList& FakeUserManager::GetLoggedInUsers() const { 69 return logged_in_users_; 70 } 71 72 void FakeUserManager::UserLoggedIn(const std::string& email, 73 const std::string& username_hash, 74 bool browser_restart) { 75 for (UserList::const_iterator it = user_list_.begin(); 76 it != user_list_.end(); 77 ++it) { 78 if ((*it)->username_hash() == username_hash) { 79 (*it)->set_is_logged_in(true); 80 logged_in_users_.push_back(*it); 81 82 if (!primary_user_) 83 primary_user_ = *it; 84 break; 85 } 86 } 87 } 88 89 User* FakeUserManager::GetActiveUserInternal() const { 90 if (user_list_.size()) { 91 if (!active_user_id_.empty()) { 92 for (UserList::const_iterator it = user_list_.begin(); 93 it != user_list_.end(); ++it) { 94 if ((*it)->email() == active_user_id_) 95 return *it; 96 } 97 } 98 return user_list_[0]; 99 } 100 return NULL; 101 } 102 103 const User* FakeUserManager::GetActiveUser() const { 104 return GetActiveUserInternal(); 105 } 106 107 User* FakeUserManager::GetActiveUser() { 108 return GetActiveUserInternal(); 109 } 110 111 void FakeUserManager::SwitchActiveUser(const std::string& email) { 112 active_user_id_ = email; 113 } 114 115 void FakeUserManager::SaveUserDisplayName( 116 const std::string& username, 117 const base::string16& display_name) { 118 for (UserList::iterator it = user_list_.begin(); 119 it != user_list_.end(); ++it) { 120 if ((*it)->email() == username) { 121 (*it)->set_display_name(display_name); 122 return; 123 } 124 } 125 } 126 127 MultiProfileUserController* FakeUserManager::GetMultiProfileUserController() { 128 return multi_profile_user_controller_; 129 } 130 131 SupervisedUserManager* FakeUserManager::GetSupervisedUserManager() { 132 return supervised_user_manager_.get(); 133 } 134 135 UserImageManager* FakeUserManager::GetUserImageManager( 136 const std::string& /* user_id */) { 137 return NULL; 138 } 139 140 const UserList& FakeUserManager::GetLRULoggedInUsers() { 141 return user_list_; 142 } 143 144 UserList FakeUserManager::GetUnlockUsers() const { 145 return user_list_; 146 } 147 148 const std::string& FakeUserManager::GetOwnerEmail() { 149 return owner_email_; 150 } 151 152 bool FakeUserManager::IsKnownUser(const std::string& email) const { 153 return true; 154 } 155 156 const User* FakeUserManager::FindUser(const std::string& email) const { 157 const UserList& users = GetUsers(); 158 for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) { 159 if ((*it)->email() == email) 160 return *it; 161 } 162 return NULL; 163 } 164 165 User* FakeUserManager::FindUserAndModify(const std::string& email) { 166 return NULL; 167 } 168 169 const User* FakeUserManager::GetLoggedInUser() const { 170 return NULL; 171 } 172 173 User* FakeUserManager::GetLoggedInUser() { 174 return NULL; 175 } 176 177 const User* FakeUserManager::GetPrimaryUser() const { 178 return primary_user_; 179 } 180 181 User* FakeUserManager::GetUserByProfile(Profile* profile) const { 182 const std::string& user_name = profile->GetProfileName(); 183 for (UserList::const_iterator it = user_list_.begin(); 184 it != user_list_.end(); ++it) { 185 if ((*it)->email() == user_name) 186 return *it; 187 } 188 return primary_user_; 189 } 190 191 Profile* FakeUserManager::GetProfileByUser(const User* user) const { 192 std::map<const User*, Profile*>::const_iterator it = 193 user_to_profile_.find(user); 194 return it == user_to_profile_.end() ? NULL : it->second; 195 } 196 197 base::string16 FakeUserManager::GetUserDisplayName( 198 const std::string& username) const { 199 return base::string16(); 200 } 201 202 std::string FakeUserManager::GetUserDisplayEmail( 203 const std::string& username) const { 204 return std::string(); 205 } 206 207 bool FakeUserManager::IsCurrentUserOwner() const { 208 return false; 209 } 210 211 bool FakeUserManager::IsCurrentUserNew() const { 212 return false; 213 } 214 215 bool FakeUserManager::IsCurrentUserNonCryptohomeDataEphemeral() const { 216 return false; 217 } 218 219 bool FakeUserManager::CanCurrentUserLock() const { 220 return false; 221 } 222 223 bool FakeUserManager::IsUserLoggedIn() const { 224 return logged_in_users_.size() > 0; 225 } 226 227 bool FakeUserManager::IsLoggedInAsRegularUser() const { 228 return true; 229 } 230 231 bool FakeUserManager::IsLoggedInAsDemoUser() const { 232 return false; 233 } 234 235 bool FakeUserManager::IsLoggedInAsPublicAccount() const { 236 return false; 237 } 238 239 bool FakeUserManager::IsLoggedInAsGuest() const { 240 return false; 241 } 242 243 bool FakeUserManager::IsLoggedInAsLocallyManagedUser() const { 244 return false; 245 } 246 247 bool FakeUserManager::IsLoggedInAsKioskApp() const { 248 const User* active_user = GetActiveUser(); 249 return active_user ? 250 active_user->GetType() == User::USER_TYPE_KIOSK_APP : 251 false; 252 } 253 254 bool FakeUserManager::IsLoggedInAsStub() const { 255 return false; 256 } 257 258 bool FakeUserManager::IsSessionStarted() const { 259 return false; 260 } 261 262 bool FakeUserManager::UserSessionsRestored() const { 263 return false; 264 } 265 266 bool FakeUserManager::HasBrowserRestarted() const { 267 return false; 268 } 269 270 bool FakeUserManager::IsUserNonCryptohomeDataEphemeral( 271 const std::string& email) const { 272 return false; 273 } 274 275 UserFlow* FakeUserManager::GetCurrentUserFlow() const { 276 return NULL; 277 } 278 279 UserFlow* FakeUserManager::GetUserFlow(const std::string& email) const { 280 return NULL; 281 } 282 283 bool FakeUserManager::GetAppModeChromeClientOAuthInfo( 284 std::string* chrome_client_id, 285 std::string* chrome_client_secret) { 286 return false; 287 } 288 289 bool FakeUserManager::AreLocallyManagedUsersAllowed() const { 290 return true; 291 } 292 293 base::FilePath FakeUserManager::GetUserProfileDir( 294 const std::string&email) const { 295 return base::FilePath(); 296 } 297 298 bool FakeUserManager::RespectLocalePreference( 299 Profile* profile, 300 const User* user, 301 scoped_ptr<locale_util::SwitchLanguageCallback> callback) const { 302 return false; 303 } 304 305 } // namespace chromeos 306