user.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 5#ifndef COMPONENTS_USER_MANAGER_USER_H_ 6#define COMPONENTS_USER_MANAGER_USER_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/strings/string16.h" 13#include "components/user_manager/user_image/user_image.h" 14#include "components/user_manager/user_info.h" 15#include "components/user_manager/user_manager_export.h" 16#include "components/user_manager/user_type.h" 17#include "third_party/skia/include/core/SkBitmap.h" 18#include "ui/gfx/image/image_skia.h" 19 20namespace chromeos { 21class ChromeUserManager; 22class FakeLoginUtils; 23class FakeUserManager; 24class MockUserManager; 25class SupervisedUserManagerImpl; 26class UserAddingScreenTest; 27class UserImageManagerImpl; 28class UserManagerBase; 29class UserSessionManager; 30} 31 32namespace user_manager { 33 34// A class representing information about a previously logged in user. 35// Each user has a canonical email (username), returned by |email()| and 36// may have a different displayed email (in the raw form as entered by user), 37// returned by |displayed_email()|. 38// Displayed emails are for use in UI only, anywhere else users must be referred 39// to by |email()|. 40class USER_MANAGER_EXPORT User : public UserInfo { 41 public: 42 // User OAuth token status according to the last check. 43 // Please note that enum values 1 and 2 were used for OAuth1 status and are 44 // deprecated now. 45 typedef enum { 46 OAUTH_TOKEN_STATUS_UNKNOWN = 0, 47 OAUTH2_TOKEN_STATUS_INVALID = 3, 48 OAUTH2_TOKEN_STATUS_VALID = 4, 49 } OAuthTokenStatus; 50 51 // These special values are used instead of actual default image indices. 52 typedef enum { 53 USER_IMAGE_INVALID = -3, 54 55 // Returned as |image_index| when user profile image is used as user image. 56 USER_IMAGE_PROFILE = -2, 57 58 // Returned as |image_index| when user-selected file or photo is used as 59 // user image. 60 USER_IMAGE_EXTERNAL = -1, 61 } UserImageType; 62 63 enum WallpaperType { 64 /* DAILY = 0 */ // Removed. Do not re-use the id! 65 CUSTOMIZED = 1, // Selected by user. 66 DEFAULT = 2, // Default. 67 /* UNKNOWN = 3 */ // Removed. Do not re-use the id! 68 ONLINE = 4, // WallpaperInfo.file denotes an URL. 69 POLICY = 5, // Controlled by policy, can't be changed by the user. 70 WALLPAPER_TYPE_COUNT = 6 71 }; 72 73 // Returns the user type. 74 virtual UserType GetType() const = 0; 75 76 // The email the user used to log in. 77 const std::string& email() const { return email_; } 78 79 // The displayed user name. 80 base::string16 display_name() const { return display_name_; } 81 82 // UserInfo 83 virtual std::string GetEmail() const OVERRIDE; 84 virtual base::string16 GetDisplayName() const OVERRIDE; 85 virtual base::string16 GetGivenName() const OVERRIDE; 86 virtual const gfx::ImageSkia& GetImage() const OVERRIDE; 87 virtual std::string GetUserID() const OVERRIDE; 88 89 // Returns the account name part of the email. Use the display form of the 90 // email if available and use_display_name == true. Otherwise use canonical. 91 std::string GetAccountName(bool use_display_email) const; 92 93 // Whether the user has a default image. 94 bool HasDefaultImage() const; 95 96 // True if user image can be synced. 97 virtual bool CanSyncImage() const; 98 99 int image_index() const { return image_index_; } 100 bool has_raw_image() const { return user_image_.has_raw_image(); } 101 // Returns raw representation of static user image. 102 const UserImage::RawImage& raw_image() const { 103 return user_image_.raw_image(); 104 } 105 106 // Whether |raw_image| contains data in format that is considered safe to 107 // decode in sensitive environment (on Login screen). 108 bool image_is_safe_format() const { return user_image_.is_safe_format(); } 109 110 // Returns the URL of user image, if there is any. Currently only the profile 111 // image has a URL, for other images empty URL is returned. 112 GURL image_url() const { return user_image_.url(); } 113 114 // True if user image is a stub (while real image is being loaded from file). 115 bool image_is_stub() const { return image_is_stub_; } 116 117 // True if image is being loaded from file. 118 bool image_is_loading() const { return image_is_loading_; } 119 120 // The displayed (non-canonical) user email. 121 virtual std::string display_email() const; 122 123 // OAuth token status for this user. 124 OAuthTokenStatus oauth_token_status() const { return oauth_token_status_; } 125 126 // Whether online authentication against GAIA should be enforced during the 127 // user's next sign-in. 128 bool force_online_signin() const { return force_online_signin_; } 129 130 // True if the user's session can be locked (i.e. the user has a password with 131 // which to unlock the session). 132 bool can_lock() const; 133 134 // Returns empty string when home dir hasn't been mounted yet. 135 std::string username_hash() const; 136 137 // True if current user is logged in. 138 bool is_logged_in() const; 139 140 // True if current user is active within the current session. 141 bool is_active() const; 142 143 // True if the user Profile is created. 144 bool is_profile_created() const { return profile_is_created_; } 145 146 protected: 147 friend class chromeos::ChromeUserManager; 148 friend class chromeos::SupervisedUserManagerImpl; 149 friend class chromeos::UserImageManagerImpl; 150 friend class chromeos::UserManagerBase; 151 friend class chromeos::UserSessionManager; 152 153 // For testing: 154 friend class chromeos::MockUserManager; 155 friend class chromeos::FakeLoginUtils; 156 friend class chromeos::FakeUserManager; 157 friend class chromeos::UserAddingScreenTest; 158 159 // Do not allow anyone else to create new User instances. 160 static User* CreateRegularUser(const std::string& email); 161 static User* CreateGuestUser(); 162 static User* CreateKioskAppUser(const std::string& kiosk_app_username); 163 static User* CreateSupervisedUser(const std::string& username); 164 static User* CreateRetailModeUser(); 165 static User* CreatePublicAccountUser(const std::string& email); 166 167 explicit User(const std::string& email); 168 virtual ~User(); 169 170 const std::string* GetAccountLocale() const { return account_locale_.get(); } 171 172 // Setters are private so only UserManager can call them. 173 void SetAccountLocale(const std::string& resolved_account_locale); 174 175 void SetImage(const UserImage& user_image, int image_index); 176 177 void SetImageURL(const GURL& image_url); 178 179 // Sets a stub image until the next |SetImage| call. |image_index| may be 180 // one of |USER_IMAGE_EXTERNAL| or |USER_IMAGE_PROFILE|. 181 // If |is_loading| is |true|, that means user image is being loaded from file. 182 void SetStubImage(const UserImage& stub_user_image, 183 int image_index, 184 bool is_loading); 185 186 void set_display_name(const base::string16& display_name) { 187 display_name_ = display_name; 188 } 189 190 void set_given_name(const base::string16& given_name) { 191 given_name_ = given_name; 192 } 193 194 void set_display_email(const std::string& display_email) { 195 display_email_ = display_email; 196 } 197 198 const UserImage& user_image() const { return user_image_; } 199 200 void set_oauth_token_status(OAuthTokenStatus status) { 201 oauth_token_status_ = status; 202 } 203 204 void set_force_online_signin(bool force_online_signin) { 205 force_online_signin_ = force_online_signin; 206 } 207 208 void set_username_hash(const std::string& username_hash) { 209 username_hash_ = username_hash; 210 } 211 212 void set_is_logged_in(bool is_logged_in) { is_logged_in_ = is_logged_in; } 213 214 void set_can_lock(bool can_lock) { can_lock_ = can_lock; } 215 216 void set_is_active(bool is_active) { is_active_ = is_active; } 217 218 void set_profile_is_created() { profile_is_created_ = true; } 219 220 // True if user has google account (not a guest or managed user). 221 bool has_gaia_account() const; 222 223 private: 224 std::string email_; 225 base::string16 display_name_; 226 base::string16 given_name_; 227 // The displayed user email, defaults to |email_|. 228 std::string display_email_; 229 UserImage user_image_; 230 OAuthTokenStatus oauth_token_status_; 231 bool force_online_signin_; 232 233 // This is set to chromeos locale if account data has been downloaded. 234 // (Or failed to download, but at least one download attempt finished). 235 // An empty string indicates error in data load, or in 236 // translation of Account locale to chromeos locale. 237 scoped_ptr<std::string> account_locale_; 238 239 // Used to identify homedir mount point. 240 std::string username_hash_; 241 242 // Either index of a default image for the user, |USER_IMAGE_EXTERNAL| or 243 // |USER_IMAGE_PROFILE|. 244 int image_index_; 245 246 // True if current user image is a stub set by a |SetStubImage| call. 247 bool image_is_stub_; 248 249 // True if current user image is being loaded from file. 250 bool image_is_loading_; 251 252 // True if user is able to lock screen. 253 bool can_lock_; 254 255 // True if user is currently logged in in current session. 256 bool is_logged_in_; 257 258 // True if user is currently logged in and active in current session. 259 bool is_active_; 260 261 // True if user Profile is created 262 bool profile_is_created_; 263 264 DISALLOW_COPY_AND_ASSIGN(User); 265}; 266 267// List of known users. 268typedef std::vector<User*> UserList; 269 270} // namespace user_manager 271 272#endif // COMPONENTS_USER_MANAGER_USER_H_ 273