xref: /aosp_15_r20/external/cronet/base/metrics/field_trial_param_associator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_METRICS_FIELD_TRIAL_PARAM_ASSOCIATOR_H_
6 #define BASE_METRICS_FIELD_TRIAL_PARAM_ASSOCIATOR_H_
7 
8 #include <map>
9 #include <string>
10 #include <utility>
11 
12 #include "base/base_export.h"
13 #include "base/memory/singleton.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/metrics/field_trial_params.h"
16 #include "base/synchronization/lock.h"
17 #include "base/types/pass_key.h"
18 
19 class AppShimController;
20 
21 namespace base {
22 
23 // Keeps track of the parameters of all field trials and ensures access to them
24 // is thread-safe.
25 class BASE_EXPORT FieldTrialParamAssociator {
26  public:
27   FieldTrialParamAssociator();
28 
29   FieldTrialParamAssociator(const FieldTrialParamAssociator&) = delete;
30   FieldTrialParamAssociator& operator=(const FieldTrialParamAssociator&) =
31       delete;
32 
33   ~FieldTrialParamAssociator();
34 
35   // Retrieve the singleton.
36   static FieldTrialParamAssociator* GetInstance();
37 
38   // Sets parameters for the given field trial name and group.
39   bool AssociateFieldTrialParams(const std::string& trial_name,
40                                  const std::string& group_name,
41                                  const FieldTrialParams& params);
42 
43   // Gets the parameters for a field trial and its chosen group. If not found in
44   // field_trial_params_, then tries to looks it up in shared memory. Returns
45   // false if no params are available or the passed |field_trial| is null.
46   bool GetFieldTrialParams(FieldTrial* field_trial, FieldTrialParams* params);
47 
48   // Gets the parameters for a field trial and its chosen group. Does not
49   // fallback to looking it up in shared memory. This should only be used if you
50   // know for sure the params are in the mapping, like if you're in the browser
51   // process, and even then you should probably just use GetFieldTrialParams().
52   bool GetFieldTrialParamsWithoutFallback(const std::string& trial_name,
53                                           const std::string& group_name,
54                                           FieldTrialParams* params);
55 
56   // Clears the internal field_trial_params_ mapping, plus removes all params in
57   // shared memory.
58   void ClearAllParamsForTesting();
59 
60   // Clears a single field trial param.
61   // Note: this does NOT remove the param in shared memory.
62   void ClearParamsForTesting(const std::string& trial_name,
63                              const std::string& group_name);
64 
65   // Clears the internal field_trial_params_ mapping.
66   void ClearAllCachedParamsForTesting();
67 
68   // Clears the internal field_trial_params_ mapping for use by
69   // AppShimController when switching over from initial "early access" field
70   // trial information to the real long-term field trial information.
71   void ClearAllCachedParams(PassKey<AppShimController>);
72 
73  private:
74   friend struct DefaultSingletonTraits<FieldTrialParamAssociator>;
75 
76   // (field_trial_name, field_trial_group)
77   typedef std::pair<std::string, std::string> FieldTrialKey;
78   // The following type can be used for lookups without needing to copy strings.
79   typedef std::pair<const std::string&, const std::string&> FieldTrialRefKey;
80 
81   Lock lock_;
82   std::map<FieldTrialKey, FieldTrialParams> field_trial_params_;
83 };
84 
85 }  // namespace base
86 
87 #endif  // BASE_METRICS_FIELD_TRIAL_PARAM_ASSOCIATOR_H_
88