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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
// Copyright 2022 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/privacy_sandbox/privacy_sandbox_settings_delegate.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/feature_list.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_notice_confirmation.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_onboarding_factory.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/tpcd/experiment/experiment_manager.h"
#include "chrome/browser/tpcd/experiment/tpcd_experiment_features.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/tpcd_experiment_eligibility.h"
#include "components/privacy_sandbox/tracking_protection_onboarding.h"
#include "components/privacy_sandbox/tracking_protection_settings.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/tribool.h"
#include "content/public/common/content_features.h"
#include "net/cookies/cookie_util.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/webapps/webapp_registry.h"
#endif
namespace {
using TpcdExperimentEligibility = privacy_sandbox::TpcdExperimentEligibility;
signin::Tribool GetPrivacySandboxRestrictedByAccountCapability(
signin::IdentityManager* identity_manager) {
const auto core_account_info =
identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
const AccountInfo account_info =
identity_manager->FindExtendedAccountInfo(core_account_info);
return account_info.capabilities.can_run_chrome_privacy_sandbox_trials();
}
const base::FeatureParam<bool> kCookieDeprecationUseProfileFiltering{
&features::kCookieDeprecationFacilitatedTesting, "use_profile_filtering",
false};
} // namespace
PrivacySandboxSettingsDelegate::PrivacySandboxSettingsDelegate(
Profile* profile,
tpcd::experiment::ExperimentManager* experiment_manager,
PrivacySandboxCountries* privacy_sandbox_countries)
: profile_(profile),
experiment_manager_(experiment_manager),
privacy_sandbox_countries_(privacy_sandbox_countries)
#if BUILDFLAG(IS_ANDROID)
,
webapp_registry_(std::make_unique<WebappRegistry>())
#endif
{
}
PrivacySandboxSettingsDelegate::~PrivacySandboxSettingsDelegate() = default;
bool PrivacySandboxSettingsDelegate::IsRestrictedNoticeEnabled() const {
return privacy_sandbox::IsRestrictedNoticeRequired(
privacy_sandbox_countries_);
}
bool PrivacySandboxSettingsDelegate::IsPrivacySandboxRestricted() const {
// If the Sandbox was ever reported as restricted, it is always restricted.
// TODO (crbug.com/1428546): Adjust when we have a graduation flow.
bool was_ever_reported_as_restricted =
profile_->GetPrefs()->GetBoolean(prefs::kPrivacySandboxM1Restricted);
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile_);
if (!identity_manager ||
!identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)) {
// The user isn't signed in so we can't apply any capabilties-based
// restrictions.
return was_ever_reported_as_restricted;
}
auto restricted_by_capability =
GetPrivacySandboxRestrictedByAccountCapability(identity_manager);
// The Privacy Sandbox is not considered restricted unless the
// capability has a definitive false signal.
bool is_restricted = restricted_by_capability == signin::Tribool::kFalse;
// If the capability is restricting the Sandbox, "latch", so the sandbox is
// always restricted.
if (is_restricted) {
profile_->GetPrefs()->SetBoolean(prefs::kPrivacySandboxM1Restricted, true);
}
return was_ever_reported_as_restricted || is_restricted;
}
bool PrivacySandboxSettingsDelegate::IsPrivacySandboxCurrentlyUnrestricted()
const {
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile_);
if (!identity_manager ||
!identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)) {
// The user isn't signed in so we can't apply any capabilties-based
// restrictions.
return false;
}
const AccountInfo account_info =
identity_manager->FindExtendedPrimaryAccountInfo(
signin::ConsentLevel::kSignin);
auto capability =
account_info.capabilities.can_run_chrome_privacy_sandbox_trials();
return capability == signin::Tribool::kTrue;
}
bool PrivacySandboxSettingsDelegate::IsSubjectToM1NoticeRestricted() const {
// If the feature is deactivated, the notice shouldn't be shown.
if (!privacy_sandbox::IsRestrictedNoticeRequired(
privacy_sandbox_countries_)) {
return false;
}
return PrivacySandboxRestrictedNoticeRequired();
}
bool PrivacySandboxSettingsDelegate::IsIncognitoProfile() const {
return profile_->IsIncognitoProfile();
}
bool PrivacySandboxSettingsDelegate::HasAppropriateTopicsConsent() const {
// If the profile doesn't require a release 4 consent, then it always has
// an appropriate (i.e. not required) Topics consent.
if (!privacy_sandbox::IsConsentRequired(privacy_sandbox_countries_)) {
return true;
}
// Ideally we could consult the PrivacySandboxService, and centralise this
// logic. However, that service depends on PrivacySandboxSettings, which will
// own this delegate, and so including it here would create a circular
// dependency.
return profile_->GetPrefs()->GetBoolean(
prefs::kPrivacySandboxTopicsConsentGiven);
}
bool PrivacySandboxSettingsDelegate::PrivacySandboxRestrictedNoticeRequired()
const {
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile_);
if (!identity_manager ||
!identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)) {
// The user isn't signed in so we can't apply any capabilties-based
// restrictions.
return false;
}
const AccountInfo account_info =
identity_manager->FindExtendedPrimaryAccountInfo(
signin::ConsentLevel::kSignin);
auto capability =
account_info.capabilities
.is_subject_to_chrome_privacy_sandbox_restricted_measurement_notice();
return capability == signin::Tribool::kTrue;
}
bool PrivacySandboxSettingsDelegate::IsCookieDeprecationExperimentEligible()
const {
if (!base::FeatureList::IsEnabled(
features::kCookieDeprecationFacilitatedTesting)) {
return false;
}
if (!features::kCookieDeprecationFacilitatedTestingEnableOTRProfiles.Get() &&
(profile_->IsOffTheRecord() || profile_->IsGuestSession())) {
return false;
}
// Uses per-profile filtering if enabled.
if (kCookieDeprecationUseProfileFiltering.Get()) {
// The 3PCD experiment eligibility persists for the browser session.
if (!is_cookie_deprecation_experiment_eligible_.has_value()) {
is_cookie_deprecation_experiment_eligible_ =
GetCookieDeprecationExperimentCurrentEligibility().is_eligible();
}
return *is_cookie_deprecation_experiment_eligible_;
}
if (experiment_manager_) {
return experiment_manager_->IsClientEligible().value_or(false);
}
return false;
}
TpcdExperimentEligibility PrivacySandboxSettingsDelegate::
GetCookieDeprecationExperimentCurrentEligibility() const {
if (tpcd::experiment::kForceEligibleForTesting.Get()) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kForcedEligible);
}
// Whether third-party cookies are blocked.
if (tpcd::experiment::kExclude3PCBlocked.Get()) {
const auto cookie_controls_mode =
static_cast<content_settings::CookieControlsMode>(
profile_->GetPrefs()->GetInteger(prefs::kCookieControlsMode));
if (cookie_controls_mode ==
content_settings::CookieControlsMode::kBlockThirdParty) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::k3pCookiesBlocked);
}
scoped_refptr<content_settings::CookieSettings> cookie_settings =
CookieSettingsFactory::GetForProfile(profile_);
DCHECK(cookie_settings);
if (cookie_settings->GetDefaultCookieSetting() ==
ContentSetting::CONTENT_SETTING_BLOCK) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::k3pCookiesBlocked);
}
}
// Whether the privacy sandbox Ads APIs notice has been seen.
//
// TODO(linnan): Consider checking whether the restricted notice has been
// acknowledged (`prefs::kPrivacySandboxM1RestrictedNoticeAcknowledged`) as
// well.
if (tpcd::experiment::kExcludeNotSeenAdsAPIsNotice.Get()) {
const bool row_notice_acknowledged = profile_->GetPrefs()->GetBoolean(
prefs::kPrivacySandboxM1RowNoticeAcknowledged);
const bool eaa_notice_acknowledged = profile_->GetPrefs()->GetBoolean(
prefs::kPrivacySandboxM1EEANoticeAcknowledged);
if (!row_notice_acknowledged && !eaa_notice_acknowledged) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kHasNotSeenNotice);
}
}
// Whether it's a dasher account.
if (tpcd::experiment::kExcludeDasherAccount.Get() &&
IsSubjectToEnterprisePolicies()) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kEnterpriseUser);
}
// TODO(linnan): Consider moving the following client-level filtering to
// `ExperimentManager`.
// Whether it's a new client.
if (tpcd::experiment::kExcludeNewUser.Get()) {
base::Time install_date =
base::Time::FromTimeT(g_browser_process->local_state()->GetInt64(
metrics::prefs::kInstallDate));
if (install_date.is_null() ||
base::Time::Now() - install_date <
tpcd::experiment::kInstallTimeForNewUser.Get()) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kNewUser);
}
}
// Whether PWA or TWA has been installed on Android.
#if BUILDFLAG(IS_ANDROID)
if (tpcd::experiment::kExcludePwaOrTwaInstalled.Get() &&
!webapp_registry_->GetOriginsWithInstalledApp().empty()) {
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kPwaOrTwaInstalled);
}
#endif
return TpcdExperimentEligibility(
TpcdExperimentEligibility::Reason::kEligible);
}
bool PrivacySandboxSettingsDelegate::IsSubjectToEnterprisePolicies() const {
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile_);
if (!identity_manager ||
!identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)) {
// The user isn't signed in so we can't apply any capabilties-based
// restrictions.
return false;
}
const AccountInfo account_info =
identity_manager->FindExtendedPrimaryAccountInfo(
signin::ConsentLevel::kSignin);
auto capability =
account_info.capabilities.is_subject_to_enterprise_policies();
return capability == signin::Tribool::kTrue;
}
#if BUILDFLAG(IS_ANDROID)
void PrivacySandboxSettingsDelegate::OverrideWebappRegistryForTesting(
std::unique_ptr<WebappRegistry> webapp_registry) {
DCHECK(webapp_registry);
webapp_registry_ = std::move(webapp_registry);
}
#endif
bool PrivacySandboxSettingsDelegate::IsCookieDeprecationLabelAllowed() const {
if (!IsCookieDeprecationExperimentEligible()) {
return false;
}
auto* tracking_protection_onboarding =
TrackingProtectionOnboardingFactory::GetForProfile(profile_);
if (!tracking_protection_onboarding) {
return false;
}
if (tpcd::experiment::kDisable3PCookies.Get()) {
switch (tracking_protection_onboarding->GetOnboardingStatus()) {
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kIneligible:
return false;
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kEligible:
return !tpcd::experiment::kNeedOnboardingForLabel.Get();
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kOnboarded:
return true;
}
} else if (tpcd::experiment::kEnableSilentOnboarding.Get()) {
switch (tracking_protection_onboarding->GetSilentOnboardingStatus()) {
case privacy_sandbox::TrackingProtectionOnboarding::
SilentOnboardingStatus::kIneligible:
return false;
case privacy_sandbox::TrackingProtectionOnboarding::
SilentOnboardingStatus::kEligible:
return !tpcd::experiment::kNeedOnboardingForLabel.Get();
case privacy_sandbox::TrackingProtectionOnboarding::
SilentOnboardingStatus::kOnboarded:
return true;
}
} else {
return true;
}
}
bool PrivacySandboxSettingsDelegate::
AreThirdPartyCookiesBlockedByCookieDeprecationExperiment() const {
if (net::cookie_util::IsForceThirdPartyCookieBlockingEnabled()) {
return false;
}
if (!IsCookieDeprecationExperimentEligible()) {
return false;
}
if (!tpcd::experiment::kDisable3PCookies.Get()) {
return false;
}
auto* tracking_protection_onboarding =
TrackingProtectionOnboardingFactory::GetForProfile(profile_);
if (!tracking_protection_onboarding) {
return false;
}
// Third-party cookies are not disabled until the profile gets onboarded.
switch (tracking_protection_onboarding->GetOnboardingStatus()) {
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kIneligible:
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kEligible:
return false;
case privacy_sandbox::TrackingProtectionOnboarding::OnboardingStatus::
kOnboarded:
break;
}
// Respect user preferences.
auto* tracking_protection_settings =
TrackingProtectionSettingsFactory::GetForProfile(profile_);
if (tracking_protection_settings &&
tracking_protection_settings->AreAllThirdPartyCookiesBlocked()) {
return false;
}
const auto cookie_controls_mode =
static_cast<content_settings::CookieControlsMode>(
profile_->GetPrefs()->GetInteger(prefs::kCookieControlsMode));
if (cookie_controls_mode ==
content_settings::CookieControlsMode::kBlockThirdParty) {
return false;
}
return true;
}
|