File: experiment_manager_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 8,446 bytes parent folder | download | duplicates (5)
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
// 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/experiment_manager_impl.h"

#include <optional>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/metrics/field_trial_params.h"
#include "base/no_destructor.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tpcd/experiment/tpcd_experiment_features.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tpcd_pref_names.h"
#include "components/privacy_sandbox/tpcd_utils.h"
#include "components/variations/synthetic_trials.h"
#include "content/public/common/content_features.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace tpcd::experiment {
namespace {

const base::FeatureParam<std::string> kSyntheticTrialGroupOverride{
    &features::kCookieDeprecationFacilitatedTesting,
    "synthetic_trial_group_override", ""};

bool NeedsOnboardingForExperiment() {
  if (!kDisable3PCookies.Get() && !kEnableSilentOnboarding.Get()) {
    return false;
  }

  return kNeedOnboardingForSyntheticTrial.Get();
}

}  // namespace

// static
ExperimentManagerImpl* ExperimentManagerImpl::GetForProfile(Profile* profile) {
  if (!base::FeatureList::IsEnabled(
          features::kCookieDeprecationFacilitatedTesting)) {
    return nullptr;
  }

#if BUILDFLAG(IS_CHROMEOS)
  // Ash internal profile should not be accounted for the experiment
  // eligibility, and therefore should not create the experiment manager.
  if (!ash::IsUserBrowserContext(profile)) {
    return nullptr;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  if (!features::kCookieDeprecationFacilitatedTestingEnableOTRProfiles.Get() &&
      (profile->IsOffTheRecord() || profile->IsGuestSession())) {
    return nullptr;
  }

  return GetInstance();
}

// static
ExperimentManagerImpl* ExperimentManagerImpl::GetInstance() {
  static base::NoDestructor<ExperimentManagerImpl> instance;
  return instance.get();
}

ExperimentManagerImpl::ExperimentManagerImpl() {
  CHECK(base::FeatureList::IsEnabled(
      features::kCookieDeprecationFacilitatedTesting));

  PrefService* local_state = g_browser_process->local_state();
  CHECK(local_state);

  const int currentVersion = kVersion.Get();
  if (local_state->GetInteger(prefs::kTPCDExperimentClientStateVersion) !=
      currentVersion) {
    local_state->SetInteger(prefs::kTPCDExperimentClientStateVersion,
                            currentVersion);
    local_state->ClearPref(prefs::kTPCDExperimentClientState);
    did_version_change_ = true;
  }

  // If client eligibility is already known, do not recompute it.
  if (IsClientEligible().has_value()) {
    // The user must be re-registered to the synthetic trial on restart.
    MaybeUpdateSyntheticTrialRegistration();
    return;
  }

  // `ExperimentManager` is a singleton that lives forever, therefore it's safe
  // to use `base::Unretained()`.
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&ExperimentManagerImpl::CaptureEligibilityInLocalStatePref,
                     base::Unretained(this)),
      kDecisionDelayTime.Get());
}

ExperimentManagerImpl::~ExperimentManagerImpl() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void ExperimentManagerImpl::SetClientEligibility(
    bool is_eligible,
    EligibilityDecisionCallback on_eligibility_decision_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (std::optional<bool> client_is_eligible = IsClientEligible()) {
    // If client eligibility is already known, just run callback.
    client_is_eligible_ = *client_is_eligible;
    std::move(on_eligibility_decision_callback).Run(client_is_eligible_);
    return;
  }

  // Wait to run callback when decision is made in
  // `CaptureEligibilityInLocalStatePref`
  client_is_eligible_ = client_is_eligible_ && is_eligible;
  callbacks_.push_back(std::move(on_eligibility_decision_callback));
}

void ExperimentManagerImpl::CaptureEligibilityInLocalStatePref() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Update kTPCDExperimentClientState in the local state prefs.
  g_browser_process->local_state()->SetInteger(
      prefs::kTPCDExperimentClientState,
      client_is_eligible_
          ? static_cast<int>(utils::ExperimentState::kEligible)
          : static_cast<int>(utils::ExperimentState::kIneligible));

  // Register or unregister for the synthetic trial based on the new
  // eligibility local state pref.
  MaybeUpdateSyntheticTrialRegistration();

  // Run the EligibilityDecisionCallback for every profile that marked its
  // eligibility.
  for (auto& callback : callbacks_) {
    std::move(callback).Run(client_is_eligible_);
  }
  callbacks_.clear();
}

void ExperimentManagerImpl::MaybeUpdateSyntheticTrialRegistration() {
  if (!CanRegisterSyntheticTrial()) {
    return;
  }

  std::optional<bool> is_client_eligible = IsClientEligible();
  CHECK(is_client_eligible.has_value());

  std::string eligible_group_name =
      !kSyntheticTrialGroupOverride.Get().empty()
          ? kSyntheticTrialGroupOverride.Get()
          : features::kCookieDeprecationLabel.Get();
  std::string group_name = *is_client_eligible
                               ? eligible_group_name
                               : kSyntheticTrialInvalidGroupName;
  ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
      kSyntheticTrialName, group_name,
      variations::SyntheticTrialAnnotationMode::kCurrentLog);
}

std::optional<bool> ExperimentManagerImpl::IsClientEligible() const {
  if (kForceEligibleForTesting.Get()) {
    return true;
  }

  switch (g_browser_process->local_state()->GetInteger(
      prefs::kTPCDExperimentClientState)) {
    case static_cast<int>(utils::ExperimentState::kEligible):
    case static_cast<int>(utils::ExperimentState::kOnboarded):
      return true;
    case static_cast<int>(utils::ExperimentState::kIneligible):
      return false;
    case static_cast<int>(utils::ExperimentState::kUnknownEligibility):
      return std::nullopt;
    default:
      // invalid
      return false;
  }
}

bool ExperimentManagerImpl::DidVersionChange() const {
  return did_version_change_;
}

void ExperimentManagerImpl::NotifyProfileTrackingProtectionOnboarded() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!NeedsOnboardingForExperiment()) {
    return;
  }

  switch (g_browser_process->local_state()->GetInteger(
      prefs::kTPCDExperimentClientState)) {
    case static_cast<int>(utils::ExperimentState::kEligible):
      break;
    case static_cast<int>(utils::ExperimentState::kIneligible):
    case static_cast<int>(utils::ExperimentState::kOnboarded):
      return;
    case static_cast<int>(utils::ExperimentState::kUnknownEligibility):
      if (kForceEligibleForTesting.Get()) {
        break;
      } else {
        return;
      }
    default:
      // invalid
      return;
  }

  g_browser_process->local_state()->SetInteger(
      prefs::kTPCDExperimentClientState,
      static_cast<int>(utils::ExperimentState::kOnboarded));

  MaybeUpdateSyntheticTrialRegistration();
}

bool ExperimentManagerImpl::CanRegisterSyntheticTrial() const {
  switch (g_browser_process->local_state()->GetInteger(
      prefs::kTPCDExperimentClientState)) {
    case static_cast<int>(utils::ExperimentState::kEligible):
      return !NeedsOnboardingForExperiment();
    case static_cast<int>(utils::ExperimentState::kIneligible):
    case static_cast<int>(utils::ExperimentState::kOnboarded):
      return true;
    case static_cast<int>(utils::ExperimentState::kUnknownEligibility):
      if (kForceEligibleForTesting.Get()) {
        return !NeedsOnboardingForExperiment();
      } else {
        return false;
      }
    default:
      // invalid
      return true;
  }
}

}  // namespace tpcd::experiment