File: user.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (340 lines) | stat: -rw-r--r-- 12,025 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_USER_MANAGER_USER_H_
#define COMPONENTS_USER_MANAGER_USER_H_

#include <memory>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_image/user_image.h"
#include "components/user_manager/user_manager_export.h"
#include "components/user_manager/user_type.h"

class PrefService;

namespace ash {
class FakeChromeUserManager;
class UserAddingScreenTest;
class UserSessionManager;
class UserImageManagerImpl;
}  // namespace ash

namespace chromeos {
class SupervisedUserManagerImpl;
}

namespace gfx {
class ImageSkia;
}

namespace policy {
class ProfilePolicyConnectorTest;
}

namespace user_manager {

class UserManagerImpl;
class FakeUserManager;

// A class representing information about a previously logged in user.
//   Each user has an |AccountId| containing canonical email (username),
// returned by |GetAccountId().GetUserEmail()| and may have a different
// displayed email (in the raw form as entered by user), returned by
// |displayed_email()|.
//   Displayed emails are for use in UI only, anywhere else users must be
// referred to by |GetAccountId()|. Internal details of AccountId should not
// be relied on unless you have special knowledge of the account type.
class USER_MANAGER_EXPORT User {
 public:
  // User OAuth token status according to the last check.
  // Please note that enum values 1 and 2 were used for OAuth1 status and are
  // deprecated now.
  typedef enum {
    OAUTH_TOKEN_STATUS_UNKNOWN = 0,
    OAUTH2_TOKEN_STATUS_INVALID = 3,
    OAUTH2_TOKEN_STATUS_VALID = 4,
  } OAuthTokenStatus;

  // Returns true if user type has gaia account.
  static bool TypeHasGaiaAccount(UserType user_type);

  // Returns true if user represents any type of the kiosk.
  static bool TypeIsKiosk(UserType user_type);

  User(const User&) = delete;
  User& operator=(const User&) = delete;

  ~User();

  std::string GetDisplayEmail() const;
  std::u16string GetDisplayName() const;
  std::u16string GetGivenName() const;
  const gfx::ImageSkia& GetImage() const;
  const AccountId& GetAccountId() const;

  // Returns the user type.
  UserType GetType() const { return type_; }

  // Returns true if user has gaia account. True for users of types
  // UserType::kRegular and UserType::kChild.
  bool HasGaiaAccount() const;

  // Returns true if user is child.
  bool IsChild() const;

  // The displayed (non-canonical) user email.
  std::string display_email() const;

  // Returns whether the User is managed by policy.
  const std::optional<bool>& is_managed() const;

  // True if the user is affiliated to the device. Returns false if the
  // affiliation is not known. Use IsAffiliatedAsync if it's possible the call
  // is done before affiliation is established.
  bool IsAffiliated() const;

  // Runs the callback immediately if the affiliation is known, otherwise later
  // when the affiliation is established.
  void IsAffiliatedAsync(base::OnceCallback<void(bool)> is_affiliated_callback);

  // True if the user is a device local account user.
  bool IsDeviceLocalAccount() const;

  // True if the user is a kiosk.
  bool IsKioskType() const;

  // Returns PrefService of the Profile corresponding this User.
  // If Profile and its PrefService is not yet ready, or it is already
  // destroyed, this API returns nullptr.
  PrefService* GetProfilePrefs() { return profile_prefs_.get(); }
  const PrefService* GetProfilePrefs() const { return profile_prefs_.get(); }

  // The displayed user name.
  std::u16string display_name() const { return display_name_; }

  // If the user has to use SAML to log in.
  bool using_saml() const { return using_saml_; }

  // Returns the account name part of the email. Use the display form of the
  // email if available and use_display_name == true. Otherwise use canonical.
  std::string GetAccountName(bool use_display_email) const;

  const std::string* GetAccountLocale() const { return account_locale_.get(); }

  // True if the user's session can be locked (i.e. the user has a password with
  // which to unlock the session).
  // This depends on Profile preference, and if it's not yet ready, this
  // returns false as fallback.
  bool CanLock() const;

  int image_index() const { return image_index_; }
  bool has_image_bytes() const { return user_image_->has_image_bytes(); }
  // Returns bytes representation of static user image for WebUI.
  scoped_refptr<base::RefCountedBytes> image_bytes() const {
    return user_image_->image_bytes();
  }
  // Returns image format of the bytes representation of static user image
  // for WebUI.
  UserImage::ImageFormat image_format() const {
    return user_image_->image_format();
  }

  // Whether |user_image_| contains data in format that is considered safe to
  // decode in sensitive environment (on Login screen).
  bool image_is_safe_format() const { return user_image_->is_safe_format(); }

  // Returns the URL of user image, if there is any. Currently only the profile
  // image has a URL, for other images empty URL is returned.
  GURL image_url() const { return user_image_->url(); }

  // True if user image is a stub (while real image is being loaded from file).
  bool image_is_stub() const { return image_is_stub_; }

  // True if image is being loaded from file.
  bool image_is_loading() const { return image_is_loading_; }

  // OAuth token status for this user.
  OAuthTokenStatus oauth_token_status() const { return oauth_token_status_; }

  // Whether online authentication against GAIA should be enforced during the
  // user's next sign-in.
  bool force_online_signin() const { return force_online_signin_; }

  // Returns empty string when home dir hasn't been mounted yet.
  const std::string& username_hash() const;

  // True if current user is logged in.
  bool is_logged_in() const;

  // True if current user is active within the current session.
  bool is_active() const;

  // True if the user Profile is created.
  bool is_profile_created() const { return profile_is_created_; }

  // True if user has google account (not a guest or managed user).
  bool has_gaia_account() const;

