1// Copyright (c) 2012 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
5#ifndef CHROME_BROWSER_METRICS_FIELD_TRIAL_SYNCHRONIZER_H_
6#define CHROME_BROWSER_METRICS_FIELD_TRIAL_SYNCHRONIZER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/metrics/field_trial.h"
14
15// This class is used by the browser process to communicate FieldTrial setting
16// (field trial name and group) to any previously started renderers.
17//
18// This class registers itself as an observer of FieldTrialList. FieldTrialList
19// notifies this class by calling it's OnFieldTrialGroupFinalized method when a
20// group is selected (finalized) for a FieldTrial and OnFieldTrialGroupFinalized
21// method sends the FieldTrial's name and the group to all renderer processes.
22// Each renderer process creates the FieldTrial, and by using a 100% probability
23// for the FieldTrial, forces the FieldTrial to have the same group string.
24
25class FieldTrialSynchronizer
26    : public base::RefCountedThreadSafe<FieldTrialSynchronizer>,
27      public base::FieldTrialList::Observer {
28 public:
29  // Construction also sets up the global singleton instance.  This instance is
30  // used to communicate between the UI and other threads, and is destroyed only
31  // as the main thread (browser_main) terminates, which means all other threads
32  // have completed, and will not need this instance any further. It adds itself
33  // as an observer of FieldTrialList so that it gets notified whenever a group
34  // is finalized in the browser process.
35  FieldTrialSynchronizer();
36
37  // Notify all renderer processes about the |group_name| that is finalized for
38  // the given field trail (|field_trial_name|). This is called on UI thread.
39  void NotifyAllRenderers(const std::string& field_trial_name,
40                          const std::string& group_name);
41
42  // FieldTrialList::Observer methods:
43
44  // This method is called by the FieldTrialList singleton when a trial's group
45  // is finalized. This method contacts all renderers (by calling
46  // NotifyAllRenderers) to create a FieldTrial that carries the randomly
47  // selected state from the browser process into all the renderer processes.
48  virtual void OnFieldTrialGroupFinalized(
49      const std::string& name,
50      const std::string& group_name) OVERRIDE;
51
52 private:
53  friend class base::RefCountedThreadSafe<FieldTrialSynchronizer>;
54  virtual ~FieldTrialSynchronizer();
55
56  DISALLOW_COPY_AND_ASSIGN(FieldTrialSynchronizer);
57};
58
59#endif  // CHROME_BROWSER_METRICS_FIELD_TRIAL_SYNCHRONIZER_H_
60