File: sync_user_settings_impl.cc

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 (486 lines) | stat: -rw-r--r-- 17,658 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/service/sync_user_settings_impl.h"

#include <utility>

#include "base/check.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/version.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/sync/base/features.h"
#include "components/sync/base/passphrase_enums.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/engine/nigori/nigori.h"
#include "components/sync/service/sync_prefs.h"
#include "components/sync/service/sync_service_crypto.h"
#include "components/version_info/version_info.h"
#include "google_apis/gaia/gaia_id.h"

namespace syncer {

namespace {

// Converts `selected_types` to the corresponding DataTypeSet (e.g.
// {kExtensions} becomes {EXTENSIONS, EXTENSION_SETTINGS}).
DataTypeSet UserSelectableTypesToDataTypes(UserSelectableTypeSet selected_types,
                                           bool is_explicit_browser_sign_in) {
  DataTypeSet preferred_types;
  for (UserSelectableType type : selected_types) {
    if (type == UserSelectableType::kPayments && !is_explicit_browser_sign_in) {
      // If sign-in is implicit (legacy desktop Dice state), types such as
      // AUTOFILL_WALLET_METADATA must be excluded.
      preferred_types.PutAll(Intersection(
          UserSelectableTypeToAllDataTypes(type),
          DataTypeSet{AUTOFILL_WALLET_DATA, AUTOFILL_WALLET_CREDENTIAL,
                      AUTOFILL_WALLET_USAGE}));
      continue;
    }
    preferred_types.PutAll(UserSelectableTypeToAllDataTypes(type));
  }
  return preferred_types;
}

#if BUILDFLAG(IS_CHROMEOS)
DataTypeSet UserSelectableOsTypesToDataTypes(
    UserSelectableOsTypeSet selected_types) {
  DataTypeSet preferred_types;
  for (UserSelectableOsType type : selected_types) {
    preferred_types.PutAll(UserSelectableOsTypeToAllDataTypes(type));
  }
  return preferred_types;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

int GetCurrentMajorProductVersion() {
  DCHECK(version_info::GetVersion().IsValid());
  return version_info::GetVersion().components()[0];
}

// Checks if the AUTOFILL_WALLET_CREDENTIAL should be ignored if it is the only
// encrypted datatype.
bool ShouldAutofillWalletCredentialBeIgnoredIfOnlyEncryptedType(
    const SyncPrefs& prefs) {
  // Explicit sign-in to the browser via native UI, making this scenario an edge
  // case as more features will usually be enabled, including PASSWORDS. Thus,
  // AUTOFILL_WALLET_CREDENTIAL is not the only active encrypted type.
  if (prefs.IsExplicitBrowserSignin()) {
    return false;
  }
  // Similar to above: more features will usually be enabled, including
  // PASSWORDS, making this an edge case.
  if (base::FeatureList::IsEnabled(
          syncer::kReplaceSyncPromosWithSignInPromos)) {
    return false;
  }
  // AUTOFILL_WALLET_CREDENTIAL is the only active encrypted type for the
  // previously signed-in-non-syncing users.
  return true;
}

}  // namespace

SyncUserSettingsImpl::SyncUserSettingsImpl(Delegate* delegate,
                                           SyncServiceCrypto* crypto,
                                           SyncPrefs* prefs,
                                           DataTypeSet registered_data_types)
    : delegate_(delegate),
      crypto_(crypto),
      prefs_(prefs),
      registered_data_types_(registered_data_types) {
  CHECK(delegate_);
  CHECK(crypto_);
  CHECK(prefs_);
  prefs_observation_.Observe(prefs_);
}

SyncUserSettingsImpl::~SyncUserSettingsImpl() = default;

bool SyncUserSettingsImpl::IsInitialSyncFeatureSetupComplete() const {
  return prefs_->IsInitialSyncFeatureSetupComplete();
}

#if !BUILDFLAG(IS_CHROMEOS)
void SyncUserSettingsImpl::SetInitialSyncFeatureSetupComplete(
    SyncFirstSetupCompleteSource source) {
  if (IsInitialSyncFeatureSetupComplete()) {
    return;
  }
  UMA_HISTOGRAM_ENUMERATION("Signin.SyncFirstSetupCompleteSource", source);
  prefs_->SetInitialSyncFeatureSetupComplete();
  delegate_->OnInitialSyncFeatureSetupCompleted();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

bool SyncUserSettingsImpl::IsSyncEverythingEnabled() const {
  return prefs_->HasKeepEverythingSynced();
}

UserSelectableTypeSet SyncUserSettingsImpl::GetSelectedTypes() const {
  UserSelectableTypeSet types;

  switch (delegate_->GetSyncAccountStateForPrefs()) {
    case SyncPrefs::SyncAccountState::kNotSignedIn: {
      return UserSelectableTypeSet();
    }
    case SyncPrefs::SyncAccountState::kSignedInNotSyncing: {
      types = prefs_->GetSelectedTypesForAccount(
          delegate_->GetSyncAccountInfoForPrefs().gaia);
      break;
    }
    case SyncPrefs::SyncAccountState::kSyncing: {
      types = prefs_->GetSelectedTypesForSyncingUser();
      break;
    }
  }
  types.RetainAll(GetRegisteredSelectableTypes());

  return types;
}

bool SyncUserSettingsImpl::IsTypeManagedByPolicy(
    UserSelectableType type) const {
  return prefs_->IsTypeManagedByPolicy(type);
}

bool SyncUserSettingsImpl::IsTypeManagedByCustodian(
    UserSelectableType type) const {
  return prefs_->IsTypeManagedByCustodian(type);
}

SyncUserSettings::UserSelectableTypePrefState
SyncUserSettingsImpl::GetTypePrefStateForAccount(
    UserSelectableType type) const {
  if (delegate_->GetSyncAccountStateForPrefs() !=
      SyncPrefs::SyncAccountState::kSignedInNotSyncing) {
    return SyncUserSettings::UserSelectableTypePrefState::kNotApplicable;
  }
  if (prefs_->IsTypeDisabledByUserForAccount(
          type, delegate_->GetSyncAccountInfoForPrefs().gaia)) {
    return SyncUserSettings::UserSelectableTypePrefState::kDisabled;
  }
  return SyncUserSettings::UserSelectableTypePrefState::kEnabledOrDefault;
}

void SyncUserSettingsImpl::SetSelectedTypes(bool sync_everything,
                                            UserSelectableTypeSet types) {
  UserSelectableTypeSet registered_types = GetRegisteredSelectableTypes();
  DCHECK(registered_types.HasAll(types))
      << "\n registered: " << UserSelectableTypeSetToString(registered_types)
      << "\n setting to: " << UserSelectableTypeSetToString(types);

  switch (delegate_->GetSyncAccountStateForPrefs()) {
    case SyncPrefs::SyncAccountState::kNotSignedIn:
      NOTREACHED();
    case SyncPrefs::SyncAccountState::kSignedInNotSyncing:
      for (UserSelectableType type : registered_types) {
        SetSelectedType(type, types.Has(type) || sync_everything);
      }
      break;
    case SyncPrefs::SyncAccountState::kSyncing:
      prefs_->SetSelectedTypesForSyncingUser(sync_everything, registered_types,
                                             types);
      break;
  }
}

void SyncUserSettingsImpl::SetSelectedType(UserSelectableType type,
                                           bool is_type_on) {
  UserSelectableTypeSet registered_types = GetRegisteredSelectableTypes();
  CHECK(registered_types.Has(type));

  switch (delegate_->GetSyncAccountStateForPrefs()) {
    case SyncPrefs::SyncAccountState::kNotSignedIn:
      NOTREACHED();
    case SyncPrefs::SyncAccountState::kSignedInNotSyncing: {
      prefs_->SetSelectedTypeForAccount(
          type, is_type_on, delegate_->GetSyncAccountInfoForPrefs().gaia);
      break;
    }
    case SyncPrefs::SyncAccountState::kSyncing: {
      DUMP_WILL_BE_CHECK(!IsSyncEverythingEnabled());
      syncer::UserSelectableTypeSet selected_types =
          is_type_on ? base::Union(GetSelectedTypes(), {type})
                     : base::Difference(GetSelectedTypes(), {type});
      SetSelectedTypes(IsSyncEverythingEnabled(), selected_types);
      break;
    }
  }
}

void SyncUserSettingsImpl::ResetSelectedType(UserSelectableType type) {
  CHECK_EQ(SyncPrefs::SyncAccountState::kSignedInNotSyncing,
           delegate_->GetSyncAccountStateForPrefs());
  prefs_->ResetSelectedTypeForAccount(
      type, delegate_->GetSyncAccountInfoForPrefs().gaia);
}

void SyncUserSettingsImpl::KeepAccountSettingsPrefsOnlyForUsers(
    const std::vector<GaiaId>& available_gaia_ids) {
  prefs_->KeepAccountSettingsPrefsOnlyForUsers(available_gaia_ids);
}

UserSelectableTypeSet SyncUserSettingsImpl::GetRegisteredSelectableTypes()
    const {
  UserSelectableTypeSet registered_types;
  for (UserSelectableType type : UserSelectableTypeSet::All()) {
    if (!base::Intersection(registered_data_types_,
                            UserSelectableTypeToAllDataTypes(type))
             .empty()) {
      registered_types.Put(type);
    }
  }
  return registered_types;
}

#if BUILDFLAG(IS_CHROMEOS)
void SyncUserSettingsImpl::SetSyncFeatureDisabledViaDashboard() {
  prefs_->SetSyncFeatureDisabledViaDashboard();
}

void SyncUserSettingsImpl::ClearSyncFeatureDisabledViaDashboard() {
  if (!IsSyncFeatureDisabledViaDashboard()) {
    return;
  }
  prefs_->ClearSyncFeatureDisabledViaDashboard();
  delegate_->OnSyncFeatureDisabledViaDashboardCleared();
}

bool SyncUserSettingsImpl::IsSyncFeatureDisabledViaDashboard() const {
  return prefs_->IsSyncFeatureDisabledViaDashboard();
}

bool SyncUserSettingsImpl::IsSyncAllOsTypesEnabled() const {
  return prefs_->IsSyncAllOsTypesEnabled();
}

UserSelectableOsTypeSet SyncUserSettingsImpl::GetSelectedOsTypes() const {
  UserSelectableOsTypeSet types = prefs_->GetSelectedOsTypes();
  types.RetainAll(GetRegisteredSelectableOsTypes());
  return types;
}

bool SyncUserSettingsImpl::IsOsTypeManagedByPolicy(
    UserSelectableOsType type) const {
  return prefs_->IsOsTypeManagedByPolicy(type);
}

void SyncUserSettingsImpl::SetSelectedOsTypes(bool sync_all_os_types,
                                              UserSelectableOsTypeSet types) {
  UserSelectableOsTypeSet registered_types = GetRegisteredSelectableOsTypes();
  DCHECK(registered_types.HasAll(types));
  prefs_->SetSelectedOsTypes(sync_all_os_types, registered_types, types);
}

UserSelectableOsTypeSet SyncUserSettingsImpl::GetRegisteredSelectableOsTypes()
    const {
  UserSelectableOsTypeSet registered_types;
  for (UserSelectableOsType type : UserSelectableOsTypeSet::All()) {
    if (!base::Intersection(registered_data_types_,
                            UserSelectableOsTypeToAllDataTypes(type))
             .empty()) {
      registered_types.Put(type);
    }
  }
  return registered_types;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

bool SyncUserSettingsImpl::IsCustomPassphraseAllowed() const {
  return delegate_->IsCustomPassphraseAllowed();
}

bool SyncUserSettingsImpl::IsEncryptEverythingEnabled() const {
  return crypto_->IsEncryptEverythingEnabled();
}

bool SyncUserSettingsImpl::IsPassphraseRequired() const {
  return crypto_->IsPassphraseRequired();
}

bool SyncUserSettingsImpl::IsPassphraseRequiredForPreferredDataTypes() const {
  // If there is an encrypted datatype enabled and we don't have the proper
  // passphrase, we must prompt the user for a passphrase. The only way for the
  // user to avoid entering their passphrase is to disable the encrypted types.
  return IsEncryptedDatatypePreferred() && IsPassphraseRequired();
}

bool SyncUserSettingsImpl::IsPassphrasePromptMutedForCurrentProductVersion()
    const {
  return prefs_->GetPassphrasePromptMutedProductVersion() ==
         GetCurrentMajorProductVersion();
}

void SyncUserSettingsImpl::MarkPassphrasePromptMutedForCurrentProductVersion() {
  prefs_->SetPassphrasePromptMutedProductVersion(
      GetCurrentMajorProductVersion());
}

bool SyncUserSettingsImpl::IsTrustedVaultKeyRequired() const {
  return crypto_->IsTrustedVaultKeyRequired();
}

bool SyncUserSettingsImpl::IsTrustedVaultKeyRequiredForPreferredDataTypes()
    const {
  return IsEncryptedDatatypePreferred() && crypto_->IsTrustedVaultKeyRequired();
}

bool SyncUserSettingsImpl::IsTrustedVaultRecoverabilityDegraded() const {
  return IsEncryptedDatatypePreferred() &&
         crypto_->IsTrustedVaultRecoverabilityDegraded();
}

bool SyncUserSettingsImpl::IsUsingExplicitPassphrase() const {
  // TODO(crbug.com/40923935): Either make this method return a Tribool, so the
  // "unknown" case is properly communicated, or just remove it altogether
  // (callers can always use the global IsExplicitPassphrase() helper).
  std::optional<PassphraseType> type = GetPassphraseType();
  if (!type.has_value()) {
    return false;
  }
  return IsExplicitPassphrase(*type);
}

base::Time SyncUserSettingsImpl::GetExplicitPassphraseTime() const {
  return crypto_->GetExplicitPassphraseTime();
}

std::optional<PassphraseType> SyncUserSettingsImpl::GetPassphraseType() const {
  return crypto_->GetPassphraseType();
}

void SyncUserSettingsImpl::SetEncryptionPassphrase(
    const std::string& passphrase) {
  crypto_->SetEncryptionPassphrase(passphrase);
}

bool SyncUserSettingsImpl::SetDecryptionPassphrase(
    const std::string& passphrase) {
  DCHECK(IsPassphraseRequired())
      << "SetDecryptionPassphrase must not be called when "
         "IsPassphraseRequired() is false.";

  DVLOG(1) << "Setting passphrase for decryption.";

  return crypto_->SetDecryptionPassphrase(passphrase);
}

void SyncUserSettingsImpl::SetExplicitPassphraseDecryptionNigoriKey(
    std::unique_ptr<Nigori> nigori) {
  return crypto_->SetExplicitPassphraseDecryptionNigoriKey(std::move(nigori));
}

std::unique_ptr<Nigori>
SyncUserSettingsImpl::GetExplicitPassphraseDecryptionNigoriKey() const {
  return crypto_->GetExplicitPassphraseDecryptionNigoriKey();
}

DataTypeSet SyncUserSettingsImpl::GetPreferredDataTypes() const {
  DataTypeSet types = UserSelectableTypesToDataTypes(
      GetSelectedTypes(), prefs_->IsExplicitBrowserSignin() ||
                              delegate_->GetSyncAccountStateForPrefs() ==
                                  SyncPrefs::SyncAccountState::kSyncing);

  types.PutAll(AlwaysPreferredUserTypes());
#if BUILDFLAG(IS_CHROMEOS)
  types.PutAll(UserSelectableOsTypesToDataTypes(GetSelectedOsTypes()));
#endif
  types.RetainAll(registered_data_types_);

  // Control types (in practice, NIGORI) are always considered "preferred", even
  // though they're technically not registered.
  types.PutAll(ControlTypes());

  static_assert(55 == GetNumDataTypes(),
                "If adding a new sync data type, update the list below below if"
                " you want to disable the new data type for local sync, aka"
                " roaming profiles on Windows.");
  if (prefs_->IsLocalSyncEnabled()) {
    types.Remove(APP_LIST);
    // Note: AUTOFILL_WALLET_CREDENTIAL *is* supported - the user can still save
    // CVVs for local credit cards.
    types.Remove(AUTOFILL_VALUABLE);
    types.Remove(AUTOFILL_WALLET_DATA);
    types.Remove(AUTOFILL_WALLET_METADATA);
    types.Remove(AUTOFILL_WALLET_OFFER);
    types.Remove(AUTOFILL_WALLET_USAGE);
    types.Remove(COLLABORATION_GROUP);
    types.Remove(CONTACT_INFO);
    types.Remove(COOKIES);
    types.Remove(HISTORY);
    types.Remove(HISTORY_DELETE_DIRECTIVES);
    types.Remove(INCOMING_PASSWORD_SHARING_INVITATION);
    types.Remove(OUTGOING_PASSWORD_SHARING_INVITATION);
    types.Remove(PLUS_ADDRESS);
    types.Remove(PLUS_ADDRESS_SETTING);
    types.Remove(SECURITY_EVENTS);
    types.Remove(SEND_TAB_TO_SELF);
    types.Remove(SHARED_TAB_GROUP_ACCOUNT_DATA);
    types.Remove(SHARED_TAB_GROUP_DATA);
    types.Remove(SHARING_MESSAGE);
    types.Remove(USER_CONSENTS);
    types.Remove(USER_EVENTS);
    types.Remove(WORKSPACE_DESK);
  }
  return types;
}

DataTypeSet SyncUserSettingsImpl::GetAllEncryptedDataTypes() const {
  return crypto_->GetAllEncryptedDataTypes();
}

bool SyncUserSettingsImpl::IsEncryptedDatatypePreferred() const {
  DataTypeSet preferred_types = GetPreferredDataTypes();
  const DataTypeSet encrypted_types = GetAllEncryptedDataTypes();
  DCHECK(encrypted_types.HasAll(AlwaysEncryptedUserTypes()));
  if (ShouldAutofillWalletCredentialBeIgnoredIfOnlyEncryptedType(*prefs_)) {
    // Remove AUTOFILL_WALLET_CREDENTIAL from the set to avoid that the
    // function returns true for the case where the set ONLY includes
    // AUTOFILL_WALLET_CREDENTIAL. This feature alone is not sufficient to
    // trigger error UI, which may be confusing to some users given that strings
    // may allude to passwords. This is a side effect of
    // AUTOFILL_WALLET_CREDENTIAL being listed as AlwaysEncryptedUserTypes().
    preferred_types.Remove(syncer::AUTOFILL_WALLET_CREDENTIAL);
  }
  return !Intersection(preferred_types, encrypted_types).empty();
}

std::string SyncUserSettingsImpl::GetEncryptionBootstrapToken() const {
  const GaiaId& gaia_id = delegate_->GetSyncAccountInfoForPrefs().gaia;
  if (gaia_id.empty()) {
    return std::string();
  }
  return prefs_->GetEncryptionBootstrapTokenForAccount(gaia_id);
}

void SyncUserSettingsImpl::SetEncryptionBootstrapToken(
    const std::string& token) {
  const GaiaId& gaia_id = delegate_->GetSyncAccountInfoForPrefs().gaia;
  if (gaia_id.empty()) {
    // The user must be signed in, so the only legit scenario where SyncService
    // uses no Gaia ID is local sync (roaming profiles).
    CHECK(prefs_->IsLocalSyncEnabled());
    return;
  }
  prefs_->SetEncryptionBootstrapTokenForAccount(token, gaia_id);
}

bool SyncUserSettingsImpl::IsSyncClientDisabledByPolicy() const {
  return prefs_->IsSyncClientDisabledByPolicy();
}

void SyncUserSettingsImpl::OnSyncManagedPrefChange(bool is_sync_managed) {
  delegate_->OnSyncClientDisabledByPolicyChanged();
}

void SyncUserSettingsImpl::OnSelectedTypesPrefChange() {
  delegate_->OnSelectedTypesChanged();
}

}  // namespace syncer