1// Copyright (c) 2012 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#include "chrome/browser/chromeos/login/user.h" 6 7#include "base/logging.h" 8#include "base/strings/stringprintf.h" 9#include "base/strings/utf_string_conversions.h" 10#include "chrome/browser/chromeos/login/default_user_images.h" 11#include "chrome/browser/chromeos/login/user_manager.h" 12#include "grit/theme_resources.h" 13#include "ui/base/resource/resource_bundle.h" 14 15namespace chromeos { 16 17namespace { 18 19// Returns account name portion of an email. 20std::string GetUserName(const std::string& email) { 21 std::string::size_type i = email.find('@'); 22 if (i == 0 || i == std::string::npos) { 23 return email; 24 } 25 return email.substr(0, i); 26} 27 28} // namespace 29 30class RegularUser : public User { 31 public: 32 explicit RegularUser(const std::string& email); 33 virtual ~RegularUser(); 34 35 // Overridden from User: 36 virtual UserType GetType() const OVERRIDE; 37 virtual bool can_lock() const OVERRIDE; 38 39 private: 40 DISALLOW_COPY_AND_ASSIGN(RegularUser); 41}; 42 43class GuestUser : public User { 44 public: 45 GuestUser(); 46 virtual ~GuestUser(); 47 48 // Overridden from User: 49 virtual UserType GetType() const OVERRIDE; 50 51 private: 52 DISALLOW_COPY_AND_ASSIGN(GuestUser); 53}; 54 55class KioskAppUser : public User { 56 public: 57 explicit KioskAppUser(const std::string& app_id); 58 virtual ~KioskAppUser(); 59 60 // Overridden from User: 61 virtual UserType GetType() const OVERRIDE; 62 63 private: 64 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); 65}; 66 67class LocallyManagedUser : public User { 68 public: 69 explicit LocallyManagedUser(const std::string& username); 70 virtual ~LocallyManagedUser(); 71 72 // Overridden from User: 73 virtual UserType GetType() const OVERRIDE; 74 virtual bool can_lock() const OVERRIDE; 75 virtual std::string display_email() const OVERRIDE; 76 77 private: 78 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser); 79}; 80 81class RetailModeUser : public User { 82 public: 83 RetailModeUser(); 84 virtual ~RetailModeUser(); 85 86 // Overridden from User: 87 virtual UserType GetType() const OVERRIDE; 88 89 private: 90 DISALLOW_COPY_AND_ASSIGN(RetailModeUser); 91}; 92 93class PublicAccountUser : public User { 94 public: 95 explicit PublicAccountUser(const std::string& email); 96 virtual ~PublicAccountUser(); 97 98 // Overridden from User: 99 virtual UserType GetType() const OVERRIDE; 100 101 private: 102 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); 103}; 104 105UserContext::UserContext() { 106} 107 108UserContext::UserContext(const std::string& username, 109 const std::string& password, 110 const std::string& auth_code) 111 : username(username), 112 password(password), 113 auth_code(auth_code) { 114} 115 116UserContext::UserContext(const std::string& username, 117 const std::string& password, 118 const std::string& auth_code, 119 const std::string& username_hash) 120 : username(username), 121 password(password), 122 auth_code(auth_code), 123 username_hash(username_hash) { 124} 125 126UserContext::~UserContext() { 127} 128 129bool UserContext::operator==(const UserContext& context) const { 130 return context.username == username && 131 context.password == password && 132 context.auth_code == auth_code && 133 context.username_hash == username_hash; 134} 135 136string16 User::GetDisplayName() const { 137 // Fallback to the email account name in case display name haven't been set. 138 return display_name_.empty() ? 139 UTF8ToUTF16(GetAccountName(true)) : 140 display_name_; 141} 142 143std::string User::GetAccountName(bool use_display_email) const { 144 if (use_display_email && !display_email_.empty()) 145 return GetUserName(display_email_); 146 else 147 return GetUserName(email_); 148} 149 150bool User::HasDefaultImage() const { 151 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; 152} 153 154std::string User::display_email() const { 155 return display_email_; 156} 157 158bool User::can_lock() const { 159 return false; 160} 161 162std::string User::username_hash() const { 163 return username_hash_; 164} 165 166bool User::is_logged_in() const { 167 return is_logged_in_; 168} 169 170bool User::is_active() const { 171 return is_active_; 172} 173 174User* User::CreateRegularUser(const std::string& email) { 175 return new RegularUser(email); 176} 177 178User* User::CreateGuestUser() { 179 return new GuestUser; 180} 181 182User* User::CreateKioskAppUser(const std::string& kiosk_app_username) { 183 return new KioskAppUser(kiosk_app_username); 184} 185 186User* User::CreateLocallyManagedUser(const std::string& username) { 187 return new LocallyManagedUser(username); 188} 189 190User* User::CreateRetailModeUser() { 191 return new RetailModeUser; 192} 193 194User* User::CreatePublicAccountUser(const std::string& email) { 195 return new PublicAccountUser(email); 196} 197 198User::User(const std::string& email) 199 : email_(email), 200 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), 201 image_index_(kInvalidImageIndex), 202 image_is_stub_(false), 203 image_is_loading_(false), 204 is_logged_in_(false), 205 is_active_(false) { 206} 207 208User::~User() {} 209 210void User::SetImage(const UserImage& user_image, int image_index) { 211 user_image_ = user_image; 212 image_index_ = image_index; 213 image_is_stub_ = false; 214 image_is_loading_ = false; 215 DCHECK(HasDefaultImage() || user_image.has_raw_image()); 216} 217 218void User::SetImageURL(const GURL& image_url) { 219 user_image_.set_url(image_url); 220} 221 222void User::SetStubImage(int image_index, bool is_loading) { 223 user_image_ = UserImage( 224 *ResourceBundle::GetSharedInstance(). 225 GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING)); 226 image_index_ = image_index; 227 image_is_stub_ = true; 228 image_is_loading_ = is_loading; 229} 230 231RegularUser::RegularUser(const std::string& email) : User(email) { 232 set_display_email(email); 233} 234 235RegularUser::~RegularUser() {} 236 237User::UserType RegularUser::GetType() const { 238 return USER_TYPE_REGULAR; 239} 240 241bool RegularUser::can_lock() const { 242 return true; 243} 244 245GuestUser::GuestUser() : User(UserManager::kGuestUserName) { 246 set_display_email(std::string()); 247} 248 249GuestUser::~GuestUser() {} 250 251User::UserType GuestUser::GetType() const { 252 return USER_TYPE_GUEST; 253} 254 255KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) 256 : User(kiosk_app_username) { 257 set_display_email(std::string()); 258} 259 260KioskAppUser::~KioskAppUser() {} 261 262User::UserType KioskAppUser::GetType() const { 263 return USER_TYPE_KIOSK_APP; 264} 265 266LocallyManagedUser::LocallyManagedUser(const std::string& username) 267 : User(username) { 268} 269 270LocallyManagedUser::~LocallyManagedUser() {} 271 272User::UserType LocallyManagedUser::GetType() const { 273 return USER_TYPE_LOCALLY_MANAGED; 274} 275 276bool LocallyManagedUser::can_lock() const { 277 return true; 278} 279 280std::string LocallyManagedUser::display_email() const { 281 return UTF16ToUTF8(display_name()); 282} 283 284RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) { 285 set_display_email(std::string()); 286} 287 288RetailModeUser::~RetailModeUser() {} 289 290User::UserType RetailModeUser::GetType() const { 291 return USER_TYPE_RETAIL_MODE; 292} 293 294PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { 295} 296 297PublicAccountUser::~PublicAccountUser() {} 298 299User::UserType PublicAccountUser::GetType() const { 300 return USER_TYPE_PUBLIC_ACCOUNT; 301} 302 303} // namespace chromeos 304