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   virtual ~UserContext();
     36   bool operator==(const UserContext& context) const;
     37   std::string username;
     38   std::string password;
     39   std::string auth_code;
     40   std::string username_hash;
     41 };
     42 
     43 // A class representing information about a previously logged in user.
     44 // Each user has a canonical email (username), returned by |email()| and
     45 // may have a different displayed email (in the raw form as entered by user),
     46 // returned by |displayed_email()|.
     47 // Displayed emails are for use in UI only, anywhere else users must be referred
     48 // to by |email()|.
     49 class User {
     50  public:
     51   // The user type. Used in a histogram; do not modify existing types.
     52   typedef enum {
     53     // Regular user, has a user name and password.
     54     USER_TYPE_REGULAR = 0,
     55     // Guest user, logs in without authentication.
     56     USER_TYPE_GUEST = 1,
     57     // Retail mode user, logs in without authentication. This is a special user
     58     // type used in retail mode only.
     59     USER_TYPE_RETAIL_MODE = 2,
     60     // Public account user, logs in without authentication. Available only if
     61     // enabled through policy.
     62     USER_TYPE_PUBLIC_ACCOUNT = 3,
     63     // Locally managed user, logs in only with local authentication.
     64     USER_TYPE_LOCALLY_MANAGED = 4,
     65     // Kiosk app robot, logs in without authentication.
     66     USER_TYPE_KIOSK_APP = 5,
     67     // Maximum histogram value.
     68     NUM_USER_TYPES = 6
     69   } UserType;
     70 
     71   // User OAuth token status according to the last check.
     72   // Please note that enum values 1 and 2 were used for OAuth1 status and are
     73   // deprecated now.
     74   typedef enum {
     75      OAUTH_TOKEN_STATUS_UNKNOWN  = 0,
     76      OAUTH2_TOKEN_STATUS_INVALID = 3,
     77      OAUTH2_TOKEN_STATUS_VALID   = 4,
     78   } OAuthTokenStatus;
     79 
     80   // Returned as |image_index| when user-selected file or photo is used as
     81   // user image.
     82   static const int kExternalImageIndex = -1;
     83   // Returned as |image_index| when user profile image is used as user image.
     84   static const int kProfileImageIndex = -2;
     85   static const int kInvalidImageIndex = -3;
     86 
     87   enum WallpaperType {
     88     DAILY = 0,
     89     CUSTOMIZED = 1,
     90     DEFAULT = 2,
     91     UNKNOWN = 3,
     92     ONLINE = 4,
     93     WALLPAPER_TYPE_COUNT = 5
     94   };
     95 
     96   // Returns the user type.
     97   virtual UserType GetType() const = 0;
     98 
     99   // The email the user used to log in.
    100   const std::string& email() const { return email_; }
    101 
    102   // Returns the human name to display for this user.
    103   string16 GetDisplayName() const;
    104 
    105   // Returns the account name part of the email. Use the display form of the
    106   // email if available and use_display_name == true. Otherwise use canonical.
    107   std::string GetAccountName(bool use_display_email) const;
    108 
    109   // The image for this user.
    110   const gfx::ImageSkia& image() const { return user_image_.image(); }
    111 
    112   // Whether the user has a default image.
    113   bool HasDefaultImage() const;
    114 
    115   int image_index() const { return image_index_; }
    116   bool has_raw_image() const { return user_image_.has_raw_image(); }
    117   // Returns raw representation of static user image.
    118   const UserImage::RawImage& raw_image() const {
    119     return user_image_.raw_image();
    120   }
    121   bool has_animated_image() const { return user_image_.has_animated_image(); }
    122   // Returns raw representation of animated user image.
    123   const UserImage::RawImage& animated_image() const {
    124     return user_image_.animated_image();
    125   }
    126 
    127   // Whether |raw_image| contains data in format that is considered safe to
    128   // decode in sensitive environment (on Login screen).
    129   bool image_is_safe_format() const { return user_image_.is_safe_format(); }
    130 
    131   // Returns the URL of user image, if there is any. Currently only the profile
    132   // image has a URL, for other images empty URL is returned.
    133   GURL image_url() const { return user_image_.url(); }
    134 
    135   // True if user image is a stub (while real image is being loaded from file).
    136   bool image_is_stub() const { return image_is_stub_; }
    137 
    138   // True if image is being loaded from file.
    139   bool image_is_loading() const { return image_is_loading_; }
    140 
    141   // OAuth token status for this user.
    142   OAuthTokenStatus oauth_token_status() const { return oauth_token_status_; }
    143 
    144   // The displayed user name.
    145   string16 display_name() const { return display_name_; }
    146 
    147   // The displayed (non-canonical) user email.
    148   virtual std::string display_email() const;
    149 
    150   // True if the user's session can be locked (i.e. the user has a password with
    151   // which to unlock the session).
    152   virtual bool can_lock() const;
    153 
    154   virtual std::string username_hash() const;
    155 
    156   // True if current user is logged in.
    157   virtual bool is_logged_in() const;
    158 
    159   // True if current user is active within the current session.
    160   virtual bool is_active() const;
    161 
    162  protected:
    163   friend class UserManagerImpl;
    164   friend class UserImageManagerImpl;
    165   // For testing:
    166   friend class MockUserManager;
    167 
    168   // Do not allow anyone else to create new User instances.
    169   static User* CreateRegularUser(const std::string& email);
    170   static User* CreateGuestUser();
    171   static User* CreateKioskAppUser(const std::string& kiosk_app_username);
    172   static User* CreateLocallyManagedUser(const std::string& username);
    173   static User* CreateRetailModeUser();
    174   static User* CreatePublicAccountUser(const std::string& email);
    175 
    176   explicit User(const std::string& email);
    177   virtual ~User();
    178 
    179   // Setters are private so only UserManager can call them.
    180   void SetImage(const UserImage& user_image, int image_index);
    181 
    182   void SetImageURL(const GURL& image_url);
    183 
    184   // Sets a stub image until the next |SetImage| call. |image_index| may be
    185   // one of |kExternalImageIndex| or |kProfileImageIndex|.
    186   // If |is_loading| is |true|, that means user image is being loaded from file.
    187   void SetStubImage(int image_index, bool is_loading);
    188 
    189   void set_oauth_token_status(OAuthTokenStatus status) {
    190     oauth_token_status_ = status;
    191   }
    192 
    193   void set_display_name(const string16& display_name) {
    194     display_name_ = display_name;
    195   }
    196 
    197   void set_display_email(const std::string& display_email) {
    198     display_email_ = display_email;
    199   }
    200 
    201   const UserImage& user_image() const { return user_image_; }
    202 
    203   void set_username_hash(const std::string& username_hash) {
    204     username_hash_ = username_hash;
    205   }
    206 
    207   void set_is_logged_in(bool is_logged_in) {
    208     is_logged_in_ = is_logged_in;
    209   }
    210 
    211   void set_is_active(bool is_active) {
    212     is_active_ = is_active;
    213   }
    214 
    215  private:
    216   std::string email_;
    217   string16 display_name_;
    218   // The displayed user email, defaults to |email_|.
    219   std::string display_email_;
    220   UserImage user_image_;
    221   OAuthTokenStatus oauth_token_status_;
    222 
    223   // Used to identify homedir mount point.
    224   std::string username_hash_;
    225 
    226   // Either index of a default image for the user, |kExternalImageIndex| or
    227   // |kProfileImageIndex|.
    228   int image_index_;
    229 
    230   // True if current user image is a stub set by a |SetStubImage| call.
    231   bool image_is_stub_;
    232 
    233   // True if current user image is being loaded from file.
    234   bool image_is_loading_;
    235 
    236   // True if user is currently logged in in current session.
    237   bool is_logged_in_;
    238 
    239   // True if user is currently logged in and active in current session.
    240   bool is_active_;
    241 
    242   DISALLOW_COPY_AND_ASSIGN(User);
    243 };
    244 
    245 // List of known users.
    246 typedef std::vector<User*> UserList;
    247 
    248 }  // namespace chromeos
    249 
    250 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USER_H_
    251