Commit c22870bf authored by Thomas Tangl's avatar Thomas Tangl Committed by Commit Bot

[unified-consent] Revoke unified consent when service is disabled on startup

When a unified consent child service is disabled on startup, unified
consent gets disabled.

This is a follow up to crrev.com/c/1151324.

Bug: 867480
Change-Id: Ic7cf4fdd0ebafa648954a401162ed649e1e2f40d
Reviewed-on: https://chromium-review.googlesource.com/1162160
Commit-Queue: Thomas Tangl <tangltom@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580536}
parent 11dfaf2c
......@@ -46,6 +46,12 @@ UnifiedConsentService::UnifiedConsentService(
base::BindRepeating(
&UnifiedConsentService::OnUnifiedConsentGivenPrefChanged,
base::Unretained(this)));
// If somebody disabled any of the non-personalized services while Chrome
// wasn't running, disable unified consent.
if (!AreAllNonPersonalizedServicesEnabled() && IsUnifiedConsentGiven()) {
SetUnifiedConsentGiven(false);
}
}
UnifiedConsentService::~UnifiedConsentService() {}
......@@ -232,4 +238,17 @@ void UnifiedConsentService::MigrateProfileToUnifiedConsent() {
SetSyncEverythingIfPossible(false);
}
bool UnifiedConsentService::AreAllNonPersonalizedServicesEnabled() {
for (int i = 0; i <= static_cast<int>(Service::kLast); ++i) {
Service service = static_cast<Service>(i);
if (service_client_->GetServiceState(service) == ServiceState::kDisabled)
return false;
}
if (!pref_service_->GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled))
return false;
return true;
}
} // namespace unified_consent
......@@ -97,6 +97,9 @@ class UnifiedConsentService : public KeyedService,
// inconsistencies with sync-related prefs.
void MigrateProfileToUnifiedConsent();
// Returns true if all non-personalized services are enabled.
bool AreAllNonPersonalizedServicesEnabled();
std::unique_ptr<UnifiedConsentServiceClient> service_client_;
PrefService* pref_service_;
identity::IdentityManager* identity_manager_;
......
......@@ -126,20 +126,8 @@ class UnifiedConsentServiceTest : public testing::Test {
consent_service_->service_client_.get();
}
// Returns true if all supported non-personalized services are enabled.
bool AreAllNonPersonalizedServicesEnabled() {
for (int service = 0; service <= static_cast<int>(Service::kLast);
++service) {
if (service_client_->GetServiceState(static_cast<Service>(service)) ==
ServiceState::kDisabled) {
return false;
}
}
if (!pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled))
return false;
return true;
return consent_service_->AreAllNonPersonalizedServicesEnabled();
}
protected:
......@@ -253,6 +241,36 @@ TEST_F(UnifiedConsentServiceTest, DisableUnfiedConsentWhenServiceIsDisabled) {
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
}
// Test whether unified consent is disabled when any of its dependent services
// gets disabled before startup.
TEST_F(UnifiedConsentServiceTest,
DisableUnfiedConsentWhenServiceIsDisabled_OnStartup) {
CreateConsentService();
identity_test_environment_.SetPrimaryAccount("testaccount");
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
EXPECT_FALSE(AreAllNonPersonalizedServicesEnabled());
// Enable Unified Consent enables all supported non-personalized features
pref_service_.SetBoolean(prefs::kUnifiedConsentGiven, true);
EXPECT_TRUE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_TRUE(AreAllNonPersonalizedServicesEnabled());
// Simulate shutdown.
consent_service_->Shutdown();
consent_service_.reset();
// Disable child service.
pref_service_.SetBoolean(kSpellCheckDummyEnabled, false);
// Unified Consent is disabled during creation of the consent service because
// not all non-personalized services are enabled.
CreateConsentService();
EXPECT_FALSE(AreAllNonPersonalizedServicesEnabled());
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
}
#if !defined(OS_CHROMEOS)
TEST_F(UnifiedConsentServiceTest, Migration_SyncingEverything) {
base::HistogramTester histogram_tester;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment