Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / user_manager / user.cc
blob575d07f589c7f4e84742d3660e3c3c58914f4117
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.
5 #include "components/user_manager/user.h"
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"
16 namespace user_manager {
18 namespace {
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;
26 return email.substr(0, i);
29 } // namespace
31 class RegularUser : public User {
32 public:
33 explicit RegularUser(const std::string& email);
34 virtual ~RegularUser();
36 // Overridden from User:
37 virtual UserType GetType() const OVERRIDE;
38 virtual bool CanSyncImage() const OVERRIDE;
40 private:
41 DISALLOW_COPY_AND_ASSIGN(RegularUser);
44 class GuestUser : public User {
45 public:
46 GuestUser();
47 virtual ~GuestUser();
49 // Overridden from User:
50 virtual UserType GetType() const OVERRIDE;
52 private:
53 DISALLOW_COPY_AND_ASSIGN(GuestUser);
56 class KioskAppUser : public User {
57 public:
58 explicit KioskAppUser(const std::string& app_id);
59 virtual ~KioskAppUser();
61 // Overridden from User:
62 virtual UserType GetType() const OVERRIDE;
64 private:
65 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
68 class SupervisedUser : public User {
69 public:
70 explicit SupervisedUser(const std::string& username);
71 virtual ~SupervisedUser();
73 // Overridden from User:
74 virtual UserType GetType() const OVERRIDE;
75 virtual std::string display_email() const OVERRIDE;
77 private:
78 DISALLOW_COPY_AND_ASSIGN(SupervisedUser);
81 class RetailModeUser : public User {
82 public:
83 RetailModeUser();
84 virtual ~RetailModeUser();
86 // Overridden from User:
87 virtual UserType GetType() const OVERRIDE;
89 private:
90 DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
93 class PublicAccountUser : public User {
94 public:
95 explicit PublicAccountUser(const std::string& email);
96 virtual ~PublicAccountUser();
98 // Overridden from User:
99 virtual UserType GetType() const OVERRIDE;
101 private:
102 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
105 std::string User::GetEmail() const {
106 return display_email();
109 base::string16 User::GetDisplayName() const {
110 // Fallback to the email account name in case display name haven't been set.
111 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true))
112 : display_name_;
115 base::string16 User::GetGivenName() const {
116 return given_name_;
119 const gfx::ImageSkia& User::GetImage() const {
120 return user_image_.image();
123 std::string User::GetUserID() const {
124 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
127 std::string User::GetAccountName(bool use_display_email) const {
128 if (use_display_email && !display_email_.empty())
129 return GetUserName(display_email_);
130 else
131 return GetUserName(email_);
134 bool User::HasDefaultImage() const {
135 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
138 bool User::CanSyncImage() const {
139 return false;
142 std::string User::display_email() const {
143 return display_email_;
146 bool User::can_lock() const {
147 return can_lock_;
150 std::string User::username_hash() const {
151 return username_hash_;
154 bool User::is_logged_in() const {
155 return is_logged_in_;
158 bool User::is_active() const {
159 return is_active_;
162 User* User::CreateRegularUser(const std::string& email) {
163 return new RegularUser(email);
166 User* User::CreateGuestUser() {
167 return new GuestUser;
170 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
171 return new KioskAppUser(kiosk_app_username);
174 User* User::CreateSupervisedUser(const std::string& username) {
175 return new SupervisedUser(username);
178 User* User::CreateRetailModeUser() {
179 return new RetailModeUser;
182 User* User::CreatePublicAccountUser(const std::string& email) {
183 return new PublicAccountUser(email);
186 User::User(const std::string& email)
187 : email_(email),
188 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
189 force_online_signin_(false),
190 image_index_(USER_IMAGE_INVALID),
191 image_is_stub_(false),
192 image_is_loading_(false),
193 can_lock_(false),
194 is_logged_in_(false),
195 is_active_(false),
196 profile_is_created_(false) {
199 User::~User() {
202 void User::SetAccountLocale(const std::string& resolved_account_locale) {
203 account_locale_.reset(new std::string(resolved_account_locale));
206 void User::SetImage(const UserImage& user_image, int image_index) {
207 user_image_ = user_image;
208 image_index_ = image_index;
209 image_is_stub_ = false;
210 image_is_loading_ = false;
211 DCHECK(HasDefaultImage() || user_image.has_raw_image());
214 void User::SetImageURL(const GURL& image_url) {
215 user_image_.set_url(image_url);
218 void User::SetStubImage(const UserImage& stub_user_image,
219 int image_index,
220 bool is_loading) {
221 user_image_ = stub_user_image;
222 image_index_ = image_index;
223 image_is_stub_ = true;
224 image_is_loading_ = is_loading;
227 RegularUser::RegularUser(const std::string& email) : User(email) {
228 set_can_lock(true);
229 set_display_email(email);
232 RegularUser::~RegularUser() {
235 UserType RegularUser::GetType() const {
236 return user_manager::USER_TYPE_REGULAR;
239 bool RegularUser::CanSyncImage() const {
240 return true;
243 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) {
244 set_display_email(std::string());
247 GuestUser::~GuestUser() {
250 UserType GuestUser::GetType() const {
251 return user_manager::USER_TYPE_GUEST;
254 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
255 : User(kiosk_app_username) {
256 set_display_email(kiosk_app_username);
259 KioskAppUser::~KioskAppUser() {
262 UserType KioskAppUser::GetType() const {
263 return user_manager::USER_TYPE_KIOSK_APP;
266 SupervisedUser::SupervisedUser(const std::string& username) : User(username) {
267 set_can_lock(true);
270 SupervisedUser::~SupervisedUser() {
273 UserType SupervisedUser::GetType() const {
274 return user_manager::USER_TYPE_SUPERVISED;
277 std::string SupervisedUser::display_email() const {
278 return base::UTF16ToUTF8(display_name());
281 RetailModeUser::RetailModeUser() : User(chromeos::login::kRetailModeUserName) {
282 set_display_email(std::string());
285 RetailModeUser::~RetailModeUser() {
288 UserType RetailModeUser::GetType() const {
289 return user_manager::USER_TYPE_RETAIL_MODE;
292 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
295 PublicAccountUser::~PublicAccountUser() {
298 UserType PublicAccountUser::GetType() const {
299 return user_manager::USER_TYPE_PUBLIC_ACCOUNT;
302 bool User::has_gaia_account() const {
303 COMPILE_ASSERT(user_manager::NUM_USER_TYPES == 6, num_user_types_unexpected);
304 switch (GetType()) {
305 case user_manager::USER_TYPE_REGULAR:
306 return true;
307 case user_manager::USER_TYPE_GUEST:
308 case user_manager::USER_TYPE_RETAIL_MODE:
309 case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
310 case user_manager::USER_TYPE_SUPERVISED:
311 case user_manager::USER_TYPE_KIOSK_APP:
312 return false;
313 default:
314 NOTREACHED();
316 return false;
319 } // namespace user_manager