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