chromium/components/unified_consent/unified_consent_service.cc

// 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/unified_consent/unified_consent_service.h"

#include "base/check_op.h"
#include "build/build_config.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/sync/base/features.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "components/unified_consent/pref_names.h"

namespace unified_consent {

// static
UnifiedConsentService::SyncState UnifiedConsentService::GetSyncState(
    const syncer::SyncService* sync_service) {}

// static
bool UnifiedConsentService::ShouldEnableUrlKeyedAnonymizedDataCollection(
    SyncState old_sync_state,
    SyncState new_sync_state) {}

// static
bool UnifiedConsentService::ShouldDisableUrlKeyedAnonymizedDataCollection(
    SyncState old_sync_state,
    SyncState new_sync_state) {}

UnifiedConsentService::UnifiedConsentService(
    sync_preferences::PrefServiceSyncable* pref_service,
    signin::IdentityManager* identity_manager,
    syncer::SyncService* sync_service,
    const std::vector<std::string>& service_pref_names)
    :{}

UnifiedConsentService::~UnifiedConsentService() = default;

// static
void UnifiedConsentService::RegisterPrefs(
    user_prefs::PrefRegistrySyncable* registry) {}

void UnifiedConsentService::SetUrlKeyedAnonymizedDataCollectionEnabled(
    bool enabled) {}

void UnifiedConsentService::Shutdown() {}

void UnifiedConsentService::OnPrimaryAccountChanged(
    const signin::PrimaryAccountChangeEvent& event) {}

void UnifiedConsentService::OnStateChanged(syncer::SyncService* sync) {}

void UnifiedConsentService::OnIsSyncingChanged() {}

void UnifiedConsentService::StartObservingServicePrefChanges() {}

void UnifiedConsentService::StopObservingServicePrefChanges() {}

void UnifiedConsentService::ServicePrefChanged(const std::string& name) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
MigrationState UnifiedConsentService::GetMigrationState() {
  int migration_state_int =
      pref_service_->GetInteger(prefs::kUnifiedConsentMigrationState);
  DCHECK_LE(static_cast<int>(MigrationState::kNotInitialized),
            migration_state_int);
  DCHECK_GE(static_cast<int>(MigrationState::kCompleted), migration_state_int);
  return static_cast<MigrationState>(migration_state_int);
}

void UnifiedConsentService::SetMigrationState(MigrationState migration_state) {
  pref_service_->SetInteger(prefs::kUnifiedConsentMigrationState,
                            static_cast<int>(migration_state));
}

void UnifiedConsentService::MigrateProfileToUnifiedConsent() {
  DCHECK_EQ(GetMigrationState(), MigrationState::kNotInitialized);

  // TODO(crbug.com/40066949): Simplify once kSync becomes unreachable or is
  // deleted from the codebase. See ConsentLevel::kSync documentation for
  // details.
  if (!identity_manager_->HasPrimaryAccount(signin::ConsentLevel::kSync)) {
    SetMigrationState(MigrationState::kCompleted);
    return;
  }

  UpdateSettingsForMigration();
}

void UnifiedConsentService::UpdateSettingsForMigration() {
  if (!sync_service_->IsEngineInitialized()) {
    SetMigrationState(MigrationState::kInProgressWaitForSyncInit);
    return;
  }

  // Set URL-keyed anonymized metrics to the state it had before unified
  // consent.
  // TODO(crbug.com/40066949): Simplify (remove the following block) after
  // Sync-the-feature users are migrated to ConsentLevel::kSignin, and thus
  // IsSyncFeatureEnabled() always returns false. (The UKM state for kSignin
  // users is set in OnStateChanged(), so no need for the logic here.)
  bool url_keyed_metrics_enabled =
      sync_service_->IsSyncFeatureEnabled() &&
      sync_service_->GetUserSettings()->GetSelectedTypes().Has(
          syncer::UserSelectableType::kHistory) &&
      !sync_service_->GetUserSettings()->IsUsingExplicitPassphrase();
  SetUrlKeyedAnonymizedDataCollectionEnabled(url_keyed_metrics_enabled);
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

}  //  namespace unified_consent