Adapt overrides related to the Privacy Sandbox and Brave-specific tests
Functionality has been split between PrivacySandboxService, which still lives in //chrome/browser/privacy_sandbox, and PrivacySandboxSettings, which has moved from //chrome/browser to //components/privacy_sandbox, and so the chromium_src override for privacy_sandbox_settings.cc is no longer valid. We need to do a similar change for the new PrivacySandboxSettings, but we don't have a OnPrivacySandboxPrefChanged() callback there that we can override now, so instead we define a BravePrivacySandboxSettings subclass that adds such callback to monitor the privacy sandbox-related preferences and add an override for privacy_sandbox_settings_factory.cc to instantiate it instead of upstream's PrivacySandboxSettings. Last, we also split the original privacy_sandbox_settings_unittest.cc into a privacy_sandbox_service_unittest.cc, that will remain inside //c/b/privacy_sandbox, and a new privacy_sandbox_settings_unittest.cc, which will live under //components/privacy_sandbox. Note that all the same tests are still available, simply migrated to the new split. Chromium changes: https://source.chromium.org/chromium/chromium/src/+/ad47e2121dd4d3a5b2e892cf74ca88c8cac59c93 https://source.chromium.org/chromium/chromium/src/+/784b8b664b7188712e76a425f7b16c09774ebf02 commit ad47e2121dd4d3a5b2e892cf74ca88c8cac59c93 Author: sauski <sauski@google.com> Date: Thu Dec 16 16:34:29 2021 +0000 Privacy Sandbox: Split existing service based on functionality The current PrivacySandboxSettings service, located in chrome/ serves two high level purposes: 1) Determine whether APIs are allowed or not 2) Support Privacy Sandbox user facing controls Consumers of the logic for (1) are heavily component based. With planned API launches this will only increase, and so having this functionality available in components/ is very desirable. The logic for (2) however is tightly bound to the chrome/ UI on Desktop + Android. It also has significant reconciliation logic, touching many parts of sync & identity. (2) is of questionable value for other embedders, and attempting to componetize it introduces a large set of dependencies. This CL is the first of achieving componetization of (1), which is the splitting of functionality into two separate services, both of which remain in chrome/ for now. *No* new or changed functionality is introduced by this change. Functions and tests are simply moved, with only minor adjustments to reflect the new separation. Bug: 1154686, 1210405 commit 784b8b664b7188712e76a425f7b16c09774ebf02 Author: sauski <sauski@google.com> Date: Fri Jan 7 10:09:36 2022 +0000 Privacy Sandbox: Move PrivacySandboxSettings to components To allow APIs which live outside of chrome/ access to the PrivacySandboxSettings, the service is moved to components/. This follows on from previous work which separated the chrome/ specific logic into a separate service. Bug: 1154686
This commit is contained in:
committed by
mkarolin
parent
5d2680840e
commit
adfb6bf48b
@@ -153,6 +153,7 @@ brave_chrome_browser_deps = [
|
||||
"//brave/components/ntp_tiles",
|
||||
"//brave/components/p3a",
|
||||
"//brave/components/p3a:buildflags",
|
||||
"//brave/components/privacy_sandbox",
|
||||
"//brave/components/resources",
|
||||
"//brave/components/sidebar/buildflags",
|
||||
"//brave/components/skus/browser",
|
||||
|
||||
@@ -23,6 +23,7 @@ include_rules = [
|
||||
"+brave/components/ntp_tiles",
|
||||
"+brave/components/omnibox",
|
||||
"+brave/components/permissions",
|
||||
"+brave/components/privacy_sandbox",
|
||||
"+brave/components/sidebar",
|
||||
"+brave/components/sync",
|
||||
"+brave/components/tor",
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
source_set("unit_tests") {
|
||||
testonly = true
|
||||
sources = [ "privacy_sandbox_settings_unittest.cc" ]
|
||||
sources = [ "privacy_sandbox_service_unittest.cc" ]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
@@ -18,7 +18,9 @@ source_set("unit_tests") {
|
||||
"//components/content_settings/core/test:test_support",
|
||||
"//components/federated_learning",
|
||||
"//components/policy/core/common:test_support",
|
||||
"//components/privacy_sandbox:privacy_sandbox",
|
||||
"//components/privacy_sandbox:privacy_sandbox_prefs",
|
||||
"//components/privacy_sandbox:test_support",
|
||||
"//components/signin/public/identity_manager",
|
||||
"//components/signin/public/identity_manager:test_support",
|
||||
"//components/strings:components_strings",
|
||||
|
||||
@@ -0,0 +1,398 @@
|
||||
// Copyright 2021 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
|
||||
|
||||
#include "base/test/gtest_util.h"
|
||||
#include "base/test/icu_test_util.h"
|
||||
#include "base/test/metrics/histogram_tester.h"
|
||||
#include "base/test/metrics/user_action_tester.h"
|
||||
#include "base/test/scoped_feature_list.h"
|
||||
#include "chrome/browser/content_settings/cookie_settings_factory.h"
|
||||
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
|
||||
#include "chrome/browser/federated_learning/floc_id_provider.h"
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
|
||||
#include "chrome/browser/signin/identity_manager_factory.h"
|
||||
#include "chrome/browser/ui/webui/federated_learning/floc_internals.mojom.h"
|
||||
#include "chrome/common/chrome_features.h"
|
||||
#include "chrome/test/base/testing_profile.h"
|
||||
#include "components/content_settings/core/browser/cookie_settings.h"
|
||||
#include "components/content_settings/core/browser/host_content_settings_map.h"
|
||||
#include "components/content_settings/core/common/pref_names.h"
|
||||
#include "components/federated_learning/features/features.h"
|
||||
#include "components/federated_learning/floc_id.h"
|
||||
#include "components/policy/core/common/mock_policy_service.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_test_util.h"
|
||||
#include "components/signin/public/identity_manager/account_info.h"
|
||||
#include "components/signin/public/identity_manager/identity_test_environment.h"
|
||||
#include "components/strings/grit/components_strings.h"
|
||||
#include "components/sync/base/user_selectable_type.h"
|
||||
#include "components/sync/driver/test_sync_service.h"
|
||||
#include "components/sync_preferences/testing_pref_service_syncable.h"
|
||||
#include "content/public/common/content_features.h"
|
||||
#include "content/public/test/browser_task_environment.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "third_party/blink/public/common/features.h"
|
||||
#include "third_party/blink/public/mojom/federated_learning/floc.mojom.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
#include "url/origin.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class MockFlocIdProvider : public federated_learning::FlocIdProvider {
|
||||
public:
|
||||
blink::mojom::InterestCohortPtr GetInterestCohortForJsApi(
|
||||
const GURL& url,
|
||||
const absl::optional<url::Origin>& top_frame_origin) const override {
|
||||
return blink::mojom::InterestCohort::New();
|
||||
}
|
||||
MOCK_METHOD(federated_learning::mojom::WebUIFlocStatusPtr,
|
||||
GetFlocStatusForWebUi,
|
||||
(),
|
||||
(const, override));
|
||||
MOCK_METHOD(void, MaybeRecordFlocToUkm, (ukm::SourceId), (override));
|
||||
MOCK_METHOD(base::Time, GetApproximateNextComputeTime, (), (const, override));
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class PrivacySandboxServiceTest : public testing::Test {
|
||||
public:
|
||||
PrivacySandboxServiceTest()
|
||||
: browser_task_environment_(
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
|
||||
|
||||
void SetUp() override {
|
||||
InitializePrefsBeforeStart();
|
||||
|
||||
privacy_sandbox_service_ = std::make_unique<PrivacySandboxService>(
|
||||
PrivacySandboxSettingsFactory::GetForProfile(profile()),
|
||||
CookieSettingsFactory::GetForProfile(profile()).get(),
|
||||
profile()->GetPrefs(), policy_service(), sync_service(),
|
||||
identity_test_env()->identity_manager(), mock_floc_id_provider());
|
||||
}
|
||||
|
||||
virtual void InitializePrefsBeforeStart() {}
|
||||
|
||||
TestingProfile* profile() { return &profile_; }
|
||||
PrivacySandboxService* privacy_sandbox_service() {
|
||||
return privacy_sandbox_service_.get();
|
||||
}
|
||||
PrivacySandboxSettings* privacy_sandbox_settings() {
|
||||
return PrivacySandboxSettingsFactory::GetForProfile(profile());
|
||||
}
|
||||
|
||||
base::test::ScopedFeatureList* feature_list() { return &feature_list_; }
|
||||
sync_preferences::TestingPrefServiceSyncable* prefs() {
|
||||
return profile()->GetTestingPrefService();
|
||||
}
|
||||
HostContentSettingsMap* host_content_settings_map() {
|
||||
return HostContentSettingsMapFactory::GetForProfile(profile());
|
||||
}
|
||||
syncer::TestSyncService* sync_service() { return &sync_service_; }
|
||||
policy::MockPolicyService* policy_service() { return &mock_policy_service_; }
|
||||
signin::IdentityTestEnvironment* identity_test_env() {
|
||||
return &identity_test_env_;
|
||||
}
|
||||
MockFlocIdProvider* mock_floc_id_provider() {
|
||||
return &mock_floc_id_provider_;
|
||||
}
|
||||
|
||||
private:
|
||||
content::BrowserTaskEnvironment browser_task_environment_;
|
||||
signin::IdentityTestEnvironment identity_test_env_;
|
||||
testing::NiceMock<policy::MockPolicyService> mock_policy_service_;
|
||||
|
||||
TestingProfile profile_;
|
||||
base::test::ScopedFeatureList feature_list_;
|
||||
syncer::TestSyncService sync_service_;
|
||||
MockFlocIdProvider mock_floc_id_provider_;
|
||||
|
||||
std::unique_ptr<PrivacySandboxService> privacy_sandbox_service_;
|
||||
};
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, GetFlocIdForDisplay) {
|
||||
// Check that the cohort identifier is correctly converted to a string when
|
||||
// available.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
federated_learning::FlocId floc_id = federated_learning::FlocId::CreateValid(
|
||||
123456, base::Time(), base::Time::Now(),
|
||||
/*sorting_lsh_version=*/0);
|
||||
floc_id.SaveToPrefs(profile()->GetTestingPrefService());
|
||||
|
||||
// No valid ID is obtained since FLoC is actually disabled.
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_service()->GetFlocIdForDisplay());
|
||||
|
||||
// If the FLoC preference, the Sandbox Preference, or the feature is disabled,
|
||||
// or the FLoC ID is invalid, the invalid string should be returned.
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_service()->GetFlocIdForDisplay());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_service()->GetFlocIdForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_service()->GetFlocIdForDisplay());
|
||||
|
||||
floc_id.UpdateStatusAndSaveToPrefs(
|
||||
profile()->GetTestingPrefService(),
|
||||
federated_learning::FlocId::Status::kInvalidReset);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_service()->GetFlocIdForDisplay());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, GetFlocIdNextUpdateForDisplay) {
|
||||
// Check that date FLoC will be next updated is returned when available.
|
||||
MockFlocIdProvider mock_floc_id_provider;
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
std::map<base::TimeDelta, std::u16string> offsets_to_expected_string = {
|
||||
{base::Hours(23),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(25),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Days(2),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(60),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(167), // 1 hour less than 7 days.
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)}};
|
||||
|
||||
for (const auto& offset_expected : offsets_to_expected_string) {
|
||||
EXPECT_EQ(offset_expected.second,
|
||||
privacy_sandbox_service()->GetFlocIdNextUpdateForDisplay(
|
||||
base::Time::Now()));
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_floc_id_provider);
|
||||
}
|
||||
|
||||
// Disabling the FLoC feature should also invalidate the next compute time.
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_floc_id_provider);
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, GetFlocStatusForDisplay) {
|
||||
// Check the status of the user's FLoC is correctly returned. This depends
|
||||
// on whether the FLoC origin trial feature is enabled, and whether the user
|
||||
// has FLoC enabled.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
// Will report not active since nothing is actually enabled.
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_service()->GetFlocStatusForDisplay());
|
||||
|
||||
// The Privacy Sandbox APIs pref & FLoC pref should disable the trial when
|
||||
// either is disabled.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_service()->GetFlocStatusForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_service()->GetFlocStatusForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
|
||||
// Will report not active again since nothing is actually enabled.
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_service()->GetFlocStatusForDisplay());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, IsFlocIdResettable) {
|
||||
// Check that if FLoC is functional the FLoC ID is resettable, regardless of
|
||||
// whether the FLoC ID is currently valid.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
federated_learning::FlocId floc_id = federated_learning::FlocId::CreateValid(
|
||||
123456, base::Time(), base::Time::Now(),
|
||||
/*sorting_lsh_version=*/0);
|
||||
floc_id.SaveToPrefs(profile()->GetTestingPrefService());
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocIdResettable());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocIdResettable());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocIdResettable());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocIdResettable());
|
||||
|
||||
floc_id.UpdateStatusAndSaveToPrefs(
|
||||
profile()->GetTestingPrefService(),
|
||||
federated_learning::FlocId::Status::kInvalidReset);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
|
||||
// In Brave, trying to re-enable FLoC won't make a difference.
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocIdResettable());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, IsFlocPrefEnabled) {
|
||||
// IsFlocPrefEnabled should directly reflect the state of the FLoC pref, which
|
||||
// will always be false regardless of our attempts to set it to true.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocPrefEnabled());
|
||||
|
||||
// The Privacy Sandbox APIs pref should not impact the return value.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocPrefEnabled());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_service()->IsFlocPrefEnabled());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, SetFlocPrefEnabled) {
|
||||
// The FLoc pref should NEVER be updated by this function, regardless of
|
||||
// other Sandbox State or any calls to SetFlocPrefEnabled().
|
||||
base::UserActionTester user_action_tester;
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
|
||||
privacy_sandbox_service()->SetFlocPrefEnabled(false);
|
||||
EXPECT_FALSE(profile()->GetTestingPrefService()->GetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled));
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
|
||||
// Disabling the sandbox shouldn't make a difference on the FLoC preference,
|
||||
// which should remain disabled regardless of calls to SetFlocPrefEnabled().
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
privacy_sandbox_service()->SetFlocPrefEnabled(true);
|
||||
EXPECT_FALSE(profile()->GetTestingPrefService()->GetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxServiceTest, OnPrivacySandboxPrefChanged) {
|
||||
// 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::kPrivacySandboxApisEnabled, 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::kPrivacySandboxApisEnabled, true);
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
|
||||
#define OnPrivacySandboxPrefChanged() OnPrivacySandboxPrefChanged_ChromiumImpl()
|
||||
|
||||
#include "src/chrome/browser/privacy_sandbox/privacy_sandbox_settings.cc"
|
||||
|
||||
#undef OnPrivacySandboxPrefChanged
|
||||
|
||||
void PrivacySandboxSettings::OnPrivacySandboxPrefChanged() {
|
||||
OnPrivacySandboxPrefChanged_ChromiumImpl();
|
||||
|
||||
// Make sure that Private Sandbox features remain disabled even if we manually
|
||||
// access the Pref service and try to change the preference from there.
|
||||
if (pref_service_->GetBoolean(prefs::kPrivacySandboxApisEnabled))
|
||||
pref_service_->SetBoolean(prefs::kPrivacySandboxApisEnabled, false);
|
||||
if (pref_service_->GetBoolean(prefs::kPrivacySandboxFlocEnabled))
|
||||
pref_service_->SetBoolean(prefs::kPrivacySandboxFlocEnabled, false);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
|
||||
#define OnPrivacySandboxPrefChanged() \
|
||||
OnPrivacySandboxPrefChanged(); \
|
||||
void OnPrivacySandboxPrefChanged_ChromiumImpl()
|
||||
|
||||
#include "src/chrome/browser/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
|
||||
#undef OnPrivacySandboxPrefChanged
|
||||
|
||||
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
|
||||
|
||||
#include "brave/components/privacy_sandbox/brave_privacy_sandbox_settings.h"
|
||||
|
||||
#define BuildServiceInstanceFor BuildServiceInstanceFor_ChromiumImpl
|
||||
#include "src/chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.cc"
|
||||
#undef BuildServiceInstanceFor
|
||||
|
||||
KeyedService* PrivacySandboxSettingsFactory::BuildServiceInstanceFor(
|
||||
content::BrowserContext* context) const {
|
||||
Profile* profile = Profile::FromBrowserContext(context);
|
||||
|
||||
return new BravePrivacySandboxSettings(
|
||||
HostContentSettingsMapFactory::GetForProfile(profile),
|
||||
CookieSettingsFactory::GetForProfile(profile).get(), profile->GetPrefs());
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_FACTORY_H_
|
||||
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_FACTORY_H_
|
||||
|
||||
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
|
||||
|
||||
#define BuildServiceInstanceFor \
|
||||
BuildServiceInstanceFor_ChromiumImpl(content::BrowserContext*) const; \
|
||||
KeyedService* BuildServiceInstanceFor
|
||||
|
||||
#include "src/chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
|
||||
|
||||
#undef BuildServiceInstanceFor
|
||||
|
||||
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PRIVACY_SANDBOX_PRIVACY_SANDBOX_SETTINGS_FACTORY_H_
|
||||
@@ -0,0 +1,27 @@
|
||||
# Copyright 2021 The Brave Authors. All rights reserved.
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
# You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
source_set("unit_tests") {
|
||||
testonly = true
|
||||
sources = [ "privacy_sandbox_settings_unittest.cc" ]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//brave/components/privacy_sandbox",
|
||||
"//chrome/test:test_support",
|
||||
"//components/content_settings/core/browser",
|
||||
"//components/content_settings/core/common",
|
||||
"//components/content_settings/core/test:test_support",
|
||||
"//components/federated_learning",
|
||||
"//components/privacy_sandbox:privacy_sandbox",
|
||||
"//components/privacy_sandbox:privacy_sandbox_prefs",
|
||||
"//components/privacy_sandbox:test_support",
|
||||
"//components/sync_preferences:test_support",
|
||||
"//content/public/common",
|
||||
"//content/test:test_support",
|
||||
"//testing/gtest:gtest",
|
||||
"//url",
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
specific_include_rules = {
|
||||
"privacy_sandbox_settings_unittest.cc": [
|
||||
"+brave/components/privacy_sandbox",
|
||||
"+chrome/test/base/testing_profile.h",
|
||||
],
|
||||
}
|
||||
+94
-465
@@ -2,178 +2,71 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
|
||||
#include "base/json/values_util.h"
|
||||
#include "base/test/gtest_util.h"
|
||||
#include "base/test/icu_test_util.h"
|
||||
#include "base/test/metrics/histogram_tester.h"
|
||||
#include "base/test/metrics/user_action_tester.h"
|
||||
#include "base/test/scoped_feature_list.h"
|
||||
#include "chrome/browser/content_settings/cookie_settings_factory.h"
|
||||
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
|
||||
#include "chrome/browser/federated_learning/floc_id_provider.h"
|
||||
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
#include "chrome/browser/signin/identity_manager_factory.h"
|
||||
#include "chrome/browser/ui/webui/federated_learning/floc_internals.mojom.h"
|
||||
#include "chrome/common/chrome_features.h"
|
||||
#include "brave/components/privacy_sandbox/brave_privacy_sandbox_settings.h"
|
||||
#include "chrome/test/base/testing_profile.h"
|
||||
#include "components/content_settings/core/browser/cookie_settings.h"
|
||||
#include "components/content_settings/core/browser/host_content_settings_map.h"
|
||||
#include "components/content_settings/core/common/pref_names.h"
|
||||
#include "components/content_settings/core/test/content_settings_mock_provider.h"
|
||||
#include "components/content_settings/core/test/content_settings_test_utils.h"
|
||||
#include "components/federated_learning/features/features.h"
|
||||
#include "components/federated_learning/floc_id.h"
|
||||
#include "components/policy/core/common/mock_policy_service.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
|
||||
#include "components/signin/public/identity_manager/account_info.h"
|
||||
#include "components/signin/public/identity_manager/identity_test_environment.h"
|
||||
#include "components/strings/grit/components_strings.h"
|
||||
#include "components/sync/base/user_selectable_type.h"
|
||||
#include "components/sync/driver/test_sync_service.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_test_util.h"
|
||||
#include "components/sync_preferences/testing_pref_service_syncable.h"
|
||||
#include "content/public/common/content_features.h"
|
||||
#include "content/public/test/browser_task_environment.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "third_party/blink/public/common/features.h"
|
||||
#include "third_party/blink/public/mojom/federated_learning/floc.mojom.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
#include "url/origin.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class MockFlocIdProvider : public federated_learning::FlocIdProvider {
|
||||
public:
|
||||
blink::mojom::InterestCohortPtr GetInterestCohortForJsApi(
|
||||
const GURL& url,
|
||||
const absl::optional<url::Origin>& top_frame_origin) const override {
|
||||
return blink::mojom::InterestCohort::New();
|
||||
}
|
||||
MOCK_METHOD(federated_learning::mojom::WebUIFlocStatusPtr,
|
||||
GetFlocStatusForWebUi,
|
||||
(),
|
||||
(const, override));
|
||||
MOCK_METHOD(void, MaybeRecordFlocToUkm, (ukm::SourceId), (override));
|
||||
MOCK_METHOD(base::Time, GetApproximateNextComputeTime, (), (const, override));
|
||||
};
|
||||
|
||||
class MockPrivacySandboxObserver : public PrivacySandboxSettings::Observer {
|
||||
public:
|
||||
MOCK_METHOD(void, OnFlocDataAccessibleSinceUpdated, (bool), (override));
|
||||
};
|
||||
|
||||
// Define an additional content setting value to simulate an unmanaged default
|
||||
// content setting.
|
||||
const ContentSetting kNoSetting = static_cast<ContentSetting>(-1);
|
||||
|
||||
struct CookieContentSettingException {
|
||||
std::string primary_pattern;
|
||||
std::string secondary_pattern;
|
||||
ContentSetting content_setting;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class PrivacySandboxSettingsTest : public testing::Test {
|
||||
public:
|
||||
PrivacySandboxSettingsTest()
|
||||
: browser_task_environment_(
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
|
||||
content_settings::CookieSettings::RegisterProfilePrefs(prefs()->registry());
|
||||
HostContentSettingsMap::RegisterProfilePrefs(prefs()->registry());
|
||||
privacy_sandbox::RegisterProfilePrefs(prefs()->registry());
|
||||
|
||||
host_content_settings_map_ = new HostContentSettingsMap(
|
||||
&prefs_, false /* is_off_the_record */, false /* store_last_modified */,
|
||||
false /* restore_session */);
|
||||
cookie_settings_ = new content_settings::CookieSettings(
|
||||
host_content_settings_map_.get(), &prefs_, false, "chrome-extension");
|
||||
}
|
||||
~PrivacySandboxSettingsTest() override {
|
||||
host_content_settings_map()->ShutdownOnUIThread();
|
||||
}
|
||||
|
||||
void SetUp() override {
|
||||
InitializePrefsBeforeStart();
|
||||
|
||||
privacy_sandbox_settings_ = std::make_unique<PrivacySandboxSettings>(
|
||||
HostContentSettingsMapFactory::GetForProfile(profile()),
|
||||
CookieSettingsFactory::GetForProfile(profile()).get(),
|
||||
profile()->GetPrefs(), policy_service(), sync_service(),
|
||||
identity_test_env()->identity_manager());
|
||||
privacy_sandbox_settings_ = std::make_unique<BravePrivacySandboxSettings>(
|
||||
host_content_settings_map(), cookie_settings(), prefs());
|
||||
}
|
||||
|
||||
virtual void InitializePrefsBeforeStart() {}
|
||||
|
||||
// Sets up preferences and content settings based on provided parameters.
|
||||
void SetupTestState(
|
||||
bool privacy_sandbox_enabled,
|
||||
bool block_third_party_cookies,
|
||||
ContentSetting default_cookie_setting,
|
||||
std::vector<CookieContentSettingException> user_cookie_exceptions,
|
||||
ContentSetting managed_cookie_setting,
|
||||
std::vector<CookieContentSettingException> managed_cookie_exceptions) {
|
||||
// Setup block-third-party-cookies settings.
|
||||
profile()->GetTestingPrefService()->SetUserPref(
|
||||
prefs::kCookieControlsMode,
|
||||
std::make_unique<base::Value>(static_cast<int>(
|
||||
block_third_party_cookies
|
||||
? content_settings::CookieControlsMode::kBlockThirdParty
|
||||
: content_settings::CookieControlsMode::kOff)));
|
||||
|
||||
// Setup cookie content settings.
|
||||
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
|
||||
auto user_provider = std::make_unique<content_settings::MockProvider>();
|
||||
auto managed_provider = std::make_unique<content_settings::MockProvider>();
|
||||
|
||||
if (default_cookie_setting != kNoSetting) {
|
||||
user_provider->SetWebsiteSetting(
|
||||
ContentSettingsPattern::Wildcard(),
|
||||
ContentSettingsPattern::Wildcard(), ContentSettingsType::COOKIES,
|
||||
std::make_unique<base::Value>(default_cookie_setting));
|
||||
}
|
||||
|
||||
for (const auto& exception : user_cookie_exceptions) {
|
||||
user_provider->SetWebsiteSetting(
|
||||
ContentSettingsPattern::FromString(exception.primary_pattern),
|
||||
ContentSettingsPattern::FromString(exception.secondary_pattern),
|
||||
ContentSettingsType::COOKIES,
|
||||
std::make_unique<base::Value>(exception.content_setting));
|
||||
}
|
||||
|
||||
if (managed_cookie_setting != kNoSetting) {
|
||||
managed_provider->SetWebsiteSetting(
|
||||
ContentSettingsPattern::Wildcard(),
|
||||
ContentSettingsPattern::Wildcard(), ContentSettingsType::COOKIES,
|
||||
std::make_unique<base::Value>(managed_cookie_setting));
|
||||
}
|
||||
|
||||
for (const auto& exception : managed_cookie_exceptions) {
|
||||
managed_provider->SetWebsiteSetting(
|
||||
ContentSettingsPattern::FromString(exception.primary_pattern),
|
||||
ContentSettingsPattern::FromString(exception.secondary_pattern),
|
||||
ContentSettingsType::COOKIES,
|
||||
std::make_unique<base::Value>(exception.content_setting));
|
||||
}
|
||||
|
||||
content_settings::TestUtils::OverrideProvider(
|
||||
map, std::move(user_provider),
|
||||
HostContentSettingsMap::DEFAULT_PROVIDER);
|
||||
content_settings::TestUtils::OverrideProvider(
|
||||
map, std::move(managed_provider),
|
||||
HostContentSettingsMap::POLICY_PROVIDER);
|
||||
|
||||
privacy_sandbox_settings()->SetPrivacySandboxEnabled(
|
||||
privacy_sandbox_enabled);
|
||||
sync_preferences::TestingPrefServiceSyncable* prefs() { return &prefs_; }
|
||||
HostContentSettingsMap* host_content_settings_map() {
|
||||
return host_content_settings_map_.get();
|
||||
}
|
||||
content_settings::CookieSettings* cookie_settings() {
|
||||
return cookie_settings_.get();
|
||||
}
|
||||
|
||||
TestingProfile* profile() { return &profile_; }
|
||||
PrivacySandboxSettings* privacy_sandbox_settings() {
|
||||
return privacy_sandbox_settings_.get();
|
||||
}
|
||||
base::test::ScopedFeatureList* feature_list() { return &feature_list_; }
|
||||
syncer::TestSyncService* sync_service() { return &sync_service_; }
|
||||
policy::MockPolicyService* policy_service() { return &mock_policy_service_; }
|
||||
signin::IdentityTestEnvironment* identity_test_env() {
|
||||
return &identity_test_env_;
|
||||
}
|
||||
|
||||
private:
|
||||
content::BrowserTaskEnvironment browser_task_environment_;
|
||||
signin::IdentityTestEnvironment identity_test_env_;
|
||||
testing::NiceMock<policy::MockPolicyService> mock_policy_service_;
|
||||
|
||||
TestingProfile profile_;
|
||||
base::test::ScopedFeatureList feature_list_;
|
||||
syncer::TestSyncService sync_service_;
|
||||
sync_preferences::TestingPrefServiceSyncable prefs_;
|
||||
scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
|
||||
scoped_refptr<content_settings::CookieSettings> cookie_settings_;
|
||||
|
||||
std::unique_ptr<PrivacySandboxSettings> privacy_sandbox_settings_;
|
||||
};
|
||||
@@ -181,12 +74,13 @@ class PrivacySandboxSettingsTest : public testing::Test {
|
||||
TEST_F(PrivacySandboxSettingsTest, PreferenceOverridesDefaultContentSetting) {
|
||||
// Even if we try to enable the Privacy Sandbox, it should remain disabled, so
|
||||
// the sandbox preference should never override the default cookie content.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
// All should be DISABLED: FLoC, Conversion measurement & reporting, fledge...
|
||||
@@ -210,7 +104,8 @@ TEST_F(PrivacySandboxSettingsTest, PreferenceOverridesDefaultContentSetting) {
|
||||
GURL("https://another-embedded.com")}));
|
||||
|
||||
// An allow exception should not override the preference value.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -221,7 +116,7 @@ TEST_F(PrivacySandboxSettingsTest, PreferenceOverridesDefaultContentSetting) {
|
||||
ContentSetting::CONTENT_SETTING_ALLOW},
|
||||
{"https://embedded.com", "https://another-test.com",
|
||||
ContentSetting::CONTENT_SETTING_ALLOW}},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowedForContext(
|
||||
@@ -249,7 +144,8 @@ TEST_F(PrivacySandboxSettingsTest, PreferenceOverridesDefaultContentSetting) {
|
||||
TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
// Even if we try to enable the Privacy Sandbox, it should remain disabled, so
|
||||
// targeted cookie block exceptions should never apply.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -258,7 +154,7 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
ContentSetting::CONTENT_SETTING_BLOCK},
|
||||
{"https://another-embedded.com", "*",
|
||||
ContentSetting::CONTENT_SETTING_BLOCK}},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowedForContext(
|
||||
@@ -286,7 +182,8 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
// setting exists. What the managed default setting actually is should *not*
|
||||
// affect whether APIs are enabled. The cookie managed state is reflected in
|
||||
// the privacy sandbox preferences directly.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -321,7 +218,8 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
GURL("https://another-embedded.com")}));
|
||||
|
||||
// Managed content setting exceptions.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -369,7 +267,8 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
GURL("https://another-embedded.com")}));
|
||||
|
||||
// A less specific block exception.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -382,7 +281,7 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
ContentSetting::CONTENT_SETTING_BLOCK},
|
||||
{"https://[*.]embedded.com", "https://[*.]another-test.com",
|
||||
ContentSetting::CONTENT_SETTING_BLOCK}},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
// It doesn't matter, everything should be DISABLED again.
|
||||
@@ -395,14 +294,15 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
GURL("https://embedded.com")));
|
||||
|
||||
// Exceptions which specify a top frame origin.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
|
||||
/*user_cookie_exceptions=*/
|
||||
{{"https://embedded.com", "https://test.com",
|
||||
ContentSetting::CONTENT_SETTING_BLOCK}},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/
|
||||
{{"https://embedded.com", "https://test.com",
|
||||
ContentSetting::CONTENT_SETTING_BLOCK}});
|
||||
@@ -429,13 +329,14 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
GURL("https://another-embedded.com")}));
|
||||
|
||||
// Exceptions which specify a wildcard top frame origin.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/
|
||||
{{"https://embedded.com", "*", ContentSetting::CONTENT_SETTING_BLOCK}},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
// It doesn't matter, everything should be DISABLED again.
|
||||
@@ -465,12 +366,13 @@ TEST_F(PrivacySandboxSettingsTest, CookieBlockExceptionsNeverApply) {
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, IsFledgeAllowed) {
|
||||
// FLEDGE should be disabled if 3P cookies are blocked.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/true,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
|
||||
@@ -482,12 +384,13 @@ TEST_F(PrivacySandboxSettingsTest, IsFledgeAllowed) {
|
||||
{GURL("https://embedded.com")}));
|
||||
|
||||
// FLEDGE should be disabled if all cookies are blocked.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
|
||||
@@ -500,7 +403,8 @@ TEST_F(PrivacySandboxSettingsTest, IsFledgeAllowed) {
|
||||
|
||||
// FLEDGE should be disabled if the privacy sandbox is disabled, regardless
|
||||
// of other cookie settings.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -522,7 +426,8 @@ TEST_F(PrivacySandboxSettingsTest, IsFledgeAllowed) {
|
||||
|
||||
// The managed cookie content setting should not override a disabled privacy
|
||||
// sandbox setting.
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
@@ -540,30 +445,33 @@ TEST_F(PrivacySandboxSettingsTest, IsFledgeAllowed) {
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, IsPrivacySandboxAllowed) {
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/true,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
// Trying to enable the privacy sandbox doesn't make a difference in Brave.
|
||||
@@ -577,16 +485,17 @@ TEST_F(PrivacySandboxSettingsTest, IsPrivacySandboxAllowed) {
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, IsFlocAllowed) {
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/true,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
|
||||
// FLoC should be disabled since the privacy sandbox APIs can't be enabled.
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(true);
|
||||
prefs()->SetBoolean(prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
// Check that even bypassing PrivacySandboxSettings::SetFlocPrefEnabled,
|
||||
@@ -597,325 +506,46 @@ TEST_F(PrivacySandboxSettingsTest, IsFlocAllowed) {
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(true);
|
||||
prefs()->SetBoolean(prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/true,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(false);
|
||||
prefs()->SetBoolean(prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
SetupTestState(
|
||||
privacy_sandbox_test_util::SetupTestState(
|
||||
prefs(), host_content_settings_map(),
|
||||
/*privacy_sandbox_enabled=*/false,
|
||||
/*block_third_party_cookies=*/false,
|
||||
/*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
|
||||
/*user_cookie_exceptions=*/{},
|
||||
/*managed_cookie_setting=*/kNoSetting,
|
||||
/*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
|
||||
/*managed_cookie_exceptions=*/{});
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(true);
|
||||
prefs()->SetBoolean(prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, GetFlocIdForDisplay) {
|
||||
// Check that the cohort identifier is correctly converted to a string when
|
||||
// available.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
federated_learning::FlocId floc_id = federated_learning::FlocId::CreateValid(
|
||||
123456, base::Time(), base::Time::Now(),
|
||||
/*sorting_lsh_version=*/0);
|
||||
floc_id.SaveToPrefs(profile()->GetTestingPrefService());
|
||||
|
||||
// No valid ID is obtained since FLoC is actually disabled.
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_settings()->GetFlocIdForDisplay());
|
||||
|
||||
// If the FLoC preference, the Sandbox Preference, or the feature is disabled,
|
||||
// or the FLoC ID is invalid, the invalid string should be returned.
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_settings()->GetFlocIdForDisplay());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_settings()->GetFlocIdForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_settings()->GetFlocIdForDisplay());
|
||||
|
||||
floc_id.UpdateStatusAndSaveToPrefs(
|
||||
profile()->GetTestingPrefService(),
|
||||
federated_learning::FlocId::Status::kInvalidReset);
|
||||
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_INVALID),
|
||||
privacy_sandbox_settings()->GetFlocIdForDisplay());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, GetFlocIdNextUpdateForDisplay) {
|
||||
// Check that date FLoC will be next updated is returned when available.
|
||||
MockFlocIdProvider mock_floc_id_provider;
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
std::map<base::TimeDelta, std::u16string> offsets_to_expected_string = {
|
||||
{base::Hours(23),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(25),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Days(2),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(60),
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)},
|
||||
{base::Hours(167), // 1 hour less than 7 days.
|
||||
l10n_util::GetStringUTF16(
|
||||
IDS_PRIVACY_SANDBOX_FLOC_TIME_TO_NEXT_COMPUTE_INVALID)}};
|
||||
|
||||
for (const auto& offset_expected : offsets_to_expected_string) {
|
||||
EXPECT_EQ(
|
||||
offset_expected.second,
|
||||
privacy_sandbox_settings()->GetFlocIdNextUpdateForDisplay(
|
||||
&mock_floc_id_provider, profile()->GetPrefs(), base::Time::Now()));
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_floc_id_provider);
|
||||
}
|
||||
|
||||
// Disabling the FLoC feature should also invalidate the next compute time.
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_floc_id_provider);
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, GetFlocStatusForDisplay) {
|
||||
// Check the status of the user's FLoC is correctly returned. This depends
|
||||
// on whether the FLoC origin trial feature is enabled, and whether the user
|
||||
// has FLoC enabled.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
|
||||
// Will report not active since nothing is actually enabled.
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_settings()->GetFlocStatusForDisplay());
|
||||
|
||||
// The Privacy Sandbox APIs pref & FLoC pref should disable the trial when
|
||||
// either is disabled.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_settings()->GetFlocStatusForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_settings()->GetFlocStatusForDisplay());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
|
||||
// Will report not active again since nothing is actually enabled.
|
||||
EXPECT_EQ(
|
||||
l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_FLOC_STATUS_NOT_ACTIVE),
|
||||
privacy_sandbox_settings()->GetFlocStatusForDisplay());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, IsFlocIdResettable) {
|
||||
// Check that if FLoC is functional the FLoC ID is resettable, regardless of
|
||||
// whether the FLoC ID is currently valid.
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
federated_learning::FlocId floc_id = federated_learning::FlocId::CreateValid(
|
||||
123456, base::Time(), base::Time::Now(),
|
||||
/*sorting_lsh_version=*/0);
|
||||
floc_id.SaveToPrefs(profile()->GetTestingPrefService());
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, true);
|
||||
|
||||
// In Brave, we actually don't enable anything here.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocAllowed());
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocIdResettable());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{}, {blink::features::kInterestCohortAPIOriginTrial});
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocIdResettable());
|
||||
|
||||
feature_list()->Reset();
|
||||
feature_list()->InitWithFeatures(
|
||||
{blink::features::kInterestCohortAPIOriginTrial}, {});
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocIdResettable());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocIdResettable());
|
||||
|
||||
floc_id.UpdateStatusAndSaveToPrefs(
|
||||
profile()->GetTestingPrefService(),
|
||||
federated_learning::FlocId::Status::kInvalidReset);
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
|
||||
// In Brave, trying to re-enable FLoC won't make a difference.
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocIdResettable());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, IsFlocPrefEnabled) {
|
||||
// IsFlocPrefEnabled should directly reflect the state of the FLoC pref, which
|
||||
// will always be false regardless of our attempts to set it to true.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, true);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocPrefEnabled());
|
||||
|
||||
// The Privacy Sandbox APIs pref should not impact the return value.
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocPrefEnabled());
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled, false);
|
||||
EXPECT_FALSE(privacy_sandbox_settings()->IsFlocPrefEnabled());
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, SetFlocPrefEnabled) {
|
||||
// The FLoc pref should NEVER be updated by this function, regardless of
|
||||
// other Sandbox State or any calls to SetFlocPrefEnabled().
|
||||
base::UserActionTester user_action_tester;
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(false);
|
||||
EXPECT_FALSE(profile()->GetTestingPrefService()->GetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled));
|
||||
ASSERT_EQ(0, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
|
||||
// Disabling the sandbox shouldn't make a difference on the FLoC preference,
|
||||
// which should remain disabled regardless of calls to SetFlocPrefEnabled().
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, false);
|
||||
privacy_sandbox_settings()->SetFlocPrefEnabled(true);
|
||||
EXPECT_FALSE(profile()->GetTestingPrefService()->GetBoolean(
|
||||
prefs::kPrivacySandboxFlocEnabled));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocEnabled"));
|
||||
ASSERT_EQ(1, user_action_tester.GetActionCount(
|
||||
"Settings.PrivacySandbox.FlocDisabled"));
|
||||
}
|
||||
|
||||
TEST_F(PrivacySandboxSettingsTest, OnPrivacySandboxPrefChanged) {
|
||||
// When either the main Privacy Sandbox pref, or the FLoC pref, are changed
|
||||
// the FLoC ID should be reset.
|
||||
MockPrivacySandboxObserver mock_privacy_sandbox_observer;
|
||||
privacy_sandbox_settings()->AddObserver(&mock_privacy_sandbox_observer);
|
||||
EXPECT_CALL(mock_privacy_sandbox_observer,
|
||||
OnFlocDataAccessibleSinceUpdated(/*reset_compute_timer=*/true));
|
||||
|
||||
profile()->GetTestingPrefService()->SetBoolean(
|
||||
prefs::kPrivacySandboxApisEnabled, 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::kPrivacySandboxApisEnabled, true);
|
||||
testing::Mock::VerifyAndClearExpectations(&mock_privacy_sandbox_observer);
|
||||
}
|
||||
|
||||
class PrivacySandboxSettingsTestCookiesClearOnExitTurnedOff
|
||||
: public PrivacySandboxSettingsTest {
|
||||
public:
|
||||
void InitializePrefsBeforeStart() override {
|
||||
profile()->GetTestingPrefService()->SetUserPref(
|
||||
prefs::kPrivacySandboxFlocDataAccessibleSince,
|
||||
std::make_unique<base::Value>(
|
||||
::base::TimeToValue(base::Time::FromTimeT(12345))));
|
||||
prefs()->SetUserPref(prefs::kPrivacySandboxFlocDataAccessibleSince,
|
||||
std::make_unique<base::Value>(::base::TimeToValue(
|
||||
base::Time::FromTimeT(12345))));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -929,14 +559,13 @@ class PrivacySandboxSettingsTestCookiesClearOnExitTurnedOn
|
||||
: public PrivacySandboxSettingsTest {
|
||||
public:
|
||||
void InitializePrefsBeforeStart() override {
|
||||
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
|
||||
map->SetDefaultContentSetting(ContentSettingsType::COOKIES,
|
||||
ContentSetting::CONTENT_SETTING_SESSION_ONLY);
|
||||
host_content_settings_map()->SetDefaultContentSetting(
|
||||
ContentSettingsType::COOKIES,
|
||||
ContentSetting::CONTENT_SETTING_SESSION_ONLY);
|
||||
|
||||
profile()->GetTestingPrefService()->SetUserPref(
|
||||
prefs::kPrivacySandboxFlocDataAccessibleSince,
|
||||
std::make_unique<base::Value>(
|
||||
::base::TimeToValue(base::Time::FromTimeT(12345))));
|
||||
prefs()->SetUserPref(prefs::kPrivacySandboxFlocDataAccessibleSince,
|
||||
std::make_unique<base::Value>(::base::TimeToValue(
|
||||
base::Time::FromTimeT(12345))));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -471,13 +471,16 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
|
||||
// We also create the same exception for firebase apps, since they
|
||||
// are tightly bound to google, and require google auth to work.
|
||||
// See: #5075, #9852, #10367
|
||||
if (prefs_->GetBoolean(kGoogleLoginControlType)) {
|
||||
const auto google_auth_rule = Rule(
|
||||
ContentSettingsPattern::FromString(kGoogleAuthPattern),
|
||||
ContentSettingsPattern::Wildcard(),
|
||||
base::Value::FromUniquePtrValue(
|
||||
ContentSettingToValue(CONTENT_SETTING_ALLOW)),
|
||||
base::Time(), SessionModel::Durable);
|
||||
//
|
||||
// PS: kGoogleLoginControlType preference might not be registered for tests.
|
||||
if (prefs_->FindPreference(kGoogleLoginControlType) &&
|
||||
prefs_->GetBoolean(kGoogleLoginControlType)) {
|
||||
const auto google_auth_rule =
|
||||
Rule(ContentSettingsPattern::FromString(kGoogleAuthPattern),
|
||||
ContentSettingsPattern::Wildcard(),
|
||||
base::Value::FromUniquePtrValue(
|
||||
ContentSettingToValue(CONTENT_SETTING_ALLOW)),
|
||||
base::Time(), SessionModel::Durable);
|
||||
rules.emplace_back(CloneRule(google_auth_rule));
|
||||
brave_cookie_rules_[incognito].emplace_back(CloneRule(google_auth_rule));
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
source_set("privacy_sandbox") {
|
||||
sources = [
|
||||
"brave_privacy_sandbox_settings.cc",
|
||||
"brave_privacy_sandbox_settings.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//components/content_settings/core/browser",
|
||||
"//components/prefs",
|
||||
"//components/privacy_sandbox",
|
||||
"//components/privacy_sandbox:privacy_sandbox_prefs",
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "brave/components/privacy_sandbox/brave_privacy_sandbox_settings.h"
|
||||
|
||||
#include "components/content_settings/core/browser/cookie_settings.h"
|
||||
#include "components/content_settings/core/browser/host_content_settings_map.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
|
||||
|
||||
BravePrivacySandboxSettings::BravePrivacySandboxSettings(
|
||||
HostContentSettingsMap* host_content_settings_map,
|
||||
content_settings::CookieSettings* cookie_settings,
|
||||
PrefService* pref_service)
|
||||
: PrivacySandboxSettings(host_content_settings_map,
|
||||
cookie_settings,
|
||||
pref_service),
|
||||
pref_service_(pref_service) {
|
||||
// Register observers for the Privacy Sandbox & FLoC preferences.
|
||||
user_prefs_registrar_.Init(pref_service_);
|
||||
user_prefs_registrar_.Add(
|
||||
prefs::kPrivacySandboxApisEnabled,
|
||||
base::BindRepeating(
|
||||
&BravePrivacySandboxSettings::OnPrivacySandboxPrefChanged,
|
||||
base::Unretained(this)));
|
||||
user_prefs_registrar_.Add(
|
||||
prefs::kPrivacySandboxFlocEnabled,
|
||||
base::BindRepeating(
|
||||
&BravePrivacySandboxSettings::OnPrivacySandboxPrefChanged,
|
||||
base::Unretained(this)));
|
||||
}
|
||||
|
||||
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.
|
||||
if (pref_service_->GetBoolean(prefs::kPrivacySandboxApisEnabled)) {
|
||||
pref_service_->SetBoolean(prefs::kPrivacySandboxApisEnabled, false);
|
||||
}
|
||||
if (pref_service_->GetBoolean(prefs::kPrivacySandboxFlocEnabled)) {
|
||||
pref_service_->SetBoolean(prefs::kPrivacySandboxFlocEnabled, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_COMPONENTS_PRIVACY_SANDBOX_BRAVE_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
#define BRAVE_COMPONENTS_PRIVACY_SANDBOX_BRAVE_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
|
||||
#include "components/content_settings/core/browser/cookie_settings.h"
|
||||
#include "components/prefs/pref_change_registrar.h"
|
||||
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
|
||||
|
||||
class HostContentSettingsMap;
|
||||
class PrefService;
|
||||
|
||||
namespace content_settings {
|
||||
class CookieSettings;
|
||||
}
|
||||
|
||||
class BravePrivacySandboxSettings : public PrivacySandboxSettings {
|
||||
public:
|
||||
BravePrivacySandboxSettings(HostContentSettingsMap* host_content_settings_map,
|
||||
content_settings::CookieSettings* cookie_settings,
|
||||
PrefService* pref_service);
|
||||
~BravePrivacySandboxSettings() override;
|
||||
|
||||
private:
|
||||
// Callback to ensure we don't ever enable the Privacy Sandbox.
|
||||
void OnPrivacySandboxPrefChanged();
|
||||
|
||||
raw_ptr<PrefService> pref_service_;
|
||||
PrefChangeRegistrar user_prefs_registrar_;
|
||||
};
|
||||
|
||||
#endif // BRAVE_COMPONENTS_PRIVACY_SANDBOX_BRAVE_PRIVACY_SANDBOX_SETTINGS_H_
|
||||
@@ -177,6 +177,7 @@ test("brave_unit_tests") {
|
||||
"//brave/browser/widevine:unittest",
|
||||
"//brave/chromium_src/chrome/browser/privacy_sandbox:unit_tests",
|
||||
"//brave/chromium_src/components/autofill_assistant/browser:unit_tests",
|
||||
"//brave/chromium_src/components/privacy_sandbox:unit_tests",
|
||||
"//brave/chromium_src/net/base:unit_tests",
|
||||
"//brave/common:network_constants",
|
||||
"//brave/common:pref_names",
|
||||
|
||||
Reference in New Issue
Block a user