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.
5 #include "chrome/browser/prerender/prerender_field_trial.h"
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 "base/prefs/pref_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "chrome/browser/net/prediction_options.h"
15 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
16 #include "chrome/browser/prerender/prerender_manager.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "components/metrics/metrics_service.h"
23 #include "components/variations/variations_associated_data.h"
25 using base::FieldTrial
;
26 using base::FieldTrialList
;
27 using base::StringToInt
;
35 const char kDisabledGroup
[] = "Disabled";
36 const char kEnabledGroup
[] = "Enabled";
38 const char kLocalPredictorSpecTrialName
[] = "PrerenderLocalPredictorSpec";
39 const char kLocalPredictorKeyName
[] = "LocalPredictor";
40 const char kLocalPredictorUnencryptedSyncOnlyKeyName
[] =
41 "LocalPredictorUnencryptedSyncOnly";
42 const char kLocalPredictorNetworkPredictionEnabledOnly
[] =
43 "LocalPredictorNetworkPredictionEnabledOnly";
44 const char kLocalPredictorOnCellularOnly
[] = "LocalPredictorOnCellularOnly";
45 const char kSideEffectFreeWhitelistKeyName
[] = "SideEffectFreeWhitelist";
46 const char kPrerenderLaunchKeyName
[] = "PrerenderLaunch";
47 const char kPrerenderAlwaysControlKeyName
[] = "PrerenderAlwaysControl";
48 const char kPrerenderPrefetchKeyName
[] = "PrerenderPrefetch";
49 const char kPrerenderQueryPrerenderServiceKeyName
[] =
50 "PrerenderQueryPrerenderService";
51 const char kPrerenderQueryPrerenderServiceCurrentURLKeyName
[] =
52 "PrerenderQueryPrerenderServiceCurrentURL";
53 const char kPrerenderQueryPrerenderServiceCandidateURLsKeyName
[] =
54 "PrerenderQueryPrerenderServiceCandidateURLs";
55 const char kPrerenderServiceBehaviorIDKeyName
[] = "PrerenderServiceBehaviorID";
56 const char kPrerenderServiceFetchTimeoutKeyName
[] =
57 "PrerenderServiceFetchTimeoutMs";
58 const char kPrefetchListTimeoutKeyName
[] = "PrefetchListTimeoutSeconds";
59 const char kPrerenderTTLKeyName
[] = "PrerenderTTLSeconds";
60 const char kPrerenderPriorityHalfLifeTimeKeyName
[] =
61 "PrerenderPriorityHalfLifeTimeSeconds";
62 const char kMaxConcurrentPrerenderKeyName
[] = "MaxConcurrentPrerenders";
63 const char kMaxLaunchPrerenderKeyName
[] = "MaxLaunchPrerenders";
64 const char kSkipFragment
[] = "SkipFragment";
65 const char kSkipHTTPS
[] = "SkipHTTPS";
66 const char kSkipWhitelist
[] = "SkipWhitelist";
67 const char kSkipServiceWhitelist
[] = "SkipServiceWhitelist";
68 const char kSkipLoggedIn
[] = "SkipLoggedIn";
69 const char kSkipDefaultNoPrerender
[] = "SkipDefaultNoPrerender";
70 const char kPrerenderServiceURLPrefixParameterName
[] =
71 "PrerenderServiceURLPrefix";
72 const char kDefaultPrerenderServiceURLPrefix
[] =
73 "https://clients4.google.com/prerenderservice/?q=";
74 const int kMinPrerenderServiceTimeoutMs
= 1;
75 const int kMaxPrerenderServiceTimeoutMs
= 10000;
76 const int kDefaultPrerenderServiceTimeoutMs
= 1000;
77 const int kMinPrefetchListTimeoutSeconds
= 1;
78 const int kMaxPrefetchListTimeoutSeconds
= 1800;
79 const int kDefaultPrefetchListTimeoutSeconds
= 300;
80 const char kSkipPrerenderLocalCanadidates
[] = "SkipPrerenderLocalCandidates";
81 const char kSkipPrerenderServiceCanadidates
[] =
82 "SkipPrerenderServiceCandidates";
84 void SetupPrerenderFieldTrial() {
85 const FieldTrial::Probability divisor
= 1000;
87 FieldTrial::Probability control_probability
;
88 FieldTrial::Probability experiment_multi_prerender_probability
;
89 FieldTrial::Probability experiment_15min_ttl_probability
;
90 FieldTrial::Probability experiment_no_use_probability
;
91 FieldTrial::Probability experiment_match_complete_probability
;
93 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
94 if (channel
== chrome::VersionInfo::CHANNEL_STABLE
||
95 channel
== chrome::VersionInfo::CHANNEL_BETA
) {
96 // Use very conservatives and stable settings in beta and stable.
97 const FieldTrial::Probability release_prerender_enabled_probability
= 970;
98 const FieldTrial::Probability release_control_probability
= 10;
99 const FieldTrial::Probability
100 release_experiment_multi_prerender_probability
= 0;
101 const FieldTrial::Probability release_experiment_15min_ttl_probability
= 10;
102 const FieldTrial::Probability release_experiment_no_use_probability
= 0;
103 const FieldTrial::Probability
104 release_experiment_match_complete_probability
= 10;
106 release_prerender_enabled_probability
+
107 release_control_probability
+
108 release_experiment_multi_prerender_probability
+
109 release_experiment_15min_ttl_probability
+
110 release_experiment_no_use_probability
+
111 release_experiment_match_complete_probability
== divisor
,
112 "release experiment probabilities must equal divisor");
114 control_probability
= release_control_probability
;
115 experiment_multi_prerender_probability
=
116 release_experiment_multi_prerender_probability
;
117 experiment_15min_ttl_probability
= release_experiment_15min_ttl_probability
;
118 experiment_no_use_probability
= release_experiment_no_use_probability
;
119 experiment_match_complete_probability
=
120 release_experiment_match_complete_probability
;
122 // In testing channels, use more experiments and a larger control group to
123 // improve quality of data.
124 const FieldTrial::Probability dev_prerender_enabled_probability
= 200;
125 const FieldTrial::Probability dev_control_probability
= 200;
126 const FieldTrial::Probability
127 dev_experiment_multi_prerender_probability
= 200;
128 const FieldTrial::Probability dev_experiment_15min_ttl_probability
= 100;
129 const FieldTrial::Probability dev_experiment_no_use_probability
= 100;
130 const FieldTrial::Probability
131 dev_experiment_match_complete_probability
= 200;
132 static_assert(dev_prerender_enabled_probability
+
133 dev_control_probability
+
134 dev_experiment_multi_prerender_probability
+
135 dev_experiment_15min_ttl_probability
+
136 dev_experiment_no_use_probability
+
137 dev_experiment_match_complete_probability
== divisor
,
138 "dev experiment probabilities must equal divisor");
140 control_probability
= dev_control_probability
;
141 experiment_multi_prerender_probability
=
142 dev_experiment_multi_prerender_probability
;
143 experiment_15min_ttl_probability
= dev_experiment_15min_ttl_probability
;
144 experiment_no_use_probability
= dev_experiment_no_use_probability
;
145 experiment_match_complete_probability
=
146 dev_experiment_match_complete_probability
;
149 int prerender_enabled_group
= -1;
150 scoped_refptr
<FieldTrial
> trial(
151 FieldTrialList::FactoryGetFieldTrial(
152 "Prerender", divisor
, "PrerenderEnabled",
153 2014, 12, 31, FieldTrial::SESSION_RANDOMIZED
,
154 &prerender_enabled_group
));
155 const int control_group
=
156 trial
->AppendGroup("PrerenderControl",
157 control_probability
);
158 const int experiment_multi_prerender_group
=
159 trial
->AppendGroup("PrerenderMulti",
160 experiment_multi_prerender_probability
);
161 const int experiment_15_min_TTL_group
=
162 trial
->AppendGroup("Prerender15minTTL",
163 experiment_15min_ttl_probability
);
164 const int experiment_no_use_group
=
165 trial
->AppendGroup("PrerenderNoUse",
166 experiment_no_use_probability
);
167 const int experiment_match_complete_group
=
168 trial
->AppendGroup("MatchComplete",
169 experiment_match_complete_probability
);
171 const int trial_group
= trial
->group();
172 if (trial_group
== prerender_enabled_group
) {
173 PrerenderManager::SetMode(
174 PrerenderManager::PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP
);
175 } else if (trial_group
== control_group
) {
176 PrerenderManager::SetMode(
177 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP
);
178 } else if (trial_group
== experiment_multi_prerender_group
) {
179 PrerenderManager::SetMode(
180 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP
);
181 } else if (trial_group
== experiment_15_min_TTL_group
) {
182 PrerenderManager::SetMode(
183 PrerenderManager::PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP
);
184 } else if (trial_group
== experiment_no_use_group
) {
185 PrerenderManager::SetMode(
186 PrerenderManager::PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP
);
187 } else if (trial_group
== experiment_match_complete_group
) {
188 PrerenderManager::SetMode(
189 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
197 void ConfigurePrerender(const base::CommandLine
& command_line
) {
198 enum PrerenderOption
{
199 PRERENDER_OPTION_AUTO
,
200 PRERENDER_OPTION_DISABLED
,
201 PRERENDER_OPTION_ENABLED
,
204 PrerenderOption prerender_option
= PRERENDER_OPTION_AUTO
;
205 if (command_line
.HasSwitch(switches::kPrerenderMode
)) {
206 const string switch_value
=
207 command_line
.GetSwitchValueASCII(switches::kPrerenderMode
);
209 if (switch_value
== switches::kPrerenderModeSwitchValueAuto
) {
210 prerender_option
= PRERENDER_OPTION_AUTO
;
211 } else if (switch_value
== switches::kPrerenderModeSwitchValueDisabled
) {
212 prerender_option
= PRERENDER_OPTION_DISABLED
;
213 } else if (switch_value
.empty() ||
214 switch_value
== switches::kPrerenderModeSwitchValueEnabled
) {
215 // The empty string means the option was provided with no value, and that
217 prerender_option
= PRERENDER_OPTION_ENABLED
;
219 prerender_option
= PRERENDER_OPTION_DISABLED
;
220 LOG(ERROR
) << "Invalid --prerender option received on command line: "
222 LOG(ERROR
) << "Disabling prerendering!";
226 switch (prerender_option
) {
227 case PRERENDER_OPTION_AUTO
:
228 SetupPrerenderFieldTrial();
230 case PRERENDER_OPTION_DISABLED
:
231 PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_DISABLED
);
233 case PRERENDER_OPTION_ENABLED
:
234 PrerenderManager::SetMode(PrerenderManager::PRERENDER_MODE_ENABLED
);
241 bool IsOmniboxEnabled(Profile
* profile
) {
245 if (!PrerenderManager::IsPrerenderingPossible())
248 // Override any field trial groups if the user has set a command line flag.
249 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
250 switches::kPrerenderFromOmnibox
)) {
251 const string switch_value
=
252 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
253 switches::kPrerenderFromOmnibox
);
255 if (switch_value
== switches::kPrerenderFromOmniboxSwitchValueEnabled
)
258 if (switch_value
== switches::kPrerenderFromOmniboxSwitchValueDisabled
)
261 DCHECK_EQ(switches::kPrerenderFromOmniboxSwitchValueAuto
, switch_value
);
264 return (FieldTrialList::FindFullName("PrerenderFromOmnibox") !=
265 "OmniboxPrerenderDisabled");
269 PrerenderLocalPredictorSpec is a field trial, and its value must have the
271 key1=value1:key2=value2:key3=value3
272 eg "LocalPredictor=Enabled:SideEffectFreeWhitelist=Enabled"
273 The function below extracts the value corresponding to a key provided from the
276 string
GetLocalPredictorSpecValue(string spec_key
) {
277 vector
<string
> elements
;
278 base::SplitString(FieldTrialList::FindFullName(kLocalPredictorSpecTrialName
),
280 for (int i
= 0; i
< static_cast<int>(elements
.size()); i
++) {
281 vector
<string
> key_value
;
282 base::SplitString(elements
[i
], '=', &key_value
);
283 if (key_value
.size() == 2 && key_value
[0] == spec_key
)
289 bool IsUnencryptedSyncEnabled(Profile
* profile
) {
290 ProfileSyncService
* service
= ProfileSyncServiceFactory::GetInstance()->
291 GetForProfile(profile
);
292 return service
&& service
->GetOpenTabsUIDelegate() &&
293 !service
->EncryptEverythingEnabled();
296 // Indicates whether the Local Predictor is enabled based on field trial
298 bool IsLocalPredictorEnabled() {
299 #if defined(OS_ANDROID) || defined(OS_IOS)
302 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kDisablePrerenderLocalPredictor
) &&
304 GetLocalPredictorSpecValue(kLocalPredictorKeyName
) == kEnabledGroup
;
307 bool ShouldDisableLocalPredictorBasedOnSyncAndConfiguration(Profile
* profile
) {
309 GetLocalPredictorSpecValue(kLocalPredictorUnencryptedSyncOnlyKeyName
) ==
311 !IsUnencryptedSyncEnabled(profile
);
314 bool ShouldDisableLocalPredictorDueToPreferencesAndNetwork(Profile
* profile
) {
316 net::NetworkChangeNotifier::IsConnectionCellular(
317 net::NetworkChangeNotifier::GetConnectionType());
318 // If the user is not on a cellular connection, but we require a cellular
319 // connection, we must temporarily disable our local predictions.
321 GetLocalPredictorSpecValue(kLocalPredictorOnCellularOnly
) ==
326 // If we don't care whether or not network prediction will actually be
327 // exercised, we do not need to temporarily disable our predictions.
328 if (GetLocalPredictorSpecValue(kLocalPredictorNetworkPredictionEnabledOnly
) !=
333 // We should temporarily disable iff the predictive network action would
336 return !chrome_browser_net::CanPrefetchAndPrerenderUI(profile
->GetPrefs());
339 bool IsLoggedInPredictorEnabled() {
340 return IsLocalPredictorEnabled();
343 bool IsSideEffectFreeWhitelistEnabled() {
344 return IsLocalPredictorEnabled() &&
345 GetLocalPredictorSpecValue(kSideEffectFreeWhitelistKeyName
) !=
349 bool IsLocalPredictorPrerenderLaunchEnabled() {
350 return GetLocalPredictorSpecValue(kPrerenderLaunchKeyName
) != kDisabledGroup
;
353 bool IsLocalPredictorPrerenderAlwaysControlEnabled() {
354 // If we prefetch rather than prerender, we automatically also prerender
355 // as a control group only.
356 return (GetLocalPredictorSpecValue(kPrerenderAlwaysControlKeyName
) ==
357 kEnabledGroup
) || IsLocalPredictorPrerenderPrefetchEnabled();
360 bool IsLocalPredictorPrerenderPrefetchEnabled() {
361 return GetLocalPredictorSpecValue(kPrerenderPrefetchKeyName
) ==
365 bool ShouldQueryPrerenderService(Profile
* profile
) {
366 return IsUnencryptedSyncEnabled(profile
) &&
367 GetLocalPredictorSpecValue(kPrerenderQueryPrerenderServiceKeyName
) ==
371 bool ShouldQueryPrerenderServiceForCurrentURL() {
372 return GetLocalPredictorSpecValue(
373 kPrerenderQueryPrerenderServiceCurrentURLKeyName
) != kDisabledGroup
;
376 bool ShouldQueryPrerenderServiceForCandidateURLs() {
377 return GetLocalPredictorSpecValue(
378 kPrerenderQueryPrerenderServiceCandidateURLsKeyName
) != kDisabledGroup
;
381 string
GetPrerenderServiceURLPrefix() {
382 string prefix
= variations::GetVariationParamValue(
383 kLocalPredictorSpecTrialName
,
384 kPrerenderServiceURLPrefixParameterName
);
385 return prefix
.empty() ? kDefaultPrerenderServiceURLPrefix
: prefix
;
388 int GetPrerenderServiceBehaviorID() {
390 StringToInt(GetLocalPredictorSpecValue(kPrerenderServiceBehaviorIDKeyName
),
392 // The behavior ID must be non-negative.
393 return std::max(id
, 0);
396 int GetPrerenderServiceFetchTimeoutMs() {
398 StringToInt(GetLocalPredictorSpecValue(kPrerenderServiceFetchTimeoutKeyName
),
400 // If the value is outside the valid range, use the default value.
401 return (result
< kMinPrerenderServiceTimeoutMs
||
402 result
> kMaxPrerenderServiceTimeoutMs
) ?
403 kDefaultPrerenderServiceTimeoutMs
: result
;
406 int GetPrerenderPrefetchListTimeoutSeconds() {
408 StringToInt(GetLocalPredictorSpecValue(kPrefetchListTimeoutKeyName
), &result
);
409 // If the value is outside the valid range, use the default value.
410 return (result
< kMinPrefetchListTimeoutSeconds
||
411 result
> kMaxPrefetchListTimeoutSeconds
) ?
412 kDefaultPrefetchListTimeoutSeconds
: result
;
415 int GetLocalPredictorTTLSeconds() {
417 StringToInt(GetLocalPredictorSpecValue(kPrerenderTTLKeyName
), &ttl
);
418 // If the value is outside of 10s or 600s, use a default value of 180s.
419 return (ttl
< 10 || ttl
> 600) ? 180 : ttl
;
422 int GetLocalPredictorPrerenderPriorityHalfLifeTimeSeconds() {
424 StringToInt(GetLocalPredictorSpecValue(kPrerenderPriorityHalfLifeTimeKeyName
),
426 // Sanity check: Ensure the half life time is non-negative.
427 return std::max(half_life_time
, 0);
430 int GetLocalPredictorMaxConcurrentPrerenders() {
432 StringToInt(GetLocalPredictorSpecValue(kMaxConcurrentPrerenderKeyName
),
434 // Sanity check: Ensure the number of prerenders is between 1 and 10.
435 return std::min(std::max(num_prerenders
, 1), 10);
438 int GetLocalPredictorMaxLaunchPrerenders() {
440 StringToInt(GetLocalPredictorSpecValue(kMaxLaunchPrerenderKeyName
),
442 // Sanity check: Ensure the number of prerenders is between 1 and 10.
443 return std::min(std::max(num_prerenders
, 1), 10);
446 bool SkipLocalPredictorFragment() {
447 return GetLocalPredictorSpecValue(kSkipFragment
) == kEnabledGroup
;
450 bool SkipLocalPredictorHTTPS() {
451 return GetLocalPredictorSpecValue(kSkipHTTPS
) == kEnabledGroup
;
454 bool SkipLocalPredictorWhitelist() {
455 return GetLocalPredictorSpecValue(kSkipWhitelist
) == kEnabledGroup
;
458 bool SkipLocalPredictorServiceWhitelist() {
459 return GetLocalPredictorSpecValue(kSkipServiceWhitelist
) == kEnabledGroup
;
462 bool SkipLocalPredictorLoggedIn() {
463 return GetLocalPredictorSpecValue(kSkipLoggedIn
) == kEnabledGroup
;
466 bool SkipLocalPredictorDefaultNoPrerender() {
467 return GetLocalPredictorSpecValue(kSkipDefaultNoPrerender
) == kEnabledGroup
;
470 bool SkipLocalPredictorLocalCandidates() {
471 return GetLocalPredictorSpecValue(kSkipPrerenderLocalCanadidates
) ==
475 bool SkipLocalPredictorServiceCandidates() {
476 return GetLocalPredictorSpecValue(kSkipPrerenderServiceCanadidates
) ==
480 } // namespace prerender