Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / user.cc
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 "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
15
16 namespace chromeos {
17
18 namespace {
19
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;
25   }
26   return email.substr(0, i);
27 }
28
29 }  // namespace
30
31 const int User::kExternalImageIndex;
32 const int User::kProfileImageIndex;
33 const int User::kInvalidImageIndex;
34
35 class RegularUser : public User {
36  public:
37   explicit RegularUser(const std::string& email);
38   virtual ~RegularUser();
39
40   // Overridden from User:
41   virtual UserType GetType() const OVERRIDE;
42   virtual bool CanSyncImage() const OVERRIDE;
43
44  private:
45   DISALLOW_COPY_AND_ASSIGN(RegularUser);
46 };
47
48 class GuestUser : public User {
49  public:
50   GuestUser();
51   virtual ~GuestUser();
52
53   // Overridden from User:
54   virtual UserType GetType() const OVERRIDE;
55
56  private:
57   DISALLOW_COPY_AND_ASSIGN(GuestUser);
58 };
59
60 class KioskAppUser : public User {
61  public:
62   explicit KioskAppUser(const std::string& app_id);
63   virtual ~KioskAppUser();
64
65   // Overridden from User:
66   virtual UserType GetType() const OVERRIDE;
67
68  private:
69   DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
70 };
71
72 class LocallyManagedUser : public User {
73  public:
74   explicit LocallyManagedUser(const std::string& username);
75   virtual ~LocallyManagedUser();
76
77   // Overridden from User:
78   virtual UserType GetType() const OVERRIDE;
79   virtual std::string display_email() const OVERRIDE;
80
81  private:
82   DISALLOW_COPY_AND_ASSIGN(LocallyManagedUser);
83 };
84
85 class RetailModeUser : public User {
86  public:
87   RetailModeUser();
88   virtual ~RetailModeUser();
89
90   // Overridden from User:
91   virtual UserType GetType() const OVERRIDE;
92
93  private:
94   DISALLOW_COPY_AND_ASSIGN(RetailModeUser);
95 };
96
97 class PublicAccountUser : public User {
98  public:
99   explicit PublicAccountUser(const std::string& email);
100   virtual ~PublicAccountUser();
101
102   // Overridden from User:
103   virtual UserType GetType() const OVERRIDE;
104
105  private:
106   DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
107 };
108
109 UserContext::UserContext() : using_oauth(true), auth_flow(AUTH_FLOW_OFFLINE) {
110 }
111
112 UserContext::UserContext(const std::string& username,
113                          const std::string& password,
114                          const std::string& auth_code)
115     : username(username),
116       password(password),
117       auth_code(auth_code),
118       using_oauth(true),
119       auth_flow(AUTH_FLOW_OFFLINE) {
120 }
121
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),
127       password(password),
128       auth_code(auth_code),
129       username_hash(username_hash),
130       using_oauth(true),
131       auth_flow(AUTH_FLOW_OFFLINE) {
132 }
133
134 UserContext::UserContext(const std::string& username,
135                          const std::string& password,
136                          const std::string& auth_code,
137                          const std::string& username_hash,
138                          bool using_oauth,
139                          AuthFlow auth_flow)
140     :  username(username),
141        password(password),
142        auth_code(auth_code),
143        username_hash(username_hash),
144        using_oauth(using_oauth),
145        auth_flow(auth_flow) {
146 }
147
148 UserContext::~UserContext() {
149 }
150
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;
158 }
159
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)) :
164       display_name_;
165 }
166
167 std::string User::GetAccountName(bool use_display_email) const {
168   if (use_display_email && !display_email_.empty())
169     return GetUserName(display_email_);
170   else
171     return GetUserName(email_);
172 }
173
174 bool User::HasDefaultImage() const {
175   return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
176 }
177
178 bool User::CanSyncImage() const {
179   return false;
180 }
181
182 std::string User::display_email() const {
183   return display_email_;
184 }
185
186 bool User::can_lock() const {
187   return can_lock_;
188 }
189
190 std::string User::username_hash() const {
191   return username_hash_;
192 }
193
194 bool User::is_logged_in() const {
195   return is_logged_in_;
196 }
197
198 bool User::is_active() const {
199   return is_active_;
200 }
201
202 User* User::CreateRegularUser(const std::string& email) {
203   return new RegularUser(email);
204 }
205
206 User* User::CreateGuestUser() {
207   return new GuestUser;
208 }
209
210 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) {
211   return new KioskAppUser(kiosk_app_username);
212 }
213
214 User* User::CreateLocallyManagedUser(const std::string& username) {
215   return new LocallyManagedUser(username);
216 }
217
218 User* User::CreateRetailModeUser() {
219   return new RetailModeUser;
220 }
221
222 User* User::CreatePublicAccountUser(const std::string& email) {
223   return new PublicAccountUser(email);
224 }
225
226 User::User(const std::string& email)
227     : email_(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),
233       can_lock_(false),
234       is_logged_in_(false),
235       is_active_(false),
236       profile_is_created_(false) {
237 }
238
239 User::~User() {}
240
241 void User::SetAccountLocale(const std::string& resolved_account_locale) {
242   account_locale_.reset(new std::string(resolved_account_locale));
243 }
244
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());
251 }
252
253 void User::SetImageURL(const GURL& image_url) {
254   user_image_.set_url(image_url);
255 }
256
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;
264 }
265
266 RegularUser::RegularUser(const std::string& email) : User(email) {
267   set_can_lock(true);
268   set_display_email(email);
269 }
270
271 RegularUser::~RegularUser() {}
272
273 User::UserType RegularUser::GetType() const {
274   return USER_TYPE_REGULAR;
275 }
276
277 bool RegularUser::CanSyncImage() const {
278   return true;
279 }
280
281 GuestUser::GuestUser() : User(UserManager::kGuestUserName) {
282   set_display_email(std::string());
283 }
284
285 GuestUser::~GuestUser() {}
286
287 User::UserType GuestUser::GetType() const {
288   return USER_TYPE_GUEST;
289 }
290
291 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username)
292     : User(kiosk_app_username) {
293   set_display_email(std::string());
294 }
295
296 KioskAppUser::~KioskAppUser() {}
297
298 User::UserType KioskAppUser::GetType() const {
299   return USER_TYPE_KIOSK_APP;
300 }
301
302 LocallyManagedUser::LocallyManagedUser(const std::string& username)
303     : User(username) {
304   set_can_lock(true);
305 }
306
307 LocallyManagedUser::~LocallyManagedUser() {}
308
309 User::UserType LocallyManagedUser::GetType() const {
310   return USER_TYPE_LOCALLY_MANAGED;
311 }
312
313 std::string LocallyManagedUser::display_email() const {
314   return base::UTF16ToUTF8(display_name());
315 }
316
317 RetailModeUser::RetailModeUser() : User(UserManager::kRetailModeUserName) {
318   set_display_email(std::string());
319 }
320
321 RetailModeUser::~RetailModeUser() {}
322
323 User::UserType RetailModeUser::GetType() const {
324   return USER_TYPE_RETAIL_MODE;
325 }
326
327 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) {
328 }
329
330 PublicAccountUser::~PublicAccountUser() {}
331
332 User::UserType PublicAccountUser::GetType() const {
333   return USER_TYPE_PUBLIC_ACCOUNT;
334 }
335
336 bool User::has_gaia_account() const {
337   COMPILE_ASSERT(NUM_USER_TYPES == 6, num_user_types_unexpected);
338   switch (GetType()) {
339     case USER_TYPE_REGULAR:
340       return true;
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:
346       return false;
347     default:
348       NOTREACHED();
349   }
350   return false;
351 }
352
353 }  // namespace chromeos