summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prerender/prerender_field_trial.cc
blob: 240232906a282ddc7d2286f00a9b217a50cb20f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/prerender/prerender_field_trial.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/metrics/metrics_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"

namespace prerender {

namespace {

int omnibox_original_group_id = 0;
int omnibox_conservative_group_id = 0;

const char* kOmniboxHeuristicNames[] = {
  "Original",
  "Conservative",
};
COMPILE_ASSERT(arraysize(kOmniboxHeuristicNames) == OMNIBOX_HEURISTIC_MAX,
               OmniboxHeuristic_name_count_mismatch);

const char* NameFromOmniboxHeuristic(OmniboxHeuristic heuristic) {
  DCHECK_LT(static_cast<unsigned int>(heuristic),
            arraysize(kOmniboxHeuristicNames));
  return kOmniboxHeuristicNames[heuristic];
}

}  // end namespace

// If the command line contains the --prerender-from-omnibox switch, enable
// prerendering from the Omnibox. If not, enter the user into a field trial.
void ConfigurePrerenderFromOmnibox();

void ConfigurePrefetchAndPrerender(const CommandLine& command_line) {
  enum PrerenderOption {
    PRERENDER_OPTION_AUTO,
    PRERENDER_OPTION_DISABLED,
    PRERENDER_OPTION_ENABLED,
    PRERENDER_OPTION_PREFETCH_ONLY,
  };

  PrerenderOption prerender_option = PRERENDER_OPTION_AUTO;
  if (command_line.HasSwitch(switches::kPrerender)) {
    const std::string switch_value =
        command_line.GetSwitchValueASCII(switches::kPrerender);

    if (switch_value == switches::kPrerenderSwitchValueAuto) {
      prerender_option = PRERENDER_OPTION_AUTO;
    } else if (switch_value == switches::kPrerenderSwitchValueDisabled) {
      prerender_option = PRERENDER_OPTION_DISABLED;
    } else if (switch_value.empty() ||
               switch_value == switches::kPrerenderSwitchValueEnabled) {
      // The empty string means the option was provided with no value, and that
      // means enable.
      prerender_option = PRERENDER_OPTION_ENABLED;
    } else if (switch_value == switches::kPrerenderSwitchValuePrefetchOnly) {
      prerender_option = PRERENDER_OPTION_PREFETCH_ONLY;
    } else {
      prerender_option = PRERENDER_OPTION_DISABLED;
      LOG(ERROR) << "Invalid --prerender option received on command line: "
                 << switch_value;
      LOG(ERROR) << "Disabling prerendering!";
    }
  }

  switch (prerender_option) {
    case PRERENDER_OPTION_AUTO: {
      const base::FieldTrial::Probability kPrerenderDivisor = 1000;
      const base::FieldTrial::Probability kPrerenderExp1Probability = 250;
      const base::FieldTrial::Probability kPrerenderControl1Probability = 250;
      const base::FieldTrial::Probability kPrerenderExp2Probability = 250;
      const base::FieldTrial::Probability kPrerenderControl2Probability = 250;
      COMPILE_ASSERT(
          kPrerenderExp1Probability + kPrerenderControl1Probability +
          kPrerenderExp2Probability + kPrerenderControl2Probability ==
          kPrerenderDivisor,
          PrerenderFieldTrial_numerator_matches_denominator);
      scoped_refptr<base::FieldTrial> trial(
          new base::FieldTrial("Prefetch", kPrerenderDivisor,
                               "ContentPrefetchPrerender1", 2012, 6, 30));

      const int kPrerenderExperiment1Group = trial->kDefaultGroupNumber;
      const int kPrerenderControl1Group =
          trial->AppendGroup("ContentPrefetchPrerenderControl1",
                             kPrerenderControl1Probability);
      const int kPrerenderExperiment2Group =
          trial->AppendGroup("ContentPrefetchPrerender2",
                             kPrerenderExp2Probability);
      const int kPrerenderControl2Group =
          trial->AppendGroup("ContentPrefetchPrerenderControl2",
                             kPrerenderControl2Probability);
      const int trial_group = trial->group();
      if (trial_group == kPrerenderExperiment1Group ||
                 trial_group == kPrerenderExperiment2Group) {
        ResourceDispatcherHost::set_is_prefetch_enabled(false);
        PrerenderManager::SetMode(
            PrerenderManager::PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP);
      } else if (trial_group == kPrerenderControl1Group ||
                 trial_group == kPrerenderControl2Group) {
        ResourceDispatcherHost::set_is_prefetch_enabled(false);
        PrerenderManager::SetMode(
            PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
      } else {
        NOTREACHED();
      }
      break;
    }
    case PRERENDER_OPTION_DISABLED:
      ResourceDispatcherHost::set_is_prefetch_enabled(false);
      PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED);
      break;
    case PRERENDER_OPTION_ENABLED:
      ResourceDispatcherHost::set_is_prefetch_enabled(false);
      PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_ENABLED);
      break;
    case PRERENDER_OPTION_PREFETCH_ONLY:
      ResourceDispatcherHost::set_is_prefetch_enabled(true);
      PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED);
      break;
    default:
      NOTREACHED();
  }

  UMA_HISTOGRAM_ENUMERATION("Prerender.Sessions",
                            PrerenderManager::GetMode(),
                            PrerenderManager::PRERENDER_MODE_MAX);

  ConfigurePrerenderFromOmnibox();
}

