unified_consent_service.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/unified_consent/unified_consent_service.h"
  5. #include "base/check_op.h"
  6. #include "base/metrics/histogram_macros.h"
  7. #include "base/scoped_observation.h"
  8. #include "base/threading/sequenced_task_runner_handle.h"
  9. #include "build/build_config.h"
  10. #include "components/pref_registry/pref_registry_syncable.h"
  11. #include "components/sync/base/user_selectable_type.h"
  12. #include "components/sync/driver/sync_service.h"
  13. #include "components/sync/driver/sync_user_settings.h"
  14. #include "components/sync_preferences/pref_service_syncable.h"
  15. #include "components/unified_consent/pref_names.h"
  16. namespace unified_consent {
  17. UnifiedConsentService::UnifiedConsentService(
  18. sync_preferences::PrefServiceSyncable* pref_service,
  19. signin::IdentityManager* identity_manager,
  20. syncer::SyncService* sync_service,
  21. const std::vector<std::string>& service_pref_names)
  22. : pref_service_(pref_service),
  23. identity_manager_(identity_manager),
  24. sync_service_(sync_service),
  25. service_pref_names_(service_pref_names) {
  26. DCHECK(pref_service_);
  27. DCHECK(identity_manager_);
  28. DCHECK(sync_service_);
  29. if (GetMigrationState() == MigrationState::kNotInitialized)
  30. MigrateProfileToUnifiedConsent();
  31. pref_service_->AddObserver(this);
  32. identity_manager_->AddObserver(this);
  33. sync_service_->AddObserver(this);
  34. }
  35. UnifiedConsentService::~UnifiedConsentService() {}
  36. // static
  37. void UnifiedConsentService::RegisterPrefs(
  38. user_prefs::PrefRegistrySyncable* registry) {
  39. registry->RegisterBooleanPref(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
  40. false);
  41. registry->RegisterIntegerPref(
  42. prefs::kUnifiedConsentMigrationState,
  43. static_cast<int>(MigrationState::kNotInitialized));
  44. }
  45. void UnifiedConsentService::SetUrlKeyedAnonymizedDataCollectionEnabled(
  46. bool enabled) {
  47. if (GetMigrationState() != MigrationState::kCompleted)
  48. SetMigrationState(MigrationState::kCompleted);
  49. pref_service_->SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
  50. enabled);
  51. }
  52. void UnifiedConsentService::Shutdown() {
  53. pref_service_->RemoveObserver(this);
  54. identity_manager_->RemoveObserver(this);
  55. sync_service_->RemoveObserver(this);
  56. }
  57. void UnifiedConsentService::OnPrimaryAccountChanged(
  58. const signin::PrimaryAccountChangeEvent& event) {
  59. if (event.GetEventTypeFor(signin::ConsentLevel::kSync) ==
  60. signin::PrimaryAccountChangeEvent::Type::kCleared) {
  61. // By design, clearing the primary account disables URL-keyed data
  62. // collection.
  63. SetUrlKeyedAnonymizedDataCollectionEnabled(false);
  64. }
  65. }
  66. void UnifiedConsentService::OnStateChanged(syncer::SyncService* sync) {
  67. // Start observing pref changes when the user enters sync setup.
  68. // Note: Only |sync->IsSetupInProgress()| is used (i.e. no check for
  69. // |IsFirstSetupComplete()|), because on Android |SetFirstSetupComplete()| is
  70. // called automatically during the first setup, i.e. the value could change in
  71. // the meantime.
  72. if (sync->IsSetupInProgress() && !pref_service_->IsSyncing()) {
  73. StartObservingServicePrefChanges();
  74. } else {
  75. StopObservingServicePrefChanges();
  76. // If the user cancelled the sync setup, clear all observed changes.
  77. if (!sync->CanSyncFeatureStart())
  78. service_pref_changes_.clear();
  79. }
  80. if (!sync_service_->CanSyncFeatureStart() ||
  81. !sync_service_->IsEngineInitialized()) {
  82. return;
  83. }
  84. if (GetMigrationState() == MigrationState::kInProgressWaitForSyncInit)
  85. UpdateSettingsForMigration();
  86. }
  87. void UnifiedConsentService::OnIsSyncingChanged() {
  88. if (pref_service_->IsSyncing() && !service_pref_changes_.empty()) {
  89. // Re-apply all observed service pref changes.
  90. // If any service prefs had a value coming in through Sync, then that
  91. // would've overridden any changes that the user made during the first sync
  92. // setup. So re-apply the local changes to make sure they stick.
  93. for (const auto& pref_change : service_pref_changes_) {
  94. pref_service_->Set(pref_change.first, pref_change.second);
  95. }
  96. service_pref_changes_.clear();
  97. }
  98. }
  99. void UnifiedConsentService::StartObservingServicePrefChanges() {
  100. if (!service_pref_change_registrar_.IsEmpty())
  101. return;
  102. service_pref_change_registrar_.Init(pref_service_);
  103. for (const std::string& pref_name : service_pref_names_) {
  104. service_pref_change_registrar_.Add(
  105. pref_name,
  106. base::BindRepeating(&UnifiedConsentService::ServicePrefChanged,
  107. base::Unretained(this)));
  108. }
  109. }
  110. void UnifiedConsentService::StopObservingServicePrefChanges() {
  111. service_pref_change_registrar_.RemoveAll();
  112. }
  113. void UnifiedConsentService::ServicePrefChanged(const std::string& name) {
  114. DCHECK(sync_service_->IsSetupInProgress());
  115. const base::Value& value = pref_service_->GetValue(name);
  116. service_pref_changes_[name] = value.Clone();
  117. }
  118. MigrationState UnifiedConsentService::GetMigrationState() {
  119. int migration_state_int =
  120. pref_service_->GetInteger(prefs::kUnifiedConsentMigrationState);
  121. DCHECK_LE(static_cast<int>(MigrationState::kNotInitialized),
  122. migration_state_int);
  123. DCHECK_GE(static_cast<int>(MigrationState::kCompleted), migration_state_int);
  124. return static_cast<MigrationState>(migration_state_int);
  125. }
  126. void UnifiedConsentService::SetMigrationState(MigrationState migration_state) {
  127. pref_service_->SetInteger(prefs::kUnifiedConsentMigrationState,
  128. static_cast<int>(migration_state));
  129. }
  130. void UnifiedConsentService::MigrateProfileToUnifiedConsent() {
  131. DCHECK_EQ(GetMigrationState(), MigrationState::kNotInitialized);
  132. if (!identity_manager_->HasPrimaryAccount(signin::ConsentLevel::kSync)) {
  133. SetMigrationState(MigrationState::kCompleted);
  134. return;
  135. }
  136. UpdateSettingsForMigration();
  137. }
  138. void UnifiedConsentService::UpdateSettingsForMigration() {
  139. if (!sync_service_->IsEngineInitialized()) {
  140. SetMigrationState(MigrationState::kInProgressWaitForSyncInit);
  141. return;
  142. }
  143. // Set URL-keyed anonymized metrics to the state it had before unified
  144. // consent.
  145. bool url_keyed_metrics_enabled =
  146. sync_service_->IsSyncFeatureEnabled() &&
  147. sync_service_->GetUserSettings()->GetSelectedTypes().Has(
  148. syncer::UserSelectableType::kHistory) &&
  149. !sync_service_->GetUserSettings()->IsUsingExplicitPassphrase();
  150. SetUrlKeyedAnonymizedDataCollectionEnabled(url_keyed_metrics_enabled);
  151. }
  152. } // namespace unified_consent