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