Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user.cc
blobc21222894579b6336e124baf8dcc61414cd83627
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 "google_apis/gaia/gaia_auth_util.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
17 namespace chromeos {
19 namespace {
21 // Returns account name portion of an email.
22 std::string GetUserName(const std::string& email) {
23 std::string::size_type i = email.find('@');
24 if (i == 0 || i == std::string::npos) {
25 return email;
27 return email.substr(0, i);
30 } // namespace
32 const int User::kExternalImageIndex;
33 const int User::kProfileImageIndex;
34 const int User::kInvalidImageIndex;
36 class RegularUser : public User {
37 public:
38 explicit RegularUser(const std::string& email);
39 virtual ~RegularUser();
41 // Overridden from User:
42 virtual UserType GetType() const OVERRIDE;
43 virtual bool CanSyncImage() 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 std::string display_email() const OVERRIDE;
82 private:
83 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser);
86 class RetailModeUser : public User {
87 public:
88 RetailModeUser();
89 virtual ~RetailModeUser();
91 // Overridden from User:
92 virtual UserType GetType() const OVERRIDE;
94 private:
95 DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
98 class PublicAccountUser : public User {
99 public:
100 explicit PublicAccountUser(const std::string& email);
101 virtual ~PublicAccountUser();
103 // Overridden from User:
104 virtual UserType GetType() const OVERRIDE;
106 private:
107 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
110 UserContext::UserContext() : using_oauth(true), auth_flow(AUTH_FLOW_OFFLINE) {
113 UserContext::UserContext(const std::string& username,
114 const std::string& password,
115 const std::string& auth_code)
116 : username(username),
117 password(password),
118 need_password_hashing(true),
119 auth_code(auth_code),
120 using_oauth(true),
121 auth_flow(AUTH_FLOW_OFFLINE) {}
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 need_password_hashing(true),
130 auth_code(auth_code),
131 username_hash(username_hash),
132 using_oauth(true),
133 auth_flow(AUTH_FLOW_OFFLINE) {}
135 UserContext::UserContext(const std::string& username,
136 const std::string& password,
137 const std::string& auth_code,
138 const std::string& username_hash,
139 bool using_oauth,
140 AuthFlow auth_flow)
141 : username(username),
142 password(password),
143 need_password_hashing(true),
144 auth_code(auth_code),
145 username_hash(username_hash),
146 using_oauth(using_oauth),
147 auth_flow(auth_flow) {}
149 UserContext::~UserContext() {
152 bool UserContext::operator==(const UserContext& context) const {
153 return context.username == username && context.password == password &&
154 context.key_label == key_label &&
155 context.need_password_hashing == need_password_hashing &&
156 context.auth_code == auth_code &&
157 context.username_hash == username_hash &&
158 context.using_oauth == using_oauth && context.auth_flow == auth_flow;
161 void UserContext::CopyFrom(const UserContext& other) {
162 username = other.username;
163 password = other.password;
164 key_label = other.key_label;
165 need_password_hashing = other.need_password_hashing;
166 auth_code = other.auth_code;
167 username_hash = other.username_hash;
168 using_oauth = other.using_oauth;
169 auth_flow = other.auth_flow;
172 std::string User::GetEmail() const {
173 return display_email();
176 base::string16 User::GetDisplayName() const {
177 // Fallback to the email account name in case display name haven't been set.
178 return display_name_.empty() ?
179 base::UTF8ToUTF16(GetAccountName(true)) :
180 display_name_;
183 base::string16 User::GetGivenName() const {
184 return given_name_;
187 const gfx::ImageSkia& User::GetImage() const {
188 return user_image_.image();
191 std::string User::GetUserID() const {
192 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
195 std::string User::GetAccountName(bool use_display_email) const {
196 if (use_display_email && !display_email_.empty())
197 return GetUserName(display_email_);
198 else
199 return GetUserName(email_);
202 bool User::HasDefaultImage() const {
203 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
206 bool User::CanSyncImage() const {
207 return false;
210 std::string User::display_email() const {
211 return display_email_;
214 bool User::can_lock() const {
215 return can_lock_;
218 std::string User::username_hash() const {
219 return username_hash_;
222 bool User::is_logged_in() const {
223 return is_logged_in_;
226 bool User::is_active() const {
227 return is_active_;
230 User* User::CreateRegularUser(const std::string& email) {
231 return new RegularUser(email);
234 User* User::CreateGuestUser() {
235 return new GuestUser;
238 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
239 return new KioskAppUser(kiosk_app_username);
242 User* User::CreateLocallyManagedUser(const std::string& username) {
243 return new LocallyManagedUser(username);
246 User* User::CreateRetailModeUser() {
247 return new RetailModeUser;
250 User* User::CreatePublicAccountUser(const std::string& email) {
251 return new PublicAccountUser(email);
254 User::User(const std::string& email)
255 : email_(email),
256 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
257 force_online_signin_(false),
258 image_index_(kInvalidImageIndex),
259 image_is_stub_(false),
260 image_is_loading_(false),
261 can_lock_(false),
262 is_logged_in_(false),
263 is_active_(false),
264 profile_is_created_(false) {
267 User::~User() {}
269 void User::SetAccountLocale(const std::string& resolved_account_locale) {
270 account_locale_.reset(new std::string(resolved_account_locale));
273 void User::SetImage(const UserImage& user_image, int image_index) {
274 user_image_ = user_image;
275 image_index_ = image_index;
276 image_is_stub_ = false;
277 image_is_loading_ = false;
278 DCHECK(HasDefaultImage() || user_image.has_raw_image());
281 void User::SetImageURL(const GURL& image_url) {
282 user_image_.set_url(image_url);
285 void User::SetStubImage(int image_index, bool is_loading) {
286 user_image_ = UserImage(
287 *ResourceBundle::GetSharedInstance().
288 GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING));
289 image_index_ = image_index;
290 image_is_stub_ = true;
291 image_is_loading_ = is_loading;
294 RegularUser::RegularUser(const std::string& email) : User(email) {
295 set_can_lock(true);
296 set_display_email(email);
299 RegularUser::~RegularUser() {}
301 User::UserType RegularUser::GetType() const {
302 return USER_TYPE_REGULAR;
305 bool RegularUser::CanSyncImage() const {
306 return true;
309 GuestUser::GuestUser() : User(UserManager::kGuestUserName) {
310 set_display_email(std::string());
313 GuestUser::~GuestUser() {}
315 User::UserType GuestUser::GetType() const {
316 return USER_TYPE_GUEST;
319 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
320 : User(kiosk_app_username) {
321 set_display_email(kiosk_app_username);
324 KioskAppUser::~KioskAppUser() {}
326 User::UserType KioskAppUser::GetType() const {
327 return USER_TYPE_KIOSK_APP;
330 LocallyManagedUser::LocallyManagedUser(const std::string& username)
331 : User(username) {
332 set_can_lock(true);
335 LocallyManagedUser::~LocallyManagedUser() {}
337 User::UserType LocallyManagedUser::GetType() const {
338 return USER_TYPE_LOCALLY_MANAGED;
341 std::string LocallyManagedUser::display_email() const {
342 return base::UTF16ToUTF8(display_name());
345 RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) {
346 set_display_email(std::string());
349 RetailModeUser::~RetailModeUser() {}
351 User::UserType RetailModeUser::GetType() const {
352 return USER_TYPE_RETAIL_MODE;
355 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
358 PublicAccountUser::~PublicAccountUser() {}
360 User::UserType PublicAccountUser::GetType() const {
361 return USER_TYPE_PUBLIC_ACCOUNT;
364 bool User::has_gaia_account() const {
365 COMPILE_ASSERT(NUM_USER_TYPES == 6, num_user_types_unexpected);
366 switch (GetType()) {
367 case USER_TYPE_REGULAR:
368 return true;
369 case USER_TYPE_GUEST:
370 case USER_TYPE_RETAIL_MODE:
371 case USER_TYPE_PUBLIC_ACCOUNT:
372 case USER_TYPE_LOCALLY_MANAGED:
373 case USER_TYPE_KIOSK_APP:
374 return false;
375 default:
376 NOTREACHED();
378 return false;
381 } // namespace chromeos