File: sticky_activation_manager.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 6,173 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/sticky_activation_manager.h"

#include "base/debug/dump_without_crashing.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_list_including_low_anonymity.h"
#include "base/strings/string_split.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/variations/pref_names.h"

namespace variations {
namespace {

// Used as the group names for studies that we know have STICKY_AFTER_QUERY
// activation, but haven't been made active yet.
//
// Note: We intentionally use the same character as the separator for the pref,
// since a) that character is already reserved and can't appear naturally in
// these strings and b) to guarantee it's not something we'd load or save to the
// pref, as doing so would make it invalid.
const char kInactiveStickyTrialSentinel[] = "/";

// Parses the sticky studies pref value, which is expected to be of the format
// "Study1/Group1/Study2/Group2" and returns as a map from trial names to
// groups names.
StickyActivationManager::TrialNameToGroupNameMap ParsePref(
    const std::string& pref_value) {
  StickyActivationManager::TrialNameToGroupNameMap result;

  // Note: Even though base::FieldTrial::ParseFieldTrialsString() provides more
  // features than we need, by using it we benefit from the validation it does.
  std::vector<base::FieldTrial::State> entries;
  if (!base::FieldTrial::ParseFieldTrialsString(
          pref_value, /*override_trials=*/false, entries)) {
    // This is not a CHECK() since the pref value is external, but we still want
    // to monitor the occurrence of invalid prefs in case there is a a code
    // issue, so dump without crashing to signal the issue.
    base::debug::DumpWithoutCrashing();
    return result;
  }
  for (const auto& entry : entries) {
    result[std::string(entry.trial_name)] = std::string(entry.group_name);
  }
  return result;
}

// Encodes `trials` as a string pref value of the format
// "Study1/Group1/Study2/Group2".
std::string EncodePref(
    const StickyActivationManager::TrialNameToGroupNameMap& trials) {
  std::string pref_value;
  for (const auto& [key, value] : trials) {
    if (value == kInactiveStickyTrialSentinel) {
      continue;
    }
    if (!pref_value.empty()) {
      base::StrAppend(&pref_value, {"/"});
    }
    base::StrAppend(&pref_value, {key, "/", value});
  }
  return pref_value;
}

}  // namespace

StickyActivationManager::StickyActivationManager(PrefService* local_state,
                                                 bool sticky_activation_enabled)
    : local_state_(local_state),
      sticky_activation_enabled_(sticky_activation_enabled) {
  if (local_state && sticky_activation_enabled_) {
    loaded_sticky_trials_ =
        ParsePref(local_state_->GetString(prefs::kVariationsStickyStudies));
  }
}

StickyActivationManager::~StickyActivationManager() {
  if (monitoring_started_ && sticky_activation_enabled_) {
    base::FieldTrialListIncludingLowAnonymity::RemoveObserver(this);
  }
}

// static
void StickyActivationManager::RegisterPrefs(PrefRegistrySimple& registry) {
  registry.RegisterStringPref(prefs::kVariationsStickyStudies, "",
                              PrefRegistry::LOSSY_PREF);
}

void StickyActivationManager::StartMonitoring() {
  CHECK(!monitoring_started_);
  monitoring_started_ = true;

  if (!sticky_activation_enabled_) {
    return;
  }

  // Clear the loaded sticky trials, since these are no longer needed. The
  // entries that were activated have been copied over to
  // `active_sticky_trials_`.
  loaded_sticky_trials_.clear();

  base::FieldTrialListIncludingLowAnonymity::AddObserver(this);

  UpdatePref();
}

bool StickyActivationManager::ShouldActivate(const std::string& trial_name,
                                             const std::string& group_name) {
  CHECK(!monitoring_started_);
  if (!sticky_activation_enabled_) {
    return false;
  }

  auto it = loaded_sticky_trials_.find(trial_name);
  if (it != loaded_sticky_trials_.end() && it->second == group_name) {
    active_sticky_trials_[trial_name] = group_name;
    return true;
  }
  // Otherwise, we know this is a sticky trial and it's not active yet, so
  // reserve a slot for it so we can tell it's a sticky trial when we observe
  // its activation.
  active_sticky_trials_[trial_name] = kInactiveStickyTrialSentinel;
  return false;
}

void StickyActivationManager::OnFieldTrialGroupFinalized(
    const base::FieldTrial& trial,
    const std::string& group_name) {
  CHECK(monitoring_started_);
  CHECK(sticky_activation_enabled_);

  // Check whether the trial is present in `active_sticky_trials_`, which is how
  // we track which trials have the STICKY_AFTER_QUERY activation type.
  auto it = active_sticky_trials_.find(trial.trial_name());
  if (it != active_sticky_trials_.end()) {
    // We don't expect to be notified of the same trial twice, so the entry for
    // this trial should be the sentinel.
    //
    // Note: We DCHECK() instead of CHECK() here because this code being hit
    // relies both on a client-side coding bug, but also a specific server-side
    // payload that would exercise this (i.e. existence of STICKY_AFTER_QUERY
    // studies). We don't want a case where the client-side bug is introduced
    // but the server-side payload not exercising this to make it to Stable and
    // then start crashing lots of users, so use a DCHECK.
    DCHECK_EQ(it->second, kInactiveStickyTrialSentinel);

    it->second = group_name;
    UpdatePref();
  }
}

void StickyActivationManager::UpdatePref() {
  CHECK(monitoring_started_);
  CHECK(sticky_activation_enabled_);

  // TODO: crbug.com/435630455 - Instead of updating the pref each time,
  // schedule an update so that we can batch multiple updates together.
  if (!local_state_) {
    return;
  }

  std::string pref_value = EncodePref(active_sticky_trials_);
  local_state_->SetString(prefs::kVariationsStickyStudies, pref_value);
}

}  // namespace variations