Home | History | Annotate | Download | only in users
      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 "base/task_runner.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/grit/theme_resources.h"
     11 #include "components/user_manager/user_image/user_image.h"
     12 #include "components/user_manager/user_type.h"
     13 #include "ui/base/resource/resource_bundle.h"
     14 
     15 namespace {
     16 
     17 class FakeTaskRunner : public base::TaskRunner {
     18  public:
     19   virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
     20                                const base::Closure& task,
     21                                base::TimeDelta delay) OVERRIDE {
     22     task.Run();
     23     return true;
     24   }
     25   virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
     26 
     27  protected:
     28   virtual ~FakeTaskRunner() {}
     29 };
     30 
     31 }  // namespace
     32 
     33 namespace chromeos {
     34 
     35 FakeUserManager::FakeUserManager()
     36     : ChromeUserManager(new FakeTaskRunner(), new FakeTaskRunner()),
     37       supervised_user_manager_(new FakeSupervisedUserManager),
     38       primary_user_(NULL),
     39       multi_profile_user_controller_(NULL) {
     40 }
     41 
     42 FakeUserManager::~FakeUserManager() {
     43   // Can't use STLDeleteElements because of the private destructor of User.
     44   for (user_manager::UserList::iterator it = user_list_.begin();
     45        it != user_list_.end();
     46        it = user_list_.erase(it)) {
     47     delete *it;
     48   }
     49 }
     50 
     51 const user_manager::User* FakeUserManager::AddUser(const std::string& email) {
     52   user_manager::User* user = user_manager::User::CreateRegularUser(email);
     53   user->set_username_hash(
     54       ProfileHelper::GetUserIdHashByUserIdForTesting(email));
     55   user->SetStubImage(user_manager::UserImage(
     56                          *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     57                              IDR_PROFILE_PICTURE_LOADING)),
     58                      user_manager::User::USER_IMAGE_PROFILE,
     59                      false);
     60   user_list_.push_back(user);
     61   return user;
     62 }
     63 
     64 const user_manager::User* FakeUserManager::AddPublicAccountUser(
     65     const std::string& email) {
     66   user_manager::User* user = user_manager::User::CreatePublicAccountUser(email);
     67   user->set_username_hash(
     68       ProfileHelper::GetUserIdHashByUserIdForTesting(email));
     69   user->SetStubImage(user_manager::UserImage(
     70                          *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     71                              IDR_PROFILE_PICTURE_LOADING)),
     72                      user_manager::User::USER_IMAGE_PROFILE,
     73                      false);
     74   user_list_.push_back(user);
     75   return user;
     76 }
     77 
     78 void FakeUserManager::AddKioskAppUser(const std::string& kiosk_app_username) {
     79   user_manager::User* user =
     80       user_manager::User::CreateKioskAppUser(kiosk_app_username);
     81   user->set_username_hash(
     82       ProfileHelper::GetUserIdHashByUserIdForTesting(kiosk_app_username));
     83   user_list_.push_back(user);
     84 }
     85 
     86 void FakeUserManager::RemoveUserFromList(const std::string& email) {
     87   user_manager::UserList::iterator it = user_list_.begin();
     88   while (it != user_list_.end() && (*it)->email() != email) ++it;
     89   if (it != user_list_.end()) {
     90     delete *it;
     91     user_list_.erase(it);
     92   }
     93 }
     94 
     95 void FakeUserManager::LoginUser(const std::string& email) {
     96   UserLoggedIn(
     97       email, ProfileHelper::GetUserIdHashByUserIdForTesting(email), false);
     98 }
     99 
    100 const user_manager::UserList& FakeUserManager::GetUsers() const {
    101   return user_list_;
    102 }
    103 
    104 user_manager::UserList FakeUserManager::GetUsersAdmittedForMultiProfile()
    105     const {
    106   user_manager::UserList result;
    107   for (user_manager::UserList::const_iterator it = user_list_.begin();
    108        it != user_list_.end();
    109        ++it) {
    110     if ((*it)->GetType() == user_manager::USER_TYPE_REGULAR &&
    111         !(*it)->is_logged_in())
    112       result.push_back(*it);
    113   }
    114   return result;
    115 }
    116 
    117 const user_manager::UserList& FakeUserManager::GetLoggedInUsers() const {
    118   return logged_in_users_;
    119 }
    120 
    121 void FakeUserManager::UserLoggedIn(const std::string& email,
    122                                    const std::string& username_hash,
    123                                    bool browser_restart) {
    124   for (user_manager::UserList::const_iterator it = user_list_.begin();
    125        it != user_list_.end();
    126        ++it) {
    127     if ((*it)->username_hash() == username_hash) {
    128       (*it)->set_is_logged_in(true);
    129       (*it)->set_profile_is_created();
    130       logged_in_users_.push_back(*it);
    131 
    132       if (!primary_user_)
    133         primary_user_ = *it;
    134       break;
    135     }
    136   }
    137 }
    138 
    139 user_manager::User* FakeUserManager::GetActiveUserInternal() const {
    140   if (user_list_.size()) {
    141     if (!active_user_id_.empty()) {
    142       for (user_manager::UserList::const_iterator it = user_list_.begin();
    143            it != user_list_.end();
    144            ++it) {
    145         if ((*it)->email() == active_user_id_)
    146           return *it;
    147       }
    148     }
    149     return user_list_[0];
    150   }
    151   return NULL;
    152 }
    153 
    154 const user_manager::User* FakeUserManager::GetActiveUser() const {
    155   return GetActiveUserInternal();
    156 }
    157 
    158 user_manager::User* FakeUserManager::GetActiveUser() {
    159   return GetActiveUserInternal();
    160 }
    161 
    162 void FakeUserManager::SwitchActiveUser(const std::string& email) {
    163   active_user_id_ = email;
    164   ProfileHelper::Get()->ActiveUserHashChanged(
    165       ProfileHelper::GetUserIdHashByUserIdForTesting(email));
    166 }
    167 
    168 void FakeUserManager::SaveUserDisplayName(
    169     const std::string& username,
    170     const base::string16& display_name) {
    171   for (user_manager::UserList::iterator it = user_list_.begin();
    172        it != user_list_.end();
    173        ++it) {
    174     if ((*it)->email() == username) {
    175       (*it)->set_display_name(display_name);
    176       return;
    177     }
    178   }
    179 }
    180 
    181 MultiProfileUserController* FakeUserManager::GetMultiProfileUserController() {
    182   return multi_profile_user_controller_;
    183 }
    184 
    185 SupervisedUserManager* FakeUserManager::GetSupervisedUserManager() {
    186   return supervised_user_manager_.get();
    187 }
    188 
    189 UserImageManager* FakeUserManager::GetUserImageManager(
    190     const std::string& /* user_id */) {
    191   return NULL;
    192 }
    193 
    194 const user_manager::UserList& FakeUserManager::GetLRULoggedInUsers() const {
    195   return user_list_;
    196 }
    197 
    198 user_manager::UserList FakeUserManager::GetUnlockUsers() const {
    199   return user_list_;
    200 }
    201 
    202 const std::string& FakeUserManager::GetOwnerEmail() const {
    203   return owner_email_;
    204 }
    205 
    206 bool FakeUserManager::IsKnownUser(const std::string& email) const {
    207   return true;
    208 }
    209 
    210 const user_manager::User* FakeUserManager::FindUser(
    211     const std::string& email) const {
    212   const user_manager::UserList& users = GetUsers();
    213   for (user_manager::UserList::const_iterator it = users.begin();
    214        it != users.end();
    215        ++it) {
    216     if ((*it)->email() == email)
    217       return *it;
    218   }
    219   return NULL;
    220 }
    221 
    222 user_manager::User* FakeUserManager::FindUserAndModify(
    223     const std::string& email) {
    224   return NULL;
    225 }
    226 
    227 const user_manager::User* FakeUserManager::GetLoggedInUser() const {
    228   return NULL;
    229 }
    230 
    231 user_manager::User* FakeUserManager::GetLoggedInUser() {
    232   return NULL;
    233 }
    234 
    235 const user_manager::User* FakeUserManager::GetPrimaryUser() const {
    236   return primary_user_;
    237 }
    238 
    239 base::string16 FakeUserManager::GetUserDisplayName(
    240     const std::string& username) const {
    241   return base::string16();
    242 }
    243 
    244 std::string FakeUserManager::GetUserDisplayEmail(
    245     const std::string& username) const {
    246   return std::string();
    247 }
    248 
    249 bool FakeUserManager::IsCurrentUserOwner() const {
    250   return false;
    251 }
    252 
    253 bool FakeUserManager::IsCurrentUserNew() const {
    254   return false;
    255 }
    256 
    257 bool FakeUserManager::IsCurrentUserNonCryptohomeDataEphemeral() const {
    258   return false;
    259 }
    260 
    261 bool FakeUserManager::CanCurrentUserLock() const {
    262   return false;
    263 }
    264 
    265 bool FakeUserManager::IsUserLoggedIn() const {
    266   return logged_in_users_.size() > 0;
    267 }
    268 
    269 bool FakeUserManager::IsLoggedInAsRegularUser() const {
    270   return true;
    271 }
    272 
    273 bool FakeUserManager::IsLoggedInAsDemoUser() const {
    274   return false;
    275 }
    276 
    277 bool FakeUserManager::IsLoggedInAsPublicAccount() const {
    278   return false;
    279 }
    280 
    281 bool FakeUserManager::IsLoggedInAsGuest() const {
    282   return false;
    283 }
    284 
    285 bool FakeUserManager::IsLoggedInAsSupervisedUser() const {
    286   return false;
    287 }
    288 
    289 bool FakeUserManager::IsLoggedInAsKioskApp() const {
    290   const user_manager::User* active_user = GetActiveUser();
    291   return active_user
    292              ? active_user->GetType() == user_manager::USER_TYPE_KIOSK_APP
    293              : false;
    294 }
    295 
    296 bool FakeUserManager::IsLoggedInAsStub() const {
    297   return false;
    298 }
    299 
    300 bool FakeUserManager::IsSessionStarted() const {
    301   return false;
    302 }
    303 
    304 bool FakeUserManager::IsUserNonCryptohomeDataEphemeral(
    305     const std::string& email) const {
    306   return false;
    307 }
    308 
    309 UserFlow* FakeUserManager::GetCurrentUserFlow() const {
    310   return NULL;
    311 }
    312 
    313 UserFlow* FakeUserManager::GetUserFlow(const std::string& email) const {
    314   return NULL;
    315 }
    316 
    317 bool FakeUserManager::AreSupervisedUsersAllowed() const {
    318   return true;
    319 }
    320 
    321 bool FakeUserManager::AreEphemeralUsersEnabled() const {
    322   return false;
    323 }
    324 
    325 const std::string& FakeUserManager::GetApplicationLocale() const {
    326   static const std::string default_locale("en-US");
    327   return default_locale;
    328 }
    329 
    330 PrefService* FakeUserManager::GetLocalState() const {
    331   return NULL;
    332 }
    333 
    334 bool FakeUserManager::IsEnterpriseManaged() const {
    335   return false;
    336 }
    337 
    338 bool FakeUserManager::IsDemoApp(const std::string& user_id) const {
    339   return false;
    340 }
    341 
    342 bool FakeUserManager::IsKioskApp(const std::string& user_id) const {
    343   return false;
    344 }
    345 
    346 bool FakeUserManager::IsPublicAccountMarkedForRemoval(
    347     const std::string& user_id) const {
    348   return false;
    349 }
    350 
    351 }  // namespace chromeos
    352