File: sticky_activation_manager.h

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 (97 lines) | stat: -rw-r--r-- 4,068 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
// 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.

#ifndef COMPONENTS_VARIATIONS_STICKY_ACTIVATION_MANAGER_H_
#define COMPONENTS_VARIATIONS_STICKY_ACTIVATION_MANAGER_H_

#include <map>
#include <string>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial.h"

class PrefService;
class PrefRegistrySimple;

namespace variations {

// Manages the set of field trials marked with the activation type
// STICKY_AFTER_QUERY. Responsible for persisting information about activated
// trials to Local State and using that information to determine if a trial
// should be activated on the next startup.
//
// A sticky trial should be activated on startup if it was active in the
// previous session (including from stickiness on startup) and its group
// selection did not change (i.e. due to a change to its config or something
// external like the client's randomization inputs).
class COMPONENT_EXPORT(VARIATIONS) StickyActivationManager
    : public base::FieldTrialList::Observer {
 public:
  // Map from trial name to group name. We use std::map since sorted order is
  // useful for deterministic serialization to string.
  //
  // It's possible that base::flat_map would be a better type here, but this
  // does get updated many times and current docs are unclear whether its size
  // (O(100) entries) qualifies as small.
  using TrialNameToGroupNameMap = std::map<std::string, std::string>;

  // `local_state` may be null for tests, in which case no prior stickiness
  // information will be loaded and none will be saved.
  explicit StickyActivationManager(PrefService* local_state,
                                   bool sticky_activation_enabled = false);

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

  ~StickyActivationManager() override;

  // Registers the prefs used by this class.
  static void RegisterPrefs(PrefRegistrySimple& registry);

  // Checks if the specified trial should be activated on startup, based on the
  // persisted information for sticky trials. Should only be called prior to
  // StartMonitoring() and should only be called on sticky activation trials.
  // Internally, this marks the trial as sticky for the purpose of monitoring
  // its activation state.
  bool ShouldActivate(const std::string& trial_name,
                      const std::string& group_name);

  // Starts monitoring field trial activations. May not be called more than
  // once. Note: This is a no-op if `sticky_activation_enabled_` is false.
  void StartMonitoring();

 private:
  // base::FieldTrialList::Observer:
  void OnFieldTrialGroupFinalized(const base::FieldTrial& trial,
                                  const std::string& group_name) override;

  // Updates the pref based on `active_sticky_trials_`.
  void UpdatePref();

  raw_ptr<PrefService> local_state_;

  // Whether support for STICKY_AFTER_QUERY activation for studies is enabled.
  // TODO: crbug.com/435630455 - Fully enable and remove this once ready.
  bool sticky_activation_enabled_ = false;

  // Whether StartMonitoring() has been called. Note: This just tracks that the
  // function was called (to prevent it being called multiple times), but may
  // not reflect whether monitoring is actually happening, since the function
  // can be no-op if `sticky_activation_enabled_` is false.
  bool monitoring_started_ = false;

  // The field trials loaded at startup from prefs.
  // Note: This is cleared upon calling StartMonitoring(), as it is no longer
  // needed after that.
  TrialNameToGroupNameMap loaded_sticky_trials_;

  // The currently active trials, for persistence to prefs. Updated via calls to
  // ShouldActivate() and observer callbacks to OnFieldTrialGroupFinalized().
  TrialNameToGroupNameMap active_sticky_trials_;
};

}  // namespace variations

#endif  // COMPONENTS_VARIATIONS_STICKY_ACTIVATION_MANAGER_H_