Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user.cc
blobc2df4bc38406ab3f908a75df9b8ac222578d21e8
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.
5 #include "chrome/browser/chromeos/login/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 "chrome/browser/chromeos/login/default_user_images.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
16 namespace chromeos {
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 const int User::kExternalImageIndex;
32 const int User::kProfileImageIndex;
33 const int User::kInvalidImageIndex;
35 class RegularUser : public User {
36 public:
37 explicit RegularUser(const std::string& email);
38 virtual ~RegularUser();
40 // Overridden from User:
41 virtual UserType GetType() const OVERRIDE;
42 virtual bool CanSyncImage() const OVERRIDE;
43 virtual bool can_lock() const OVERRIDE;
45 private:
46 DISALLOW_COPY_AND_ASSIGN(RegularUser);
49 class GuestUser : public User {
50 public:
51 GuestUser();
52 virtual ~GuestUser();
54 // Overridden from User:
55 virtual UserType GetType() const OVERRIDE;
57 private:
58 DISALLOW_COPY_AND_ASSIGN(GuestUser);
61 class KioskAppUser : public User {
62 public:
63 explicit KioskAppUser(const std::string& app_id);
64 virtual ~KioskAppUser();
66 // Overridden from User:
67 virtual UserType GetType() const OVERRIDE;
69 private:
70 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
73 class LocallyManagedUser : public User {
74 public:
75 explicit LocallyManagedUser(const std::string& username);
76 virtual ~LocallyManagedUser();
78 // Overridden from User:
79 virtual UserType GetType() const OVERRIDE;
80 virtual bool can_lock() const OVERRIDE;
81 virtual std::string display_email() const OVERRIDE;
83 private:
84 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser);
87 class RetailModeUser : public User {
88 public:
89 RetailModeUser();
90 virtual ~RetailModeUser();
92 // Overridden from User:
93 virtual UserType GetType() const OVERRIDE;
95 private:
96 DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
99 class PublicAccountUser : public User {
100 public:
101 explicit PublicAccountUser(const std::string& email);
102 virtual ~PublicAccountUser();
104 // Overridden from User:
105 virtual UserType GetType() const OVERRIDE;
107 private:
108 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
111 UserContext::UserContext() : using_oauth(true) {
114 UserContext::UserContext(const std::string& username,
115 const std::string& password,
116 const std::string& auth_code)
117 : username(username),
118 password(password),
119 auth_code(auth_code),
120 using_oauth(true) {
123 UserContext::UserContext(const std::string& username,
124 const std::string& password,
125 const std::string& auth_code,
126 const std::string& username_hash)
127 : username(username),
128 password(password),
129 auth_code(auth_code),
130 username_hash(username_hash),
131 using_oauth(true) {
134 UserContext::UserContext(const std::string& username,
135 const std::string& password,
136 const std::string& auth_code,
137 const std::string& username_hash,
138 bool using_oauth)
139 : username(username),
140 password(password),
141 auth_code(auth_code),
142 username_hash(username_hash),
143 using_oauth(using_oauth) {
146 UserContext::~UserContext() {
149 bool UserContext::operator==(const UserContext& context) const {
150 return context.username == username &&
151 context.password == password &&
152 context.auth_code == auth_code &&
153 context.username_hash == username_hash &&
154 context.using_oauth == using_oauth;
157 base::string16 User::GetDisplayName() const {
158 // Fallback to the email account name in case display name haven't been set.
159 return display_name_.empty() ?
160 base::UTF8ToUTF16(GetAccountName(true)) :
161 display_name_;
164 std::string User::GetAccountName(bool use_display_email) const {
165 if (use_display_email && !display_email_.empty())
166 return GetUserName(display_email_);
167 else
168 return GetUserName(email_);
171 bool User::HasDefaultImage() const {
172 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
175 bool User::CanSyncImage() const {
176 return false;
179 std::string User::display_email() const {
180 return display_email_;
183 bool User::can_lock() const {
184 return false;
187 std::string User::username_hash() const {
188 return username_hash_;
191 bool User::is_logged_in() const {
192 return is_logged_in_;
195 bool User::is_active() const {
196 return is_active_;
199 User* User::CreateRegularUser(const std::string& email) {
200 return new RegularUser(email);
203 User* User::CreateGuestUser() {
204 return new GuestUser;
207 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
208 return new KioskAppUser(kiosk_app_username);
211 User* User::CreateLocallyManagedUser(const std::string& username) {
212 return new LocallyManagedUser(username);
215 User* User::CreateRetailModeUser() {
216 return new RetailModeUser;
219 User* User::CreatePublicAccountUser(const std::string& email) {
220 return new PublicAccountUser(email);
223 User::User(const std::string& email)
224 : email_(email),
225 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
226 image_index_(kInvalidImageIndex),
227 image_is_stub_(false),
228 image_is_loading_(false),
229 is_logged_in_(false),
230 is_active_(false),
231 profile_is_created_(false) {
234 User::~User() {}
236 void User::SetAccountLocale(const std::string& resolved_account_locale) {
237 account_locale_.reset(new std::string(resolved_account_locale));
240 void User::SetImage(const UserImage& user_image, int image_index) {
241 user_image_ = user_image;
242 image_index_ = image_index;
243 image_is_stub_ = false;
244 image_is_loading_ = false;
245 DCHECK(HasDefaultImage() || user_image.has_raw_image());
248 void User::SetImageURL(const GURL& image_url) {
249 user_image_.set_url(image_url);
252 void User::SetStubImage(int image_index, bool is_loading) {
253 user_image_ = UserImage(
254 *ResourceBundle::GetSharedInstance().
255 GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING));
256 image_index_ = image_index;
257 image_is_stub_ = true;
258 image_is_loading_ = is_loading;
261 RegularUser::RegularUser(const std::string& email) : User(email) {
262 set_display_email(email);
265 RegularUser::~RegularUser() {}
267 User::UserType RegularUser::GetType() const {
268 return USER_TYPE_REGULAR;
271 bool RegularUser::CanSyncImage() const {
272 return true;
275 bool RegularUser::can_lock() const {
276 return true;
279 GuestUser::GuestUser() : User(UserManager::kGuestUserName) {
280 set_display_email(std::string());
283 GuestUser::~GuestUser() {}
285 User::UserType GuestUser::GetType() const {
286 return USER_TYPE_GUEST;
289 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
290 : User(kiosk_app_username) {
291 set_display_email(std::string());
294 KioskAppUser::~KioskAppUser() {}
296 User::UserType KioskAppUser::GetType() const {
297 return USER_TYPE_KIOSK_APP;
300 LocallyManagedUser::LocallyManagedUser(const std::string& username)
301 : User(username) {
304 LocallyManagedUser::~LocallyManagedUser() {}
306 User::UserType LocallyManagedUser::GetType() const {
307 return USER_TYPE_LOCALLY_MANAGED;
310 bool LocallyManagedUser::can_lock() const {
311 return true;
314 std::string LocallyManagedUser::display_email() const {
315 return base::UTF16ToUTF8(display_name());
318 RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) {
319 set_display_email(std::string());
322 RetailModeUser::~RetailModeUser() {}
324 User::UserType RetailModeUser::GetType() const {
325 return USER_TYPE_RETAIL_MODE;
328 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
331 PublicAccountUser::~PublicAccountUser() {}
333 User::UserType PublicAccountUser::GetType() const {
334 return USER_TYPE_PUBLIC_ACCOUNT;
337 bool User::has_gaia_account() const {
338 COMPILE_ASSERT(NUM_USER_TYPES == 6, num_user_types_unexpected);
339 switch (GetType()) {
340 case USER_TYPE_REGULAR:
341 return true;
342 case USER_TYPE_GUEST:
343 case USER_TYPE_RETAIL_MODE:
344 case USER_TYPE_PUBLIC_ACCOUNT:
345 case USER_TYPE_LOCALLY_MANAGED:
346 case USER_TYPE_KIOSK_APP:
347 return false;
348 default:
349 NOTREACHED();
351 return false;
354 } // namespace chromeos