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