field_trial_param_associator.h 3.0 KB

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