child_process_field_trial_syncer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 COMPONENTS_VARIATIONS_CHILD_PROCESS_FIELD_TRIAL_SYNCER_H_
  5. #define COMPONENTS_VARIATIONS_CHILD_PROCESS_FIELD_TRIAL_SYNCER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/metrics/field_trial.h"
  10. #include "base/threading/thread_local.h"
  11. namespace variations {
  12. // Syncs the "activated" state of field trials between browser and child
  13. // processes. Specifically, when a field trial is activated in the browser, it
  14. // also activates it in the child process and when a field trial is activated in
  15. // the child process, it notifies the browser process to activate it.
  16. class COMPONENT_EXPORT(VARIATIONS) ChildProcessFieldTrialSyncer
  17. : public base::FieldTrialList::Observer {
  18. public:
  19. using FieldTrialActivatedCallback =
  20. base::RepeatingCallback<void(const std::string& trial_name)>;
  21. // Creates and returns the global ChildProcessFieldTrialSyncer instance for
  22. // this process. Immediately invokes |activated_callback| for each currently
  23. // active field trial. Then, |activated_callback| is invoked as field trial
  24. // are activated. |activated_callback| may be invoked from any sequence and
  25. // must therefore be thread-safe. CreateInstance() must not be called
  26. // concurrently with activating a field trial. The created instance is never
  27. // destroyed because it would be difficult to ensure that no field trial is
  28. // activated concurrently with unregistering it as an observer of
  29. // FieldTrialList (see FieldTrialList::RemoveObserver).
  30. static ChildProcessFieldTrialSyncer* CreateInstance(
  31. FieldTrialActivatedCallback activated_callback);
  32. ChildProcessFieldTrialSyncer(const ChildProcessFieldTrialSyncer&) = delete;
  33. ChildProcessFieldTrialSyncer& operator=(const ChildProcessFieldTrialSyncer&) =
  34. delete;
  35. // Deletes the global ChildProcessFieldTrialSyncer instance.
  36. static void DeleteInstanceForTesting();
  37. // Must be invoked when the browser process notifies this child process that a
  38. // field trial was activated.
  39. void SetFieldTrialGroupFromBrowser(const std::string& trial_name,
  40. const std::string& group_name);
  41. private:
  42. explicit ChildProcessFieldTrialSyncer(
  43. FieldTrialActivatedCallback activated_callback);
  44. ~ChildProcessFieldTrialSyncer() override;
  45. // Initializes field trial state change observation and invokes |callback_|
  46. // for any field trials that might have already been activated.
  47. void Init();
  48. // base::FieldTrialList::Observer:
  49. void OnFieldTrialGroupFinalized(const std::string& trial_name,
  50. const std::string& group_name) override;
  51. // Whether SetFieldTrialGroupFromBrowser() is being called on the current
  52. // thread.
  53. base::ThreadLocalBoolean in_set_field_trial_group_from_browser_;
  54. // Callback to invoke when a field trial is activated.
  55. const FieldTrialActivatedCallback activated_callback_;
  56. };
  57. } // namespace variations
  58. #endif // COMPONENTS_VARIATIONS_CHILD_PROCESS_FIELD_TRIAL_SYNCER_H_