File: profile_attributes_entry.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 (434 lines) | stat: -rw-r--r-- 19,441 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_
#define CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_

#include <stddef.h>

#include <memory>
#include <optional>
#include <string>

#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "components/signin/public/identity_manager/tribool.h"
#include "google_apis/gaia/gaia_id.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image.h"

class PrefRegistrySimple;
class PrefService;
class ProfileAttributesStorage;
struct ProfileThemeColors;

inline constexpr int kDefaultSizeForPlaceholderAvatar = 74;

enum class SigninState {
  kNotSignedIn,
  kSignedInWithUnconsentedPrimaryAccount,
  kSignedInWithConsentedPrimaryAccount,
};

enum class NameForm {
  kGaiaName,
  kLocalName,
  kGaiaAndLocalName,
};

// TODO(381117479): Currently we support two different OIDC enrollment flows:
// 1. by sending both Auth and ID token through URL param and
// 2. by sending encrypted user information in the auth header
// Method 1 is completed but it will be deprecated once we completely implement,
// test and rollout method 2.
struct ProfileManagementOidcTokens {
  ProfileManagementOidcTokens();
  ProfileManagementOidcTokens(const std::string& auth_token,
                              const std::string& id_token,
                              const std::u16string& identity_name);
  ProfileManagementOidcTokens(const std::string& auth_token,
                              const std::string& id_token,
                              const std::string& state);
  // This will be the new format of the `ProfileManagementOidcTokens` struct,
  // after we fully migrate.
  explicit ProfileManagementOidcTokens(const std::string& encrypted_user_info);

  ProfileManagementOidcTokens(const ProfileManagementOidcTokens& other);
  ProfileManagementOidcTokens& operator=(
      const ProfileManagementOidcTokens& other);

  ProfileManagementOidcTokens(ProfileManagementOidcTokens&& other);
  ProfileManagementOidcTokens& operator=(ProfileManagementOidcTokens&& other);
  ~ProfileManagementOidcTokens();

  // Authorization token from the authorization response.
  std::string auth_token;

  // ID token from the authorization response or the encrypted user information.
  // Field will be renamed to encrypted_user_information once we fully migrate.
  std::string id_token;

  // Identity name of the profile. This is only relevant after the completion of
  // profile registration.
  std::u16string identity_name;

  // OIDC configuration state. This is only relevant during profile
  // registration.
  std::string state;

  // Whether the passing ID token/user info is encrypted. This field will be
  // removed after we fully migrate.
  bool is_token_encrypted = false;
};

