Don't preload rarely seen large images
[chromium-blink-merge.git] / components / user_manager / user.cc
blob6a7dec1ab8b6fb23c7f30bbfc6d94bbc1821af24
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 // static
32 bool User::TypeHasGaiaAccount(UserType user_type) {
33 return user_type == USER_TYPE_REGULAR ||
34 user_type == USER_TYPE_CHILD;
37 // Also used for regular supervised users.
38 class RegularUser : public User {
39 public:
40 explicit RegularUser(const std::string& email);
41 ~RegularUser() override;
43 // Overridden from User:
44 UserType GetType() const override;
45 bool CanSyncImage() const override;
46 void SetIsChild(bool is_child) override;
48 private:
49 bool is_child_;
51 DISALLOW_COPY_AND_ASSIGN(RegularUser);
54 class GuestUser : public User {
55 public:
56 GuestUser();
57 ~GuestUser() override;
59 // Overridden from User:
60 UserType GetType() const override;
62 private:
63 DISALLOW_COPY_AND_ASSIGN(GuestUser);
66 class KioskAppUser : public User {
67 public:
68 explicit KioskAppUser(const std::string& app_id);
69 ~KioskAppUser() override;
71 // Overridden from User:
72 UserType GetType() const override;
74 private:
75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
78 class SupervisedUser : public User {
79 public:
80 explicit SupervisedUser(const std::string& username);
81 ~SupervisedUser() override;
83 // Overridden from User:
84 UserType GetType() const override;
85 std::string display_email() const override;
87 private:
88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser);
91 class PublicAccountUser : public User {
92 public:
93 explicit PublicAccountUser(const std::string& email);
94 ~PublicAccountUser() override;
96 // Overridden from User:
97 UserType GetType() const override;
99 private:
100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
103 std::string User::GetEmail() const {
104 return display_email();
107 base::string16 User::GetDisplayName() const {
108 // Fallback to the email account name in case display name haven't been set.
109 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true))
110 : display_name_;
113 base::string16 User::GetGivenName() const {
114 return given_name_;
117 const gfx::ImageSkia& User::GetImage() const {
118 return user_image_.image();
121 UserID User::GetUserID() const {
122 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
125 void User::SetIsChild(bool is_child) {
126 VLOG(1) << "Ignoring SetIsChild call with param " << is_child;
127 if (is_child) {
128 NOTREACHED() << "Calling SetIsChild(true) for base User class."
129 << "Base class cannot be set as child";
133 bool User::HasGaiaAccount() const {
134 return TypeHasGaiaAccount(GetType());
137 bool User::IsSupervised() const {
138 UserType type = GetType();
139 return type == USER_TYPE_SUPERVISED ||
140 type == USER_TYPE_CHILD;
143 std::string User::GetAccountName(bool use_display_email) const {
144 if (use_display_email && !display_email_.empty())
145 return GetUserName(display_email_);
146 else
147 return GetUserName(email_);
150 bool User::HasDefaultImage() const {
151 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
154 bool User::CanSyncImage() const {
155 return false;
158 std::string User::display_email() const {
159 return display_email_;
162 bool User::can_lock() const {
163 return can_lock_;
166 std::string User::username_hash() const {
167 return username_hash_;
170 bool User::is_logged_in() const {
171 return is_logged_in_;
174 bool User::is_active() const {
175 return is_active_;
178 User* User::CreateRegularUser(const std::string& email) {
179 return new RegularUser(email);
182 User* User::CreateGuestUser() {
183 return new GuestUser;
186 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
187 return new KioskAppUser(kiosk_app_username);
190 User* User::CreateSupervisedUser(const std::string& username) {
191 return new SupervisedUser(username);
194 User* User::CreatePublicAccountUser(const std::string& email) {
195 return new PublicAccountUser(email);
198 User::User(const std::string& email)
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) {
211 User::~User() {
214 void User::SetAccountLocale(const std::string& resolved_account_locale) {
215 account_locale_.reset(new std::string(resolved_account_locale));
218 void User::SetImage(const UserImage& user_image, int image_index) {
219 user_image_ = user_image;
220 image_index_ = image_index;
221 image_is_stub_ = false;
222 image_is_loading_ = false;
223 DCHECK(HasDefaultImage() || user_image.has_raw_image());
226 void User::SetImageURL(const GURL& image_url) {
227 user_image_.set_url(image_url);
230 void User::SetStubImage(const UserImage& stub_user_image,
231 int image_index,
232 bool is_loading) {
233 user_image_ = stub_user_image;
234 image_index_ = image_index;
235 image_is_stub_ = true;
236 image_is_loading_ = is_loading;
239 RegularUser::RegularUser(const std::string& email)
240 : User(email), is_child_(false) {
241 set_can_lock(true);
242 set_display_email(email);
245 RegularUser::~RegularUser() {
248 UserType RegularUser::GetType() const {
249 return is_child_ ? user_manager::USER_TYPE_CHILD :
250 user_manager::USER_TYPE_REGULAR;
253 bool RegularUser::CanSyncImage() const {
254 return true;
257 void RegularUser::SetIsChild(bool is_child) {
258 VLOG(1) << "Setting user is child to " << is_child;
259 is_child_ = is_child;
262 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) {
263 set_display_email(std::string());
266 GuestUser::~GuestUser() {
269 UserType GuestUser::GetType() const {
270 return user_manager::USER_TYPE_GUEST;
273 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
274 : User(kiosk_app_username) {
275 set_display_email(kiosk_app_username);
278 KioskAppUser::~KioskAppUser() {
281 UserType KioskAppUser::GetType() const {
282 return user_manager::USER_TYPE_KIOSK_APP;
285 SupervisedUser::SupervisedUser(const std::string& username) : User(username) {
286 set_can_lock(true);
289 SupervisedUser::~SupervisedUser() {
292 UserType SupervisedUser::GetType() const {
293 return user_manager::USER_TYPE_SUPERVISED;
296 std::string SupervisedUser::display_email() const {
297 return base::UTF16ToUTF8(display_name());
300 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
303 PublicAccountUser::~PublicAccountUser() {
306 UserType PublicAccountUser::GetType() const {
307 return user_manager::USER_TYPE_PUBLIC_ACCOUNT;
310 bool User::has_gaia_account() const {
311 static_assert(user_manager::NUM_USER_TYPES == 7,
312 "NUM_USER_TYPES should equal 7");
313 switch (GetType()) {
314 case user_manager::USER_TYPE_REGULAR:
315 case user_manager::USER_TYPE_CHILD:
316 return true;
317 case user_manager::USER_TYPE_GUEST:
318 case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
319 case user_manager::USER_TYPE_SUPERVISED:
320 case user_manager::USER_TYPE_KIOSK_APP:
321 return false;
322 default:
323 NOTREACHED();
325 return false;
328 } // namespace user_manager