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 std::string
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) {
214 void User::SetAccountLocale(const std::string
& resolved_account_locale
) {
215 account_locale_
.reset(new std::string(resolved_account_locale
));
218 void User::SetImage(const UserImage
& user_image
, int image_index
) {
219 user_image_
= user_image
;
220 image_index_
= image_index
;
221 image_is_stub_
= false;
222 image_is_loading_
= false;
223 DCHECK(HasDefaultImage() || user_image
.has_raw_image());
226 void User::SetImageURL(const GURL
& image_url
) {
227 user_image_
.set_url(image_url
);
230 void User::SetStubImage(const UserImage
& stub_user_image
,
233 user_image_
= stub_user_image
;
234 image_index_
= image_index
;
235 image_is_stub_
= true;
236 image_is_loading_
= is_loading
;
239 RegularUser::RegularUser(const std::string
& email
)
240 : User(email
), is_child_(false) {
242 set_display_email(email
);
245 RegularUser::~RegularUser() {
248 UserType
RegularUser::GetType() const {
249 return is_child_
? user_manager::USER_TYPE_CHILD
:
250 user_manager::USER_TYPE_REGULAR
;
253 bool RegularUser::CanSyncImage() const {
257 void RegularUser::SetIsChild(bool is_child
) {
258 VLOG(1) << "Setting user is child to " << is_child
;
259 is_child_
= is_child
;
262 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName
) {
263 set_display_email(std::string());
266 GuestUser::~GuestUser() {
269 UserType
GuestUser::GetType() const {
270 return user_manager::USER_TYPE_GUEST
;
273 KioskAppUser::KioskAppUser(const std::string
& kiosk_app_username
)
274 : User(kiosk_app_username
) {
275 set_display_email(kiosk_app_username
);
278 KioskAppUser::~KioskAppUser() {
281 UserType
KioskAppUser::GetType() const {
282 return user_manager::USER_TYPE_KIOSK_APP
;
285 SupervisedUser::SupervisedUser(const std::string
& username
) : User(username
) {
289 SupervisedUser::~SupervisedUser() {
292 UserType
SupervisedUser::GetType() const {
293 return user_manager::USER_TYPE_SUPERVISED
;
296 std::string
SupervisedUser::display_email() const {
297 return base::UTF16ToUTF8(display_name());
300 PublicAccountUser::PublicAccountUser(const std::string
& email
) : User(email
) {
303 PublicAccountUser::~PublicAccountUser() {
306 UserType
PublicAccountUser::GetType() const {
307 return user_manager::USER_TYPE_PUBLIC_ACCOUNT
;
310 bool User::has_gaia_account() const {
311 static_assert(user_manager::NUM_USER_TYPES
== 7,
312 "NUM_USER_TYPES should equal 7");
314 case user_manager::USER_TYPE_REGULAR
:
315 case user_manager::USER_TYPE_CHILD
:
317 case user_manager::USER_TYPE_GUEST
:
318 case user_manager::USER_TYPE_PUBLIC_ACCOUNT
:
319 case user_manager::USER_TYPE_SUPERVISED
:
320 case user_manager::USER_TYPE_KIOSK_APP
:
328 } // namespace user_manager