class ProfileAttributesEntry {
 public:
  static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);

  ProfileAttributesEntry();
  ProfileAttributesEntry(const ProfileAttributesEntry&) = delete;
  ProfileAttributesEntry& operator=(const ProfileAttributesEntry&) = delete;
  virtual ~ProfileAttributesEntry() = default;

  // Gets the name of the profile to be displayed in the User Menu. The name can
  // be the GAIA name, local profile name or a combination of them.
  std::u16string GetName() const;
  // Returns |GetGAIAGivenName()| if not empty. Otherwise, returns
  // |GetGAIAName()|.
  std::u16string GetGAIANameToDisplay() const;
  // Returns true if the profile name has changed.
  bool HasProfileNameChanged();
  // Returns how the value of GetName() gets constructed.
  NameForm GetNameForm() const;

  // Gets the local profile name.
  std::u16string GetLocalProfileName() const;

  // Get the profile label set for a managed profile. This is used instead of
  // the local profile name if present.
  std::u16string GetEnterpriseProfileLabel() const;

  std::u16string GetShortcutName() const;
  // Gets the path to the profile. Should correspond to the path passed to
  // ProfileAttributesStorage::GetProfileAttributesWithPath to get this entry.
  base::FilePath GetPath() const;
  base::Time GetActiveTime() const;
  // Gets the user name of the signed in profile. This is typically the email
  // address used to sign in and the empty string for profiles that aren't
  // signed in to chrome.
  std::u16string GetUserName() const;
  // Gets the icon used as this profile's avatar. High res icon are downloaded
  // only if `download_high_res` is true, otherwise a low-res fallback is
  // returned.
  // TODO(crbug.com/40138086): Rename |size_for_placeholder_avatar| to |size|
  // and make this function resize all avatars appropriately. Remove the default
  // value of |size_for_placeholder_avatar| when all callsites pass some value.
  // Consider adding a |shape| parameter and get rid of
  // profiles::GetSizedAvatarIcon().
  gfx::Image GetAvatarIcon(
      int size_for_placeholder_avatar = kDefaultSizeForPlaceholderAvatar,
      bool use_high_res_file = true,
      const profiles::PlaceholderAvatarIconParams& icon_params = {}) const;
  // Returns true if the profile is currently running any background apps. Note
  // that a return value of false could mean an error in collection or that
  // there are currently no background apps running. However, the action which
  // results is the same in both cases (thus far).
  bool GetBackgroundStatus() const;
  // Gets the GAIA full name associated with this profile if it's signed in.
  // If GAIA full name is empty, gets the full name from the 3P identity
  // associated with this profile, currently only available for OIDC profiles.
  std::u16string GetGAIAName() const;
  // Gets the GAIA given name associated with this profile if it's signed in.
  std::u16string GetGAIAGivenName() const;
  // Gets the opaque string representation of the profile's GAIA ID if it's
  // signed in.
  GaiaId GetGAIAId() const;
  // Returns the GAIA picture for the given profile. This may return NULL
  // if the profile does not have a GAIA picture or if the picture must be
  // loaded from disk.
  const gfx::Image* GetGAIAPicture() const;
  // Returns true if the profile displays a GAIA picture instead of one of the
  // locally bundled icons.
  bool IsUsingGAIAPicture() const;
  // Returns true if a GAIA picture has been loaded or has failed to load.
  bool IsGAIAPictureLoaded() const;
  // Returns the last downloaded GAIA picture URL with size.
  std::string GetLastDownloadedGAIAPictureUrlWithSize() const;
  // Returns true if the profile is signed in as a supervised user.
  bool IsSupervised() const;
  // Returns true if the profile should not be displayed to the user in the
  // list of profiles.
  bool IsOmitted() const;
  // Returns true if the user must sign before a profile can be opened.
  // Currently, this returns true iff a profile is locked due to the force
  // sign-in policy.
  bool IsSigninRequired() const;
  // Gets the supervised user ID of the profile's signed in account, if it's a
  // supervised user.
  std::string GetSupervisedUserId() const;
  // Returns true if the profile is an ephemeral profile.
  bool IsEphemeral() const;
  // Returns true if the profile is using a default name, typically of the
  // format "Person %d".
  bool IsUsingDefaultName() const;
  // Returns Signin state.
  SigninState GetSigninState() const;
  // Returns true if the profile is signed in.
  bool IsAuthenticated() const;
  // Returns true if the account can be be managed.
  bool CanBeManaged() const;
  // Returns true if the Profile is using the default avatar, which is one of
  // the profile icons selectable at profile creation.
  bool IsUsingDefaultAvatar() const;
  // Indicates that profile was signed in through native OS credential provider.
  bool IsSignedInWithCredentialProvider() const;
  // Returns true if the profile is managed by a third party identity that is
  // not sync-ed to Google (i.e dasher-based).
  bool IsDasherlessManagement() const;
  // Returns the index of the default icon used by the profile.
  size_t GetAvatarIconIndex() const;
  // Returns the colors specified by the profile theme, or default colors if no
  // theme is specified for the profile.
  ProfileThemeColors GetProfileThemeColors() const;
  // Returns the colors specified by the profile theme, or empty if no theme is
  // set for the profile.
  std::optional<ProfileThemeColors> GetProfileThemeColorsIfSet() const;
  // Returns the metrics bucket this profile should be recorded in.
  // Note: The bucket index is assigned once and remains the same all time. 0 is
  // reserved for the guest profile.
  size_t GetMetricsBucketIndex();
  // Returns the hosted domain for the current signed-in account. Returns empty
  // string if there is no signed-in account and returns
  // |signin::constants::kNoHostedDomainFound| if the signed-in account has no
  // hosted domain (such as when it is a standard gmail.com account). Unlike
  // for other string getters, the returned value is UTF8 encoded.
  std::string GetHostedDomain() const;

  // Returns management status of the current signed-in account.
  signin::Tribool GetIsManaged() const;

  // Returns the enrollment token to get policies for a profile.
  std::string GetProfileManagementEnrollmentToken() const;

  // Returns the Oauth token and Id token from the OIDC authentication response
  // that created the profile. The existence of these tokens are also used to
  // check whether the profile is created by an OIDC authentication response.
  ProfileManagementOidcTokens GetProfileManagementOidcTokens() const;

  // Returns the signin id for a profile managed by a token. This may be empty
  // even if there is an enrollment token.
  std::string GetProfileManagementId() const;

  // Returns an account id key of the user of the profile. Empty if the profile
  // doesn't have any associated `user_manager::User`.
  std::string GetAccountIdKey() const;

  // Returns whether the current profile state is glic eligibile or not based on
  // the signed in account. Signed out profiles are ineligible.
  bool IsGlicEligible() const;

  // Gets/Sets the gaia IDs of the accounts signed into the profile (accounts
  // known by the `IdentityManager`).
  base::flat_set<GaiaId> GetGaiaIds() const;
  void SetGaiaIds(const base::flat_set<GaiaId>& gaia_ids);

  // |is_using_default| should be set to false for non default profile names.
  void SetLocalProfileName(const std::u16string& name, bool is_default_name);
  void SetEnterpriseProfileLabel(const std::u16string& name);
  void SetShortcutName(const std::u16string& name);
  void SetActiveTimeToNow();
  // Only ephemeral profiles can be set as omitted.
  void SetIsOmitted(bool is_omitted);
  void SetSupervisedUserId(const std::string& id);
  void SetBackgroundStatus(bool running_background_apps);
  void SetGAIAName(const std::u16string& name);
  void SetGAIAGivenName(const std::u16string& name);
  void SetGAIAPicture(const std::string& image_url_with_size, gfx::Image image);
  void SetIsUsingGAIAPicture(bool value);
  void SetLastDownloadedGAIAPictureUrlWithSize(
      const std::string& image_url_with_size);
  void SetSignedInWithCredentialProvider(bool value);
  void SetDasherlessManagement(bool value);
  // Only non-omitted profiles can be set as non-ephemeral. It's the
  // responsibility of the caller to make sure that the entry is set as
  // non-ephemeral only if prefs::kForceEphemeralProfiles is false.
  void SetIsEphemeral(bool value);
  void SetUserAcceptedAccountManagement(bool value);
  bool UserAcceptedAccountManagement() const;
  void SetIsUsingDefaultAvatar(bool value);
  void SetAvatarIconIndex(size_t icon_index);
  // std::nullopt resets colors to default.
  void SetProfileThemeColors(const std::optional<ProfileThemeColors>& colors);

  // Unlike for other string setters, the argument is expected to be UTF8
  // encoded.
  void SetHostedDomain(std::string hosted_domain);
  void SetIsManaged(signin::Tribool value);

  void SetProfileManagementEnrollmentToken(const std::string& enrollment_token);
  void SetProfileManagementOidcTokens(
      const ProfileManagementOidcTokens& oidc_tokens);
  void SetProfileManagementId(const std::string& id);

  void SetAuthInfo(const GaiaId& gaia_id,
                   const std::u16string& user_name,
                   bool is_consented_primary_account);

  void SetIsGlicEligible(bool value);

  // Update info about accounts. These functions are idempotent, only the first
  // call for a given input matters.
  void AddAccountName(const std::string& name);

  // Clears info about all accounts that have been added in the past via
  // AddAccountName().
  void ClearAccountNames();

  // Lock/Unlock the profile, should be called only if force-sign-in is enabled.
  void LockForceSigninProfile(bool is_lock);

  // Records aggregate metrics about all accounts used in this profile.
  void RecordAccountNamesMetric() const;

  static const char kSupervisedUserId[];
  static const char kAvatarIconKey[];
  static const char kBackgroundAppsKey[];
  static const char kProfileIsEphemeral[];
  static const char kUserNameKey[];
  static const char kGAIAIdKey[];
  static const char kIsConsentedPrimaryAccountKey[];
  static const char kNameKey[];
  static const char kEnterpriseLabelKey[];
  static const char kIsUsingDefaultNameKey[];
  static const char kIsUsingDefaultAvatarKey[];
  static const char kUseGAIAPictureKey[];
  static const char kAccountIdKey[];
  static const char kIsGlicEligible[];

 private:
  friend class ProfileAttributesStorage;
  FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
                           EntryInternalAccessors);
  FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, ProfileActiveTime);
  FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
                           DownloadHighResAvatarTest);

  // Initializes the current entry instance. The callers must subsequently call
  // InitializeLastNameToDisplay() for this entry.
  void Initialize(ProfileAttributesStorage* storage,
                  const base::FilePath& path,
                  PrefService* prefs);

  // Sets the initial name of the profile to be displayed. The name might depend
  // on other's profiles names so this must be called only after all profiles
  // has been initialized.
  void InitializeLastNameToDisplay();
  std::u16string GetLastNameToDisplay() const;

  // Returns true if:
  // - The user has chosen a local profile name on purpose. One exception where
  //   we don't show the local profile name, is when it is equal to the
  //   GAIA name.
  // - If two profiles have the same GAIA name and we need to show the local
  //   profile name to clear ambiguity.
  bool ShouldShowProfileLocalName(
      const std::u16string& gaia_name_to_display) const;

  // Returns true if the current GAIA picture should be updated with an image
  // having provided parameters. `image_is_empty` is true when attempting to
  // clear the current GAIA picture.
  bool ShouldUpdateGAIAPicture(const std::string& image_url_with_size,
                               bool image_is_empty) const;

  // Loads or uses an already loaded high resolution image of the generic
  // profile avatar.
  const gfx::Image* GetHighResAvatar() const;

  // Generates the colored placeholder avatar icon for the given |size|.
  gfx::Image GetPlaceholderAvatarIcon(
      int size,
      const profiles::PlaceholderAvatarIconParams& icon_params) const;

  // Returns if this profile has accounts (signed-in or signed-out) with
  // different account names. This is approximate as only a short hash of an
  // account name is stored so there can be false negatives.
  bool HasMultipleAccountNames() const;

  // Loads and saves the data to the local state.
  const base::Value::Dict* GetEntryData() const;

  // Internal getter that returns a base::Value*, or nullptr if the key is not
  // present.
  const base::Value* GetValue(const char* key) const;

  // Internal getters that return basic data types. If the key is not present,
  // or if the data is in a wrong data type, return empty string, 0.0, false or
  // -1 depending on the target data type. We do not assume that the data type
  // is correct because the local state file can be modified by a third party.
  std::string GetString(const char* key) const;
  std::u16string GetString16(const char* key) const;
  double GetDouble(const char* key) const;
  bool GetBool(const char* key) const;
  int GetInteger(const char* key) const;

  // Internal getter that returns one of the profile theme colors or
  // std::nullopt if the key is not present.
  std::optional<SkColor> GetProfileThemeColor(const char* key) const;

  // Type checking. Only IsDouble is implemented because others do not have
  // callsites.
  bool IsDouble(const char* key) const;

  // Internal setters that accept basic data types. Return if the original data
  // is different from the new data, i.e. whether actual update is done.
  // If the data was missing or was from a different type and `value` is the
  // default value (e.g. false, 0, empty string...), the value is explicitly
  // written but these return false.
  bool SetString(const char* key, const std::string& value);
  bool SetString16(const char* key, const std::u16string& value);
  bool SetDouble(const char* key, double value);
  bool SetBool(const char* key, bool value);
  bool SetInteger(const char* key, int value);

  // Generic setter, used to implement the more specific ones. If the value was
  // missing and `value` is the default value (e.g. false, 0, empty string...),
  // the value is written and this returns true.
  bool SetValue(const char* key, base::Value value);

  // Clears value stored for |key|. Returns if the original data is different
  // from the new data, i.e. whether actual update is done.
  bool ClearValue(const char* key);

  // Migrate/cleanup deprecated keys in profile attributes. Over time, long
  // deprecated keys should be removed as new ones are added, but this call
  // should never go away (even if it becomes an empty call for some time) as it
  // should remain *the* place to drop deprecated profile attributes keys at.
  void MigrateObsoleteProfileAttributes();

  // Internal version of `SetIsOmitted()` that doesn't trigger any
  // notifications.
  void SetIsOmittedInternal(bool is_omitted);

  raw_ptr<ProfileAttributesStorage> profile_attributes_storage_ = nullptr;
  raw_ptr<PrefService> prefs_ = nullptr;
  base::FilePath profile_path_;
  std::string storage_key_;
  std::u16string last_name_to_display_;

  // Indicates whether the profile should not be displayed to the user in the
  // list of profiles. This flag is intended to work only with ephemeral
  // profiles which get removed after the browser restart. Thus, this flag is
  // stored in memory only. Storing in memory also allows to avoid the risk of
  // having permanent profiles that the user cannot see or delete, in case the
  // ephemeral profile deletion fails.
  bool is_omitted_ = false;
};

#endif  // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_