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