1 // Copyright 2015 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.
5 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
7 #include "chrome/browser/chromeos/login/users/chrome_user_manager.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/browser/chromeos/settings/cros_settings.h"
11 #include "chrome/grit/theme_resources.h"
12 #include "components/user_manager/user_image/user_image.h"
13 #include "components/user_manager/user_type.h"
14 #include "ui/base/resource/resource_bundle.h"
18 class FakeSupervisedUserManager
;
20 FakeChromeUserManager::FakeChromeUserManager()
21 : supervised_user_manager_(new FakeSupervisedUserManager
),
22 bootstrap_manager_(NULL
),
23 multi_profile_user_controller_(NULL
) {
26 FakeChromeUserManager::~FakeChromeUserManager() {
29 const user_manager::User
* FakeChromeUserManager::AddUser(
30 const std::string
& email
) {
31 user_manager::User
* user
= user_manager::User::CreateRegularUser(email
);
32 user
->set_username_hash(
33 ProfileHelper::GetUserIdHashByUserIdForTesting(email
));
34 user
->SetStubImage(user_manager::UserImage(
35 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
36 IDR_PROFILE_PICTURE_LOADING
)),
37 user_manager::User::USER_IMAGE_PROFILE
, false);
38 users_
.push_back(user
);
42 const user_manager::User
* FakeChromeUserManager::AddPublicAccountUser(
43 const std::string
& email
) {
44 user_manager::User
* user
= user_manager::User::CreatePublicAccountUser(email
);
45 user
->set_username_hash(
46 ProfileHelper::GetUserIdHashByUserIdForTesting(email
));
47 user
->SetStubImage(user_manager::UserImage(
48 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
49 IDR_PROFILE_PICTURE_LOADING
)),
50 user_manager::User::USER_IMAGE_PROFILE
, false);
51 users_
.push_back(user
);
55 void FakeChromeUserManager::AddKioskAppUser(
56 const std::string
& kiosk_app_username
) {
57 user_manager::User
* user
=
58 user_manager::User::CreateKioskAppUser(kiosk_app_username
);
59 user
->set_username_hash(
60 ProfileHelper::GetUserIdHashByUserIdForTesting(kiosk_app_username
));
61 users_
.push_back(user
);
64 void FakeChromeUserManager::LoginUser(const std::string
& email
) {
65 UserLoggedIn(email
, ProfileHelper::GetUserIdHashByUserIdForTesting(email
),
66 false /* browser_restart */);
69 BootstrapManager
* FakeChromeUserManager::GetBootstrapManager() {
70 return bootstrap_manager_
;
73 MultiProfileUserController
*
74 FakeChromeUserManager::GetMultiProfileUserController() {
75 return multi_profile_user_controller_
;
78 SupervisedUserManager
* FakeChromeUserManager::GetSupervisedUserManager() {
79 return supervised_user_manager_
.get();
82 UserImageManager
* FakeChromeUserManager::GetUserImageManager(
83 const std::string
& /* user_id */) {
87 void FakeChromeUserManager::SetUserFlow(const std::string
& email
,
90 specific_flows_
[email
] = flow
;
93 UserFlow
* FakeChromeUserManager::GetCurrentUserFlow() const {
94 if (!IsUserLoggedIn())
95 return GetDefaultUserFlow();
96 return GetUserFlow(GetLoggedInUser()->email());
99 UserFlow
* FakeChromeUserManager::GetUserFlow(const std::string
& email
) const {
100 FlowMap::const_iterator it
= specific_flows_
.find(email
);
101 if (it
!= specific_flows_
.end())
103 return GetDefaultUserFlow();
106 void FakeChromeUserManager::ResetUserFlow(const std::string
& email
) {
107 FlowMap::iterator it
= specific_flows_
.find(email
);
108 if (it
!= specific_flows_
.end()) {
110 specific_flows_
.erase(it
);
114 void FakeChromeUserManager::SwitchActiveUser(const std::string
& email
) {
115 active_user_id_
= email
;
116 ProfileHelper::Get()->ActiveUserHashChanged(
117 ProfileHelper::GetUserIdHashByUserIdForTesting(email
));
118 if (!users_
.empty() && !active_user_id_
.empty()) {
119 for (user_manager::User
* user
: users_
)
120 user
->set_is_active(user
->email() == active_user_id_
);
124 const std::string
& FakeChromeUserManager::GetOwnerEmail() const {
128 void FakeChromeUserManager::SessionStarted() {
131 void FakeChromeUserManager::RemoveUser(
132 const std::string
& email
,
133 user_manager::RemoveUserDelegate
* delegate
) {
136 user_manager::UserList
137 FakeChromeUserManager::GetUsersAllowedForSupervisedUsersCreation() const {
138 CrosSettings
* cros_settings
= CrosSettings::Get();
139 bool allow_new_user
= true;
140 cros_settings
->GetBoolean(kAccountsPrefAllowNewUser
, &allow_new_user
);
141 bool supervised_users_allowed
= AreSupervisedUsersAllowed();
143 // Restricted either by policy or by owner.
144 if (!allow_new_user
|| !supervised_users_allowed
)
145 return user_manager::UserList();
147 return ChromeUserManager::GetUsersAllowedAsSupervisedUserManagers(GetUsers());
150 user_manager::UserList
FakeChromeUserManager::GetUsersAllowedForMultiProfile()
152 // Supervised users are not allowed to use multi-profiles.
153 if (GetLoggedInUsers().size() == 1 &&
154 GetPrimaryUser()->GetType() != user_manager::USER_TYPE_REGULAR
) {
155 return user_manager::UserList();
158 user_manager::UserList result
;
159 const user_manager::UserList
& users
= GetUsers();
160 for (user_manager::User
* user
: users
) {
161 if (user
->GetType() == user_manager::USER_TYPE_REGULAR
&&
162 !user
->is_logged_in()) {
163 result
.push_back(user
);
170 UserFlow
* FakeChromeUserManager::GetDefaultUserFlow() const {
171 if (!default_flow_
.get())
172 default_flow_
.reset(new DefaultUserFlow());
173 return default_flow_
.get();
176 bool FakeChromeUserManager::FindKnownUserPrefs(
177 const user_manager::UserID
& user_id
,
178 const base::DictionaryValue
** out_value
) {
182 void FakeChromeUserManager::UpdateKnownUserPrefs(
183 const user_manager::UserID
& user_id
,
184 const base::DictionaryValue
& values
,
188 } // namespace chromeos