  static User* CreatePublicAccountUserForTesting(const AccountId& account_id) {
    return CreatePublicAccountUser(account_id);
  }

  static User* CreatePublicAccountUserForTestingWithSAML(
      const AccountId& account_id) {
    return CreatePublicAccountUser(account_id, /* is_using_saml */ true);
  }

  static User* CreateRegularUserForTesting(const AccountId& account_id) {
    User* user = CreateRegularUser(account_id, UserType::kRegular);
    user->SetImage(std::make_unique<UserImage>(), 0);
    return user;
  }

  void AddProfileCreatedObserver(base::OnceClosure on_profile_created);

 private:
  friend class UserManagerImpl;
  friend class chromeos::SupervisedUserManagerImpl;
  friend class ash::UserImageManagerImpl;
  friend class ash::UserSessionManager;

  // For testing:
  friend class FakeUserManager;
  friend class ash::FakeChromeUserManager;
  friend class ash::UserAddingScreenTest;
  friend class policy::ProfilePolicyConnectorTest;
  FRIEND_TEST_ALL_PREFIXES(UserTest, DeviceLocalAccountAffiliation);
  FRIEND_TEST_ALL_PREFIXES(UserTest, UserSessionInitialized);

  // Do not allow anyone else to create new User instances.
  static User* CreateRegularUser(const AccountId& account_id,
                                 const UserType user_type);
  static User* CreateGuestUser(const AccountId& guest_account_id);
  static User* CreateKioskChromeAppUser(const AccountId& kiosk_app_account_id);
  static User* CreateKioskWebAppUser(const AccountId& web_kiosk_account_id);
  static User* CreateKioskIwaUser(const AccountId& kiosk_iwa_account_id);
  static User* CreatePublicAccountUser(const AccountId& account_id,
                                       bool is_using_saml = false);

  User(const AccountId& account_id, UserType type);

  // Setters are private so only UserManager can call them.
  void SetAccountLocale(const std::string& resolved_account_locale);
  void SetImage(std::unique_ptr<UserImage> user_image, int image_index);
  void SetImageURL(const GURL& image_url);
  void SetType(UserType new_type);

  // Sets a stub image until the next |SetImage| call. |image_index| may be
  // one of |UserImage::Type::kExternal| or |UserImage::Type::kProfile|.
  // If |is_loading| is |true|, that means user image is being loaded from file.
  void SetStubImage(std::unique_ptr<UserImage> stub_user_image,
                    int image_index,
                    bool is_loading);

  void set_display_name(const std::u16string& display_name) {
    display_name_ = display_name;
  }

  void set_given_name(const std::u16string& given_name) {
    given_name_ = given_name;
  }

  void set_display_email(const std::string& display_email) {
    display_email_ = display_email;
  }

  void set_using_saml(const bool using_saml) { using_saml_ = using_saml; }

  const UserImage& user_image() const { return *user_image_; }

  void set_oauth_token_status(OAuthTokenStatus status) {
    oauth_token_status_ = status;
  }

  void set_force_online_signin(bool force_online_signin) {
    force_online_signin_ = force_online_signin;
  }

  void set_username_hash(std::string_view username_hash) {
    username_hash_ = std::string(username_hash);
  }

  void set_is_logged_in(bool is_logged_in) { is_logged_in_ = is_logged_in; }

  void set_is_active(bool is_active) { is_active_ = is_active; }

  void SetProfileIsCreated();

  void SetProfilePrefs(PrefService* prefs) { profile_prefs_ = prefs; }

  void SetUserPolicyStatus(bool is_managed, bool is_affiliated);

  AccountId account_id_;
  UserType type_;
  std::u16string display_name_;
  std::u16string given_name_;
  // User email for display, which may include capitals and non-significant
  // periods. For example, "John.Steinbeck@gmail.com" is a display email, but
  // "johnsteinbeck@gmail.com" is the canonical form. Defaults to
  // account_id_.GetUserEmail().
  std::string display_email_;
  bool using_saml_ = false;
  std::unique_ptr<UserImage> user_image_;
  OAuthTokenStatus oauth_token_status_ = OAUTH_TOKEN_STATUS_UNKNOWN;
  bool force_online_signin_ = false;

  // This is set to chromeos locale if account data has been downloaded.
  // (Or failed to download, but at least one download attempt finished).
  // An empty string indicates error in data load, or in
  // translation of Account locale to chromeos locale.
  std::unique_ptr<std::string> account_locale_;

  // Used to identify homedir mount point.
  std::string username_hash_;

  // Either index of a default image for the user, |UserImage::Type::kExternal|
  // or |UserImage::Type::kProfile|.
  int image_index_ = UserImage::Type::kInvalid;

  // True if current user image is a stub set by a |SetStubImage| call.
  bool image_is_stub_ = false;

  // True if current user image is being loaded from file.
  bool image_is_loading_ = false;

  // True if user is currently logged in in current session.
  bool is_logged_in_ = false;

  // True if user is currently logged in and active in current session.
  bool is_active_ = false;

  // True if user Profile is created
  bool profile_is_created_ = false;

  // Owned by Profile.
  raw_ptr<PrefService> profile_prefs_ = nullptr;

  // True if the user is affiliated to the device.
  std::optional<bool> is_affiliated_;

  // True if the user is managed by policy.
  std::optional<bool> is_managed_;

  std::vector<base::OnceClosure> on_profile_created_observers_;
  std::vector<base::OnceCallback<void(bool is_affiliated)>>
      on_affiliation_set_callbacks_;
};

// List of known users.
using UserList = std::vector<raw_ptr<User, VectorExperimental>>;

}  // namespace user_manager

#endif  // COMPONENTS_USER_MANAGER_USER_H_