OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/login/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 "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" | |
16 | |
17 namespace chromeos { | |
18 | |
19 namespace { | |
20 | |
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; | |
26 } | |
27 return email.substr(0, i); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 const int User::kExternalImageIndex; | |
33 const int User::kProfileImageIndex; | |
34 const int User::kInvalidImageIndex; | |
35 | |
36 class RegularUser : public User { | |
37 public: | |
38 explicit RegularUser(const std::string& email); | |
39 virtual ~RegularUser(); | |
40 | |
41 // Overridden from User: | |
42 virtual UserType GetType() const OVERRIDE; | |
43 virtual bool CanSyncImage() const OVERRIDE; | |
44 | |
45 private: | |
46 DISALLOW_COPY_AND_ASSIGN(RegularUser); | |
47 }; | |
48 | |
49 class GuestUser : public User { | |
50 public: | |
51 GuestUser(); | |
52 virtual ~GuestUser(); | |
53 | |
54 // Overridden from User: | |
55 virtual UserType GetType() const OVERRIDE; | |
56 | |
57 private: | |
58 DISALLOW_COPY_AND_ASSIGN(GuestUser); | |
59 }; | |
60 | |
61 class KioskAppUser : public User { | |
62 public: | |
63 explicit KioskAppUser(const std::string& app_id); | |
64 virtual ~KioskAppUser(); | |
65 | |
66 // Overridden from User: | |
67 virtual UserType GetType() const OVERRIDE; | |
68 | |
69 private: | |
70 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); | |
71 }; | |
72 | |
73 class LocallyManagedUser : public User { | |
74 public: | |
75 explicit LocallyManagedUser(const std::string& username); | |
76 virtual ~LocallyManagedUser(); | |
77 | |
78 // Overridden from User: | |
79 virtual UserType GetType() const OVERRIDE; | |
80 virtual std::string display_email() const OVERRIDE; | |
81 | |
82 private: | |
83 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser); | |
84 }; | |
85 | |
86 class RetailModeUser : public User { | |
87 public: | |
88 RetailModeUser(); | |
89 virtual ~RetailModeUser(); | |
90 | |
91 // Overridden from User: | |
92 virtual UserType GetType() const OVERRIDE; | |
93 | |
94 private: | |
95 DISALLOW_COPY_AND_ASSIGN(RetailModeUser); | |
96 }; | |
97 | |
98 class PublicAccountUser : public User { | |
99 public: | |
100 explicit PublicAccountUser(const std::string& email); | |
101 virtual ~PublicAccountUser(); | |
102 | |
103 // Overridden from User: | |
104 virtual UserType GetType() const OVERRIDE; | |
105 | |
106 private: | |
107 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); | |
108 }; | |
109 | |
110 UserContext::UserContext() : is_using_oauth_(true), | |
111 auth_flow_(AUTH_FLOW_OFFLINE) { | |
112 } | |
113 | |
114 UserContext::UserContext(const std::string& user_id, | |
115 const std::string& password, | |
116 const std::string& auth_code) | |
117 : user_id_(user_id), | |
118 password_(password), | |
119 does_need_password_hashing_(true), | |
120 auth_code_(auth_code), | |
121 is_using_oauth_(true), | |
122 auth_flow_(AUTH_FLOW_OFFLINE) { | |
123 } | |
124 | |
125 UserContext::UserContext(const std::string& user_id, | |
126 const std::string& password, | |
127 const std::string& auth_code, | |
128 const std::string& user_id_hash) | |
129 : user_id_(user_id), | |
130 password_(password), | |
131 does_need_password_hashing_(true), | |
132 auth_code_(auth_code), | |
133 user_id_hash_(user_id_hash), | |
134 is_using_oauth_(true), | |
135 auth_flow_(AUTH_FLOW_OFFLINE) { | |
136 } | |
137 | |
138 UserContext::UserContext(const std::string& user_id, | |
139 const std::string& password, | |
140 const std::string& auth_code, | |
141 const std::string& user_id_hash, | |
142 bool is_using_oauth, | |
143 AuthFlow auth_flow) | |
144 : user_id_(user_id), | |
145 password_(password), | |
146 does_need_password_hashing_(true), | |
147 auth_code_(auth_code), | |
148 user_id_hash_(user_id_hash), | |
149 is_using_oauth_(is_using_oauth), | |
150 auth_flow_(auth_flow) { | |
151 } | |
152 | |
153 UserContext::~UserContext() { | |
154 } | |
155 | |
156 bool UserContext::operator==(const UserContext& context) const { | |
157 return context.user_id_ == user_id_ && | |
158 context.password_ == password_ && | |
159 context.does_need_password_hashing_ == does_need_password_hashing_ && | |
160 context.key_label_ == key_label_ && | |
161 context.auth_code_ == auth_code_ && | |
162 context.user_id_hash_ == user_id_hash_ && | |
163 context.is_using_oauth_ == is_using_oauth_ && | |
164 context.auth_flow_ == auth_flow_; | |
165 } | |
166 | |
167 void UserContext::CopyFrom(const UserContext& other) { | |
168 user_id_ = other.user_id_; | |
169 password_ = other.password_; | |
170 does_need_password_hashing_ = other.does_need_password_hashing_; | |
171 key_label_ = other.key_label_; | |
172 auth_code_ = other.auth_code_; | |
173 user_id_hash_ = other.user_id_hash_; | |
174 is_using_oauth_ = other.is_using_oauth_; | |
175 auth_flow_ = other.auth_flow_; | |
176 } | |
177 | |
178 const std::string& UserContext::GetUserID() const { | |
179 return user_id_; | |
180 } | |
181 | |
182 const std::string& UserContext::GetPassword() const { | |
183 return password_; | |
184 } | |
185 | |
186 bool UserContext::DoesNeedPasswordHashing() const { | |
187 return does_need_password_hashing_; | |
188 } | |
189 | |
190 const std::string& UserContext::GetKeyLabel() const { | |
191 return key_label_; | |
192 } | |
193 | |
194 const std::string& UserContext::GetAuthCode() const { | |
195 return auth_code_; | |
196 } | |
197 | |
198 const std::string& UserContext::GetUserIDHash() const { | |
199 return user_id_hash_; | |
200 } | |
201 | |
202 bool UserContext::IsUsingOAuth() const { | |
203 return is_using_oauth_; | |
204 } | |
205 | |
206 UserContext::AuthFlow UserContext::GetAuthFlow() const { | |
207 return auth_flow_; | |
208 } | |
209 | |
210 bool UserContext::HasCredentials() const { | |
211 return (!user_id_.empty() && !password_.empty()) || !auth_code_.empty(); | |
212 } | |
213 | |
214 void UserContext::SetUserID(const std::string& user_id) { | |
215 user_id_ = user_id; | |
216 } | |
217 | |
218 void UserContext::SetPassword(const std::string& password) { | |
219 password_ = password; | |
220 } | |
221 | |
222 void UserContext::SetDoesNeedPasswordHashing(bool does_need_password_hashing) { | |
223 does_need_password_hashing_ = does_need_password_hashing; | |
224 } | |
225 | |
226 void UserContext::SetKeyLabel(const std::string& key_label) { | |
227 key_label_ = key_label; | |
228 } | |
229 | |
230 void UserContext::SetAuthCode(const std::string& auth_code) { | |
231 auth_code_ = auth_code; | |
232 } | |
233 | |
234 void UserContext::SetUserIDHash(const std::string& user_id_hash) { | |
235 user_id_hash_ = user_id_hash; | |
236 } | |
237 | |
238 void UserContext::SetIsUsingOAuth(bool is_using_oauth) { | |
239 is_using_oauth_ = is_using_oauth; | |
240 } | |
241 | |
242 std::string User::GetEmail() const { | |
243 return display_email(); | |
244 } | |
245 | |
246 base::string16 User::GetDisplayName() const { | |
247 // Fallback to the email account name in case display name haven't been set. | |
248 return display_name_.empty() ? | |
249 base::UTF8ToUTF16(GetAccountName(true)) : | |
250 display_name_; | |
251 } | |
252 | |
253 base::string16 User::GetGivenName() const { | |
254 return given_name_; | |
255 } | |
256 | |
257 const gfx::ImageSkia& User::GetImage() const { | |
258 return user_image_.image(); | |
259 } | |
260 | |
261 std::string User::GetUserID() const { | |
262 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); | |
263 } | |
264 | |
265 std::string User::GetAccountName(bool use_display_email) const { | |
266 if (use_display_email && !display_email_.empty()) | |
267 return GetUserName(display_email_); | |
268 else | |
269 return GetUserName(email_); | |
270 } | |
271 | |
272 bool User::HasDefaultImage() const { | |
273 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; | |
274 } | |
275 | |
276 bool User::CanSyncImage() const { | |
277 return false; | |
278 } | |
279 | |
280 std::string User::display_email() const { | |
281 return display_email_; | |
282 } | |
283 | |
284 bool User::can_lock() const { | |
285 return can_lock_; | |
286 } | |
287 | |
288 std::string User::username_hash() const { | |
289 return username_hash_; | |
290 } | |
291 | |
292 bool User::is_logged_in() const { | |
293 return is_logged_in_; | |
294 } | |
295 | |
296 bool User::is_active() const { | |
297 return is_active_; | |
298 } | |
299 | |
300 User* User::CreateRegularUser(const std::string& email) { | |
301 return new RegularUser(email); | |
302 } | |
303 | |
304 User* User::CreateGuestUser() { | |
305 return new GuestUser; | |
306 } | |
307 | |
308 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) { | |
309 return new KioskAppUser(kiosk_app_username); | |
310 } | |
311 | |
312 User* User::CreateLocallyManagedUser(const std::string& username) { | |
313 return new LocallyManagedUser(username); | |
314 } | |
315 | |
316 User* User::CreateRetailModeUser() { | |
317 return new RetailModeUser; | |
318 } | |
319 | |
320 User* User::CreatePublicAccountUser(const std::string& email) { | |
321 return new PublicAccountUser(email); | |
322 } | |
323 | |
324 User::User(const std::string& email) | |
325 : email_(email), | |
326 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), | |
327 force_online_signin_(false), | |
328 image_index_(kInvalidImageIndex), | |
329 image_is_stub_(false), | |
330 image_is_loading_(false), | |
331 can_lock_(false), | |
332 is_logged_in_(false), | |
333 is_active_(false), | |
334 profile_is_created_(false) { | |
335 } | |
336 | |
337 User::~User() {} | |
338 | |
339 void User::SetAccountLocale(const std::string& resolved_account_locale) { | |
340 account_locale_.reset(new std::string(resolved_account_locale)); | |
341 } | |
342 | |
343 void User::SetImage(const UserImage& user_image, int image_index) { | |
344 user_image_ = user_image; | |
345 image_index_ = image_index; | |
346 image_is_stub_ = false; | |
347 image_is_loading_ = false; | |
348 DCHECK(HasDefaultImage() || user_image.has_raw_image()); | |
349 } | |
350 | |
351 void User::SetImageURL(const GURL& image_url) { | |
352 user_image_.set_url(image_url); | |
353 } | |
354 | |
355 void User::SetStubImage(int image_index, bool is_loading) { | |
356 user_image_ = UserImage( | |
357 *ResourceBundle::GetSharedInstance(). | |
358 GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING)); | |
359 image_index_ = image_index; | |
360 image_is_stub_ = true; | |
361 image_is_loading_ = is_loading; | |
362 } | |
363 | |
364 RegularUser::RegularUser(const std::string& email) : User(email) { | |
365 set_can_lock(true); | |
366 set_display_email(email); | |
367 } | |
368 | |
369 RegularUser::~RegularUser() {} | |
370 | |
371 User::UserType RegularUser::GetType() const { | |
372 return USER_TYPE_REGULAR; | |
373 } | |
374 | |
375 bool RegularUser::CanSyncImage() const { | |
376 return true; | |
377 } | |
378 | |
379 GuestUser::GuestUser() : User(UserManager::kGuestUserName) { | |
380 set_display_email(std::string()); | |
381 } | |
382 | |
383 GuestUser::~GuestUser() {} | |
384 | |
385 User::UserType GuestUser::GetType() const { | |
386 return USER_TYPE_GUEST; | |
387 } | |
388 | |
389 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) | |
390 : User(kiosk_app_username) { | |
391 set_display_email(kiosk_app_username); | |
392 } | |
393 | |
394 KioskAppUser::~KioskAppUser() {} | |
395 | |
396 User::UserType KioskAppUser::GetType() const { | |
397 return USER_TYPE_KIOSK_APP; | |
398 } | |
399 | |
400 LocallyManagedUser::LocallyManagedUser(const std::string& username) | |
401 : User(username) { | |
402 set_can_lock(true); | |
403 } | |
404 | |
405 LocallyManagedUser::~LocallyManagedUser() {} | |
406 | |
407 User::UserType LocallyManagedUser::GetType() const { | |
408 return USER_TYPE_LOCALLY_MANAGED; | |
409 } | |
410 | |
411 std::string LocallyManagedUser::display_email() const { | |
412 return base::UTF16ToUTF8(display_name()); | |
413 } | |
414 | |
415 RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) { | |
416 set_display_email(std::string()); | |
417 } | |
418 | |
419 RetailModeUser::~RetailModeUser() {} | |
420 | |
421 User::UserType RetailModeUser::GetType() const { | |
422 return USER_TYPE_RETAIL_MODE; | |
423 } | |
424 | |
425 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { | |
426 } | |
427 | |
428 PublicAccountUser::~PublicAccountUser() {} | |
429 | |
430 User::UserType PublicAccountUser::GetType() const { | |
431 return USER_TYPE_PUBLIC_ACCOUNT; | |
432 } | |
433 | |
434 bool User::has_gaia_account() const { | |
435 COMPILE_ASSERT(NUM_USER_TYPES == 6, num_user_types_unexpected); | |
436 switch (GetType()) { | |
437 case USER_TYPE_REGULAR: | |
438 return true; | |
439 case USER_TYPE_GUEST: | |
440 case USER_TYPE_RETAIL_MODE: | |
441 case USER_TYPE_PUBLIC_ACCOUNT: | |
442 case USER_TYPE_LOCALLY_MANAGED: | |
443 case USER_TYPE_KIOSK_APP: | |
444 return false; | |
445 default: | |
446 NOTREACHED(); | |
447 } | |
448 return false; | |
449 } | |
450 | |
451 } // namespace chromeos | |
OLD | NEW |