1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/tpcd/experiment/eligibility_service.h"
#include <optional>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/tpcd/experiment/eligibility_service_factory.h"
#include "chrome/browser/tpcd/experiment/experiment_manager.h"
#include "chrome/browser/tpcd/experiment/tpcd_experiment_features.h"
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
#include "components/privacy_sandbox/tpcd_experiment_eligibility.h"
#include "components/privacy_sandbox/tracking_protection_onboarding.h"
#include "content/public/browser/cookie_deprecation_label_manager.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_features.h"
#include "services/network/public/mojom/network_context.mojom.h"
namespace tpcd::experiment {
EligibilityService::EligibilityService(
Profile* profile,
privacy_sandbox::TrackingProtectionOnboarding*
tracking_protection_onboarding,
privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
ExperimentManager* experiment_manager)
: profile_(profile),
onboarding_service_(tracking_protection_onboarding),
privacy_sandbox_settings_(privacy_sandbox_settings),
experiment_manager_(experiment_manager) {
CHECK(base::FeatureList::IsEnabled(
features::kCookieDeprecationFacilitatedTesting));
CHECK(experiment_manager_);
CHECK(privacy_sandbox_settings_);
profile_eligibility_ = ProfileEligibility();
BroadcastProfileEligibility();
}
EligibilityService::~EligibilityService() = default;
// static
EligibilityService* EligibilityService::Get(Profile* profile) {
return EligibilityServiceFactory::GetForProfile(profile);
}
void EligibilityService::Shutdown() {
if (onboarding_service_) {
onboarding_service_ = nullptr;
}
privacy_sandbox_settings_ = nullptr;
}
void EligibilityService::BroadcastProfileEligibility() {
CHECK(profile_eligibility_.has_value());
std::optional<bool> is_client_eligible =
experiment_manager_->IsClientEligible();
if (is_client_eligible.has_value()) {
MarkProfileEligibility(is_client_eligible.value());
return;
}
base::UmaHistogramEnumeration(
"PrivacySandbox.CookieDeprecationFacilitatedTesting."
"ReasonForEligibilityStoredInPrefs",
profile_eligibility_->reason());
experiment_manager_->SetClientEligibility(
profile_eligibility_->is_eligible(),
base::BindOnce(&EligibilityService::MarkProfileEligibility,
weak_factory_.GetWeakPtr()));
}
void EligibilityService::MarkProfileEligibility(bool is_client_eligible) {
UpdateCookieDeprecationLabel();
// Update the eligibility for the onboarding UX flow.
if (onboarding_service_) {
if (kDisable3PCookies.Get()) {
MaybeNotifyManagerTrackingProtectionOnboarded(
onboarding_service_->GetOnboardingStatus());
} else if (kEnableSilentOnboarding.Get()) {
MaybeNotifyManagerTrackingProtectionSilentOnboarded(
onboarding_service_->GetSilentOnboardingStatus());
}
}
}
privacy_sandbox::TpcdExperimentEligibility
EligibilityService::ProfileEligibility() {
CHECK(privacy_sandbox_settings_);
return privacy_sandbox_settings_
->GetCookieDeprecationExperimentCurrentEligibility();
}
void EligibilityService::UpdateCookieDeprecationLabel() {
// For each storage partition, update the cookie deprecation label to the
// updated value from the CookieDeprecationLabelManager.
profile_->ForEachLoadedStoragePartition(
[](content::StoragePartition* storage_partition) {
if (auto* cookie_deprecation_label_manager =
storage_partition->GetCookieDeprecationLabelManager()) {
storage_partition->GetNetworkContext()->SetCookieDeprecationLabel(
cookie_deprecation_label_manager->GetValue().value_or(""));
}
});
}
void EligibilityService::MaybeNotifyManagerTrackingProtectionOnboarded(
privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus
onboarding_status) {
if (onboarding_status == privacy_sandbox::TrackingProtectionOnboarding::
OnboardingStatus::kOnboarded) {
experiment_manager_->NotifyProfileTrackingProtectionOnboarded();
}
}
void EligibilityService::MaybeNotifyManagerTrackingProtectionSilentOnboarded(
privacy_sandbox::TrackingProtectionOnboarding::SilentOnboardingStatus
onboarding_status) {
if (onboarding_status == privacy_sandbox::TrackingProtectionOnboarding::
SilentOnboardingStatus::kOnboarded) {
experiment_manager_->NotifyProfileTrackingProtectionOnboarded();
}
}
} // namespace tpcd::experiment
|