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.
4
5 #include "components/user_manager/user.h"
6
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"
15
16 namespace user_manager {
17
18 namespace {
19
20 // Returns account name portion of an email.
GetUserName(const std::string & 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;
25 }
26 return email.substr(0, i);
27 }
28
29 } // namespace
30
IsSupervised() const31 bool User::IsSupervised() const {
32 return false;
33 }
34
SetIsSupervised(bool is_supervised)35 void User::SetIsSupervised(bool is_supervised) {
36 VLOG(1) << "Ignoring SetIsSupervised call with param " << is_supervised;
37 }
38
39 class RegularUser : public User {
40 public:
41 explicit RegularUser(const std::string& email);
42 virtual ~RegularUser();
43
44 // Overridden from User:
45 virtual UserType GetType() const OVERRIDE;
46 virtual bool CanSyncImage() const OVERRIDE;
SetIsSupervised(bool is_supervised)47 virtual void SetIsSupervised(bool is_supervised) OVERRIDE {
48 VLOG(1) << "Setting user is supervised to " << is_supervised;
49 is_supervised_ = is_supervised;
50 }
IsSupervised() const51 virtual bool IsSupervised() const OVERRIDE {
52 return is_supervised_;
53 }
54
55 private:
56 bool is_supervised_;
57
58 DISALLOW_COPY_AND_ASSIGN(RegularUser);
59 };
60
61 class GuestUser : public User {
62 public:
63 GuestUser();
64 virtual ~GuestUser();
65
66 // Overridden from User:
67 virtual UserType GetType() const OVERRIDE;
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(GuestUser);
71 };
72
73 class KioskAppUser : public User {
74 public:
75 explicit KioskAppUser(const std::string& app_id);
76 virtual ~KioskAppUser();
77
78 // Overridden from User:
79 virtual UserType GetType() const OVERRIDE;
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
83 };
84
85 class SupervisedUser : public User {
86 public:
87 explicit SupervisedUser(const std::string& username);
88 virtual ~SupervisedUser();
89
90 // Overridden from User:
91 virtual UserType GetType() const OVERRIDE;
92 virtual bool IsSupervised() const OVERRIDE;
93 virtual std::string display_email() const OVERRIDE;
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(SupervisedUser);
97 };
98
99 class RetailModeUser : public User {
100 public:
101 RetailModeUser();
102 virtual ~RetailModeUser();
103
104 // Overridden from User:
105 virtual UserType GetType() const OVERRIDE;
106
107 private:
108 DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
109 };
110
111 class PublicAccountUser : public User {
112 public:
113 explicit PublicAccountUser(const std::string& email);
114 virtual ~PublicAccountUser();
115
116 // Overridden from User:
117 virtual UserType GetType() const OVERRIDE;
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
121 };
122
GetEmail() const123 std::string User::GetEmail() const {
124 return display_email();
125 }
126
GetDisplayName() const127 base::string16 User::GetDisplayName() const {
128 // Fallback to the email account name in case display name haven't been set.
129 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true))
130 : display_name_;
131 }
132
GetGivenName() const133 base::string16 User::GetGivenName() const {
134 return given_name_;
135 }
136
GetImage() const137 const gfx::ImageSkia& User::GetImage() const {
138 return user_image_.image();
139 }
140
GetUserID() const141 std::string User::GetUserID() const {
142 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email()));
143 }
144
GetAccountName(bool use_display_email) const145 std::string User::GetAccountName(bool use_display_email) const {
146 if (use_display_email && !display_email_.empty())
147 return GetUserName(display_email_);
148 else
149 return GetUserName(email_);
150 }
151
HasDefaultImage() const152 bool User::HasDefaultImage() const {
153 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
154 }
155
CanSyncImage() const156 bool User::CanSyncImage() const {
157 return false;
158 }
159
display_email() const160 std::string User::display_email() const {
161 return display_email_;
162 }
163
can_lock() const164 bool User::can_lock() const {
165 return can_lock_;
166 }
167
username_hash() const168 std::string User::username_hash() const {
169 return username_hash_;
170 }
171
is_logged_in() const172 bool User::is_logged_in() const {
173 return is_logged_in_;
174 }
175
is_active() const176 bool User::is_active() const {
177 return is_active_;
178 }
179
CreateRegularUser(const std::string & email)180 User* User::CreateRegularUser(const std::string& email) {
181 return new RegularUser(email);
182 }
183
CreateGuestUser()184 User* User::CreateGuestUser() {
185 return new GuestUser;
186 }
187
CreateKioskAppUser(const std::string & kiosk_app_username)188 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
189 return new KioskAppUser(kiosk_app_username);
190 }
191
CreateSupervisedUser(const std::string & username)192 User* User::CreateSupervisedUser(const std::string& username) {
193 return new SupervisedUser(username);
194 }
195
CreateRetailModeUser()196 User* User::CreateRetailModeUser() {
197 return new RetailModeUser;
198 }
199
CreatePublicAccountUser(const std::string & email)200 User* User::CreatePublicAccountUser(const std::string& email) {
201 return new PublicAccountUser(email);
202 }
203
User(const std::string & email)204 User::User(const std::string& email)
205 : email_(email),
206 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
207 force_online_signin_(false),
208 image_index_(USER_IMAGE_INVALID),
209 image_is_stub_(false),
210 image_is_loading_(false),
211 can_lock_(false),
212 is_logged_in_(false),
213 is_active_(false),
214 profile_is_created_(false) {
215 }
216
~User()217 User::~User() {
218 }
219
SetAccountLocale(const std::string & resolved_account_locale)220 void User::SetAccountLocale(const std::string& resolved_account_locale) {
221 account_locale_.reset(new std::string(resolved_account_locale));
222 }
223
SetImage(const UserImage & user_image,int image_index)224 void User::SetImage(const UserImage& user_image, int image_index) {
225 user_image_ = user_image;
226 image_index_ = image_index;
227 image_is_stub_ = false;
228 image_is_loading_ = false;
229 DCHECK(HasDefaultImage() || user_image.has_raw_image());
230 }
231
SetImageURL(const GURL & image_url)232 void User::SetImageURL(const GURL& image_url) {
233 user_image_.set_url(image_url);
234 }
235
SetStubImage(const UserImage & stub_user_image,int image_index,bool is_loading)236 void User::SetStubImage(const UserImage& stub_user_image,
237 int image_index,
238 bool is_loading) {
239 user_image_ = stub_user_image;
240 image_index_ = image_index;
241 image_is_stub_ = true;
242 image_is_loading_ = is_loading;
243 }
244
RegularUser(const std::string & email)245 RegularUser::RegularUser(const std::string& email)
246 : User(email), is_supervised_(false) {
247 set_can_lock(true);
248 set_display_email(email);
249 }
250
~RegularUser()251 RegularUser::~RegularUser() {
252 }
253
GetType() const254 UserType RegularUser::GetType() const {
255 return user_manager::USER_TYPE_REGULAR;
256 }
257
CanSyncImage() const258 bool RegularUser::CanSyncImage() const {
259 return true;
260 }
261
GuestUser()262 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) {
263 set_display_email(std::string());
264 }
265
~GuestUser()266 GuestUser::~GuestUser() {
267 }
268
GetType() const269 UserType GuestUser::GetType() const {
270 return user_manager::USER_TYPE_GUEST;
271 }
272
KioskAppUser(const std::string & kiosk_app_username)273 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
274 : User(kiosk_app_username) {
275 set_display_email(kiosk_app_username);
276 }
277
~KioskAppUser()278 KioskAppUser::~KioskAppUser() {
279 }
280
GetType() const281 UserType KioskAppUser::GetType() const {
282 return user_manager::USER_TYPE_KIOSK_APP;
283 }
284
SupervisedUser(const std::string & username)285 SupervisedUser::SupervisedUser(const std::string& username) : User(username) {
286 set_can_lock(true);
287 }
288
~SupervisedUser()289 SupervisedUser::~SupervisedUser() {
290 }
291
GetType() const292 UserType SupervisedUser::GetType() const {
293 return user_manager::USER_TYPE_SUPERVISED;
294 }
295
display_email() const296 std::string SupervisedUser::display_email() const {
297 return base::UTF16ToUTF8(display_name());
298 }
299
IsSupervised() const300 bool SupervisedUser::IsSupervised() const {
301 return true;
302 }
303
RetailModeUser()304 RetailModeUser::RetailModeUser() : User(chromeos::login::kRetailModeUserName) {
305 set_display_email(std::string());
306 }
307
~RetailModeUser()308 RetailModeUser::~RetailModeUser() {
309 }
310
GetType() const311 UserType RetailModeUser::GetType() const {
312 return user_manager::USER_TYPE_RETAIL_MODE;
313 }
314
PublicAccountUser(const std::string & email)315 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
316 }
317
~PublicAccountUser()318 PublicAccountUser::~PublicAccountUser() {
319 }
320
GetType() const321 UserType PublicAccountUser::GetType() const {
322 return user_manager::USER_TYPE_PUBLIC_ACCOUNT;
323 }
324
has_gaia_account() const325 bool User::has_gaia_account() const {
326 COMPILE_ASSERT(user_manager::NUM_USER_TYPES == 6, num_user_types_unexpected);
327 switch (GetType()) {
328 case user_manager::USER_TYPE_REGULAR:
329 return true;
330 case user_manager::USER_TYPE_GUEST:
331 case user_manager::USER_TYPE_RETAIL_MODE:
332 case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
333 case user_manager::USER_TYPE_SUPERVISED:
334 case user_manager::USER_TYPE_KIOSK_APP:
335 return false;
336 default:
337 NOTREACHED();
338 }
339 return false;
340 }
341
342 } // namespace user_manager
343