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 #ifndef COMPONENTS_USER_MANAGER_USER_H_
6 #define COMPONENTS_USER_MANAGER_USER_H_
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "components/user_manager/user_id.h"
14 #include "components/user_manager/user_image/user_image.h"
15 #include "components/user_manager/user_info.h"
16 #include "components/user_manager/user_manager_export.h"
17 #include "components/user_manager/user_type.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/image/image_skia.h"
22 class ChromeUserManagerImpl
;
23 class FakeChromeUserManager
;
24 class MockUserManager
;
25 class SupervisedUserManagerImpl
;
26 class UserAddingScreenTest
;
27 class UserImageManagerImpl
;
28 class UserSessionManager
;
31 namespace user_manager
{
33 class UserManagerBase
;
34 class FakeUserManager
;
36 // A class representing information about a previously logged in user.
37 // Each user has a canonical email (username), returned by |email()| and
38 // may have a different displayed email (in the raw form as entered by user),
39 // returned by |displayed_email()|.
40 // Displayed emails are for use in UI only, anywhere else users must be referred
42 class USER_MANAGER_EXPORT User
: public UserInfo
{
44 // User OAuth token status according to the last check.
45 // Please note that enum values 1 and 2 were used for OAuth1 status and are
48 OAUTH_TOKEN_STATUS_UNKNOWN
= 0,
49 OAUTH2_TOKEN_STATUS_INVALID
= 3,
50 OAUTH2_TOKEN_STATUS_VALID
= 4,
53 // These special values are used instead of actual default image indices.
55 USER_IMAGE_INVALID
= -3,
57 // Returned as |image_index| when user profile image is used as user image.
58 USER_IMAGE_PROFILE
= -2,
60 // Returned as |image_index| when user-selected file or photo is used as
62 USER_IMAGE_EXTERNAL
= -1,
65 // This enum is used to define the buckets for an enumerated UMA histogram.
67 // (a) existing enumerated constants should never be deleted or reordered,
68 // (b) new constants should only be appended at the end of the enumeration.
70 /* DAILY = 0 */ // Removed.
71 CUSTOMIZED
= 1, // Selected by user.
72 DEFAULT
= 2, // Default.
73 /* UNKNOWN = 3 */ // Removed.
74 ONLINE
= 4, // WallpaperInfo.location denotes an URL.
75 POLICY
= 5, // Controlled by policy, can't be changed by the user.
76 WALLPAPER_TYPE_COUNT
= 6
79 // Returns true if user type has gaia account.
80 static bool TypeHasGaiaAccount(UserType user_type
);
82 // Returns the user type.
83 virtual UserType
GetType() const = 0;
85 // The email the user used to log in.
86 const std::string
& email() const { return email_
; }
88 // The displayed user name.
89 base::string16
display_name() const { return display_name_
; }
91 // If the user has to use SAML to log in.
92 bool using_saml() const { return using_saml_
; }
95 std::string
GetEmail() const override
;
96 base::string16
GetDisplayName() const override
;
97 base::string16
GetGivenName() const override
;
98 const gfx::ImageSkia
& GetImage() const override
;
99 UserID
GetUserID() const override
;
101 // Allows managing child status of the user. Used for RegularUser.
102 virtual void SetIsChild(bool is_child
);
104 // Returns true if user has gaia account. True for users of types
105 // USER_TYPE_REGULAR and USER_TYPE_CHILD.
106 virtual bool HasGaiaAccount() const;
108 // Returns true if user is supervised.
109 virtual bool IsSupervised() const;
111 // Returns the account name part of the email. Use the display form of the
112 // email if available and use_display_name == true. Otherwise use canonical.
113 std::string
GetAccountName(bool use_display_email
) const;
115 // Whether the user has a default image.
116 bool HasDefaultImage() const;
118 // True if user image can be synced.
119 virtual bool CanSyncImage() const;
121 int image_index() const { return image_index_
; }
122 bool has_raw_image() const { return user_image_
.has_raw_image(); }
123 // Returns raw representation of static user image.
124 const UserImage::RawImage
& raw_image() const {
125 return user_image_
.raw_image();
128 // Whether |raw_image| contains data in format that is considered safe to
129 // decode in sensitive environment (on Login screen).
130 bool image_is_safe_format() const { return user_image_
.is_safe_format(); }
132 // Returns the URL of user image, if there is any. Currently only the profile
133 // image has a URL, for other images empty URL is returned.
134 GURL
image_url() const { return user_image_
.url(); }
136 // True if user image is a stub (while real image is being loaded from file).
137 bool image_is_stub() const { return image_is_stub_
; }
139 // True if image is being loaded from file.
140 bool image_is_loading() const { return image_is_loading_
; }
142 // The displayed (non-canonical) user email.
143 virtual std::string
display_email() const;
145 // OAuth token status for this user.
146 OAuthTokenStatus
oauth_token_status() const { return oauth_token_status_
; }
148 // Whether online authentication against GAIA should be enforced during the
149 // user's next sign-in.
150 bool force_online_signin() const { return force_online_signin_
; }
152 // True if the user's session can be locked (i.e. the user has a password with
153 // which to unlock the session).
154 bool can_lock() const;
156 // Returns empty string when home dir hasn't been mounted yet.
157 std::string
username_hash() const;
159 // True if current user is logged in.
160 bool is_logged_in() const;
162 // True if current user is active within the current session.
163 bool is_active() const;
165 // True if the user Profile is created.
166 bool is_profile_created() const { return profile_is_created_
; }
168 // True if the user is affiliated to the device.
169 bool is_affiliated() const { return is_affiliated_
; }
172 friend class UserManagerBase
;
173 friend class chromeos::ChromeUserManagerImpl
;
174 friend class chromeos::SupervisedUserManagerImpl
;
175 friend class chromeos::UserImageManagerImpl
;
176 friend class chromeos::UserSessionManager
;
179 friend class FakeUserManager
;
180 friend class chromeos::FakeChromeUserManager
;
181 friend class chromeos::MockUserManager
;
182 friend class chromeos::UserAddingScreenTest
;
184 // Do not allow anyone else to create new User instances.
185 static User
* CreateRegularUser(const UserID
& email
);
186 static User
* CreateGuestUser();
187 static User
* CreateKioskAppUser(const UserID
& kiosk_app_username
);
188 static User
* CreateSupervisedUser(const UserID
& username
);
189 static User
* CreatePublicAccountUser(const UserID
& email
);
191 explicit User(const std::string
& email
);
194 const std::string
* GetAccountLocale() const { return account_locale_
.get(); }
196 // Setters are private so only UserManager can call them.
197 void SetAccountLocale(const std::string
& resolved_account_locale
);
199 void SetImage(const UserImage
& user_image
, int image_index
);
201 void SetImageURL(const GURL
& image_url
);
203 // Sets a stub image until the next |SetImage| call. |image_index| may be
204 // one of |USER_IMAGE_EXTERNAL| or |USER_IMAGE_PROFILE|.
205 // If |is_loading| is |true|, that means user image is being loaded from file.
206 void SetStubImage(const UserImage
& stub_user_image
,
210 void set_display_name(const base::string16
& display_name
) {
211 display_name_
= display_name
;
214 void set_given_name(const base::string16
& given_name
) {
215 given_name_
= given_name
;
218 void set_display_email(const std::string
& display_email
) {
219 display_email_
= display_email
;
222 void set_using_saml(const bool using_saml
) { using_saml_
= using_saml
; }
224 const UserImage
& user_image() const { return user_image_
; }
226 void set_oauth_token_status(OAuthTokenStatus status
) {
227 oauth_token_status_
= status
;
230 void set_force_online_signin(bool force_online_signin
) {
231 force_online_signin_
= force_online_signin
;
234 void set_username_hash(const std::string
& username_hash
) {
235 username_hash_
= username_hash
;
238 void set_is_logged_in(bool is_logged_in
) { is_logged_in_
= is_logged_in
; }
240 void set_can_lock(bool can_lock
) { can_lock_
= can_lock
; }
242 void set_is_active(bool is_active
) { is_active_
= is_active
; }
244 void set_profile_is_created() { profile_is_created_
= true; }
246 // True if user has google account (not a guest or managed user).
247 bool has_gaia_account() const;
249 void set_affiliation(bool is_affiliated
) {
250 is_affiliated_
= is_affiliated
;
255 base::string16 display_name_
;
256 base::string16 given_name_
;
257 // The displayed user email, defaults to |email_|.
258 std::string display_email_
;
260 UserImage user_image_
;
261 OAuthTokenStatus oauth_token_status_
;
262 bool force_online_signin_
;
264 // This is set to chromeos locale if account data has been downloaded.
265 // (Or failed to download, but at least one download attempt finished).
266 // An empty string indicates error in data load, or in
267 // translation of Account locale to chromeos locale.
268 scoped_ptr
<std::string
> account_locale_
;
270 // Used to identify homedir mount point.
271 std::string username_hash_
;
273 // Either index of a default image for the user, |USER_IMAGE_EXTERNAL| or
274 // |USER_IMAGE_PROFILE|.
277 // True if current user image is a stub set by a |SetStubImage| call.
280 // True if current user image is being loaded from file.
281 bool image_is_loading_
;
283 // True if user is able to lock screen.
286 // True if user is currently logged in in current session.
289 // True if user is currently logged in and active in current session.
292 // True if user Profile is created
293 bool profile_is_created_
;
295 // True if the user is affiliated to the device.
298 DISALLOW_COPY_AND_ASSIGN(User
);
301 // List of known users.
302 typedef std::vector
<User
*> UserList
;
304 } // namespace user_manager
306 #endif // COMPONENTS_USER_MANAGER_USER_H_