Skip to content

Commit

Permalink
[M125] Integrate reset of cohort with ChromeBrowsingDataRemover
Browse files Browse the repository at this point in the history
(Merge to release branch M125)

- This makes sure cohorts are reset when Cookies are cleared.
- It also sets metadata entries with source testing eligible for DTRP.
This will be best appropriate for dummy entries. This is done in this CL
to ease tag along with feature merge request.

(cherry picked from commit a9b6a68)

Bug: b:335409490
Change-Id: I9e6b6bc5a4842f8fbab0d8532f0963d98ed643d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5458857
Reviewed-by: Anton Maliev <amaliev@chromium.org>
Commit-Queue: Jonathan Njeunje <njeunje@chromium.org>
Reviewed-by: Joshua Pawlicki <waffles@chromium.org>
Reviewed-by: Christian Dullweber <dullweber@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1289485}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5473105
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Jonathan Njeunje <njeunje@chromium.org>
Cr-Commit-Position: refs/branch-heads/6422@{#220}
Cr-Branched-From: 9012208-refs/heads/main@{#1287751}
  • Loading branch information
njeunje-g authored and Chromium LUCI CQ committed Apr 22, 2024
1 parent 6b32e58 commit c531ca3
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/tpcd/metadata/manager_factory.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/find_bar/find_bar_state.h"
#include "chrome/browser/ui/find_bar/find_bar_state_factory.h"
Expand Down Expand Up @@ -125,6 +126,7 @@
#include "components/search_engines/template_url_service.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/tpcd/metadata/manager.h"
#include "components/web_cache/browser/web_cache_manager.h"
#include "components/webrtc_logging/browser/log_cleanup.h"
#include "components/webrtc_logging/browser/text_log_list.h"
Expand Down Expand Up @@ -658,6 +660,11 @@ void ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(
base::Unretained(this), TracingDataType::kCookies));
safe_browsing::VerdictCacheManagerFactory::GetForProfile(profile_)
->OnCookiesDeleted();

if (tpcd::metadata::Manager* manager =
tpcd::metadata::ManagerFactory::GetForProfile(profile_)) {
manager->ResetCohorts(website_settings_filter);
}
}

if (filter_builder->GetMode() ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdint.h>

#include <memory>
#include <set>
#include <string>
#include <utility>
Expand Down Expand Up @@ -78,6 +79,7 @@
#include "chrome/browser/storage/durable_storage_permission_context.h"
#include "chrome/browser/subresource_filter/subresource_filter_profile_context_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/tpcd/metadata/manager_factory.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/trusted_vault/trusted_vault_service_factory.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
Expand Down Expand Up @@ -153,6 +155,10 @@
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/site_isolation/pref_names.h"
#include "components/sync/test/test_sync_service.h"
#include "components/tpcd/metadata/manager.h"
#include "components/tpcd/metadata/parser.h"
#include "components/tpcd/metadata/parser_test_helper.h"
#include "components/tpcd/metadata/prefs.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/background_tracing_manager.h"
#include "content/public/browser/browser_context.h"
Expand All @@ -168,6 +174,7 @@
#include "content/public/test/mock_download_manager.h"
#include "content/public/test/test_utils.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/features.h"
#include "net/base/isolation_info.h"
#include "net/base/network_anonymization_key.h"
#include "net/cookies/canonical_cookie.h"
Expand Down Expand Up @@ -753,6 +760,41 @@ class RemovePermissionPromptCountsTest {
raw_ptr<permissions::PermissionDecisionAutoBlocker> autoblocker_;
};

class RemoveTpcdMetadataCohortsTester {
public:
explicit RemoveTpcdMetadataCohortsTester(TestingProfile* profile) {
det_generator_ = new tpcd::metadata::DeterministicGenerator();
tpcd::metadata::RegisterLocalStatePrefs(local_state_.registry());
manager_ = tpcd::metadata::ManagerFactory::GetForProfile(profile);
manager_->SetRandGeneratorForTesting(det_generator_);
manager_->SetPrefServiceForTesting(&local_state_);
}
~RemoveTpcdMetadataCohortsTester() {
det_generator_ = nullptr;
manager_ = nullptr;
}

RemoveTpcdMetadataCohortsTester(const RemoveTpcdMetadataCohortsTester&) =
delete;
RemoveTpcdMetadataCohortsTester& operator=(
const RemoveTpcdMetadataCohortsTester&) = delete;

tpcd::metadata::Parser* GetParser() {
return tpcd::metadata::Parser::GetInstance();
}

tpcd::metadata::Manager* GetManager() { return manager_; }

tpcd::metadata::DeterministicGenerator* GetDetGenerator() {
return det_generator_;
}

private:
raw_ptr<tpcd::metadata::Manager> manager_;
raw_ptr<tpcd::metadata::DeterministicGenerator> det_generator_;
TestingPrefServiceSimple local_state_;
};

// Custom matcher to test the equivalence of two URL filters. Since those are
// blackbox predicates, we can only approximate the equivalence by testing
// whether the filter give the same answer for several URLs. This is currently
Expand Down Expand Up @@ -4389,3 +4431,164 @@ TEST_F(ChromeBrowsingDataRemoverDelegateMediaDeviceSaltTest,
EXPECT_EQ(salt2c, salt2b);
EXPECT_EQ(salt3c, salt3b);
}

class ChromeBrowsingDataRemoverDelegateTpcdMetadataTest
: public ChromeBrowsingDataRemoverDelegateTest {
public:
ChromeBrowsingDataRemoverDelegateTpcdMetadataTest() {
feature_list_.InitWithFeatures({net::features::kTpcdMetadataStagedRollback},
{});
}
};

TEST_F(ChromeBrowsingDataRemoverDelegateTpcdMetadataTest, ResetAllCohorts) {
auto tester = RemoveTpcdMetadataCohortsTester(GetProfile());

const std::string primary_pattern_spec = "https://example1.com";
const std::string primary_pattern_spec_2 = "https://example2.com";
const std::string secondary_pattern_spec = "https://example3.com";

// dtrp is arbitrary here, selected between (0,100).
const uint32_t dtrp = 10;
tpcd::metadata::Metadata metadata;
tpcd::metadata::helpers::AddEntryToMetadata(
metadata, primary_pattern_spec, secondary_pattern_spec,
tpcd::metadata::Parser::kSource1pDt, dtrp);
tpcd::metadata::helpers::AddEntryToMetadata(
metadata, primary_pattern_spec_2, secondary_pattern_spec,
tpcd::metadata::Parser::kSource1pDt, dtrp);

// Establish grant with deterministic cohorts.
{
// rand is set as-is here to guarantee GRACE_PERIOD_FORCED_ON.
uint32_t rand = dtrp + 1;
tester.GetDetGenerator()->set_rand(rand);

tester.GetParser()->ParseMetadata(metadata.SerializeAsString());

EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
}

// Make sure the cohorts are persisted.
{
// rand is set as-is here to guarantee GRACE_PERIOD_FORCED_OFF.
uint32_t rand = dtrp;
tester.GetDetGenerator()->set_rand(rand);

tester.GetParser()->ParseMetadata(metadata.SerializeAsString());

EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
}

// Apply deletion of cookies.
BlockUntilBrowsingDataRemoved(base::Time(), base::Time::Max(),
content::BrowsingDataRemover::DATA_TYPE_COOKIES,
false);

// Make sure the cohorts was reset.
{
EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_OFF);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_OFF);
}
}

