Commit e3f7c0ab authored by zhenw's avatar zhenw Committed by Commit bot

Use more flexible field trial control on resource prefetching for mobile web

The switches are integrated with the new predictive network actions unified preference.

BUG=405690, 406203

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

Cr-Commit-Position: refs/heads/master@{#299316}
parent 31083014
...@@ -4,21 +4,52 @@ ...@@ -4,21 +4,52 @@
#include "chrome/browser/predictors/resource_prefetch_common.h" #include "chrome/browser/predictors/resource_prefetch_common.h"
#include <stdlib.h>
#include "base/command_line.h" #include "base/command_line.h"
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/string_split.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
using base::FieldTrialList;
using std::string;
using std::vector;
namespace predictors { namespace predictors {
const char kSpeculativePrefetchingTrialName[] = const char kSpeculativePrefetchingTrialName[] =
"SpeculativeResourcePrefetching"; "SpeculativeResourcePrefetching";
/*
* SpeculativeResourcePrefetching is a field trial, and its value must have the
* following format: key1=value1:key2=value2:key3=value3
* e.g. "Prefetching=Enabled:Predictor=Url:Confidence=High"
* The function below extracts the value corresponding to a key provided from
* the SpeculativeResourcePrefetching field trial.
*/
string GetFiledTrialSpecValue(string key) {
vector<string> elements;
base::SplitString(
FieldTrialList::FindFullName(kSpeculativePrefetchingTrialName),
':',
&elements);
for (int i = 0; i < static_cast<int>(elements.size()); i++) {
vector<string> key_value;
base::SplitString(elements[i], '=', &key_value);
if (key_value.size() == 2 && key_value[0] == key)
return key_value[1];
}
return string();
}
bool IsSpeculativeResourcePrefetchingEnabled( bool IsSpeculativeResourcePrefetchingEnabled(
Profile* profile, Profile* profile,
ResourcePrefetchPredictorConfig* config) { ResourcePrefetchPredictorConfig* config) {
...@@ -28,14 +59,8 @@ bool IsSpeculativeResourcePrefetchingEnabled( ...@@ -28,14 +59,8 @@ bool IsSpeculativeResourcePrefetchingEnabled(
if (!profile || profile->IsOffTheRecord()) if (!profile || profile->IsOffTheRecord())
return false; return false;
// If the user has explicitly disabled "predictive actions" - disabled. // Enabled by command line switch. The config has the default params already
if (!profile->GetPrefs() || // set. The command line with just enable them with the default params.
!profile->GetPrefs()->GetBoolean(prefs::kNetworkPredictionEnabled)) {
return false;
}
// The config has the default params already set. The command line with just
// enable them with the default params.
if (CommandLine::ForCurrentProcess()->HasSwitch( if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSpeculativeResourcePrefetching)) { switches::kSpeculativeResourcePrefetching)) {
const std::string value = const std::string value =
...@@ -57,104 +82,73 @@ bool IsSpeculativeResourcePrefetchingEnabled( ...@@ -57,104 +82,73 @@ bool IsSpeculativeResourcePrefetchingEnabled(
} }
} }
// Disable if no field trial is specified.
std::string trial = base::FieldTrialList::FindFullName( std::string trial = base::FieldTrialList::FindFullName(
kSpeculativePrefetchingTrialName); kSpeculativePrefetchingTrialName);
if (trial.empty())
if (trial == "LearningHost") { return false;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
return true; // Enabled by field trial.
} else if (trial == "LearningURL") { std::string spec_prefetching = GetFiledTrialSpecValue("Prefetching");
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; std::string spec_predictor = GetFiledTrialSpecValue("Predictor");
return true; std::string spec_confidence = GetFiledTrialSpecValue("Confidence");
} else if (trial == "Learning") { std::string spec_more_resources = GetFiledTrialSpecValue("MoreResources");
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; std::string spec_small_db = GetFiledTrialSpecValue("SmallDB");
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
return true;
} else if (trial == "PrefetchingHost") {
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
return true;
} else if (trial == "PrefetchingURL") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
return true;
} else if (trial == "Prefetching") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
return true;
} else if (trial == "PrefetchingLowConfidence") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
if (spec_prefetching == "Learning") {
if (spec_predictor == "Url") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
} else if (spec_predictor == "Host") {
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
} else {
// Default: both Url and Host
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
}
} else if (spec_prefetching == "Enabled") {
if (spec_predictor == "Url") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
} else if (spec_predictor == "Host") {
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
} else {
// Default: both Url and Host
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
}
} else {
// Default: spec_prefetching == "Disabled"
return false;
}
if (spec_confidence == "Low") {
config->min_url_visit_count = 1; config->min_url_visit_count = 1;
config->min_resource_confidence_to_trigger_prefetch = 0.5f; config->min_resource_confidence_to_trigger_prefetch = 0.5f;
config->min_resource_hits_to_trigger_prefetch = 1; config->min_resource_hits_to_trigger_prefetch = 1;
return true; } else if (spec_confidence == "High") {
} else if (trial == "PrefetchingHighConfidence") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
config->min_url_visit_count = 3; config->min_url_visit_count = 3;
config->min_resource_confidence_to_trigger_prefetch = 0.9f; config->min_resource_confidence_to_trigger_prefetch = 0.9f;
config->min_resource_hits_to_trigger_prefetch = 3; config->min_resource_hits_to_trigger_prefetch = 3;
return true; } else {
} else if (trial == "PrefetchingMoreResources") { // default
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; config->min_url_visit_count = 2;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; config->min_resource_confidence_to_trigger_prefetch = 0.7f;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING; config->min_resource_hits_to_trigger_prefetch = 2;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING; }
if (spec_more_resources == "Enabled") {
config->max_resources_per_entry = 100; config->max_resources_per_entry = 100;
return true; }
} else if (trial == "LearningSmallDB") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->max_urls_to_track = 200;
config->max_hosts_to_track = 100;
return true;
} else if (trial == "PrefetchingSmallDB") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
config->max_urls_to_track = 200;
config->max_hosts_to_track = 100;
return true;
} else if (trial == "PrefetchingSmallDBLowConfidence") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
config->max_urls_to_track = 200;
config->max_hosts_to_track = 100;
config->min_url_visit_count = 1;
config->min_resource_confidence_to_trigger_prefetch = 0.5f;
config->min_resource_hits_to_trigger_prefetch = 1;
return true;
} else if (trial == "PrefetchingSmallDBHighConfidence") {
config->mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
config->mode |= ResourcePrefetchPredictorConfig::URL_PREFETCHING;
config->mode |= ResourcePrefetchPredictorConfig::HOST_PRFETCHING;
if (spec_small_db == "Enabled") {
config->max_urls_to_track = 200; config->max_urls_to_track = 200;
config->max_hosts_to_track = 100; config->max_hosts_to_track = 100;
config->min_url_visit_count = 3;
config->min_resource_confidence_to_trigger_prefetch = 0.9f;
config->min_resource_hits_to_trigger_prefetch = 3;
return true;
} }
return false; return true;
} }
NavigationID::NavigationID() NavigationID::NavigationID()
...@@ -209,8 +203,8 @@ ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig() ...@@ -209,8 +203,8 @@ ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig()
min_url_visit_count(2), min_url_visit_count(2),
max_resources_per_entry(50), max_resources_per_entry(50),
max_consecutive_misses(3), max_consecutive_misses(3),
min_resource_confidence_to_trigger_prefetch(0.8f), min_resource_confidence_to_trigger_prefetch(0.7f),
min_resource_hits_to_trigger_prefetch(3), min_resource_hits_to_trigger_prefetch(2),
max_prefetches_inflight_per_navigation(24), max_prefetches_inflight_per_navigation(24),
max_prefetches_inflight_per_host_per_navigation(3) { max_prefetches_inflight_per_host_per_navigation(3) {
} }
...@@ -222,8 +216,9 @@ bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const { ...@@ -222,8 +216,9 @@ bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const {
return IsURLLearningEnabled() || IsHostLearningEnabled(); return IsURLLearningEnabled() || IsHostLearningEnabled();
} }
bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabled() const { bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabled(
return IsURLPrefetchingEnabled() || IsHostPrefetchingEnabled(); Profile* profile) const {
return IsURLPrefetchingEnabled(profile) || IsHostPrefetchingEnabled(profile);
} }
bool ResourcePrefetchPredictorConfig::IsURLLearningEnabled() const { bool ResourcePrefetchPredictorConfig::IsURLLearningEnabled() const {
...@@ -234,12 +229,44 @@ bool ResourcePrefetchPredictorConfig::IsHostLearningEnabled() const { ...@@ -234,12 +229,44 @@ bool ResourcePrefetchPredictorConfig::IsHostLearningEnabled() const {
return (mode & HOST_LEARNING) > 0; return (mode & HOST_LEARNING) > 0;
} }
bool ResourcePrefetchPredictorConfig::IsURLPrefetchingEnabled() const { bool ResourcePrefetchPredictorConfig::IsURLPrefetchingEnabled(
Profile* profile) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!profile || !profile->GetPrefs() ||
!chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs())) {
return false;
}
return (mode & URL_PREFETCHING) > 0; return (mode & URL_PREFETCHING) > 0;
} }
bool ResourcePrefetchPredictorConfig::IsHostPrefetchingEnabled() const { bool ResourcePrefetchPredictorConfig::IsHostPrefetchingEnabled(
Profile* profile) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!profile || !profile->GetPrefs() ||
!chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs())) {
return false;
}
return (mode & HOST_PRFETCHING) > 0; return (mode & HOST_PRFETCHING) > 0;
} }
bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const {
return min_url_visit_count == 1 &&
std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 &&
min_resource_hits_to_trigger_prefetch == 1;
}
bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const {
return min_url_visit_count == 3 &&
std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 &&
min_resource_hits_to_trigger_prefetch == 3;
}
bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const {
return max_resources_per_entry == 100;
}
bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const {
return max_urls_to_track == 200 && max_hosts_to_track == 100;
}
} // namespace predictors } // namespace predictors
...@@ -74,11 +74,16 @@ struct ResourcePrefetchPredictorConfig { ...@@ -74,11 +74,16 @@ struct ResourcePrefetchPredictorConfig {
// Helpers to deal with mode. // Helpers to deal with mode.
bool IsLearningEnabled() const; bool IsLearningEnabled() const;
bool IsPrefetchingEnabled() const; bool IsPrefetchingEnabled(Profile* profile) const;
bool IsURLLearningEnabled() const; bool IsURLLearningEnabled() const;
bool IsHostLearningEnabled() const; bool IsHostLearningEnabled() const;
bool IsURLPrefetchingEnabled() const; bool IsURLPrefetchingEnabled(Profile* profile) const;
bool IsHostPrefetchingEnabled() const; bool IsHostPrefetchingEnabled(Profile* profile) const;
bool IsLowConfidenceForTest() const;
bool IsHighConfidenceForTest() const;
bool IsMoreResourcesEnabledForTest() const;
bool IsSmallDBEnabledForTest() const;
// If a navigation hasn't seen a load complete event in this much time, it // If a navigation hasn't seen a load complete event in this much time, it
// is considered abandoned. // is considered abandoned.
......
// Copyright 2014 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 "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/statistics_recorder.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/predictors/resource_prefetch_common.h"
#include "chrome/browser/predictors/resource_prefetch_predictor.h"
#include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/variations/entropy_provider.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/network_change_notifier.h"
using chrome_browser_net::NetworkPredictionOptions;
using net::NetworkChangeNotifier;
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;
}
};
} // namespace
namespace predictors {
class ResourcePrefetchCommonTest : public testing::Test {
public:
ResourcePrefetchCommonTest();
virtual void SetUp() OVERRIDE;
void CreateTestFieldTrial(const std::string& name,
const std::string& group_name) {
base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
name, group_name);
trial->group();
}
void SetPreference(NetworkPredictionOptions value) {
profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value);
}
void TestIsPrefetchDisabled(ResourcePrefetchPredictorConfig& config) {
EXPECT_FALSE(config.IsLearningEnabled());
EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsURLLearningEnabled());
EXPECT_FALSE(config.IsHostLearningEnabled());
EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
}
void TestIsPrefetchEnabled(ResourcePrefetchPredictorConfig& config) {
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsURLLearningEnabled());
EXPECT_TRUE(config.IsHostLearningEnabled());
EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
}
void TestIsPrefetchLearning(ResourcePrefetchPredictorConfig& config) {
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsURLLearningEnabled());
EXPECT_TRUE(config.IsHostLearningEnabled());
EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
}
void TestIsDefaultExtraConfig(ResourcePrefetchPredictorConfig& config) {
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_FALSE(config.IsSmallDBEnabledForTest());
}
protected:
base::MessageLoop loop_;
content::TestBrowserThread ui_thread_;
scoped_ptr<TestingProfile> profile_;
private:
scoped_ptr<base::FieldTrialList> field_trial_list_;
};
ResourcePrefetchCommonTest::ResourcePrefetchCommonTest()
: loop_(base::MessageLoop::TYPE_DEFAULT),
ui_thread_(content::BrowserThread::UI, &loop_),
profile_(new TestingProfile()) { }
void ResourcePrefetchCommonTest::SetUp() {
field_trial_list_.reset(new base::FieldTrialList(
new metrics::SHA1EntropyProvider("ResourcePrefetchCommonTest")));
base::StatisticsRecorder::Initialize();
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialNotSpecified) {
ResourcePrefetchPredictorConfig config;
EXPECT_FALSE(
IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchDisabled(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabled) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Disabled");
ResourcePrefetchPredictorConfig config;
EXPECT_FALSE(
IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchDisabled(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningHost) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Learning:Predictor=Host");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsURLLearningEnabled());
EXPECT_TRUE(config.IsHostLearningEnabled());
EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningURL) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Learning:Predictor=Url");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsURLLearningEnabled());
EXPECT_FALSE(config.IsHostLearningEnabled());
EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialLearning) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Learning");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchLearning(config);
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHost) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:Predictor=Host");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsURLLearningEnabled());
EXPECT_TRUE(config.IsHostLearningEnabled());
EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingURL) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:Predictor=Url");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
EXPECT_TRUE(config.IsLearningEnabled());
EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
EXPECT_TRUE(config.IsURLLearningEnabled());
EXPECT_FALSE(config.IsHostLearningEnabled());
EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetching) {
CreateTestFieldTrial("SpeculativeResourcePrefetching", "Prefetching=Enabled");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
TestIsDefaultExtraConfig(config);
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingLowConfidence) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:Confidence=Low");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
EXPECT_TRUE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_FALSE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHighConfidence) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:Confidence=High");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_TRUE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_FALSE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingMoreResources) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Learning:MoreResources=Enabled");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchLearning(config);
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_TRUE(config.IsMoreResourcesEnabledForTest());
EXPECT_FALSE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningSmallDB) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Learning:SmallDB=Enabled");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchLearning(config);
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_TRUE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDB) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:SmallDB=Enabled");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_TRUE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBLowConfidence) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:SmallDB=Enabled:Confidence=Low");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
EXPECT_TRUE(config.IsLowConfidenceForTest());
EXPECT_FALSE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_TRUE(config.IsSmallDBEnabledForTest());
}
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBHighConfidence) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled:SmallDB=Enabled:Confidence=High");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
EXPECT_FALSE(config.IsLowConfidenceForTest());
EXPECT_TRUE(config.IsHighConfidenceForTest());
EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
EXPECT_TRUE(config.IsSmallDBEnabledForTest());
}
// Verifies whether prefetching in the field trial is disabled according to
// the network type. But learning should not be disabled by network.
TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabledByNetwork) {
CreateTestFieldTrial("SpeculativeResourcePrefetching",
"Prefetching=Enabled");
ResourcePrefetchPredictorConfig config;
EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
TestIsPrefetchEnabled(config);
// Set preference to WIFI_ONLY: prefetch when not on cellular.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
TestIsPrefetchEnabled(config);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
TestIsPrefetchLearning(config);
}
// Set preference to ALWAYS: always prefetch.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
TestIsPrefetchEnabled(config);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
TestIsPrefetchEnabled(config);
}
// Set preference to NEVER: never prefetch.
SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
TestIsPrefetchLearning(config);
}
{
scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
TestIsPrefetchLearning(config);
}
}
} // namespace predictors
...@@ -312,9 +312,9 @@ ResourcePrefetchPredictor::ResourcePrefetchPredictor( ...@@ -312,9 +312,9 @@ ResourcePrefetchPredictor::ResourcePrefetchPredictor(
// Some form of learning has to be enabled. // Some form of learning has to be enabled.
DCHECK(config_.IsLearningEnabled()); DCHECK(config_.IsLearningEnabled());
if (config_.IsURLPrefetchingEnabled()) if (config_.IsURLPrefetchingEnabled(profile_))
DCHECK(config_.IsURLLearningEnabled()); DCHECK(config_.IsURLLearningEnabled());
if (config_.IsHostPrefetchingEnabled()) if (config_.IsHostPrefetchingEnabled(profile_))
DCHECK(config_.IsHostLearningEnabled()); DCHECK(config_.IsHostLearningEnabled());
} }
...@@ -577,8 +577,9 @@ bool ResourcePrefetchPredictor::GetPrefetchData( ...@@ -577,8 +577,9 @@ bool ResourcePrefetchPredictor::GetPrefetchData(
*key_type = PREFETCH_KEY_TYPE_URL; *key_type = PREFETCH_KEY_TYPE_URL;
const GURL& main_frame_url = navigation_id.main_frame_url; const GURL& main_frame_url = navigation_id.main_frame_url;
bool use_url_data = config_.IsPrefetchingEnabled() ? bool use_url_data = config_.IsPrefetchingEnabled(profile_) ?
config_.IsURLPrefetchingEnabled() : config_.IsURLLearningEnabled(); config_.IsURLPrefetchingEnabled(profile_) :
config_.IsURLLearningEnabled();
if (use_url_data) { if (use_url_data) {
PrefetchDataMap::const_iterator iterator = PrefetchDataMap::const_iterator iterator =
url_table_cache_->find(main_frame_url.spec()); url_table_cache_->find(main_frame_url.spec());
...@@ -588,8 +589,9 @@ bool ResourcePrefetchPredictor::GetPrefetchData( ...@@ -588,8 +589,9 @@ bool ResourcePrefetchPredictor::GetPrefetchData(
if (!prefetch_requests->empty()) if (!prefetch_requests->empty())
return true; return true;
bool use_host_data = config_.IsPrefetchingEnabled() ? bool use_host_data = config_.IsPrefetchingEnabled(profile_) ?
config_.IsHostPrefetchingEnabled() : config_.IsHostLearningEnabled(); config_.IsHostPrefetchingEnabled(profile_) :
config_.IsHostLearningEnabled();
if (use_host_data) { if (use_host_data) {
PrefetchDataMap::const_iterator iterator = PrefetchDataMap::const_iterator iterator =
host_table_cache_->find(main_frame_url.host()); host_table_cache_->find(main_frame_url.host());
...@@ -712,7 +714,7 @@ void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { ...@@ -712,7 +714,7 @@ void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() {
content::Source<Profile>(profile_)); content::Source<Profile>(profile_));
// Initialize the prefetch manager only if prefetching is enabled. // Initialize the prefetch manager only if prefetching is enabled.
if (config_.IsPrefetchingEnabled()) { if (config_.IsPrefetchingEnabled(profile_)) {
prefetch_manager_ = new ResourcePrefetcherManager( prefetch_manager_ = new ResourcePrefetcherManager(
this, config_, profile_->GetRequestContext()); this, config_, profile_->GetRequestContext());
} }
......
...@@ -585,6 +585,7 @@ ...@@ -585,6 +585,7 @@
'browser/power/process_power_collector_unittest.cc', 'browser/power/process_power_collector_unittest.cc',
'browser/predictors/autocomplete_action_predictor_table_unittest.cc', 'browser/predictors/autocomplete_action_predictor_table_unittest.cc',
'browser/predictors/autocomplete_action_predictor_unittest.cc', 'browser/predictors/autocomplete_action_predictor_unittest.cc',
'browser/predictors/resource_prefetch_common_unittest.cc',
'browser/predictors/resource_prefetch_predictor_unittest.cc', 'browser/predictors/resource_prefetch_predictor_unittest.cc',
'browser/predictors/resource_prefetch_predictor_tables_unittest.cc', 'browser/predictors/resource_prefetch_predictor_tables_unittest.cc',
'browser/predictors/resource_prefetcher_unittest.cc', 'browser/predictors/resource_prefetcher_unittest.cc',
......
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