void ConfigurePrerenderFromOmnibox() {
  // Field trial to see if we're enabled.
  const base::FieldTrial::Probability kDivisor = 100;
  if (!CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kPrerenderFromOmnibox)) {
    const base::FieldTrial::Probability kEnabledProbability = 40;
    scoped_refptr<base::FieldTrial> trial(
        new base::FieldTrial("PrerenderFromOmnibox", kDivisor,
                             "OmniboxPrerenderDisabled", 2012, 8, 30));
    trial->AppendGroup("OmniboxPrerenderEnabled", kEnabledProbability);
  }

  // Field trial to see which heuristic to use.
  const base::FieldTrial::Probability kConservativeProbability = 50;
  scoped_refptr<base::FieldTrial> trial(
      new base::FieldTrial("PrerenderFromOmniboxHeuristic", kDivisor,
                           "OriginalAlgorithm", 2012, 8, 30));
  omnibox_original_group_id = base::FieldTrial::kDefaultGroupNumber;
  omnibox_conservative_group_id =
      trial->AppendGroup("ConservativeAlgorithm", kConservativeProbability);
}

bool IsOmniboxEnabled(Profile* profile) {
  if (!profile || profile->IsOffTheRecord())
    return false;

  if (!PrerenderManager::IsPrerenderingPossible())
    return false;

  if (CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kPrerenderFromOmnibox))
    return true;

  if (!MetricsServiceHelper::IsMetricsReportingEnabled())
    return false;

  const int group = base::FieldTrialList::FindValue("PrerenderFromOmnibox");
  return group != base::FieldTrial::kNotFinalized &&
         group != base::FieldTrial::kDefaultGroupNumber;
}

OmniboxHeuristic GetOmniboxHeuristicToUse() {
  const int group =
      base::FieldTrialList::FindValue("PrerenderFromOmniboxHeuristic");
  if (group == omnibox_original_group_id)
    return OMNIBOX_HEURISTIC_ORIGINAL;
  if (group == omnibox_conservative_group_id)
    return OMNIBOX_HEURISTIC_CONSERVATIVE;

  // If we don't have a group just return the original heuristic.
  return OMNIBOX_HEURISTIC_ORIGINAL;
}

std::string GetOmniboxHistogramSuffix() {
  return NameFromOmniboxHeuristic(prerender::GetOmniboxHeuristicToUse());
}

}  // namespace prerender