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"
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) {
26 return email.substr(0, i);
31 const int User::kExternalImageIndex;
32 const int User::kProfileImageIndex;
33 const int User::kInvalidImageIndex;
35 class RegularUser : public User {
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;
45 DISALLOW_COPY_AND_ASSIGN(RegularUser);
48 class GuestUser : public User {
53 // Overridden from User:
54 virtual UserType GetType() const OVERRIDE;
57 DISALLOW_COPY_AND_ASSIGN(GuestUser);
60 class KioskAppUser : public User {
62 explicit KioskAppUser(const std::string& app_id);
63 virtual ~KioskAppUser();
65 // Overridden from User:
66 virtual UserType GetType() const OVERRIDE;
69 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
72 class LocallyManagedUser : public User {
74 explicit LocallyManagedUser(const std::string& username);
75 virtual ~LocallyManagedUser();
77 // Overridden from User:
78 virtual UserType GetType() const OVERRIDE;
79 virtual std::string display_email() const OVERRIDE;
82 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser);
85 class RetailModeUser : public User {
88 virtual ~RetailModeUser();
90 // Overridden from User:
91 virtual UserType GetType() const OVERRIDE;
94 DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
97 class PublicAccountUser : public User {
99 explicit PublicAccountUser(const std::string& email);
100 virtual ~PublicAccountUser();
102 // Overridden from User:
103 virtual UserType GetType() const OVERRIDE;
106 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
109 UserContext::UserContext() : using_oauth(true), auth_flow(AUTH_FLOW_OFFLINE) {
112 UserContext::UserContext(const std::string& username,
113 const std::string& password,
114 const std::string& auth_code)
115 : username(username),
117 auth_code(auth_code),
119 auth_flow(AUTH_FLOW_OFFLINE) {
122 UserContext::UserContext(const std::string& username,
123 const std::string& password,
124 const std::string& auth_code,
125 const std::string& username_hash)
126 : username(username),
128 auth_code(auth_code),
129 username_hash(username_hash),
131 auth_flow(AUTH_FLOW_OFFLINE) {
134 UserContext::UserContext(const std::string& username,
135 const std::string& password,
136 const std::string& auth_code,
137 const std::string& username_hash,
140 : username(username),
142 auth_code(auth_code),
143 username_hash(username_hash),
144 using_oauth(using_oauth),
145 auth_flow(auth_flow) {
148 UserContext::~UserContext() {
151 bool UserContext::operator==(const UserContext& context) const {
152 return context.username == username &&
153 context.password == password &&
154 context.auth_code == auth_code &&
155 context.username_hash == username_hash &&
156 context.using_oauth == using_oauth &&
157 context.auth_flow == auth_flow;
160 base::string16 User::GetDisplayName() const {
161 // Fallback to the email account name in case display name haven't been set.
162 return display_name_.empty() ?
163 base::UTF8ToUTF16(GetAccountName(true)) :
167 std::string User::GetAccountName(bool use_display_email) const {
168 if (use_display_email && !display_email_.empty())
169 return GetUserName(display_email_);
171 return GetUserName(email_);
174 bool User::HasDefaultImage() const {
175 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
178 bool User::CanSyncImage() const {
182 std::string User::display_email() const {
183 return display_email_;
186 bool User::can_lock() const {
190 std::string User::username_hash() const {
191 return username_hash_;
194 bool User::is_logged_in() const {
195 return is_logged_in_;
198 bool User::is_active() const {
202 User* User::CreateRegularUser(const std::string& email) {
203 return new RegularUser(email);
206 User* User::CreateGuestUser() {
207 return new GuestUser;
210 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
211 return new KioskAppUser(kiosk_app_username);
214 User* User::CreateLocallyManagedUser(const std::string& username) {
215 return new LocallyManagedUser(username);
218 User* User::CreateRetailModeUser() {
219 return new RetailModeUser;
222 User* User::CreatePublicAccountUser(const std::string& email) {
223 return new PublicAccountUser(email);
226 User::User(const std::string& email)
228 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
229 force_online_signin_(false),
230 image_index_(kInvalidImageIndex),
231 image_is_stub_(false),
232 image_is_loading_(false),
234 is_logged_in_(false),
236 profile_is_created_(false) {
241 void User::SetAccountLocale(const std::string& resolved_account_locale) {
242 account_locale_.reset(new std::string(resolved_account_locale));
245 void User::SetImage(const UserImage& user_image, int image_index) {
246 user_image_ = user_image;
247 image_index_ = image_index;
248 image_is_stub_ = false;
249 image_is_loading_ = false;
250 DCHECK(HasDefaultImage() || user_image.has_raw_image());
253 void User::SetImageURL(const GURL& image_url) {
254 user_image_.set_url(image_url);
257 void User::SetStubImage(int image_index, bool is_loading) {
258 user_image_ = UserImage(
259 *ResourceBundle::GetSharedInstance().
260 GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING));
261 image_index_ = image_index;
262 image_is_stub_ = true;
263 image_is_loading_ = is_loading;
266 RegularUser::RegularUser(const std::string& email) : User(email) {
268 set_display_email(email);
271 RegularUser::~RegularUser() {}
273 User::UserType RegularUser::GetType() const {
274 return USER_TYPE_REGULAR;
277 bool RegularUser::CanSyncImage() const {
281 GuestUser::GuestUser() : User(UserManager::kGuestUserName) {
282 set_display_email(std::string());
285 GuestUser::~GuestUser() {}
287 User::UserType GuestUser::GetType() const {
288 return USER_TYPE_GUEST;
291 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
292 : User(kiosk_app_username) {
293 set_display_email(std::string());
296 KioskAppUser::~KioskAppUser() {}
298 User::UserType KioskAppUser::GetType() const {
299 return USER_TYPE_KIOSK_APP;
302 LocallyManagedUser::LocallyManagedUser(const std::string& username)
307 LocallyManagedUser::~LocallyManagedUser() {}
309 User::UserType LocallyManagedUser::GetType() const {
310 return USER_TYPE_LOCALLY_MANAGED;
313 std::string LocallyManagedUser::display_email() const {
314 return base::UTF16ToUTF8(display_name());
317 RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) {
318 set_display_email(std::string());
321 RetailModeUser::~RetailModeUser() {}
323 User::UserType RetailModeUser::GetType() const {
324 return USER_TYPE_RETAIL_MODE;
327 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
330 PublicAccountUser::~PublicAccountUser() {}
332 User::UserType PublicAccountUser::GetType() const {
333 return USER_TYPE_PUBLIC_ACCOUNT;
336 bool User::has_gaia_account() const {
337 COMPILE_ASSERT(NUM_USER_TYPES == 6, num_user_types_unexpected);
339 case USER_TYPE_REGULAR:
341 case USER_TYPE_GUEST:
342 case USER_TYPE_RETAIL_MODE:
343 case USER_TYPE_PUBLIC_ACCOUNT:
344 case USER_TYPE_LOCALLY_MANAGED:
345 case USER_TYPE_KIOSK_APP:
353 } // namespace chromeos