1// Copyright 2013 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#include "chrome/browser/chrome_browser_field_trials_desktop.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/environment.h"
11#include "base/metrics/field_trial.h"
12#include "base/prefs/pref_service.h"
13#include "base/strings/string_util.h"
14#include "chrome/browser/auto_launch_trial.h"
15#include "chrome/browser/google/google_brand.h"
16#include "chrome/browser/prerender/prerender_field_trial.h"
17#include "chrome/browser/profiles/profiles_state.h"
18#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
19#include "chrome/browser/ui/app_list/app_list_util.h"
20#include "chrome/browser/ui/sync/one_click_signin_helper.h"
21#include "chrome/common/chrome_constants.h"
22#include "chrome/common/chrome_switches.h"
23#include "chrome/common/chrome_version_info.h"
24#include "chrome/common/pref_names.h"
25#include "chrome/common/variations/variations_util.h"
26#include "content/public/common/content_constants.h"
27#include "net/spdy/spdy_session.h"
28#include "ui/base/layout.h"
29
30namespace chrome {
31
32namespace {
33
34void AutoLaunchChromeFieldTrial() {
35  std::string brand;
36  google_brand::GetBrand(&brand);
37
38  // Create a 100% field trial based on the brand code.
39  if (auto_launch_trial::IsInExperimentGroup(brand)) {
40    base::FieldTrialList::CreateFieldTrial(kAutoLaunchTrialName,
41                                           kAutoLaunchTrialAutoLaunchGroup);
42  } else if (auto_launch_trial::IsInControlGroup(brand)) {
43    base::FieldTrialList::CreateFieldTrial(kAutoLaunchTrialName,
44                                           kAutoLaunchTrialControlGroup);
45  }
46}
47
48void SetupInfiniteCacheFieldTrial() {
49  const base::FieldTrial::Probability kDivisor = 100;
50
51  base::FieldTrial::Probability infinite_cache_probability = 0;
52
53  scoped_refptr<base::FieldTrial> trial(
54      base::FieldTrialList::FactoryGetFieldTrial(
55          "InfiniteCache", kDivisor, "No", 2013, 12, 31,
56          base::FieldTrial::ONE_TIME_RANDOMIZED, NULL));
57  trial->AppendGroup("Yes", infinite_cache_probability);
58  trial->AppendGroup("Control", infinite_cache_probability);
59}
60
61void DisableShowProfileSwitcherTrialIfNecessary() {
62  // This trial is created by the VariationsService, but it needs to be disabled
63  // if multi-profiles isn't enabled.
64  base::FieldTrial* trial = base::FieldTrialList::Find("ShowProfileSwitcher");
65  if (trial && !profiles::IsMultipleProfilesEnabled())
66    trial->Disable();
67}
68
69void SetupPreReadFieldTrial() {
70  // The chrome executable will have set (or not) an environment variable with
71  // the group name into which this client belongs.
72  std::string group;
73  scoped_ptr<base::Environment> env(base::Environment::Create());
74  if (!env->GetVar(chrome::kPreReadEnvironmentVariable, &group) ||
75      group.empty()) {
76    return;
77  }
78
79  // Initialize the field trial. We declare all of the groups here (so that
80  // the dashboard creation tools can find them) but force the probability
81  // of being assigned to the group already chosen by the executable, if any,
82  // to 100%.
83  scoped_refptr<base::FieldTrial> trial(
84      base::FieldTrialList::FactoryGetFieldTrial(
85          "BrowserPreReadExperiment", 100, "100-pct-default",
86          2014, 7, 1, base::FieldTrial::SESSION_RANDOMIZED, NULL));
87  trial->AppendGroup("100-pct-control", group == "100-pct-control" ? 100 : 0);
88  trial->AppendGroup("95-pct", group == "95-pct" ? 100 : 0);
89  trial->AppendGroup("90-pct", group == "90-pct" ? 100 : 0);
90  trial->AppendGroup("85-pct", group == "85-pct" ? 100 : 0);
91  trial->AppendGroup("80-pct", group == "80-pct" ? 100 : 0);
92  trial->AppendGroup("75-pct", group == "75-pct" ? 100 : 0);
93  trial->AppendGroup("70-pct", group == "70-pct" ? 100 : 0);
94  trial->AppendGroup("65-pct", group == "65-pct" ? 100 : 0);
95  trial->AppendGroup("60-pct", group == "60-pct" ? 100 : 0);
96  trial->AppendGroup("55-pct", group == "55-pct" ? 100 : 0);
97  trial->AppendGroup("50-pct", group == "50-pct" ? 100 : 0);
98  trial->AppendGroup("45-pct", group == "45-pct" ? 100 : 0);
99  trial->AppendGroup("40-pct", group == "40-pct" ? 100 : 0);
100  trial->AppendGroup("35-pct", group == "35-pct" ? 100 : 0);
101  trial->AppendGroup("30-pct", group == "30-pct" ? 100 : 0);
102  trial->AppendGroup("25-pct", group == "25-pct" ? 100 : 0);
103  trial->AppendGroup("20-pct", group == "20-pct" ? 100 : 0);
104  trial->AppendGroup("15-pct", group == "15-pct" ? 100 : 0);
105  trial->AppendGroup("10-pct", group == "10-pct" ? 100 : 0);
106  trial->AppendGroup("5-pct", group == "5-pct" ? 100 : 0);
107  trial->AppendGroup("0-pct", group == "0-pct" ? 100 : 0);
108
109  // We have to call group in order to mark the experiment as active.
110  trial->group();
111}
112
113}  // namespace
114
115void SetupDesktopFieldTrials(const CommandLine& parsed_command_line,
116                             PrefService* local_state) {
117  prerender::ConfigurePrerender(parsed_command_line);
118  AutoLaunchChromeFieldTrial();
119  SetupInfiniteCacheFieldTrial();
120  DisableShowProfileSwitcherTrialIfNecessary();
121  SetupShowAppLauncherPromoFieldTrial(local_state);
122  SetupPreReadFieldTrial();
123}
124
125}  // namespace chrome
126