field_trial_param_associator.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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();
  21. // Retrieve the singleton.
  22. static FieldTrialParamAssociator* GetInstance();
  23. // Sets parameters for the given field trial name and group.
  24. bool AssociateFieldTrialParams(const std::string& trial_name,
  25. const std::string& group_name,
  26. const FieldTrialParams& params);
  27. // Gets the parameters for a field trial and its chosen group. If not found in
  28. // field_trial_params_, then tries to looks it up in shared memory.
  29. bool GetFieldTrialParams(const std::string& trial_name,
  30. FieldTrialParams* params);
  31. // Gets the parameters for a field trial and its chosen group. Does not
  32. // fallback to looking it up in shared memory. This should only be used if you
  33. // know for sure the params are in the mapping, like if you're in the browser
  34. // process, and even then you should probably just use GetFieldTrialParams().
  35. bool GetFieldTrialParamsWithoutFallback(const std::string& trial_name,
  36. const std::string& group_name,
  37. FieldTrialParams* params);
  38. // Clears the internal field_trial_params_ mapping, plus removes all params in
  39. // shared memory.
  40. void ClearAllParamsForTesting();
  41. // Clears a single field trial param.
  42. // Note: this does NOT remove the param in shared memory.
  43. void ClearParamsForTesting(const std::string& trial_name,
  44. const std::string& group_name);
  45. // Clears the internal field_trial_params_ mapping.
  46. void ClearAllCachedParamsForTesting();
  47. private:
  48. friend struct DefaultSingletonTraits<FieldTrialParamAssociator>;
  49. // (field_trial_name, field_trial_group)
  50. typedef std::pair<std::string, std::string> FieldTrialKey;
  51. Lock lock_;
  52. std::map<FieldTrialKey, FieldTrialParams> field_trial_params_;
  53. DISALLOW_COPY_AND_ASSIGN(FieldTrialParamAssociator);
  54. };
  55. } // namespace base
  56. #endif // BASE_METRICS_FIELD_TRIAL_PARAM_ASSOCIATOR_H_