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