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
15namespace {
16
17class 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
33namespace chromeos {
34
35FakeUserManager::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
42FakeUserManager::~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
51const 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
64const 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
78void 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
86void 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
95void FakeUserManager::LoginUser(const std::string& email) {
96  UserLoggedIn(
97      email, ProfileHelper::GetUserIdHashByUserIdForTesting(email), false);
98}
99
100const user_manager::UserList& FakeUserManager::GetUsers() const {
101  return user_list_;
102}
103
104user_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
117const user_manager::UserList& FakeUserManager::GetLoggedInUsers() const {
118  return logged_in_users_;
119}
120
121void 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
139user_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
154const user_manager::User* FakeUserManager::GetActiveUser() const {
155  return GetActiveUserInternal();
156}
157
158user_manager::User* FakeUserManager::GetActiveUser() {
159  return GetActiveUserInternal();
160}
161
162void FakeUserManager::SwitchActiveUser(const std::string& email) {
163  active_user_id_ = email;
164  ProfileHelper::Get()->ActiveUserHashChanged(
165      ProfileHelper::GetUserIdHashByUserIdForTesting(email));
166}
167
168void 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
181MultiProfileUserController* FakeUserManager::GetMultiProfileUserController() {
182  return multi_profile_user_controller_;
183}
184
185SupervisedUserManager* FakeUserManager::GetSupervisedUserManager() {
186  return supervised_user_manager_.get();
187}
188
189UserImageManager* FakeUserManager::GetUserImageManager(
190    const std::string& /* user_id */) {
191  return NULL;
192}
193
194const user_manager::UserList& FakeUserManager::GetLRULoggedInUsers() const {
195  return user_list_;
196}
197
198user_manager::UserList FakeUserManager::GetUnlockUsers() const {
199  return user_list_;
200}
201
202const std::string& FakeUserManager::GetOwnerEmail() const {
203  return owner_email_;
204}
205
206bool FakeUserManager::IsKnownUser(const std::string& email) const {
207  return true;
208}
209
210const 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
222user_manager::User* FakeUserManager::FindUserAndModify(
223    const std::string& email) {
224  return NULL;
225}
226
227const user_manager::User* FakeUserManager::GetLoggedInUser() const {
228  return NULL;
229}
230
231user_manager::User* FakeUserManager::GetLoggedInUser() {
232  return NULL;
233}
234
235const user_manager::User* FakeUserManager::GetPrimaryUser() const {
236  return primary_user_;
237}
238
239base::string16 FakeUserManager::GetUserDisplayName(
240    const std::string& username) const {
241  return base::string16();
242}
243
244std::string FakeUserManager::GetUserDisplayEmail(
245    const std::string& username) const {
246  return std::string();
247}
248
249bool FakeUserManager::IsCurrentUserOwner() const {
250  return false;
251}
252
253bool FakeUserManager::IsCurrentUserNew() const {
254  return false;
255}
256
257bool FakeUserManager::IsCurrentUserNonCryptohomeDataEphemeral() const {
258  return false;
259}
260
261bool FakeUserManager::CanCurrentUserLock() const {
262  return false;
263}
264
265bool FakeUserManager::IsUserLoggedIn() const {
266  return logged_in_users_.size() > 0;
267}
268
269bool FakeUserManager::IsLoggedInAsRegularUser() const {
270  return true;
271}
272
273bool FakeUserManager::IsLoggedInAsDemoUser() const {
274  return false;
275}
276
277bool FakeUserManager::IsLoggedInAsPublicAccount() const {
278  return false;
279}
280
281bool FakeUserManager::IsLoggedInAsGuest() const {
282  return false;
283}
284
285bool FakeUserManager::IsLoggedInAsSupervisedUser() const {
286  return false;
287}
288
289bool 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
296bool FakeUserManager::IsLoggedInAsStub() const {
297  return false;
298}
299
300bool FakeUserManager::IsSessionStarted() const {
301  return false;
302}
303
304bool FakeUserManager::IsUserNonCryptohomeDataEphemeral(
305    const std::string& email) const {
306  return false;
307}
308
309UserFlow* FakeUserManager::GetCurrentUserFlow() const {
310  return NULL;
311}
312
313UserFlow* FakeUserManager::GetUserFlow(const std::string& email) const {
314  return NULL;
315}
316
317bool FakeUserManager::AreSupervisedUsersAllowed() const {
318  return true;
319}
320
321bool FakeUserManager::AreEphemeralUsersEnabled() const {
322  return false;
323}
324
325const std::string& FakeUserManager::GetApplicationLocale() const {
326  static const std::string default_locale("en-US");
327  return default_locale;
328}
329
330PrefService* FakeUserManager::GetLocalState() const {
331  return NULL;
332}
333
334bool FakeUserManager::IsEnterpriseManaged() const {
335  return false;
336}
337
338bool FakeUserManager::IsDemoApp(const std::string& user_id) const {
339  return false;
340}
341
342bool FakeUserManager::IsKioskApp(const std::string& user_id) const {
343  return false;
344}
345
346bool FakeUserManager::IsPublicAccountMarkedForRemoval(
347    const std::string& user_id) const {
348  return false;
349}
350
351}  // namespace chromeos
352