Commit d5cb0cb0 authored by tburkard's avatar tburkard Committed by Commit bot

Provide a way to specify in Finch trials to disable prerender local

predictor, based on the following two options:
- not on cellular
- predictive actions are disabled for the current network selection
This allows to only gather stats for a the subset of people we are interested
in (eg only people on cellular, or only people who have prefetch enabled for
the current network condition)
R=jkarlin@chromium.org
TBR=bnc@chromium.org, zhenw@chromium.org

Review URL: https://codereview.chromium.org/591313005

Cr-Commit-Position: refs/heads/master@{#296730}
parent 7144431a
......@@ -30,9 +30,11 @@
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/predictors/autocomplete_action_predictor.h"
#include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/prerender/prerender_handle.h"
#include "chrome/browser/prerender/prerender_link_manager.h"
#include "chrome/browser/prerender/prerender_link_manager_factory.h"
......@@ -66,6 +68,8 @@
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/variations/entropy_provider.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_controller.h"
......@@ -98,6 +102,7 @@
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
using chrome_browser_net::NetworkPredictionOptions;
using content::BrowserThread;
using content::DevToolsAgentHost;
using content::NavigationController;
......@@ -109,6 +114,7 @@ using content::RenderWidgetHost;
using content::TestNavigationObserver;
using content::WebContents;
using content::WebContentsObserver;
using net::NetworkChangeNotifier;
using task_manager::browsertest_util::WaitForTaskManagerRows;
// Prerender tests work as follows:
......@@ -125,6 +131,20 @@ namespace prerender {
namespace {
class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
public:
virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
return NetworkChangeNotifier::CONNECTION_WIFI;
}
};
class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
public:
virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
return NetworkChangeNotifier::CONNECTION_4G;
}
};
// Constants used in the test HTML files.
const char* kReadyTitle = "READY";
const char* kPassTitle = "PASS";
......@@ -1070,6 +1090,80 @@ class PrerenderBrowserTest : virtual public InProcessBrowserTest {
command_line->AppendSwitch(switches::kAlwaysAuthorizePlugins);
}
void SetPreference(NetworkPredictionOptions value) {
browser()->profile()->GetPrefs()->SetInteger(
prefs::kNetworkPredictionOptions, value);
}
void CreateTestFieldTrial(const std::string& name,
const std::string& group_name) {
base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
name, group_name);
trial->group();
}
// Verifies, for the current field trial, whether
// ShouldDisableLocalPredictorDueToPreferencesAndNetwork produces the desired
// output.
void TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
bool preference_wifi_network_wifi,
bool preference_wifi_network_4g,
bool preference_always_network_wifi,
bool preference_always_network_4g,
bool preference_never_network_wifi,
bool preference_never_network_4g) {
Profile* profile = browser()->profile();
// Set real NetworkChangeNotifier singleton aside.
scoped_ptr<NetworkChangeNotifier::DisableForTest> disable_for_test(
new NetworkChangeNotifier::DisableForTest);
// Set preference to WIFI_ONLY: prefetch when not on cellular.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_wifi_network_wifi);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_wifi_network_4g);
}
// Set preference to ALWAYS: always prefetch.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_always_network_wifi);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_always_network_4g);
}
// Set preference to NEVER: never prefetch.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_never_network_wifi);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
EXPECT_EQ(
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
preference_never_network_4g);
}
}
virtual void SetUpOnMainThread() OVERRIDE {
current_browser()->profile()->GetPrefs()->SetBoolean(
prefs::kPromptForDownload, false);
......@@ -1669,6 +1763,7 @@ class PrerenderBrowserTest : virtual public InProcessBrowserTest {
std::string loader_query_;
Browser* explicitly_set_browser_;
base::HistogramTester histogram_tester_;
scoped_ptr<base::FieldTrialList> field_trial_list_;
};
// Checks that a page is correctly prerendered in the case of a
......@@ -4453,4 +4548,66 @@ IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
}
// Prefetch should be allowed depending on preference and network type.
// This test is for the bsae case: no Finch overrides should never disable.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
LocalPredictorDisableWorksBaseCase) {
TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
false /*preference_wifi_network_wifi*/,
false /*preference_wifi_network_4g*/,
false /*preference_always_network_wifi*/,
false /*preference_always_network_4g*/,
false /*preference_never_network_wifi*/,
false /*preference_never_network_4g*/);
}
// Prefetch should be allowed depending on preference and network type.
// LocalPredictorOnCellularOnly should disable all wifi cases.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
LocalPredictorDisableWorksCellularOnly) {
CreateTestFieldTrial("PrerenderLocalPredictorSpec",
"LocalPredictorOnCellularOnly=Enabled");
TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
true /*preference_wifi_network_wifi*/,
false /*preference_wifi_network_4g*/,
true /*preference_always_network_wifi*/,
false /*preference_always_network_4g*/,
true /*preference_never_network_wifi*/,
false /*preference_never_network_4g*/);
}
// Prefetch should be allowed depending on preference and network type.
// LocalPredictorNetworkPredictionEnabledOnly should disable whenever
// network predictions will not be exercised.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
LocalPredictorDisableWorksNetworkPredictionEnableOnly) {
CreateTestFieldTrial("PrerenderLocalPredictorSpec",
"LocalPredictorNetworkPredictionEnabledOnly=Enabled");
TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
false /*preference_wifi_network_wifi*/,
true /*preference_wifi_network_4g*/,
false /*preference_always_network_wifi*/,
false /*preference_always_network_4g*/,
true /*preference_never_network_wifi*/,
true /*preference_never_network_4g*/);
}
// Prefetch should be allowed depending on preference and network type.
// If LocalPredictorNetworkPredictionEnabledOnly and
// LocalPredictorOnCellularOnly are both selected, we must disable whenever
// network predictions are not exercised, or when we are on wifi.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
LocalPredictorDisableWorksBothOptions) {
CreateTestFieldTrial("PrerenderLocalPredictorSpec",
"LocalPredictorOnCellularOnly=Enabled:"
"LocalPredictorNetworkPredictionEnabledOnly=Enabled");
TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
true /*preference_wifi_network_wifi*/,
true /*preference_wifi_network_4g*/,
true /*preference_always_network_wifi*/,
false /*preference_always_network_4g*/,
true /*preference_never_network_wifi*/,
true /*preference_never_network_4g*/);
}
} // namespace prerender
......@@ -11,6 +11,7 @@
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/predictors/autocomplete_action_predictor.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/profiles/profile.h"
......@@ -18,6 +19,7 @@
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/metrics_service.h"
#include "components/variations/variations_associated_data.h"
......@@ -41,6 +43,9 @@ const char kLocalPredictorSpecTrialName[] = "PrerenderLocalPredictorSpec";
const char kLocalPredictorKeyName[] = "LocalPredictor";
const char kLocalPredictorUnencryptedSyncOnlyKeyName[] =
"LocalPredictorUnencryptedSyncOnly";
const char kLocalPredictorNetworkPredictionEnabledOnly[] =
"LocalPredictorNetworkPredictionEnabledOnly";
const char kLocalPredictorOnCellularOnly[] = "LocalPredictorOnCellularOnly";
const char kSideEffectFreeWhitelistKeyName[] = "SideEffectFreeWhitelist";
const char kPrerenderLaunchKeyName[] = "PrerenderLaunch";
const char kPrerenderAlwaysControlKeyName[] = "PrerenderAlwaysControl";
......@@ -331,13 +336,38 @@ bool IsLocalPredictorEnabled() {
GetLocalPredictorSpecValue(kLocalPredictorKeyName) == kEnabledGroup;
}
bool DisableLocalPredictorBasedOnSyncAndConfiguration(Profile* profile) {
bool ShouldDisableLocalPredictorBasedOnSyncAndConfiguration(Profile* profile) {
return
GetLocalPredictorSpecValue(kLocalPredictorUnencryptedSyncOnlyKeyName) ==
kEnabledGroup &&
!IsUnencryptedSyncEnabled(profile);
}
bool ShouldDisableLocalPredictorDueToPreferencesAndNetwork(Profile* profile) {
bool on_cellular =
net::NetworkChangeNotifier::IsConnectionCellular(
net::NetworkChangeNotifier::GetConnectionType());
// If the user is not on a cellular connection, but we require a cellular
// connection, we must temporarily disable our local predictions.
if (!on_cellular &&
GetLocalPredictorSpecValue(kLocalPredictorOnCellularOnly) ==
kEnabledGroup) {
return true;
}
// If we don't care whether or not network prediction will actually be
// exercised, we do not need to temporarily disable our predictions.
if (GetLocalPredictorSpecValue(kLocalPredictorNetworkPredictionEnabledOnly) !=
kEnabledGroup) {
return false;
}
// We should temporarily disable iff the predictive network action would
// not be exercised.
return !chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs());
}
bool IsLoggedInPredictorEnabled() {
return IsLocalPredictorEnabled();
}
......
......@@ -32,7 +32,11 @@ bool IsLocalPredictorEnabled();
// Indicates whether to disable the local predictor due to unencrypted sync
// settings and configuration.
bool DisableLocalPredictorBasedOnSyncAndConfiguration(Profile* profile);
bool ShouldDisableLocalPredictorBasedOnSyncAndConfiguration(Profile* profile);
// Returns whether or not the local predictor is temporarily disabled, due
// to network predictive action settings and current network.
bool ShouldDisableLocalPredictorDueToPreferencesAndNetwork(Profile* profile);
// Returns true iff the LoggedIn Predictor is enabled.
bool IsLoggedInPredictorEnabled();
......
......@@ -616,6 +616,11 @@ void PrerenderLocalPredictor::OnAddVisit(const history::BriefVisitInfo& info) {
}
if (ShouldExcludeTransitionForPrediction(info.transition))
return;
Profile* profile = prerender_manager_->profile();
if (!profile ||
ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile)) {
return;
}
RecordEvent(EVENT_ADD_VISIT_RELEVANT_TRANSITION);
base::TimeDelta max_age =
base::TimeDelta::FromMilliseconds(GetMaxLocalPredictionTimeMs());
......@@ -1126,7 +1131,8 @@ void PrerenderLocalPredictor::Init() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
RecordEvent(EVENT_INIT_STARTED);
Profile* profile = prerender_manager_->profile();
if (!profile || DisableLocalPredictorBasedOnSyncAndConfiguration(profile)) {
if (!profile ||
ShouldDisableLocalPredictorBasedOnSyncAndConfiguration(profile)) {
RecordEvent(EVENT_INIT_FAILED_UNENCRYPTED_SYNC_NOT_ENABLED);
return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment