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

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

Issue 1412813003: This CL replaces user_manager::UserID with AccountId. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@468875--Chrome-OS-handles-deletion-of-Gmail-account-poorly--Create-AccountID-structure-part2--user_names
Patch Set: Fix Win GN build. Created 5 years, 1 month 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
« no previous file with comments | « components/user_manager/user.h ('k') | components/user_manager/user_id.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
11 #include "chromeos/login/user_names.h" 11 #include "chromeos/login/user_names.h"
12 #include "components/signin/core/account_id/account_id.h"
12 #include "components/user_manager/user_image/default_user_images.h" 13 #include "components/user_manager/user_image/default_user_images.h"
13 #include "google_apis/gaia/gaia_auth_util.h" 14 #include "google_apis/gaia/gaia_auth_util.h"
14 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
15 16
16 namespace user_manager { 17 namespace user_manager {
17 18
18 namespace { 19 namespace {
19 20
20 // Returns account name portion of an email. 21 // Returns account name portion of an email.
21 std::string GetUserName(const std::string& email) { 22 std::string GetUserName(const std::string& email) {
22 std::string::size_type i = email.find('@'); 23 std::string::size_type i = email.find('@');
23 if (i == 0 || i == std::string::npos) { 24 if (i == 0 || i == std::string::npos) {
24 return email; 25 return email;
25 } 26 }
26 return email.substr(0, i); 27 return email.substr(0, i);
27 } 28 }
28 29
29 } // namespace 30 } // namespace
30 31
31 // static 32 // static
32 bool User::TypeHasGaiaAccount(UserType user_type) { 33 bool User::TypeHasGaiaAccount(UserType user_type) {
33 return user_type == USER_TYPE_REGULAR || 34 return user_type == USER_TYPE_REGULAR ||
34 user_type == USER_TYPE_CHILD; 35 user_type == USER_TYPE_CHILD;
35 } 36 }
36 37
38 const std::string& User::email() const {
39 return account_id_.GetUserEmail();
40 }
41
37 // Also used for regular supervised users. 42 // Also used for regular supervised users.
38 class RegularUser : public User { 43 class RegularUser : public User {
39 public: 44 public:
40 explicit RegularUser(const std::string& email); 45 explicit RegularUser(const AccountId& account_id);
41 ~RegularUser() override; 46 ~RegularUser() override;
42 47
43 // Overridden from User: 48 // Overridden from User:
44 UserType GetType() const override; 49 UserType GetType() const override;
45 bool CanSyncImage() const override; 50 bool CanSyncImage() const override;
46 void SetIsChild(bool is_child) override; 51 void SetIsChild(bool is_child) override;
47 52
48 private: 53 private:
49 bool is_child_; 54 bool is_child_ = false;
50 55
51 DISALLOW_COPY_AND_ASSIGN(RegularUser); 56 DISALLOW_COPY_AND_ASSIGN(RegularUser);
52 }; 57 };
53 58
54 class GuestUser : public User { 59 class GuestUser : public User {
55 public: 60 public:
56 GuestUser(); 61 GuestUser();
57 ~GuestUser() override; 62 ~GuestUser() override;
58 63
59 // Overridden from User: 64 // Overridden from User:
60 UserType GetType() const override; 65 UserType GetType() const override;
61 66
62 private: 67 private:
63 DISALLOW_COPY_AND_ASSIGN(GuestUser); 68 DISALLOW_COPY_AND_ASSIGN(GuestUser);
64 }; 69 };
65 70
66 class KioskAppUser : public User { 71 class KioskAppUser : public User {
67 public: 72 public:
68 explicit KioskAppUser(const std::string& app_id); 73 explicit KioskAppUser(const AccountId& kiosk_app_account_id);
69 ~KioskAppUser() override; 74 ~KioskAppUser() override;
70 75
71 // Overridden from User: 76 // Overridden from User:
72 UserType GetType() const override; 77 UserType GetType() const override;
73 78
74 private: 79 private:
75 DISALLOW_COPY_AND_ASSIGN(KioskAppUser); 80 DISALLOW_COPY_AND_ASSIGN(KioskAppUser);
76 }; 81 };
77 82
78 class SupervisedUser : public User { 83 class SupervisedUser : public User {
79 public: 84 public:
80 explicit SupervisedUser(const std::string& username); 85 explicit SupervisedUser(const AccountId& account_id);
81 ~SupervisedUser() override; 86 ~SupervisedUser() override;
82 87
83 // Overridden from User: 88 // Overridden from User:
84 UserType GetType() const override; 89 UserType GetType() const override;
85 std::string display_email() const override; 90 std::string display_email() const override;
86 91
87 private: 92 private:
88 DISALLOW_COPY_AND_ASSIGN(SupervisedUser); 93 DISALLOW_COPY_AND_ASSIGN(SupervisedUser);
89 }; 94 };
90 95
91 class PublicAccountUser : public User { 96 class PublicAccountUser : public User {
92 public: 97 public:
93 explicit PublicAccountUser(const std::string& email); 98 explicit PublicAccountUser(const AccountId& account_id);
94 ~PublicAccountUser() override; 99 ~PublicAccountUser() override;
95 100
96 // Overridden from User: 101 // Overridden from User:
97 UserType GetType() const override; 102 UserType GetType() const override;
98 103
99 private: 104 private:
100 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser); 105 DISALLOW_COPY_AND_ASSIGN(PublicAccountUser);
101 }; 106 };
102 107
103 std::string User::GetEmail() const { 108 std::string User::GetEmail() const {
104 return display_email(); 109 return display_email();
105 } 110 }
106 111
107 base::string16 User::GetDisplayName() const { 112 base::string16 User::GetDisplayName() const {
108 // Fallback to the email account name in case display name haven't been set. 113 // Fallback to the email account name in case display name haven't been set.
109 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true)) 114 return display_name_.empty() ? base::UTF8ToUTF16(GetAccountName(true))
110 : display_name_; 115 : display_name_;
111 } 116 }
112 117
113 base::string16 User::GetGivenName() const { 118 base::string16 User::GetGivenName() const {
114 return given_name_; 119 return given_name_;
115 } 120 }
116 121
117 const gfx::ImageSkia& User::GetImage() const { 122 const gfx::ImageSkia& User::GetImage() const {
118 return user_image_.image(); 123 return user_image_.image();
119 } 124 }
120 125
121 UserID User::GetUserID() const { 126 AccountId User::GetAccountId() const {
122 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())); 127 return AccountId::FromUserEmail(
128 gaia::CanonicalizeEmail(gaia::SanitizeEmail(email())));
123 } 129 }
124 130
125 void User::SetIsChild(bool is_child) { 131 void User::SetIsChild(bool is_child) {
126 VLOG(1) << "Ignoring SetIsChild call with param " << is_child; 132 VLOG(1) << "Ignoring SetIsChild call with param " << is_child;
127 if (is_child) { 133 if (is_child) {
128 NOTREACHED() << "Calling SetIsChild(true) for base User class." 134 NOTREACHED() << "Calling SetIsChild(true) for base User class."
129 << "Base class cannot be set as child"; 135 << "Base class cannot be set as child";
130 } 136 }
131 } 137 }
132 138
133 bool User::HasGaiaAccount() const { 139 bool User::HasGaiaAccount() const {
134 return TypeHasGaiaAccount(GetType()); 140 return TypeHasGaiaAccount(GetType());
135 } 141 }
136 142
137 bool User::IsSupervised() const { 143 bool User::IsSupervised() const {
138 UserType type = GetType(); 144 UserType type = GetType();
139 return type == USER_TYPE_SUPERVISED || 145 return type == USER_TYPE_SUPERVISED ||
140 type == USER_TYPE_CHILD; 146 type == USER_TYPE_CHILD;
141 } 147 }
142 148
143 std::string User::GetAccountName(bool use_display_email) const { 149 std::string User::GetAccountName(bool use_display_email) const {
144 if (use_display_email && !display_email_.empty()) 150 if (use_display_email && !display_email_.empty())
145 return GetUserName(display_email_); 151 return GetUserName(display_email_);
146 else 152 else
147 return GetUserName(email_); 153 return GetUserName(account_id_.GetUserEmail());
148 } 154 }
149 155
150 bool User::HasDefaultImage() const { 156 bool User::HasDefaultImage() const {
151 return image_index_ >= 0 && image_index_ < kDefaultImagesCount; 157 return image_index_ >= 0 && image_index_ < kDefaultImagesCount;
152 } 158 }
153 159
154 bool User::CanSyncImage() const { 160 bool User::CanSyncImage() const {
155 return false; 161 return false;
156 } 162 }
157 163
(...skipping 10 matching lines...) Expand all
168 } 174 }
169 175
170 bool User::is_logged_in() const { 176 bool User::is_logged_in() const {
171 return is_logged_in_; 177 return is_logged_in_;
172 } 178 }
173 179
174 bool User::is_active() const { 180 bool User::is_active() const {
175 return is_active_; 181 return is_active_;
176 } 182 }
177 183
178 User* User::CreateRegularUser(const std::string& email) { 184 User* User::CreateRegularUser(const AccountId& account_id) {
179 return new RegularUser(email); 185 return new RegularUser(account_id);
180 } 186 }
181 187
182 User* User::CreateGuestUser() { 188 User* User::CreateGuestUser() {
183 return new GuestUser; 189 return new GuestUser;
184 } 190 }
185 191
186 User* User::CreateKioskAppUser(const std::string& kiosk_app_username) { 192 User* User::CreateKioskAppUser(const AccountId& kiosk_app_account_id) {
187 return new KioskAppUser(kiosk_app_username); 193 return new KioskAppUser(kiosk_app_account_id);
188 } 194 }
189 195
190 User* User::CreateSupervisedUser(const std::string& username) { 196 User* User::CreateSupervisedUser(const AccountId& account_id) {
191 return new SupervisedUser(username); 197 return new SupervisedUser(account_id);
192 } 198 }
193 199
194 User* User::CreatePublicAccountUser(const std::string& email) { 200 User* User::CreatePublicAccountUser(const AccountId& account_id) {
195 return new PublicAccountUser(email); 201 return new PublicAccountUser(account_id);
196 } 202 }
197 203
198 User::User(const std::string& email) 204 User::User(const AccountId& account_id) : account_id_(account_id) {}
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),
209 is_affiliated_(false){
210 }
211 205
212 User::~User() { 206 User::~User() {
213 } 207 }
214 208
215 void User::SetAccountLocale(const std::string& resolved_account_locale) { 209 void User::SetAccountLocale(const std::string& resolved_account_locale) {
216 account_locale_.reset(new std::string(resolved_account_locale)); 210 account_locale_.reset(new std::string(resolved_account_locale));
217 } 211 }
218 212
219 void User::SetImage(const UserImage& user_image, int image_index) { 213 void User::SetImage(const UserImage& user_image, int image_index) {
220 user_image_ = user_image; 214 user_image_ = user_image;
221 image_index_ = image_index; 215 image_index_ = image_index;
222 image_is_stub_ = false; 216 image_is_stub_ = false;
223 image_is_loading_ = false; 217 image_is_loading_ = false;
224 DCHECK(HasDefaultImage() || user_image.has_raw_image()); 218 DCHECK(HasDefaultImage() || user_image.has_raw_image());
225 } 219 }
226 220
227 void User::SetImageURL(const GURL& image_url) { 221 void User::SetImageURL(const GURL& image_url) {
228 user_image_.set_url(image_url); 222 user_image_.set_url(image_url);
229 } 223 }
230 224
231 void User::SetStubImage(const UserImage& stub_user_image, 225 void User::SetStubImage(const UserImage& stub_user_image,
232 int image_index, 226 int image_index,
233 bool is_loading) { 227 bool is_loading) {
234 user_image_ = stub_user_image; 228 user_image_ = stub_user_image;
235 image_index_ = image_index; 229 image_index_ = image_index;
236 image_is_stub_ = true; 230 image_is_stub_ = true;
237 image_is_loading_ = is_loading; 231 image_is_loading_ = is_loading;
238 } 232 }
239 233
240 RegularUser::RegularUser(const std::string& email) 234 RegularUser::RegularUser(const AccountId& account_id) : User(account_id) {
241 : User(email), is_child_(false) {
242 set_can_lock(true); 235 set_can_lock(true);
243 set_display_email(email); 236 set_display_email(account_id.GetUserEmail());
244 } 237 }
245 238
246 RegularUser::~RegularUser() { 239 RegularUser::~RegularUser() {
247 } 240 }
248 241
249 UserType RegularUser::GetType() const { 242 UserType RegularUser::GetType() const {
250 return is_child_ ? user_manager::USER_TYPE_CHILD : 243 return is_child_ ? user_manager::USER_TYPE_CHILD :
251 user_manager::USER_TYPE_REGULAR; 244 user_manager::USER_TYPE_REGULAR;
252 } 245 }
253 246
254 bool RegularUser::CanSyncImage() const { 247 bool RegularUser::CanSyncImage() const {
255 return true; 248 return true;
256 } 249 }
257 250
258 void RegularUser::SetIsChild(bool is_child) { 251 void RegularUser::SetIsChild(bool is_child) {
259 VLOG(1) << "Setting user is child to " << is_child; 252 VLOG(1) << "Setting user is child to " << is_child;
260 is_child_ = is_child; 253 is_child_ = is_child;
261 } 254 }
262 255
263 GuestUser::GuestUser() : User(chromeos::login::kGuestUserName) { 256 GuestUser::GuestUser() : User(chromeos::login::GuestAccountId()) {
264 set_display_email(std::string()); 257 set_display_email(std::string());
265 } 258 }
266 259
267 GuestUser::~GuestUser() { 260 GuestUser::~GuestUser() {
268 } 261 }
269 262
270 UserType GuestUser::GetType() const { 263 UserType GuestUser::GetType() const {
271 return user_manager::USER_TYPE_GUEST; 264 return user_manager::USER_TYPE_GUEST;
272 } 265 }
273 266
274 KioskAppUser::KioskAppUser(const std::string& kiosk_app_username) 267 KioskAppUser::KioskAppUser(const AccountId& kiosk_app_account_id)
275 : User(kiosk_app_username) { 268 : User(kiosk_app_account_id) {
276 set_display_email(kiosk_app_username); 269 set_display_email(kiosk_app_account_id.GetUserEmail());
277 } 270 }
278 271
279 KioskAppUser::~KioskAppUser() { 272 KioskAppUser::~KioskAppUser() {
280 } 273 }
281 274
282 UserType KioskAppUser::GetType() const { 275 UserType KioskAppUser::GetType() const {
283 return user_manager::USER_TYPE_KIOSK_APP; 276 return user_manager::USER_TYPE_KIOSK_APP;
284 } 277 }
285 278
286 SupervisedUser::SupervisedUser(const std::string& username) : User(username) { 279 SupervisedUser::SupervisedUser(const AccountId& account_id) : User(account_id) {
287 set_can_lock(true); 280 set_can_lock(true);
288 } 281 }
289 282
290 SupervisedUser::~SupervisedUser() { 283 SupervisedUser::~SupervisedUser() {
291 } 284 }
292 285
293 UserType SupervisedUser::GetType() const { 286 UserType SupervisedUser::GetType() const {
294 return user_manager::USER_TYPE_SUPERVISED; 287 return user_manager::USER_TYPE_SUPERVISED;
295 } 288 }
296 289
297 std::string SupervisedUser::display_email() const { 290 std::string SupervisedUser::display_email() const {
298 return base::UTF16ToUTF8(display_name()); 291 return base::UTF16ToUTF8(display_name());
299 } 292 }
300 293
301 PublicAccountUser::PublicAccountUser(const std::string& email) : User(email) { 294 PublicAccountUser::PublicAccountUser(const AccountId& account_id)
302 } 295 : User(account_id) {}
303 296
304 PublicAccountUser::~PublicAccountUser() { 297 PublicAccountUser::~PublicAccountUser() {
305 } 298 }
306 299
307 UserType PublicAccountUser::GetType() const { 300 UserType PublicAccountUser::GetType() const {
308 return user_manager::USER_TYPE_PUBLIC_ACCOUNT; 301 return user_manager::USER_TYPE_PUBLIC_ACCOUNT;
309 } 302 }
310 303
311 bool User::has_gaia_account() const { 304 bool User::has_gaia_account() const {
312 static_assert(user_manager::NUM_USER_TYPES == 7, 305 static_assert(user_manager::NUM_USER_TYPES == 7,
313 "NUM_USER_TYPES should equal 7"); 306 "NUM_USER_TYPES should equal 7");
314 switch (GetType()) { 307 switch (GetType()) {
315 case user_manager::USER_TYPE_REGULAR: 308 case user_manager::USER_TYPE_REGULAR:
316 case user_manager::USER_TYPE_CHILD: 309 case user_manager::USER_TYPE_CHILD:
317 return true; 310 return true;
318 case user_manager::USER_TYPE_GUEST: 311 case user_manager::USER_TYPE_GUEST:
319 case user_manager::USER_TYPE_PUBLIC_ACCOUNT: 312 case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
320 case user_manager::USER_TYPE_SUPERVISED: 313 case user_manager::USER_TYPE_SUPERVISED:
321 case user_manager::USER_TYPE_KIOSK_APP: 314 case user_manager::USER_TYPE_KIOSK_APP:
322 return false; 315 return false;
323 default: 316 default:
324 NOTREACHED(); 317 NOTREACHED();
325 } 318 }
326 return false; 319 return false;
327 } 320 }
328 321
329 } // namespace user_manager 322 } // namespace user_manager
OLDNEW
« no previous file with comments | « components/user_manager/user.h ('k') | components/user_manager/user_id.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698