Chromium Code Reviews| 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 "components/user_manager/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 "chromeos/login/user_names.h" | 11 #include "chromeos/login/user_names.h" |
| 12 #include "components/signin/core/account_id/account_id.h" | |
| 12 #include "components/user_manager/user_image/default_user_images.h" | 13 #include "components/user_manager/user_image/default_user_images.h" |
| 13 #include "google_apis/gaia/gaia_auth_util.h" | 14 #include "google_apis/gaia/gaia_auth_util.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 15 | 16 |
| 16 namespace user_manager { | 17 namespace user_manager { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Returns account name portion of an email. | 21 // Returns account name portion of an email. |
| 21 std::string GetUserName(const std::string& email) { | 22 std::string GetUserName(const std::string& email) { |
| 22 std::string::size_type i = email.find('@'); | 23 std::string::size_type i = email.find('@'); |
| 23 if (i == 0 || i == std::string::npos) { | 24 if (i == 0 || i == std::string::npos) { |
| 24 return email; | 25 return email; |
| 25 } | 26 } |
| 26 return email.substr(0, i); | 27 return email.substr(0, i); |
| 27 } | 28 } |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 // static | 32 // static |
| 32 bool User::TypeHasGaiaAccount(UserType user_type) { | 33 bool User::TypeHasGaiaAccount(UserType user_type) { |
| 33 return user_type == USER_TYPE_REGULAR || | 34 return user_type == USER_TYPE_REGULAR || |
| 34 user_type == USER_TYPE_CHILD; | 35 user_type == USER_TYPE_CHILD; |
| 35 } | 36 } |
| 36 | 37 |
| 38 const std::string& User::email() const { | |
| 39 return account_id_.GetUserEmail(); | |
| 40 } | |
| 41 | |
| 37 // Also used for regular supervised users. | 42 // Also used for regular supervised users. |
| 38 class RegularUser : public User { | 43 class RegularUser : public User { |
| 39 public: | 44 public: |
| 40 explicit RegularUser(const std::string& email); | 45 explicit RegularUser(const AccountId& account_id); |
| 41 ~RegularUser() override; | 46 ~RegularUser() override; |
| 42 | 47 |
| 43 // Overridden from User: | 48 // Overridden from User: |
| 44 UserType GetType() const override; | 49 UserType GetType() const override; |
| 45 bool CanSyncImage() const override; | 50 bool CanSyncImage() const override; |
| 46 void SetIsChild(bool is_child) override; | 51 void SetIsChild(bool is_child) override; |
| 47 | 52 |
| 48 private: | 53 private: |
| 49 bool is_child_; | 54 bool is_child_; |
|
achuithb
2015/10/28 23:11:46
= false
Alexander Alekseev
2015/10/29 02:00:42
Done.
| |
| 50 | 55 |
| 51 DISALLOW_COPY_AND_ASSIGN(RegularUser); | 56 DISALLOW_COPY_AND_ASSIGN(RegularUser); |
| 52 }; | 57 }; |
| 53 | 58 |
| 54 class GuestUser : public User { | 59 class GuestUser : public User { |
| 55 public: | 60 public: |
| 56 GuestUser(); | 61 GuestUser(); |
| 57 ~GuestUser() override; | 62 ~GuestUser() override; |
| 58 | 63 |
| 59 // Overridden from User: | 64 // Overridden from User: |
| 60 UserType GetType() const override; | 65 UserType GetType() const override; |
| 61 | 66 |
| 62 private: | 67 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(GuestUser); | 68 DISALLOW_COPY_AND_ASSIGN(GuestUser); |
| 64 }; | 69 }; |
| 65 | 70 |
| 66 class KioskAppUser : public User { | 71 class KioskAppUser : public User { |
| 67 public: | 72 public: |
| 68 explicit KioskAppUser(const std::string& app_id); | 73 explicit KioskAppUser(const AccountId& kiosk_app_account_id); |
| 69 ~KioskAppUser() override; | 74 ~KioskAppUser() override; |
| 70 | 75 |
| 71 // Overridden from User: | 76 // Overridden from User: |
| 72 UserType GetType() const override; | 77 UserType GetType() const override; |
| 73 | 78 |
| 74 private: | 79 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); | 80 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); |
| 76 }; | 81 }; |
| 77 | 82 |
| 78 class SupervisedUser : public User { | 83 class SupervisedUser : public User { |
| 79 public: | 84 public: |
| 80 explicit SupervisedUser(const std::string& username); | 85 explicit SupervisedUser(const AccountId& account_id); |
| 81 ~SupervisedUser() override; | 86 ~SupervisedUser() override; |
| 82 | 87 |
| 83 // Overridden from User: | 88 // Overridden from User: |
| 84 UserType GetType() const override; | 89 UserType GetType() const override; |
| 85 std::string display_email() const override; | 90 std::string display_email() const override; |
| 86 | 91 |
| 87 private: | 92 private: |
| 88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); | 93 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 class PublicAccountUser : public User { | 96 class PublicAccountUser : public User { |
| 92 public: | 97 public: |
| 93 explicit PublicAccountUser(const std::string& email); | 98 explicit PublicAccountUser(const AccountId& account_id); |
| 94 ~PublicAccountUser() override; | 99 ~PublicAccountUser() override; |
| 95 | 100 |
| 96 // Overridden from User: | 101 // Overridden from User: |
| 97 UserType GetType() const override; | 102 UserType GetType() const override; |
| 98 | 103 |
| 99 private: | 104 private: |
| 100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); | 105 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); |
| 101 }; | 106 }; |
| 102 | 107 |
| 103 std::string User::GetEmail() const { | 108 std::string User::GetEmail() const { |
| 104 return display_email(); | 109 return display_email(); |
| 105 } | 110 } |
| 106 | 111 |
| 107 base::string16 User::GetDisplayName() const { | 112 base::string16 User::GetDisplayName() const { |
| 108 // Fallback to the email account name in case display name haven't been set. | 113 // Fallback to the email account name in case display name haven't been set. |
| 109 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true)) | 114 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true)) |
| 110 : display_name_; | 115 : display_name_; |
| 111 } | 116 } |
| 112 | 117 |
| 113 base::string16 User::GetGivenName() const { | 118 base::string16 User::GetGivenName() const { |
| 114 return given_name_; | 119 return given_name_; |
| 115 } | 120 } |
| 116 | 121 |
| 117 const gfx::ImageSkia& User::GetImage() const { | 122 const gfx::ImageSkia& User::GetImage() const { |
| 118 return user_image_.image(); | 123 return user_image_.image(); |
| 119 } | 124 } |
| 120 | 125 |
| 121 UserID User::GetUserID() const { | 126 AccountId User::GetAccountId() const { |
| 122 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); | 127 return AccountId::FromUserEmail( |
| 128 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()))); | |
| 123 } | 129 } |
| 124 | 130 |
| 125 void User::SetIsChild(bool is_child) { | 131 void User::SetIsChild(bool is_child) { |
| 126 VLOG(1) << "Ignoring SetIsChild call with param " << is_child; | 132 VLOG(1) << "Ignoring SetIsChild call with param " << is_child; |
| 127 if (is_child) { | 133 if (is_child) { |
| 128 NOTREACHED() << "Calling SetIsChild(true) for base User class." | 134 NOTREACHED() << "Calling SetIsChild(true) for base User class." |
| 129 << "Base class cannot be set as child"; | 135 << "Base class cannot be set as child"; |
| 130 } | 136 } |
| 131 } | 137 } |
| 132 | 138 |
| 133 bool User::HasGaiaAccount() const { | 139 bool User::HasGaiaAccount() const { |
| 134 return TypeHasGaiaAccount(GetType()); | 140 return TypeHasGaiaAccount(GetType()); |
| 135 } | 141 } |
| 136 | 142 |
| 137 bool User::IsSupervised() const { | 143 bool User::IsSupervised() const { |
| 138 UserType type = GetType(); | 144 UserType type = GetType(); |
| 139 return type == USER_TYPE_SUPERVISED || | 145 return type == USER_TYPE_SUPERVISED || |
| 140 type == USER_TYPE_CHILD; | 146 type == USER_TYPE_CHILD; |
| 141 } | 147 } |
| 142 | 148 |
| 143 std::string User::GetAccountName(bool use_display_email) const { | 149 std::string User::GetAccountName(bool use_display_email) const { |
| 144 if (use_display_email && !display_email_.empty()) | 150 if (use_display_email && !display_email_.empty()) |
| 145 return GetUserName(display_email_); | 151 return GetUserName(display_email_); |
| 146 else | 152 else |
| 147 return GetUserName(email_); | 153 return GetUserName(account_id_.GetUserEmail()); |
| 148 } | 154 } |
| 149 | 155 |
| 150 bool User::HasDefaultImage() const { | 156 bool User::HasDefaultImage() const { |
| 151 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; | 157 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; |
| 152 } | 158 } |
| 153 | 159 |
| 154 bool User::CanSyncImage() const { | 160 bool User::CanSyncImage() const { |
| 155 return false; | 161 return false; |
| 156 } | 162 } |
| 157 | 163 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 168 } | 174 } |
| 169 | 175 |
| 170 bool User::is_logged_in() const { | 176 bool User::is_logged_in() const { |
| 171 return is_logged_in_; | 177 return is_logged_in_; |
| 172 } | 178 } |
| 173 | 179 |
| 174 bool User::is_active() const { | 180 bool User::is_active() const { |
| 175 return is_active_; | 181 return is_active_; |
| 176 } | 182 } |
| 177 | 183 |
| 178 User* User::CreateRegularUser(const std::string& email) { | 184 User* User::CreateRegularUser(const AccountId& account_id) { |
| 179 return new RegularUser(email); | 185 return new RegularUser(account_id); |
| 180 } | 186 } |
| 181 | 187 |
| 182 User* User::CreateGuestUser() { | 188 User* User::CreateGuestUser() { |
| 183 return new GuestUser; | 189 return new GuestUser; |
| 184 } | 190 } |
| 185 | 191 |
| 186 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) { | 192 User* User::CreateKioskAppUser(const AccountId& kiosk_app_account_id) { |
| 187 return new KioskAppUser(kiosk_app_username); | 193 return new KioskAppUser(kiosk_app_account_id); |
| 188 } | 194 } |
| 189 | 195 |
| 190 User* User::CreateSupervisedUser(const std::string& username) { | 196 User* User::CreateSupervisedUser(const AccountId& account_id) { |
| 191 return new SupervisedUser(username); | 197 return new SupervisedUser(account_id); |
| 192 } | 198 } |
| 193 | 199 |
| 194 User* User::CreatePublicAccountUser(const std::string& email) { | 200 User* User::CreatePublicAccountUser(const AccountId& account_id) { |
| 195 return new PublicAccountUser(email); | 201 return new PublicAccountUser(account_id); |
| 196 } | 202 } |
| 197 | 203 |
| 198 User::User(const std::string& email) | 204 User::User(const AccountId& account_id) : account_id_(account_id) {} |
| 199 : email_(email), | |
| 200 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), | |
| 201 force_online_signin_(false), | |
| 202 image_index_(USER_IMAGE_INVALID), | |
| 203 image_is_stub_(false), | |
| 204 image_is_loading_(false), | |
| 205 can_lock_(false), | |
| 206 is_logged_in_(false), | |
| 207 is_active_(false), | |
| 208 profile_is_created_(false), | |
| 209 is_affiliated_(false){ | |
| 210 } | |
| 211 | 205 |
| 212 User::~User() { | 206 User::~User() { |
| 213 } | 207 } |
| 214 | 208 |
| 215 void User::SetAccountLocale(const std::string& resolved_account_locale) { | 209 void User::SetAccountLocale(const std::string& resolved_account_locale) { |
| 216 account_locale_.reset(new std::string(resolved_account_locale)); | 210 account_locale_.reset(new std::string(resolved_account_locale)); |
| 217 } | 211 } |
| 218 | 212 |
| 219 void User::SetImage(const UserImage& user_image, int image_index) { | 213 void User::SetImage(const UserImage& user_image, int image_index) { |
| 220 user_image_ = user_image; | 214 user_image_ = user_image; |
| 221 image_index_ = image_index; | 215 image_index_ = image_index; |
| 222 image_is_stub_ = false; | 216 image_is_stub_ = false; |
| 223 image_is_loading_ = false; | 217 image_is_loading_ = false; |
| 224 DCHECK(HasDefaultImage() || user_image.has_raw_image()); | 218 DCHECK(HasDefaultImage() || user_image.has_raw_image()); |
| 225 } | 219 } |
| 226 | 220 |
| 227 void User::SetImageURL(const GURL& image_url) { | 221 void User::SetImageURL(const GURL& image_url) { |
| 228 user_image_.set_url(image_url); | 222 user_image_.set_url(image_url); |
| 229 } | 223 } |
| 230 | 224 |
| 231 void User::SetStubImage(const UserImage& stub_user_image, | 225 void User::SetStubImage(const UserImage& stub_user_image, |
| 232 int image_index, | 226 int image_index, |
| 233 bool is_loading) { | 227 bool is_loading) { |
| 234 user_image_ = stub_user_image; | 228 user_image_ = stub_user_image; |
| 235 image_index_ = image_index; | 229 image_index_ = image_index; |
| 236 image_is_stub_ = true; | 230 image_is_stub_ = true; |
| 237 image_is_loading_ = is_loading; | 231 image_is_loading_ = is_loading; |
| 238 } | 232 } |
| 239 | 233 |
| 240 RegularUser::RegularUser(const std::string& email) | 234 RegularUser::RegularUser(const AccountId& account_id) |
| 241 : User(email), is_child_(false) { | 235 : User(account_id), is_child_(false) { |
| 242 set_can_lock(true); | 236 set_can_lock(true); |
| 243 set_display_email(email); | 237 set_display_email(account_id.GetUserEmail()); |
| 244 } | 238 } |
| 245 | 239 |
| 246 RegularUser::~RegularUser() { | 240 RegularUser::~RegularUser() { |
| 247 } | 241 } |
| 248 | 242 |
| 249 UserType RegularUser::GetType() const { | 243 UserType RegularUser::GetType() const { |
| 250 return is_child_ ? user_manager::USER_TYPE_CHILD : | 244 return is_child_ ? user_manager::USER_TYPE_CHILD : |
| 251 user_manager::USER_TYPE_REGULAR; | 245 user_manager::USER_TYPE_REGULAR; |
| 252 } | 246 } |
| 253 | 247 |
| 254 bool RegularUser::CanSyncImage() const { | 248 bool RegularUser::CanSyncImage() const { |
| 255 return true; | 249 return true; |
| 256 } | 250 } |
| 257 | 251 |
| 258 void RegularUser::SetIsChild(bool is_child) { | 252 void RegularUser::SetIsChild(bool is_child) { |
| 259 VLOG(1) << "Setting user is child to " << is_child; | 253 VLOG(1) << "Setting user is child to " << is_child; |
| 260 is_child_ = is_child; | 254 is_child_ = is_child; |
| 261 } | 255 } |
| 262 | 256 |
| 263 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) { | 257 GuestUser::GuestUser() : User(chromeos::login::GuestAccountId()) { |
| 264 set_display_email(std::string()); | 258 set_display_email(std::string()); |
| 265 } | 259 } |
| 266 | 260 |
| 267 GuestUser::~GuestUser() { | 261 GuestUser::~GuestUser() { |
| 268 } | 262 } |
| 269 | 263 |
| 270 UserType GuestUser::GetType() const { | 264 UserType GuestUser::GetType() const { |
| 271 return user_manager::USER_TYPE_GUEST; | 265 return user_manager::USER_TYPE_GUEST; |
| 272 } | 266 } |
| 273 | 267 |
| 274 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) | 268 KioskAppUser::KioskAppUser(const AccountId& kiosk_app_account_id) |
| 275 : User(kiosk_app_username) { | 269 : User(kiosk_app_account_id) { |
| 276 set_display_email(kiosk_app_username); | 270 set_display_email(kiosk_app_account_id.GetUserEmail()); |
| 277 } | 271 } |
| 278 | 272 |
| 279 KioskAppUser::~KioskAppUser() { | 273 KioskAppUser::~KioskAppUser() { |
| 280 } | 274 } |
| 281 | 275 |
| 282 UserType KioskAppUser::GetType() const { | 276 UserType KioskAppUser::GetType() const { |
| 283 return user_manager::USER_TYPE_KIOSK_APP; | 277 return user_manager::USER_TYPE_KIOSK_APP; |
| 284 } | 278 } |
| 285 | 279 |
| 286 SupervisedUser::SupervisedUser(const std::string& username) : User(username) { | 280 SupervisedUser::SupervisedUser(const AccountId& account_id) : User(account_id) { |
| 287 set_can_lock(true); | 281 set_can_lock(true); |
| 288 } | 282 } |
| 289 | 283 |
| 290 SupervisedUser::~SupervisedUser() { | 284 SupervisedUser::~SupervisedUser() { |
| 291 } | 285 } |
| 292 | 286 |
| 293 UserType SupervisedUser::GetType() const { | 287 UserType SupervisedUser::GetType() const { |
| 294 return user_manager::USER_TYPE_SUPERVISED; | 288 return user_manager::USER_TYPE_SUPERVISED; |
| 295 } | 289 } |
| 296 | 290 |
| 297 std::string SupervisedUser::display_email() const { | 291 std::string SupervisedUser::display_email() const { |
| 298 return base::UTF16ToUTF8(display_name()); | 292 return base::UTF16ToUTF8(display_name()); |
| 299 } | 293 } |
| 300 | 294 |
| 301 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { | 295 PublicAccountUser::PublicAccountUser(const AccountId& account_id) |
| 302 } | 296 : User(account_id) {} |
| 303 | 297 |
| 304 PublicAccountUser::~PublicAccountUser() { | 298 PublicAccountUser::~PublicAccountUser() { |
| 305 } | 299 } |
| 306 | 300 |
| 307 UserType PublicAccountUser::GetType() const { | 301 UserType PublicAccountUser::GetType() const { |
| 308 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; | 302 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; |
| 309 } | 303 } |
| 310 | 304 |
| 311 bool User::has_gaia_account() const { | 305 bool User::has_gaia_account() const { |
| 312 static_assert(user_manager::NUM_USER_TYPES == 7, | 306 static_assert(user_manager::NUM_USER_TYPES == 7, |
| 313 "NUM_USER_TYPES should equal 7"); | 307 "NUM_USER_TYPES should equal 7"); |
| 314 switch (GetType()) { | 308 switch (GetType()) { |
| 315 case user_manager::USER_TYPE_REGULAR: | 309 case user_manager::USER_TYPE_REGULAR: |
| 316 case user_manager::USER_TYPE_CHILD: | 310 case user_manager::USER_TYPE_CHILD: |
| 317 return true; | 311 return true; |
| 318 case user_manager::USER_TYPE_GUEST: | 312 case user_manager::USER_TYPE_GUEST: |
| 319 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: | 313 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: |
| 320 case user_manager::USER_TYPE_SUPERVISED: | 314 case user_manager::USER_TYPE_SUPERVISED: |
| 321 case user_manager::USER_TYPE_KIOSK_APP: | 315 case user_manager::USER_TYPE_KIOSK_APP: |
| 322 return false; | 316 return false; |
| 323 default: | 317 default: |
| 324 NOTREACHED(); | 318 NOTREACHED(); |
| 325 } | 319 } |
| 326 return false; | 320 return false; |
| 327 } | 321 } |
| 328 | 322 |
| 329 } // namespace user_manager | 323 } // namespace user_manager |
| OLD | NEW |