Update To 11.40.268.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 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::GetUsersAllowedForMultiProfile() const {
105   user_manager::UserList result;
106   for (user_manager::UserList::const_iterator it = user_list_.begin();
107        it != user_list_.end();
108        ++it) {
109     if ((*it)->GetType() == user_manager::USER_TYPE_REGULAR &&
110         !(*it)->is_logged_in())
111       result.push_back(*it);
112   }
113   return result;
114 }
115
116 const user_manager::UserList& FakeUserManager::GetLoggedInUsers() const {
117   return logged_in_users_;
118 }
119
120 void FakeUserManager::UserLoggedIn(const std::string& email,
121                                    const std::string& username_hash,
122                                    bool browser_restart) {
123   for (user_manager::UserList::const_iterator it = user_list_.begin();
124        it != user_list_.end();
125        ++it) {
126     if ((*it)->username_hash() == username_hash) {
127       (*it)->set_is_logged_in(true);
128       (*it)->set_profile_is_created();
129       logged_in_users_.push_back(*it);
130
131       if (!primary_user_)
132         primary_user_ = *it;
133       break;
134     }
135   }
136 }
137
138 user_manager::User* FakeUserManager::GetActiveUserInternal() const {
139   if (user_list_.size()) {
140     if (!active_user_id_.empty()) {
141       for (user_manager::UserList::const_iterator it = user_list_.begin();
142            it != user_list_.end();
143            ++it) {
144         if ((*it)->email() == active_user_id_)
145           return *it;
146       }
147     }
148     return user_list_[0];
149   }
150   return NULL;
151 }
152
153 const user_manager::User* FakeUserManager::GetActiveUser() const {
154   return GetActiveUserInternal();
155 }
156
157 user_manager::User* FakeUserManager::GetActiveUser() {
158   return GetActiveUserInternal();
159 }
160
161 void FakeUserManager::SwitchActiveUser(const std::string& email) {
162   active_user_id_ = email;
163   ProfileHelper::Get()->ActiveUserHashChanged(
164       ProfileHelper::GetUserIdHashByUserIdForTesting(email));
165 }
166
167 void FakeUserManager::SaveUserDisplayName(
168     const std::string& username,
169     const base::string16& display_name) {
170   for (user_manager::UserList::iterator it = user_list_.begin();
171        it != user_list_.end();
172        ++it) {
173     if ((*it)->email() == username) {
174       (*it)->set_display_name(display_name);
175       return;
176     }
177   }
178 }
179
180 MultiProfileUserController* FakeUserManager::GetMultiProfileUserController() {
181   return multi_profile_user_controller_;
182 }
183
184 SupervisedUserManager* FakeUserManager::GetSupervisedUserManager() {
185   return supervised_user_manager_.get();
186 }
187
188 UserImageManager* FakeUserManager::GetUserImageManager(
189     const std::string& /* user_id */) {
190   return NULL;
191 }
192
193 const user_manager::UserList& FakeUserManager::GetLRULoggedInUsers() const {
194   return user_list_;
195 }
196
197 user_manager::UserList FakeUserManager::GetUnlockUsers() const {
198   return user_list_;
199 }
200
201 const std::string& FakeUserManager::GetOwnerEmail() const {
202   return owner_email_;
203 }
204
205 bool FakeUserManager::IsKnownUser(const std::string& email) const {
206   return true;
207 }
208
209 const user_manager::User* FakeUserManager::FindUser(
210     const std::string& email) const {
211   const user_manager::UserList& users = GetUsers();
212   for (user_manager::UserList::const_iterator it = users.begin();
213        it != users.end();
214        ++it) {
215     if ((*it)->email() == email)
216       return *it;
217   }
218   return NULL;
219 }
220
221 user_manager::User* FakeUserManager::FindUserAndModify(
222     const std::string& email) {
223   return NULL;
224 }
225
226 const user_manager::User* FakeUserManager::GetLoggedInUser() const {
227   return NULL;
228 }
229
230 user_manager::User* FakeUserManager::GetLoggedInUser() {
231   return NULL;
232 }
233
234 const user_manager::User* FakeUserManager::GetPrimaryUser() const {
235   return primary_user_;
236 }
237
238 base::string16 FakeUserManager::GetUserDisplayName(
239     const std::string& username) const {
240   return base::string16();
241 }
242
243 std::string FakeUserManager::GetUserDisplayEmail(
244     const std::string& username) const {
245   return std::string();
246 }
247
248 bool FakeUserManager::IsCurrentUserOwner() const {
249   return false;
250 }
251
252 bool FakeUserManager::IsCurrentUserNew() const {
253   return false;
254 }
255
256 bool FakeUserManager::IsCurrentUserNonCryptohomeDataEphemeral() const {
257   return false;
258 }
259
260 bool FakeUserManager::CanCurrentUserLock() const {
261   return false;
262 }
263
264 bool FakeUserManager::IsUserLoggedIn() const {
265   return logged_in_users_.size() > 0;
266 }
267
268 bool FakeUserManager::IsLoggedInAsRegularUser() const {
269   return true;
270 }
271
272 bool FakeUserManager::IsLoggedInAsDemoUser() const {
273   return false;
274 }
275
276 bool FakeUserManager::IsLoggedInAsPublicAccount() const {
277   return false;
278 }
279
280 bool FakeUserManager::IsLoggedInAsGuest() const {
281   return false;
282 }
283
284 bool FakeUserManager::IsLoggedInAsSupervisedUser() const {
285   return false;
286 }
287
288 bool FakeUserManager::IsLoggedInAsKioskApp() const {
289   const user_manager::User* active_user = GetActiveUser();
290   return active_user
291              ? active_user->GetType() == user_manager::USER_TYPE_KIOSK_APP
292              : false;
293 }
294
295 bool FakeUserManager::IsLoggedInAsStub() const {
296   return false;
297 }
298
299 bool FakeUserManager::IsSessionStarted() const {
300   return false;
301 }
302
303 bool FakeUserManager::IsUserNonCryptohomeDataEphemeral(
304     const std::string& email) const {
305   return false;
306 }
307
308 UserFlow* FakeUserManager::GetCurrentUserFlow() const {
309   return NULL;
310 }
311
312 UserFlow* FakeUserManager::GetUserFlow(const std::string& email) const {
313   return NULL;
314 }
315
316 bool FakeUserManager::AreSupervisedUsersAllowed() const {
317   return true;
318 }
319
320 bool FakeUserManager::AreEphemeralUsersEnabled() const {
321   return false;
322 }
323
324 const std::string& FakeUserManager::GetApplicationLocale() const {
325   static const std::string default_locale("en-US");
326   return default_locale;
327 }
328
329 PrefService* FakeUserManager::GetLocalState() const {
330   return NULL;
331 }
332
333 bool FakeUserManager::IsEnterpriseManaged() const {
334   return false;
335 }
336
337 bool FakeUserManager::IsDemoApp(const std::string& user_id) const {
338   return false;
339 }
340
341 bool FakeUserManager::IsKioskApp(const std::string& user_id) const {
342   return false;
343 }
344
345 bool FakeUserManager::IsPublicAccountMarkedForRemoval(
346     const std::string& user_id) const {
347   return false;
348 }
349
350 }  // namespace chromeos