Home | History | Annotate | Download | only in prerender
      1 // Copyright (c) 2011 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/prerender/prerender_field_trial.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/logging.h"
      9 #include "base/metrics/field_trial.h"
     10 #include "base/metrics/histogram.h"
     11 #include "content/browser/renderer_host/resource_dispatcher_host.h"
     12 #include "chrome/browser/prerender/prerender_manager.h"
     13 #include "chrome/common/chrome_switches.h"
     14 
     15 
     16 namespace prerender {
     17 
     18 void ConfigurePrefetchAndPrerender(const CommandLine& command_line) {
     19   enum PrerenderOption {
     20     PRERENDER_OPTION_AUTO,
     21     PRERENDER_OPTION_DISABLED,
     22     PRERENDER_OPTION_ENABLED,
     23     PRERENDER_OPTION_PREFETCH_ONLY,
     24   };
     25 
     26   PrerenderOption prerender_option = PRERENDER_OPTION_AUTO;
     27   if (command_line.HasSwitch(switches::kPrerender)) {
     28     const std::string switch_value =
     29         command_line.GetSwitchValueASCII(switches::kPrerender);
     30 
     31     if (switch_value == switches::kPrerenderSwitchValueAuto) {
     32 #if defined(OS_CHROMEOS)
     33       // Prerender is temporarily disabled on CrOS.
     34       // http://crosbug.com/12483
     35       prerender_option = PRERENDER_OPTION_DISABLED;
     36 #else
     37       prerender_option = PRERENDER_OPTION_AUTO;
     38 #endif
     39     } else if (switch_value == switches::kPrerenderSwitchValueDisabled) {
     40       prerender_option = PRERENDER_OPTION_DISABLED;
     41     } else if (switch_value.empty() ||
     42                switch_value == switches::kPrerenderSwitchValueEnabled) {
     43       // The empty string means the option was provided with no value, and that
     44       // means enable.
     45       prerender_option = PRERENDER_OPTION_ENABLED;
     46     } else if (switch_value == switches::kPrerenderSwitchValuePrefetchOnly) {
     47       prerender_option = PRERENDER_OPTION_PREFETCH_ONLY;
     48     } else {
     49       prerender_option = PRERENDER_OPTION_DISABLED;
     50       LOG(ERROR) << "Invalid --prerender option received on command line: "
     51                  << switch_value;
     52       LOG(ERROR) << "Disabling prerendering!";
     53     }
     54   }
     55 
     56   switch (prerender_option) {
     57     case PRERENDER_OPTION_AUTO: {
     58       const base::FieldTrial::Probability kPrefetchDivisor = 1000;
     59       const base::FieldTrial::Probability kYesPrefetchProbability = 0;
     60       const base::FieldTrial::Probability kPrerenderExp1Probability = 0;
     61       const base::FieldTrial::Probability kPrerenderControl1Probability = 0;
     62       const base::FieldTrial::Probability kPrerenderExp2Probability = 0;
     63       const base::FieldTrial::Probability kPrerenderControl2Probability = 0;
     64 
     65       scoped_refptr<base::FieldTrial> trial(
     66           new base::FieldTrial("Prefetch", kPrefetchDivisor,
     67                                "ContentPrefetchDisabled", 2011, 6, 30));
     68 
     69       const int kNoPrefetchGroup = trial->kDefaultGroupNumber;
     70       const int kYesPrefetchGroup =
     71           trial->AppendGroup("ContentPrefetchEnabled", kYesPrefetchProbability);
     72       const int kPrerenderExperiment1Group =
     73           trial->AppendGroup("ContentPrefetchPrerender1",
     74                              kPrerenderExp1Probability);
     75       const int kPrerenderControl1Group =
     76           trial->AppendGroup("ContentPrefetchPrerenderControl1",
     77                              kPrerenderControl1Probability);
     78       const int kPrerenderExperiment2Group =
     79           trial->AppendGroup("ContentPrefetchPrerender2",
     80                              kPrerenderExp2Probability);
     81       const int kPrerenderControl2Group =
     82           trial->AppendGroup("ContentPrefetchPrerenderControl2",
     83                              kPrerenderControl2Probability);
     84       const int trial_group = trial->group();
     85       if (trial_group == kYesPrefetchGroup) {
     86         ResourceDispatcherHost::set_is_prefetch_enabled(true);
     87         PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED);
     88       } else if (trial_group == kNoPrefetchGroup) {
     89         ResourceDispatcherHost::set_is_prefetch_enabled(false);
     90         PrerenderManager::SetMode(
     91             PrerenderManager::PRERENDER_MODE_DISABLED);
     92       } else if (trial_group == kPrerenderExperiment1Group ||
     93                  trial_group == kPrerenderExperiment2Group) {
     94         ResourceDispatcherHost::set_is_prefetch_enabled(true);
     95         PrerenderManager::SetMode(
     96             PrerenderManager::PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP);
     97       } else if (trial_group == kPrerenderControl1Group ||
     98                  trial_group == kPrerenderControl2Group) {
     99         ResourceDispatcherHost::set_is_prefetch_enabled(true);
    100         PrerenderManager::SetMode(
    101             PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
    102       } else {
    103         NOTREACHED();
    104       }
    105       break;
    106     }
    107     case PRERENDER_OPTION_DISABLED:
    108       ResourceDispatcherHost::set_is_prefetch_enabled(false);
    109       PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED);
    110       break;
    111     case PRERENDER_OPTION_ENABLED:
    112       ResourceDispatcherHost::set_is_prefetch_enabled(true);
    113       PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_ENABLED);
    114       break;
    115     case PRERENDER_OPTION_PREFETCH_ONLY:
    116       ResourceDispatcherHost::set_is_prefetch_enabled(true);
    117       PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED);
    118       break;
    119     default:
    120       NOTREACHED();
    121   }
    122 
    123   UMA_HISTOGRAM_ENUMERATION("Prerender.Sessions",
    124                             PrerenderManager::GetMode(),
    125                             PrerenderManager::PRERENDER_MODE_MAX);
    126 }
    127 
    128 }  // namespace prerender
    129