TEST_F(ChromeBrowsingDataRemoverDelegateTpcdMetadataTest, ResetOneCohort) {
auto tester = RemoveTpcdMetadataCohortsTester(GetProfile());

const std::string primary_pattern_spec = "https://example1.com";
const std::string primary_pattern_spec_2 = "https://example2.com";
const std::string secondary_pattern_spec = "https://example3.com";

// dtrp is arbitrary here, selected between (0,100).
const uint32_t dtrp = 10;
tpcd::metadata::Metadata metadata;
tpcd::metadata::helpers::AddEntryToMetadata(
metadata, primary_pattern_spec, secondary_pattern_spec,
tpcd::metadata::Parser::kSource1pDt, dtrp);
tpcd::metadata::helpers::AddEntryToMetadata(
metadata, primary_pattern_spec_2, secondary_pattern_spec,
tpcd::metadata::Parser::kSource1pDt, dtrp);

// Establish grant with deterministic cohorts.
{
// rand is set as-is here to guarantee GRACE_PERIOD_FORCED_ON.
uint32_t rand = dtrp + 1;
tester.GetDetGenerator()->set_rand(rand);

tester.GetParser()->ParseMetadata(metadata.SerializeAsString());

EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
}

// Make sure the cohorts are persisted.
{
// rand is set as-is here to guarantee GRACE_PERIOD_FORCED_OFF.
uint32_t rand = dtrp;
tester.GetDetGenerator()->set_rand(rand);

tester.GetParser()->ParseMetadata(metadata.SerializeAsString());

EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
}

// Apply deletion of cookies.
std::unique_ptr<BrowsingDataFilterBuilder> filter(
BrowsingDataFilterBuilder::Create(
BrowsingDataFilterBuilder::Mode::kDelete));
filter->AddRegisterableDomain(GURL(primary_pattern_spec).host());
BlockUntilOriginDataRemoved(base::Time(), base::Time::Max(),
content::BrowsingDataRemover::DATA_TYPE_COOKIES,
std::move(filter));

// Make sure only one cohort was reset.
{
EXPECT_EQ(
tester.GetManager()
->GetGrants()
.front()
.metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_OFF);
EXPECT_EQ(
tester.GetManager()->GetGrants().back().metadata.tpcd_metadata_cohort(),
content_settings::mojom::TpcdMetadataCohort::GRACE_PERIOD_FORCED_ON);
}
}
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7051,6 +7051,7 @@ test("unit_tests") {
"//components/tab_groups",
"//components/tpcd/metadata",
"//components/tpcd/metadata:metadata_proto",
"//components/tpcd/metadata:prefs",
"//components/tpcd/metadata:test_support",
"//components/tracing:background_tracing_utils",
"//components/tracing:startup_tracing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ TEST_P(TpcdMetadataComponentInstallerPolicyTest, ComponentReady_kIllicitDtrp) {
tpcd::metadata::Metadata metadata;
tpcd::metadata::helpers::AddEntryToMetadata(
metadata, primary_pattern_spec, secondary_pattern_spec,
tpcd::metadata::Parser::kSourceTest, std::nullopt,
tpcd::metadata::Parser::kSourceCriticalSector, std::nullopt,
tpcd::metadata::Parser::kMaxDtrp);
ASSERT_EQ(metadata.metadata_entries_size(), 1);

Expand All @@ -294,7 +294,8 @@ TEST_P(TpcdMetadataComponentInstallerPolicyTest, ComponentReady_FiresCallback) {

tpcd::metadata::Metadata metadata;
tpcd::metadata::helpers::AddEntryToMetadata(metadata, primary_pattern_spec,
secondary_pattern_spec);
secondary_pattern_spec,
Parser::kSourceCriticalSector);
ASSERT_EQ(metadata.metadata_entries_size(), 1);

ExecFakeComponentInstallation(metadata.SerializeAsString());
Expand Down Expand Up @@ -330,7 +331,8 @@ TEST_P(TpcdMetadataComponentInstallerPolicyTest,

tpcd::metadata::Metadata metadata;
tpcd::metadata::helpers::AddEntryToMetadata(metadata, primary_pattern_spec,
secondary_pattern_spec);
secondary_pattern_spec,
Parser::kSourceCriticalSector);
ASSERT_EQ(metadata.metadata_entries_size(), 1);

ExecFakeComponentInstallation(metadata.SerializeAsString());
Expand Down
Loading

0 comments on commit c531ca3

Please sign in to comment.