Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: components/user_manager/user.cc

Issue 1165323004: We should use UserID object to identify users instead of username. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/user_manager/user.h" 5 #include "components/user_manager/user.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/thread_restrictions.h" 10 #include "base/threading/thread_restrictions.h"
(...skipping 19 matching lines...) Expand all
30 30
31 // static 31 // static
32 bool User::TypeHasGaiaAccount(UserType user_type) { 32 bool User::TypeHasGaiaAccount(UserType user_type) {
33 return user_type == USER_TYPE_REGULAR || 33 return user_type == USER_TYPE_REGULAR ||
34 user_type == USER_TYPE_CHILD; 34 user_type == USER_TYPE_CHILD;
35 } 35 }
36 36
37 // Also used for regular supervised users. 37 // Also used for regular supervised users.
38 class RegularUser : public User { 38 class RegularUser : public User {
39 public: 39 public:
40 explicit RegularUser(const std::string& email); 40 explicit RegularUser(const UserID& user_id);
41 ~RegularUser() override; 41 ~RegularUser() override;
42 42
43 // Overridden from User: 43 // Overridden from User:
44 UserType GetType() const override; 44 UserType GetType() const override;
45 bool CanSyncImage() const override; 45 bool CanSyncImage() const override;
46 void SetIsChild(bool is_child) override; 46 void SetIsChild(bool is_child) override;
47 47
48 private: 48 private:
49 bool is_child_; 49 bool is_child_;
50 50
51 DISALLOW_COPY_AND_ASSIGN(RegularUser); 51 DISALLOW_COPY_AND_ASSIGN(RegularUser);
52 }; 52 };
53 53
54 class GuestUser : public User { 54 class GuestUser : public User {
55 public: 55 public:
56 GuestUser(); 56 GuestUser();
57 ~GuestUser() override; 57 ~GuestUser() override;
58 58
59 // Overridden from User: 59 // Overridden from User:
60 UserType GetType() const override; 60 UserType GetType() const override;
61 61
62 private: 62 private:
63 DISALLOW_COPY_AND_ASSIGN(GuestUser); 63 DISALLOW_COPY_AND_ASSIGN(GuestUser);
64 }; 64 };
65 65
66 class KioskAppUser : public User { 66 class KioskAppUser : public User {
67 public: 67 public:
68 explicit KioskAppUser(const std::string& app_id); 68 explicit KioskAppUser(const UserID& app_id);
69 ~KioskAppUser() override; 69 ~KioskAppUser() override;
70 70
71 // Overridden from User: 71 // Overridden from User:
72 UserType GetType() const override; 72 UserType GetType() const override;
73 73
74 private: 74 private:
75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); 75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
76 }; 76 };
77 77
78 class SupervisedUser : public User { 78 class SupervisedUser : public User {
79 public: 79 public:
80 explicit SupervisedUser(const std::string& username); 80 explicit SupervisedUser(const UserID& user_id);
81 ~SupervisedUser() override; 81 ~SupervisedUser() override;
82 82
83 // Overridden from User: 83 // Overridden from User:
84 UserType GetType() const override; 84 UserType GetType() const override;
85 std::string display_email() const override; 85 std::string display_email() const override;
86 86
87 private: 87 private:
88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); 88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser);
89 }; 89 };
90 90
91 class PublicAccountUser : public User { 91 class PublicAccountUser : public User {
92 public: 92 public:
93 explicit PublicAccountUser(const std::string& email); 93 explicit PublicAccountUser(const UserID& user_id);
94 ~PublicAccountUser() override; 94 ~PublicAccountUser() override;
95 95
96 // Overridden from User: 96 // Overridden from User:
97 UserType GetType() const override; 97 UserType GetType() const override;
98 98
99 private: 99 private:
100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); 100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
101 }; 101 };
102 102
103 std::string User::GetEmail() const { 103 std::string User::GetEmail() const {
104 return display_email(); 104 return display_email();
105 } 105 }
106 106
107 base::string16 User::GetDisplayName() const { 107 base::string16 User::GetDisplayName() const {
108 // Fallback to the email account name in case display name haven't been set. 108 // Fallback to the email account name in case display name haven't been set.
109 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true)) 109 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true))
110 : display_name_; 110 : display_name_;
111 } 111 }
112 112
113 base::string16 User::GetGivenName() const { 113 base::string16 User::GetGivenName() const {
114 return given_name_; 114 return given_name_;
115 } 115 }
116 116
117 const gfx::ImageSkia& User::GetImage() const { 117 const gfx::ImageSkia& User::GetImage() const {
118 return user_image_.image(); 118 return user_image_.image();
119 } 119 }
120 120
121 UserID User::GetUserID() const { 121 const UserID& User::GetUserID() const {
122 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); 122 return user_id_;
123 //return UserID(std::string() /* gaia_id */, gaia::CanonicalizeEmail(gaia::San itizeEmail(email())));
123 } 124 }
124 125
125 void User::SetIsChild(bool is_child) { 126 void User::SetIsChild(bool is_child) {
126 VLOG(1) << "Ignoring SetIsChild call with param " << is_child; 127 VLOG(1) << "Ignoring SetIsChild call with param " << is_child;
127 if (is_child) { 128 if (is_child) {
128 NOTREACHED() << "Calling SetIsChild(true) for base User class." 129 NOTREACHED() << "Calling SetIsChild(true) for base User class."
129 << "Base class cannot be set as child"; 130 << "Base class cannot be set as child";
130 } 131 }
131 } 132 }
132 133
133 bool User::HasGaiaAccount() const { 134 bool User::HasGaiaAccount() const {
134 return TypeHasGaiaAccount(GetType()); 135 return TypeHasGaiaAccount(GetType());
135 } 136 }
136 137
137 bool User::IsSupervised() const { 138 bool User::IsSupervised() const {
138 UserType type = GetType(); 139 UserType type = GetType();
139 return type == USER_TYPE_SUPERVISED || 140 return type == USER_TYPE_SUPERVISED ||
140 type == USER_TYPE_CHILD; 141 type == USER_TYPE_CHILD;
141 } 142 }
142 143
143 std::string User::GetAccountName(bool use_display_email) const { 144 std::string User::GetAccountName(bool use_display_email) const {
144 if (use_display_email && !display_email_.empty()) 145 if (use_display_email && !display_email_.empty())
145 return GetUserName(display_email_); 146 return GetUserName(display_email_);
146 else 147 else
147 return GetUserName(email_); 148 return GetUserName(user_id_.GetUserEmail());
148 } 149 }
149 150
150 bool User::HasDefaultImage() const { 151 bool User::HasDefaultImage() const {
151 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; 152 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
152 } 153 }
153 154
154 bool User::CanSyncImage() const { 155 bool User::CanSyncImage() const {
155 return false; 156 return false;
156 } 157 }
157 158
(...skipping 10 matching lines...) Expand all
168 } 169 }
169 170
170 bool User::is_logged_in() const { 171 bool User::is_logged_in() const {
171 return is_logged_in_; 172 return is_logged_in_;
172 } 173 }
173 174
174 bool User::is_active() const { 175 bool User::is_active() const {
175 return is_active_; 176 return is_active_;
176 } 177 }
177 178
178 User* User::CreateRegularUser(const std::string& email) { 179 User* User::CreateRegularUser(const UserID& user_id) {
179 return new RegularUser(email); 180 return new RegularUser(user_id);
180 } 181 }
181 182
182 User* User::CreateGuestUser() { 183 User* User::CreateGuestUser() {
183 return new GuestUser; 184 return new GuestUser;
184 } 185 }
185 186
186 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) { 187 User* User::CreateKioskAppUser(const UserID& user_id) {
187 return new KioskAppUser(kiosk_app_username); 188 return new KioskAppUser(user_id);
188 } 189 }
189 190
190 User* User::CreateSupervisedUser(const std::string& username) { 191 User* User::CreateSupervisedUser(const UserID& user_id) {
191 return new SupervisedUser(username); 192 return new SupervisedUser(user_id);
192 } 193 }
193 194
194 User* User::CreatePublicAccountUser(const std::string& email) { 195 User* User::CreatePublicAccountUser(const UserID& user_id) {
195 return new PublicAccountUser(email); 196 return new PublicAccountUser(user_id);
196 } 197 }
197 198
198 User::User(const std::string& email) 199 User::User(const UserID& user_id)
199 : email_(email), 200 : user_id_(user_id),
200 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN), 201 oauth_token_status_(OAUTH_TOKEN_STATUS_UNKNOWN),
201 force_online_signin_(false), 202 force_online_signin_(false),
202 image_index_(USER_IMAGE_INVALID), 203 image_index_(USER_IMAGE_INVALID),
203 image_is_stub_(false), 204 image_is_stub_(false),
204 image_is_loading_(false), 205 image_is_loading_(false),
205 can_lock_(false), 206 can_lock_(false),
206 is_logged_in_(false), 207 is_logged_in_(false),
207 is_active_(false), 208 is_active_(false),
208 profile_is_created_(false) { 209 profile_is_created_(false) {
209 } 210 }
(...skipping 19 matching lines...) Expand all
229 230
230 void User::SetStubImage(const UserImage& stub_user_image, 231 void User::SetStubImage(const UserImage& stub_user_image,
231 int image_index, 232 int image_index,
232 bool is_loading) { 233 bool is_loading) {
233 user_image_ = stub_user_image; 234 user_image_ = stub_user_image;
234 image_index_ = image_index; 235 image_index_ = image_index;
235 image_is_stub_ = true; 236 image_is_stub_ = true;
236 image_is_loading_ = is_loading; 237 image_is_loading_ = is_loading;
237 } 238 }
238 239
239 RegularUser::RegularUser(const std::string& email) 240 RegularUser::RegularUser(const UserID& user_id)
240 : User(email), is_child_(false) { 241 : User(user_id), is_child_(false) {
241 set_can_lock(true); 242 set_can_lock(true);
242 set_display_email(email); 243 set_display_email(user_id.GetUserEmail());
243 } 244 }
244 245
245 RegularUser::~RegularUser() { 246 RegularUser::~RegularUser() {
246 } 247 }
247 248
248 UserType RegularUser::GetType() const { 249 UserType RegularUser::GetType() const {
249 return is_child_ ? user_manager::USER_TYPE_CHILD : 250 return is_child_ ? user_manager::USER_TYPE_CHILD :
250 user_manager::USER_TYPE_REGULAR; 251 user_manager::USER_TYPE_REGULAR;
251 } 252 }
252 253
253 bool RegularUser::CanSyncImage() const { 254 bool RegularUser::CanSyncImage() const {
254 return true; 255 return true;
255 } 256 }
256 257
257 void RegularUser::SetIsChild(bool is_child) { 258 void RegularUser::SetIsChild(bool is_child) {
258 VLOG(1) << "Setting user is child to " << is_child; 259 VLOG(1) << "Setting user is child to " << is_child;
259 is_child_ = is_child; 260 is_child_ = is_child;
260 } 261 }
261 262
262 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) { 263 GuestUser::GuestUser() : User(chromeos::login::GetGuestUserID()) {
263 set_display_email(std::string()); 264 set_display_email(std::string());
264 } 265 }
265 266
266 GuestUser::~GuestUser() { 267 GuestUser::~GuestUser() {
267 } 268 }
268 269
269 UserType GuestUser::GetType() const { 270 UserType GuestUser::GetType() const {
270 return user_manager::USER_TYPE_GUEST; 271 return user_manager::USER_TYPE_GUEST;
271 } 272 }
272 273
273 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) 274 KioskAppUser::KioskAppUser(const UserID& kiosk_app)
274 : User(kiosk_app_username) { 275 : User(kiosk_app) {
275 set_display_email(kiosk_app_username); 276 set_display_email(kiosk_app.GetUserEmail());
276 } 277 }
277 278
278 KioskAppUser::~KioskAppUser() { 279 KioskAppUser::~KioskAppUser() {
279 } 280 }
280 281
281 UserType KioskAppUser::GetType() const { 282 UserType KioskAppUser::GetType() const {
282 return user_manager::USER_TYPE_KIOSK_APP; 283 return user_manager::USER_TYPE_KIOSK_APP;
283 } 284 }
284 285
285 SupervisedUser::SupervisedUser(const std::string& username) : User(username) { 286 SupervisedUser::SupervisedUser(const UserID& user_id) : User(user_id) {
286 set_can_lock(true); 287 set_can_lock(true);
287 } 288 }
288 289
289 SupervisedUser::~SupervisedUser() { 290 SupervisedUser::~SupervisedUser() {
290 } 291 }
291 292
292 UserType SupervisedUser::GetType() const { 293 UserType SupervisedUser::GetType() const {
293 return user_manager::USER_TYPE_SUPERVISED; 294 return user_manager::USER_TYPE_SUPERVISED;
294 } 295 }
295 296
296 std::string SupervisedUser::display_email() const { 297 std::string SupervisedUser::display_email() const {
297 return base::UTF16ToUTF8(display_name()); 298 return base::UTF16ToUTF8(display_name());
298 } 299 }
299 300
300 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { 301 PublicAccountUser::PublicAccountUser(const UserID& user_id) : User(user_id) {
301 } 302 }
302 303
303 PublicAccountUser::~PublicAccountUser() { 304 PublicAccountUser::~PublicAccountUser() {
304 } 305 }
305 306
306 UserType PublicAccountUser::GetType() const { 307 UserType PublicAccountUser::GetType() const {
307 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; 308 return user_manager::USER_TYPE_PUBLIC_ACCOUNT;
308 } 309 }
309 310
310 bool User::has_gaia_account() const { 311 bool User::has_gaia_account() const {
311 static_assert(user_manager::NUM_USER_TYPES == 7, 312 static_assert(user_manager::NUM_USER_TYPES == 7,
312 "NUM_USER_TYPES should equal 7"); 313 "NUM_USER_TYPES should equal 7");
313 switch (GetType()) { 314 switch (GetType()) {
314 case user_manager::USER_TYPE_REGULAR: 315 case user_manager::USER_TYPE_REGULAR:
315 case user_manager::USER_TYPE_CHILD: 316 case user_manager::USER_TYPE_CHILD:
316 return true; 317 return true;
317 case user_manager::USER_TYPE_GUEST: 318 case user_manager::USER_TYPE_GUEST:
318 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: 319 case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
319 case user_manager::USER_TYPE_SUPERVISED: 320 case user_manager::USER_TYPE_SUPERVISED:
320 case user_manager::USER_TYPE_KIOSK_APP: 321 case user_manager::USER_TYPE_KIOSK_APP:
321 return false; 322 return false;
322 default: 323 default:
323 NOTREACHED(); 324 NOTREACHED();
324 } 325 }
325 return false; 326 return false;
326 } 327 }
327 328
328 } // namespace user_manager 329 } // namespace user_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698