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