From 111728eb0737ceee8ea87dbb297e6aa2b4fc1082 Mon Sep 17 00:00:00 2001 From: Emerick Rogul Date: Tue, 8 Mar 2022 15:42:38 -0500 Subject: [PATCH] Disable kPrivacySandboxApisEnabledV2 privacy sandbox API preference Chromium change: https://source.chromium.org/chromium/chromium/src/+/7af9b25ea15c99177eeb7e1286c2bef002c70440 commit 7af9b25ea15c99177eeb7e1286c2bef002c70440 Author: sauski Date: Tue Feb 1 09:59:45 2022 +0000 Privacy Sandbox Settings: Introduce new un-synced primary preference This CL introduces V2 of the PrivacySandboxApisEnabled preference, which will replace the initial version as the primary control for Privacy Sandbox APIs. A migration is required as post Kartoffel release 3, the primary control will not be synced across devices. The new pref will be init appropriately during the confirmation moment of Kartoffel 3, and so is default off. All locations that consult or set the V1 preference have been updated to consult the new pref as required. The exception is the FLoC generated preference, which as FLoC has been discontinued will be removed and does not need to be updated. The PrivacySandboxSettings unit tests have been parameterized based on the value of the feature, with test utils updated to only set the appropriate pref. Bug: 1286276 --- browser/brave_profile_prefs.cc | 2 + .../privacy_sandbox_service_unittest.cc | 40 +++++++++++++++++++ .../privacy_sandbox_settings_unittest.cc | 6 ++- .../brave_privacy_sandbox_settings.cc | 10 ++++- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 926d3f0d996a..59f9e1c7d09e 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -306,6 +306,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { // Disable Chromium's privacy sandbox registry->SetDefaultPrefValue(prefs::kPrivacySandboxApisEnabled, base::Value(false)); + registry->SetDefaultPrefValue(prefs::kPrivacySandboxApisEnabledV2, + base::Value(false)); // Disable Chromium's privacy sandbox registry->SetDefaultPrefValue(prefs::kPrivacySandboxFlocEnabled, diff --git a/chromium_src/chrome/browser/privacy_sandbox/privacy_sandbox_service_unittest.cc b/chromium_src/chrome/browser/privacy_sandbox/privacy_sandbox_service_unittest.cc index b26dc794fde9..3cc4eeb8867f 100644 --- a/chromium_src/chrome/browser/privacy_sandbox/privacy_sandbox_service_unittest.cc +++ b/chromium_src/chrome/browser/privacy_sandbox/privacy_sandbox_service_unittest.cc @@ -153,3 +153,43 @@ TEST_F(PrivacySandboxServiceTest, OnPrivacySandboxPrefChanged) { prefs::kPrivacySandboxApisEnabled, true); testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer); } + +TEST_F(PrivacySandboxServiceTest, OnPrivacySandboxPrefV2Changed) { + // When either the main Privacy Sandbox pref, or the FLoC pref, are changed + // the FLoC ID should be reset. This will be propagated to the settings + // instance, which should then notify observers. + privacy_sandbox_test_util::MockPrivacySandboxObserver + mock_privacy_sandbox_observer; + PrivacySandboxSettingsFactory::GetForProfile(profile())->AddObserver( + &mock_privacy_sandbox_observer); + EXPECT_CALL(mock_privacy_sandbox_observer, + OnFlocDataAccessibleSinceUpdated(/*reset_compute_timer=*/true)); + + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxApisEnabledV2, false); + testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer); + + EXPECT_CALL(mock_privacy_sandbox_observer, + OnFlocDataAccessibleSinceUpdated(/*reset_compute_timer=*/true)); + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxFlocEnabled, false); + testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer); + + // OnFlocDataAccessibleSinceUpdated() will be called twice because the attempt + // to enable the pref will be immediately followed by setting it to false. + EXPECT_CALL(mock_privacy_sandbox_observer, + OnFlocDataAccessibleSinceUpdated(/*reset_compute_timer=*/true)) + .Times(2); + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxFlocEnabled, true); + testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer); + + // OnFlocDataAccessibleSinceUpdated() will be called twice because the attempt + // to enable the pref will be immediately followed by setting it to false. + EXPECT_CALL(mock_privacy_sandbox_observer, + OnFlocDataAccessibleSinceUpdated(/*reset_compute_timer=*/true)) + .Times(2); + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxApisEnabledV2, true); + testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer); +} diff --git a/chromium_src/components/privacy_sandbox/privacy_sandbox_settings_unittest.cc b/chromium_src/components/privacy_sandbox/privacy_sandbox_settings_unittest.cc index 07899390b8d7..53b5e11507a8 100644 --- a/chromium_src/components/privacy_sandbox/privacy_sandbox_settings_unittest.cc +++ b/chromium_src/components/privacy_sandbox/privacy_sandbox_settings_unittest.cc @@ -479,9 +479,11 @@ TEST_F(PrivacySandboxSettingsTest, IsPrivacySandboxEnabled) { EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled()); // Check that even bypassing PrivacySandboxSettings::SetPrivacySandboxEnabled, - // and manually updating the preference, we still don't get this enabled. + // and manually updating the preferences, we still don't get this enabled. profile()->GetTestingPrefService()->SetBoolean( prefs::kPrivacySandboxApisEnabled, true); + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxApisEnabledV2, true); EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled()); } @@ -503,6 +505,8 @@ TEST_F(PrivacySandboxSettingsTest, IsFlocAllowed) { // and manually updating the preferences, we still don't get this enabled. profile()->GetTestingPrefService()->SetBoolean( prefs::kPrivacySandboxApisEnabled, true); + profile()->GetTestingPrefService()->SetBoolean( + prefs::kPrivacySandboxApisEnabledV2, true); profile()->GetTestingPrefService()->SetBoolean( prefs::kPrivacySandboxFlocEnabled, true); EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed()); diff --git a/components/privacy_sandbox/brave_privacy_sandbox_settings.cc b/components/privacy_sandbox/brave_privacy_sandbox_settings.cc index 5e4337710f97..c9c3e2b2afaf 100644 --- a/components/privacy_sandbox/brave_privacy_sandbox_settings.cc +++ b/components/privacy_sandbox/brave_privacy_sandbox_settings.cc @@ -27,6 +27,11 @@ BravePrivacySandboxSettings::BravePrivacySandboxSettings( base::BindRepeating( &BravePrivacySandboxSettings::OnPrivacySandboxPrefChanged, base::Unretained(this))); + user_prefs_registrar_.Add( + prefs::kPrivacySandboxApisEnabledV2, + base::BindRepeating( + &BravePrivacySandboxSettings::OnPrivacySandboxPrefChanged, + base::Unretained(this))); user_prefs_registrar_.Add( prefs::kPrivacySandboxFlocEnabled, base::BindRepeating( @@ -38,10 +43,13 @@ BravePrivacySandboxSettings::~BravePrivacySandboxSettings() = default; void BravePrivacySandboxSettings::OnPrivacySandboxPrefChanged() { // Make sure that Private Sandbox features remain disabled even if we manually - // access the Pref service and try to change the preference from there. + // access the Pref service and try to change the preferences from there. if (pref_service_->GetBoolean(prefs::kPrivacySandboxApisEnabled)) { pref_service_->SetBoolean(prefs::kPrivacySandboxApisEnabled, false); } + if (pref_service_->GetBoolean(prefs::kPrivacySandboxApisEnabledV2)) { + pref_service_->SetBoolean(prefs::kPrivacySandboxApisEnabledV2, false); + } if (pref_service_->GetBoolean(prefs::kPrivacySandboxFlocEnabled)) { pref_service_->SetBoolean(prefs::kPrivacySandboxFlocEnabled, false); }