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.
5 #include "components/user_manager/user.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "chromeos/login/user_names.h"
12 #include "components/user_manager/user_image/default_user_images.h"
13 #include "google_apis/gaia/gaia_auth_util.h"
14 #include "ui/base/resource/resource_bundle.h"
16 namespace user_manager
{
20 // Returns account name portion of an email.
21 std::string
GetUserName(const std::string
& email
) {
22 std::string::size_type i
= email
.find('@');
23 if (i
== 0 || i
== std::string::npos
) {
26 return email
.substr(0, i
);
32 bool User::TypeHasGaiaAccount(UserType user_type
) {
33 return user_type
== USER_TYPE_REGULAR
||
34 user_type
== USER_TYPE_CHILD
;
37 // Also used for regular supervised users.
38 class RegularUser
: public User
{
40 explicit RegularUser(const std::string
& email
);
41 ~RegularUser() override
;
43 // Overridden from User:
44 UserType
GetType() const override
;
45 bool CanSyncImage() const override
;
46 void SetIsChild(bool is_child
) override
;
51 DISALLOW_COPY_AND_ASSIGN(RegularUser
);
54 class GuestUser
: public User
{
57 ~GuestUser() override
;
59 // Overridden from User:
60 UserType
GetType() const override
;
63 DISALLOW_COPY_AND_ASSIGN(GuestUser
);
66 class KioskAppUser
: public User
{
68 explicit KioskAppUser(const std::string
& app_id
);
69 ~KioskAppUser() override
;
71 // Overridden from User:
72 UserType
GetType() const override
;
75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser
);
78 class SupervisedUser
: public User
{
80 explicit SupervisedUser(const std::string
& username
);
81 ~SupervisedUser() override
;
83 // Overridden from User:
84 UserType
GetType() const override
;
85 std::string
display_email() const override
;
88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser
);
91 class PublicAccountUser
: public User
{
93 explicit PublicAccountUser(const std::string
& email
);
94 ~PublicAccountUser() override
;
96 // Overridden from User:
97 UserType
GetType() const override
;
100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser
);
103 std::string
User::GetEmail() const {
104 return display_email();
107 base::string16
User::GetDisplayName() const {
108 // Fallback to the email account name in case display name haven't been set.
109 return display_name_
.empty() ? base::UTF8ToUTF16(GetAccountName(true))
113 base::string16
User::GetGivenName() const {
117 const gfx::ImageSkia
& User::GetImage() const {
118 return user_image_
.image();
121 UserID
User::GetUserID() const {
122 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
125 void User::SetIsChild(bool is_child
) {
126 VLOG(1) << "Ignoring SetIsChild call with param " << is_child
;
128 NOTREACHED() << "Calling SetIsChild(true) for base User class."
129 << "Base class cannot be set as child";
133 bool User::HasGaiaAccount() const {
134 return TypeHasGaiaAccount(GetType());
137 bool User::IsSupervised() const {
138 UserType type
= GetType();
139 return type
== USER_TYPE_SUPERVISED
||
140 type
== USER_TYPE_CHILD
;
143 std::string
User::GetAccountName(bool use_display_email
) const {
144 if (use_display_email
&& !display_email_
.empty())
145 return GetUserName(display_email_
);
147 return GetUserName(email_
);
150 bool User::HasDefaultImage() const {
151 return image_index_
>= 0 && image_index_
< kDefaultImagesCount
;
154 bool User::CanSyncImage() const {
158 std::string
User::display_email() const {
159 return display_email_
;
162 bool User::can_lock() const {
166 std::string
User::username_hash() const {
167 return username_hash_
;
170 bool User::is_logged_in() const {
171 return is_logged_in_
;
174 bool User::is_active() const {
178 User
* User::CreateRegularUser(const std::string
& email
) {
179 return new RegularUser(email
);
182 User
* User::CreateGuestUser() {
183 return new GuestUser
;
186 User
* User::CreateKioskAppUser(const std::string
& kiosk_app_username
) {
187 return new KioskAppUser(kiosk_app_username
);
190 User
* User::CreateSupervisedUser(const std::string
& username
) {
191 return new SupervisedUser(username
);
194 User
* User::CreatePublicAccountUser(const std::string
& email
) {
195 return new PublicAccountUser(email
);
198 User::User(const std::string
& email
)
200 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN
),
201 force_online_signin_(false),
202 image_index_(USER_IMAGE_INVALID
),
203 image_is_stub_(false),
204 image_is_loading_(false),
206 is_logged_in_(false),
208 profile_is_created_(false),
209 is_affiliated_(false){
215 void User::SetAccountLocale(const std::string
& resolved_account_locale
) {
216 account_locale_
.reset(new std::string(resolved_account_locale
));
219 void User::SetImage(const UserImage
& user_image
, int image_index
) {
220 user_image_
= user_image
;
221 image_index_
= image_index
;
222 image_is_stub_
= false;
223 image_is_loading_
= false;
224 DCHECK(HasDefaultImage() || user_image
.has_raw_image());
227 void User::SetImageURL(const GURL
& image_url
) {
228 user_image_
.set_url(image_url
);
231 void User::SetStubImage(const UserImage
& stub_user_image
,
234 user_image_
= stub_user_image
;
235 image_index_
= image_index
;
236 image_is_stub_
= true;
237 image_is_loading_
= is_loading
;
240 RegularUser::RegularUser(const std::string
& email
)
241 : User(email
), is_child_(false) {
243 set_display_email(email
);
246 RegularUser::~RegularUser() {
249 UserType
RegularUser::GetType() const {
250 return is_child_
? user_manager::USER_TYPE_CHILD
:
251 user_manager::USER_TYPE_REGULAR
;
254 bool RegularUser::CanSyncImage() const {
258 void RegularUser::SetIsChild(bool is_child
) {
259 VLOG(1) << "Setting user is child to " << is_child
;
260 is_child_
= is_child
;
263 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName
) {
264 set_display_email(std::string());
267 GuestUser::~GuestUser() {
270 UserType
GuestUser::GetType() const {
271 return user_manager::USER_TYPE_GUEST
;
274 KioskAppUser::KioskAppUser(const std::string
& kiosk_app_username
)
275 : User(kiosk_app_username
) {
276 set_display_email(kiosk_app_username
);
279 KioskAppUser::~KioskAppUser() {
282 UserType
KioskAppUser::GetType() const {
283 return user_manager::USER_TYPE_KIOSK_APP
;
286 SupervisedUser::SupervisedUser(const std::string
& username
) : User(username
) {
290 SupervisedUser::~SupervisedUser() {
293 UserType
SupervisedUser::GetType() const {
294 return user_manager::USER_TYPE_SUPERVISED
;
297 std::string
SupervisedUser::display_email() const {
298 return base::UTF16ToUTF8(display_name());
301 PublicAccountUser::PublicAccountUser(const std::string
& email
) : User(email
) {
304 PublicAccountUser::~PublicAccountUser() {
307 UserType
PublicAccountUser::GetType() const {
308 return user_manager::USER_TYPE_PUBLIC_ACCOUNT
;
311 bool User::has_gaia_account() const {
312 static_assert(user_manager::NUM_USER_TYPES
== 7,
313 "NUM_USER_TYPES should equal 7");
315 case user_manager::USER_TYPE_REGULAR
:
316 case user_manager::USER_TYPE_CHILD
:
318 case user_manager::USER_TYPE_GUEST
:
319 case user_manager::USER_TYPE_PUBLIC_ACCOUNT
:
320 case user_manager::USER_TYPE_SUPERVISED
:
321 case user_manager::USER_TYPE_KIOSK_APP
:
329 } // namespace user_manager