| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/users/user.h" | 5 #include "components/user_manager/user.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "chrome/browser/chromeos/login/users/user_manager.h" | |
| 12 #include "chromeos/login/user_names.h" | 11 #include "chromeos/login/user_names.h" |
| 13 #include "components/user_manager/user_image/default_user_images.h" | 12 #include "components/user_manager/user_image/default_user_images.h" |
| 14 #include "google_apis/gaia/gaia_auth_util.h" | 13 #include "google_apis/gaia/gaia_auth_util.h" |
| 15 #include "grit/theme_resources.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 17 | 15 |
| 18 namespace chromeos { | 16 namespace user_manager { |
| 19 | 17 |
| 20 namespace { | 18 namespace { |
| 21 | 19 |
| 22 // Returns account name portion of an email. | 20 // Returns account name portion of an email. |
| 23 std::string GetUserName(const std::string& email) { | 21 std::string GetUserName(const std::string& email) { |
| 24 std::string::size_type i = email.find('@'); | 22 std::string::size_type i = email.find('@'); |
| 25 if (i == 0 || i == std::string::npos) { | 23 if (i == 0 || i == std::string::npos) { |
| 26 return email; | 24 return email; |
| 27 } | 25 } |
| 28 return email.substr(0, i); | 26 return email.substr(0, i); |
| 29 } | 27 } |
| 30 | 28 |
| 31 } // namespace | 29 } // namespace |
| 32 | 30 |
| 33 const int User::kExternalImageIndex; | |
| 34 const int User::kProfileImageIndex; | |
| 35 const int User::kInvalidImageIndex; | |
| 36 | |
| 37 class RegularUser : public User { | 31 class RegularUser : public User { |
| 38 public: | 32 public: |
| 39 explicit RegularUser(const std::string& email); | 33 explicit RegularUser(const std::string& email); |
| 40 virtual ~RegularUser(); | 34 virtual ~RegularUser(); |
| 41 | 35 |
| 42 // Overridden from User: | 36 // Overridden from User: |
| 43 virtual user_manager::UserType GetType() const OVERRIDE; | 37 virtual UserType GetType() const OVERRIDE; |
| 44 virtual bool CanSyncImage() const OVERRIDE; | 38 virtual bool CanSyncImage() const OVERRIDE; |
| 45 | 39 |
| 46 private: | 40 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(RegularUser); | 41 DISALLOW_COPY_AND_ASSIGN(RegularUser); |
| 48 }; | 42 }; |
| 49 | 43 |
| 50 class GuestUser : public User { | 44 class GuestUser : public User { |
| 51 public: | 45 public: |
| 52 GuestUser(); | 46 GuestUser(); |
| 53 virtual ~GuestUser(); | 47 virtual ~GuestUser(); |
| 54 | 48 |
| 55 // Overridden from User: | 49 // Overridden from User: |
| 56 virtual user_manager::UserType GetType() const OVERRIDE; | 50 virtual UserType GetType() const OVERRIDE; |
| 57 | 51 |
| 58 private: | 52 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(GuestUser); | 53 DISALLOW_COPY_AND_ASSIGN(GuestUser); |
| 60 }; | 54 }; |
| 61 | 55 |
| 62 class KioskAppUser : public User { | 56 class KioskAppUser : public User { |
| 63 public: | 57 public: |
| 64 explicit KioskAppUser(const std::string& app_id); | 58 explicit KioskAppUser(const std::string& app_id); |
| 65 virtual ~KioskAppUser(); | 59 virtual ~KioskAppUser(); |
| 66 | 60 |
| 67 // Overridden from User: | 61 // Overridden from User: |
| 68 virtual user_manager::UserType GetType() const OVERRIDE; | 62 virtual UserType GetType() const OVERRIDE; |
| 69 | 63 |
| 70 private: | 64 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); | 65 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); |
| 72 }; | 66 }; |
| 73 | 67 |
| 74 class SupervisedUser : public User { | 68 class SupervisedUser : public User { |
| 75 public: | 69 public: |
| 76 explicit SupervisedUser(const std::string& username); | 70 explicit SupervisedUser(const std::string& username); |
| 77 virtual ~SupervisedUser(); | 71 virtual ~SupervisedUser(); |
| 78 | 72 |
| 79 // Overridden from User: | 73 // Overridden from User: |
| 80 virtual user_manager::UserType GetType() const OVERRIDE; | 74 virtual UserType GetType() const OVERRIDE; |
| 81 virtual std::string display_email() const OVERRIDE; | 75 virtual std::string display_email() const OVERRIDE; |
| 82 | 76 |
| 83 private: | 77 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); | 78 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); |
| 85 }; | 79 }; |
| 86 | 80 |
| 87 class RetailModeUser : public User { | 81 class RetailModeUser : public User { |
| 88 public: | 82 public: |
| 89 RetailModeUser(); | 83 RetailModeUser(); |
| 90 virtual ~RetailModeUser(); | 84 virtual ~RetailModeUser(); |
| 91 | 85 |
| 92 // Overridden from User: | 86 // Overridden from User: |
| 93 virtual user_manager::UserType GetType() const OVERRIDE; | 87 virtual UserType GetType() const OVERRIDE; |
| 94 | 88 |
| 95 private: | 89 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(RetailModeUser); | 90 DISALLOW_COPY_AND_ASSIGN(RetailModeUser); |
| 97 }; | 91 }; |
| 98 | 92 |
| 99 class PublicAccountUser : public User { | 93 class PublicAccountUser : public User { |
| 100 public: | 94 public: |
| 101 explicit PublicAccountUser(const std::string& email); | 95 explicit PublicAccountUser(const std::string& email); |
| 102 virtual ~PublicAccountUser(); | 96 virtual ~PublicAccountUser(); |
| 103 | 97 |
| 104 // Overridden from User: | 98 // Overridden from User: |
| 105 virtual user_manager::UserType GetType() const OVERRIDE; | 99 virtual UserType GetType() const OVERRIDE; |
| 106 | 100 |
| 107 private: | 101 private: |
| 108 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); | 102 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); |
| 109 }; | 103 }; |
| 110 | 104 |
| 111 std::string User::GetEmail() const { | 105 std::string User::GetEmail() const { |
| 112 return display_email(); | 106 return display_email(); |
| 113 } | 107 } |
| 114 | 108 |
| 115 base::string16 User::GetDisplayName() const { | 109 base::string16 User::GetDisplayName() const { |
| 116 // Fallback to the email account name in case display name haven't been set. | 110 // Fallback to the email account name in case display name haven't been set. |
| 117 return display_name_.empty() ? | 111 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true)) |
| 118 base::UTF8ToUTF16(GetAccountName(true)) : | 112 : display_name_; |
| 119 display_name_; | |
| 120 } | 113 } |
| 121 | 114 |
| 122 base::string16 User::GetGivenName() const { | 115 base::string16 User::GetGivenName() const { |
| 123 return given_name_; | 116 return given_name_; |
| 124 } | 117 } |
| 125 | 118 |
| 126 const gfx::ImageSkia& User::GetImage() const { | 119 const gfx::ImageSkia& User::GetImage() const { |
| 127 return user_image_.image(); | 120 return user_image_.image(); |
| 128 } | 121 } |
| 129 | 122 |
| 130 std::string User::GetUserID() const { | 123 std::string User::GetUserID() const { |
| 131 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); | 124 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); |
| 132 } | 125 } |
| 133 | 126 |
| 134 std::string User::GetAccountName(bool use_display_email) const { | 127 std::string User::GetAccountName(bool use_display_email) const { |
| 135 if (use_display_email && !display_email_.empty()) | 128 if (use_display_email && !display_email_.empty()) |
| 136 return GetUserName(display_email_); | 129 return GetUserName(display_email_); |
| 137 else | 130 else |
| 138 return GetUserName(email_); | 131 return GetUserName(email_); |
| 139 } | 132 } |
| 140 | 133 |
| 141 bool User::HasDefaultImage() const { | 134 bool User::HasDefaultImage() const { |
| 142 return image_index_ >= 0 && image_index_ < user_manager::kDefaultImagesCount; | 135 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; |
| 143 } | 136 } |
| 144 | 137 |
| 145 bool User::CanSyncImage() const { | 138 bool User::CanSyncImage() const { |
| 146 return false; | 139 return false; |
| 147 } | 140 } |
| 148 | 141 |
| 149 std::string User::display_email() const { | 142 std::string User::display_email() const { |
| 150 return display_email_; | 143 return display_email_; |
| 151 } | 144 } |
| 152 | 145 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 180 } |
| 188 | 181 |
| 189 User* User::CreatePublicAccountUser(const std::string& email) { | 182 User* User::CreatePublicAccountUser(const std::string& email) { |
| 190 return new PublicAccountUser(email); | 183 return new PublicAccountUser(email); |
| 191 } | 184 } |
| 192 | 185 |
| 193 User::User(const std::string& email) | 186 User::User(const std::string& email) |
| 194 : email_(email), | 187 : email_(email), |
| 195 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), | 188 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), |
| 196 force_online_signin_(false), | 189 force_online_signin_(false), |
| 197 image_index_(kInvalidImageIndex), | 190 image_index_(USER_IMAGE_INVALID), |
| 198 image_is_stub_(false), | 191 image_is_stub_(false), |
| 199 image_is_loading_(false), | 192 image_is_loading_(false), |
| 200 can_lock_(false), | 193 can_lock_(false), |
| 201 is_logged_in_(false), | 194 is_logged_in_(false), |
| 202 is_active_(false), | 195 is_active_(false), |
| 203 profile_is_created_(false) { | 196 profile_is_created_(false) { |
| 204 } | 197 } |
| 205 | 198 |
| 206 User::~User() {} | 199 User::~User() { |
| 200 } |
| 207 | 201 |
| 208 void User::SetAccountLocale(const std::string& resolved_account_locale) { | 202 void User::SetAccountLocale(const std::string& resolved_account_locale) { |
| 209 account_locale_.reset(new std::string(resolved_account_locale)); | 203 account_locale_.reset(new std::string(resolved_account_locale)); |
| 210 } | 204 } |
| 211 | 205 |
| 212 void User::SetImage(const user_manager::UserImage& user_image, | 206 void User::SetImage(const UserImage& user_image, int image_index) { |
| 213 int image_index) { | |
| 214 user_image_ = user_image; | 207 user_image_ = user_image; |
| 215 image_index_ = image_index; | 208 image_index_ = image_index; |
| 216 image_is_stub_ = false; | 209 image_is_stub_ = false; |
| 217 image_is_loading_ = false; | 210 image_is_loading_ = false; |
| 218 DCHECK(HasDefaultImage() || user_image.has_raw_image()); | 211 DCHECK(HasDefaultImage() || user_image.has_raw_image()); |
| 219 } | 212 } |
| 220 | 213 |
| 221 void User::SetImageURL(const GURL& image_url) { | 214 void User::SetImageURL(const GURL& image_url) { |
| 222 user_image_.set_url(image_url); | 215 user_image_.set_url(image_url); |
| 223 } | 216 } |
| 224 | 217 |
| 225 void User::SetStubImage(int image_index, bool is_loading) { | 218 void User::SetStubImage(const UserImage& stub_user_image, |
| 226 user_image_ = user_manager::UserImage( | 219 int image_index, |
| 227 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 220 bool is_loading) { |
| 228 IDR_PROFILE_PICTURE_LOADING)); | 221 user_image_ = stub_user_image; |
| 229 image_index_ = image_index; | 222 image_index_ = image_index; |
| 230 image_is_stub_ = true; | 223 image_is_stub_ = true; |
| 231 image_is_loading_ = is_loading; | 224 image_is_loading_ = is_loading; |
| 232 } | 225 } |
| 233 | 226 |
| 234 RegularUser::RegularUser(const std::string& email) : User(email) { | 227 RegularUser::RegularUser(const std::string& email) : User(email) { |
| 235 set_can_lock(true); | 228 set_can_lock(true); |
| 236 set_display_email(email); | 229 set_display_email(email); |
| 237 } | 230 } |
| 238 | 231 |
| 239 RegularUser::~RegularUser() {} | 232 RegularUser::~RegularUser() { |
| 233 } |
| 240 | 234 |
| 241 user_manager::UserType RegularUser::GetType() const { | 235 UserType RegularUser::GetType() const { |
| 242 return user_manager::USER_TYPE_REGULAR; | 236 return user_manager::USER_TYPE_REGULAR; |
| 243 } | 237 } |
| 244 | 238 |
| 245 bool RegularUser::CanSyncImage() const { | 239 bool RegularUser::CanSyncImage() const { |
| 246 return true; | 240 return true; |
| 247 } | 241 } |
| 248 | 242 |
| 249 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) { | 243 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) { |
| 250 set_display_email(std::string()); | 244 set_display_email(std::string()); |
| 251 } | 245 } |
| 252 | 246 |
| 253 GuestUser::~GuestUser() {} | 247 GuestUser::~GuestUser() { |
| 248 } |
| 254 | 249 |
| 255 user_manager::UserType GuestUser::GetType() const { | 250 UserType GuestUser::GetType() const { |
| 256 return user_manager::USER_TYPE_GUEST; | 251 return user_manager::USER_TYPE_GUEST; |
| 257 } | 252 } |
| 258 | 253 |
| 259 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) | 254 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) |
| 260 : User(kiosk_app_username) { | 255 : User(kiosk_app_username) { |
| 261 set_display_email(kiosk_app_username); | 256 set_display_email(kiosk_app_username); |
| 262 } | 257 } |
| 263 | 258 |
| 264 KioskAppUser::~KioskAppUser() {} | 259 KioskAppUser::~KioskAppUser() { |
| 260 } |
| 265 | 261 |
| 266 user_manager::UserType KioskAppUser::GetType() const { | 262 UserType KioskAppUser::GetType() const { |
| 267 return user_manager::USER_TYPE_KIOSK_APP; | 263 return user_manager::USER_TYPE_KIOSK_APP; |
| 268 } | 264 } |
| 269 | 265 |
| 270 SupervisedUser::SupervisedUser(const std::string& username) | 266 SupervisedUser::SupervisedUser(const std::string& username) : User(username) { |
| 271 : User(username) { | |
| 272 set_can_lock(true); | 267 set_can_lock(true); |
| 273 } | 268 } |
| 274 | 269 |
| 275 SupervisedUser::~SupervisedUser() {} | 270 SupervisedUser::~SupervisedUser() { |
| 271 } |
| 276 | 272 |
| 277 user_manager::UserType SupervisedUser::GetType() const { | 273 UserType SupervisedUser::GetType() const { |
| 278 return user_manager::USER_TYPE_SUPERVISED; | 274 return user_manager::USER_TYPE_SUPERVISED; |
| 279 } | 275 } |
| 280 | 276 |
| 281 std::string SupervisedUser::display_email() const { | 277 std::string SupervisedUser::display_email() const { |
| 282 return base::UTF16ToUTF8(display_name()); | 278 return base::UTF16ToUTF8(display_name()); |
| 283 } | 279 } |
| 284 | 280 |
| 285 RetailModeUser::RetailModeUser() : User(chromeos::login::kRetailModeUserName) { | 281 RetailModeUser::RetailModeUser() : User(chromeos::login::kRetailModeUserName) { |
| 286 set_display_email(std::string()); | 282 set_display_email(std::string()); |
| 287 } | 283 } |
| 288 | 284 |
| 289 RetailModeUser::~RetailModeUser() {} | 285 RetailModeUser::~RetailModeUser() { |
| 286 } |
| 290 | 287 |
| 291 user_manager::UserType RetailModeUser::GetType() const { | 288 UserType RetailModeUser::GetType() const { |
| 292 return user_manager::USER_TYPE_RETAIL_MODE; | 289 return user_manager::USER_TYPE_RETAIL_MODE; |
| 293 } | 290 } |
| 294 | 291 |
| 295 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { | 292 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { |
| 296 } | 293 } |
| 297 | 294 |
| 298 PublicAccountUser::~PublicAccountUser() {} | 295 PublicAccountUser::~PublicAccountUser() { |
| 296 } |
| 299 | 297 |
| 300 user_manager::UserType PublicAccountUser::GetType() const { | 298 UserType PublicAccountUser::GetType() const { |
| 301 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; | 299 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; |
| 302 } | 300 } |
| 303 | 301 |
| 304 bool User::has_gaia_account() const { | 302 bool User::has_gaia_account() const { |
| 305 COMPILE_ASSERT(user_manager::NUM_USER_TYPES == 6, num_user_types_unexpected); | 303 COMPILE_ASSERT(user_manager::NUM_USER_TYPES == 6, num_user_types_unexpected); |
| 306 switch (GetType()) { | 304 switch (GetType()) { |
| 307 case user_manager::USER_TYPE_REGULAR: | 305 case user_manager::USER_TYPE_REGULAR: |
| 308 return true; | 306 return true; |
| 309 case user_manager::USER_TYPE_GUEST: | 307 case user_manager::USER_TYPE_GUEST: |
| 310 case user_manager::USER_TYPE_RETAIL_MODE: | 308 case user_manager::USER_TYPE_RETAIL_MODE: |
| 311 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: | 309 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: |
| 312 case user_manager::USER_TYPE_SUPERVISED: | 310 case user_manager::USER_TYPE_SUPERVISED: |
| 313 case user_manager::USER_TYPE_KIOSK_APP: | 311 case user_manager::USER_TYPE_KIOSK_APP: |
| 314 return false; | 312 return false; |
| 315 default: | 313 default: |
| 316 NOTREACHED(); | 314 NOTREACHED(); |
| 317 } | 315 } |
| 318 return false; | 316 return false; |
| 319 } | 317 } |
| 320 | 318 |
| 321 } // namespace chromeos | 319 } // namespace user_manager |
| OLD | NEW |