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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/strings/string16.h"
     13 #include "chrome/browser/chromeos/login/user_image.h"
     14 #include "third_party/skia/include/core/SkBitmap.h"
     15 #include "ui/gfx/image/image_skia.h"
     16 
     17 namespace chromeos {
     18 
     19 extern const int kDefaultImagesCount;
     20 
     21 // User context data that is being exchanged between part of ChromeOS
     22 // authentication mechanism. Includes credentials:
     23 // |username|, |password|, |auth_code| and |username_hash| which is returned
     24 // back once user homedir is mounted. |username_hash| is used to identify
     25 // user homedir mount point.
     26 struct UserContext {
     27   UserContext();
     28   UserContext(const std::string& username,
     29               const std::string& password,
     30               const std::string& auth_code);
     31   UserContext(const std::string& username,
     32               const std::string& password,
     33               const std::string& auth_code,
     34               const std::string& username_hash);
     35   UserContext(const std::string& username,
     36               const std::string& password,
     37               const std::string& auth_code,
     38               const std::string& username_hash,
     39               bool using_oauth);
     40   virtual ~UserContext();
     41   bool operator==(const UserContext& context) const;
     42   std::string username;
     43   std::string password;
     44   std::string auth_code;
     45   std::string username_hash;
     46   bool using_oauth;
     47 };
     48 
     49 // A class representing information about a previously logged in user.
     50 // Each user has a canonical email (username), returned by |email()| and
     51 // may have a different displayed email (in the raw form as entered by user),
     52 // returned by |displayed_email()|.
     53 // Displayed emails are for use in UI only, anywhere else users must be referred
     54 // to by |email()|.
     55 class User {
     56  public:
     57   // The user type. Used in a histogram; do not modify existing types.
     58   typedef enum {
     59     // Regular user, has a user name and password.
     60     USER_TYPE_REGULAR = 0,
     61     // Guest user, logs in without authentication.
     62     USER_TYPE_GUEST = 1,
     63     // Retail mode user, logs in without authentication. This is a special user
     64     // type used in retail mode only.
     65     USER_TYPE_RETAIL_MODE = 2,
     66     // Public account user, logs in without authentication. Available only if
     67     // enabled through policy.
     68     USER_TYPE_PUBLIC_ACCOUNT = 3,
     69     // Locally managed user, logs in only with local authentication.
     70     USER_TYPE_LOCALLY_MANAGED = 4,
     71     // Kiosk app robot, logs in without authentication.
     72     USER_TYPE_KIOSK_APP = 5,
     73     // Maximum histogram value.
     74     NUM_USER_TYPES = 6
     75   } UserType;
     76 
     77   // User OAuth token status according to the last check.
     78   // Please note that enum values 1 and 2 were used for OAuth1 status and are
     79   // deprecated now.
     80   typedef enum {
     81      OAUTH_TOKEN_STATUS_UNKNOWN  = 0,
     82      OAUTH2_TOKEN_STATUS_INVALID = 3,
     83      OAUTH2_TOKEN_STATUS_VALID   = 4,
     84   } OAuthTokenStatus;
     85 
     86   // Returned as |image_index| when user-selected file or photo is used as
     87   // user image.
     88   static const int kExternalImageIndex = -1;
     89   // Returned as |image_index| when user profile image is used as user image.
     90   static const int kProfileImageIndex = -2;
     91   static const int kInvalidImageIndex = -3;
     92 
     93   enum WallpaperType {
     94     DAILY = 0,
     95     CUSTOMIZED = 1,
     96     DEFAULT = 2,
     97     UNKNOWN = 3,
     98     ONLINE = 4,
     99     WALLPAPER_TYPE_COUNT = 5
    100   };
    101 
    102   // Returns the user type.
    103   virtual UserType GetType() const = 0;
    104 
    105   // The email the user used to log in.
    106   const std::string& email() const { return email_; }
    107 
    108   // Returns the human name to display for this user.
    109   base::string16 GetDisplayName() const;
    110 
    111   // Returns given name of user, or empty string if given name is unknown.
    112   const base::string16& given_name() const { return given_name_; }
    113 
    114   // Returns the account name part of the email. Use the display form of the
    115   // email if available and use_display_name == true. Otherwise use canonical.
    116   std::string GetAccountName(bool use_display_email) const;
    117 
    118   // The image for this user.
    119   const gfx::ImageSkia& image() const { return user_image_.image(); }
    120 
    121   // Whether the user has a default image.
    122   bool HasDefaultImage() const;
    123 
    124   // True if user image can be synced.
    125   virtual bool CanSyncImage() const;
    126 
    127   int image_index() const { return image_index_; }
    128   bool has_raw_image() const { return user_image_.has_raw_image(); }
    129   // Returns raw representation of static user image.
    130   const UserImage::RawImage& raw_image() const {
    131     return user_image_.raw_image();
    132   }
    133   bool has_animated_image() const { return user_image_.has_animated_image(); }
    134   // Returns raw representation of animated user image.
    135   const UserImage::RawImage& animated_image() const {
    136     return user_image_.animated_image();
    137   }
    138 
    139   // Whether |raw_image| contains data in format that is considered safe to
    140   // decode in sensitive environment (on Login screen).
    141   bool image_is_safe_format() const { return user_image_.is_safe_format(); }
    142 
    143   // Returns the URL of user image, if there is any. Currently only the profile
    144   // image has a URL, for other images empty URL is returned.
    145   GURL image_url() const { return user_image_.url(); }
    146 
    147   // True if user image is a stub (while real image is being loaded from file).
    148   bool image_is_stub() const { return image_is_stub_; }
    149 
    150   // True if image is being loaded from file.
    151   bool image_is_loading() const { return image_is_loading_; }
    152 
    153   // OAuth token status for this user.
    154   OAuthTokenStatus oauth_token_status() const { return oauth_token_status_; }
    155 
    156   // The displayed user name.
    157   base::string16 display_name() const { return display_name_; }
    158 
    159   // The displayed (non-canonical) user email.
    160   virtual std::string display_email() const;
    161 
    162   // True if the user's session can be locked (i.e. the user has a password with
    163   // which to unlock the session).
    164   virtual bool can_lock() const;
    165 
    166   virtual std::string username_hash() const;
    167 
    168   // True if current user is logged in.
    169   virtual bool is_logged_in() const;
    170 
    171   // True if current user is active within the current session.
    172   virtual bool is_active() const;
    173 
    174  protected:
    175   friend class SupervisedUserManagerImpl;
    176   friend class UserManagerImpl;
    177   friend class UserImageManagerImpl;
    178   // For testing:
    179   friend class MockUserManager;
    180   friend class FakeUserManager;
    181 
    182   // Do not allow anyone else to create new User instances.
    183   static User* CreateRegularUser(const std::string& email);
    184   static User* CreateGuestUser();
    185   static User* CreateKioskAppUser(const std::string& kiosk_app_username);
    186   static User* CreateLocallyManagedUser(const std::string& username);
    187   static User* CreateRetailModeUser();
    188   static User* CreatePublicAccountUser(const std::string& email);
    189 
    190   explicit User(const std::string& email);
    191   virtual ~User();
    192 
    193   bool is_profile_created() const {
    194     return profile_is_created_;
    195   }
    196 
    197   const std::string* GetAccountLocale() const {
    198     return account_locale_.get();
    199   }
    200 
    201   // Setters are private so only UserManager can call them.
    202   void SetAccountLocale(const std::string& resolved_account_locale);
    203 
    204   void SetImage(const UserImage& user_image, int image_index);
    205 
    206   void SetImageURL(const GURL& image_url);
    207 
    208   // Sets a stub image until the next |SetImage| call. |image_index| may be
    209   // one of |kExternalImageIndex| or |kProfileImageIndex|.
    210   // If |is_loading| is |true|, that means user image is being loaded from file.
    211   void SetStubImage(int image_index, bool is_loading);
    212 
    213   void set_oauth_token_status(OAuthTokenStatus status) {
    214     oauth_token_status_ = status;
    215   }
    216 
    217   void set_display_name(const base::string16& display_name) {
    218     display_name_ = display_name;
    219   }
    220 
    221   void set_given_name(const base::string16& given_name) { given_name_ = given_name; }
    222 
    223   void set_display_email(const std::string& display_email) {
    224     display_email_ = display_email;
    225   }
    226 
    227   const UserImage& user_image() const { return user_image_; }
    228 
    229   void set_username_hash(const std::string& username_hash) {
    230     username_hash_ = username_hash;
    231   }
    232 
    233   void set_is_logged_in(bool is_logged_in) {
    234     is_logged_in_ = is_logged_in;
    235   }
    236 
    237   void set_is_active(bool is_active) {
    238     is_active_ = is_active;
    239   }
    240 
    241   void set_profile_is_created() {
    242     profile_is_created_ = true;
    243   }
    244 
    245   // True if user has google account (not a guest or managed user).
    246   bool has_gaia_account() const;
    247 
    248  private:
    249   std::string email_;
    250   base::string16 display_name_;
    251   base::string16 given_name_;
    252   // The displayed user email, defaults to |email_|.
    253   std::string display_email_;
    254   UserImage user_image_;
    255   OAuthTokenStatus oauth_token_status_;
    256 
    257   // This is set to chromeos locale if account data has been downloaded.
    258   // (Or failed to download, but at least one download attempt finished).
    259   // An empty string indicates error in data load, or in
    260   // translation of Account locale to chromeos locale.
    261   scoped_ptr<std::string> account_locale_;
    262 
    263   // Used to identify homedir mount point.
    264   std::string username_hash_;
    265 
    266   // Either index of a default image for the user, |kExternalImageIndex| or
    267   // |kProfileImageIndex|.
    268   int image_index_;
    269 
    270   // True if current user image is a stub set by a |SetStubImage| call.
    271   bool image_is_stub_;
    272 
    273   // True if current user image is being loaded from file.
    274   bool image_is_loading_;
    275 
    276   // True if user is currently logged in in current session.
    277   bool is_logged_in_;
    278 
    279   // True if user is currently logged in and active in current session.
    280   bool is_active_;
    281 
    282   // True if user Profile is created
    283   bool profile_is_created_;
    284 
    285   DISALLOW_COPY_AND_ASSIGN(User);
    286 };
    287 
    288 // List of known users.
    289 typedef std::vector<User*> UserList;
    290 
    291 }  // namespace chromeos
    292 
    293 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USER_H_
    294