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
);
31 bool User::IsSupervised() const {
35 void User::SetIsSupervised(bool is_supervised
) {
36 VLOG(1) << "Ignoring SetIsSupervised call with param " << is_supervised
;
39 class RegularUser
: public User
{
41 explicit RegularUser(const std::string
& email
);
42 virtual ~RegularUser();
44 // Overridden from User:
45 virtual UserType
GetType() const override
;
46 virtual bool CanSyncImage() const override
;
47 virtual void SetIsSupervised(bool is_supervised
) override
{
48 VLOG(1) << "Setting user is supervised to " << is_supervised
;
49 is_supervised_
= is_supervised
;
51 virtual bool IsSupervised() const override
{
52 return is_supervised_
;
58 DISALLOW_COPY_AND_ASSIGN(RegularUser
);
61 class GuestUser
: public User
{
66 // Overridden from User:
67 virtual UserType
GetType() const override
;
70 DISALLOW_COPY_AND_ASSIGN(GuestUser
);
73 class KioskAppUser
: public User
{
75 explicit KioskAppUser(const std::string
& app_id
);
76 virtual ~KioskAppUser();
78 // Overridden from User:
79 virtual UserType
GetType() const override
;
82 DISALLOW_COPY_AND_ASSIGN(KioskAppUser
);
85 class SupervisedUser
: public User
{
87 explicit SupervisedUser(const std::string
& username
);
88 virtual ~SupervisedUser();
90 // Overridden from User:
91 virtual UserType
GetType() const override
;
92 virtual bool IsSupervised() const override
;
93 virtual std::string
display_email() const override
;
96 DISALLOW_COPY_AND_ASSIGN(SupervisedUser
);
99 class RetailModeUser
: public User
{
102 virtual ~RetailModeUser();
104 // Overridden from User:
105 virtual UserType
GetType() const override
;
108 DISALLOW_COPY_AND_ASSIGN(RetailModeUser
);
111 class PublicAccountUser
: public User
{
113 explicit PublicAccountUser(const std::string
& email
);
114 virtual ~PublicAccountUser();
116 // Overridden from User:
117 virtual UserType
GetType() const override
;
120 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser
);
123 std::string
User::GetEmail() const {
124 return display_email();
127 base::string16
User::GetDisplayName() const {
128 // Fallback to the email account name in case display name haven't been set.
129 return display_name_
.empty() ? base::UTF8ToUTF16(GetAccountName(true))
133 base::string16
User::GetGivenName() const {
137 const gfx::ImageSkia
& User::GetImage() const {
138 return user_image_
.image();
141 std::string
User::GetUserID() const {
142 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
145 std::string
User::GetAccountName(bool use_display_email
) const {
146 if (use_display_email
&& !display_email_
.empty())
147 return GetUserName(display_email_
);
149 return GetUserName(email_
);
152 bool User::HasDefaultImage() const {
153 return image_index_
>= 0 && image_index_
< kDefaultImagesCount
;
156 bool User::CanSyncImage() const {
160 std::string
User::display_email() const {
161 return display_email_
;
164 bool User::can_lock() const {
168 std::string
User::username_hash() const {
169 return username_hash_
;
172 bool User::is_logged_in() const {
173 return is_logged_in_
;
176 bool User::is_active() const {
180 User
* User::CreateRegularUser(const std::string
& email
) {
181 return new RegularUser(email
);
184 User
* User::CreateGuestUser() {
185 return new GuestUser
;
188 User
* User::CreateKioskAppUser(const std::string
& kiosk_app_username
) {
189 return new KioskAppUser(kiosk_app_username
);
192 User
* User::CreateSupervisedUser(const std::string
& username
) {
193 return new SupervisedUser(username
);
196 User
* User::CreateRetailModeUser() {
197 return new RetailModeUser
;
200 User
* User::CreatePublicAccountUser(const std::string
& email
) {
201 return new PublicAccountUser(email
);
204 User::User(const std::string
& email
)
206 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN
),
207 force_online_signin_(false),
208 image_index_(USER_IMAGE_INVALID
),
209 image_is_stub_(false),
210 image_is_loading_(false),
212 is_logged_in_(false),
214 profile_is_created_(false) {
220 void User::SetAccountLocale(const std::string
& resolved_account_locale
) {
221 account_locale_
.reset(new std::string(resolved_account_locale
));
224 void User::SetImage(const UserImage
& user_image
, int image_index
) {
225 user_image_
= user_image
;
226 image_index_
= image_index
;
227 image_is_stub_
= false;
228 image_is_loading_
= false;
229 DCHECK(HasDefaultImage() || user_image
.has_raw_image());
232 void User::SetImageURL(const GURL
& image_url
) {
233 user_image_
.set_url(image_url
);
236 void User::SetStubImage(const UserImage
& stub_user_image
,
239 user_image_
= stub_user_image
;
240 image_index_
= image_index
;
241 image_is_stub_
= true;
242 image_is_loading_
= is_loading
;
245 RegularUser::RegularUser(const std::string
& email
)
246 : User(email
), is_supervised_(false) {
248 set_display_email(email
);
251 RegularUser::~RegularUser() {
254 UserType
RegularUser::GetType() const {
255 return user_manager::USER_TYPE_REGULAR
;
258 bool RegularUser::CanSyncImage() const {
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 bool SupervisedUser::IsSupervised() const {
304 RetailModeUser::RetailModeUser() : User(chromeos::login::kRetailModeUserName
) {
305 set_display_email(std::string());
308 RetailModeUser::~RetailModeUser() {
311 UserType
RetailModeUser::GetType() const {
312 return user_manager::USER_TYPE_RETAIL_MODE
;
315 PublicAccountUser::PublicAccountUser(const std::string
& email
) : User(email
) {
318 PublicAccountUser::~PublicAccountUser() {
321 UserType
PublicAccountUser::GetType() const {
322 return user_manager::USER_TYPE_PUBLIC_ACCOUNT
;
325 bool User::has_gaia_account() const {
326 COMPILE_ASSERT(user_manager::NUM_USER_TYPES
== 6, num_user_types_unexpected
);
328 case user_manager::USER_TYPE_REGULAR
:
330 case user_manager::USER_TYPE_GUEST
:
331 case user_manager::USER_TYPE_RETAIL_MODE
:
332 case user_manager::USER_TYPE_PUBLIC_ACCOUNT
:
333 case user_manager::USER_TYPE_SUPERVISED
:
334 case user_manager::USER_TYPE_KIOSK_APP
:
342 } // namespace user_manager