Commit 02a03615 authored by Doug Arnett's avatar Doug Arnett Committed by Commit Bot

Reland "Adds predictable multimatch support in PreviewsOptimizationGuide"

This is a reland of 0346a3a1
Original change's description:
> Adds predictable multimatch support in PreviewsOptimizationGuide
> 
> Supports multiple hint matches with precendence given to the match
> that occurs first in the configuration. Also, allows matches
> without whitelisted optimization to be able to turn off whitelisting
> for a host pattern (that otherwise would be whitelisted for another
> matching pattern that occurs later in config).
> 
> Bug: 783237
> Change-Id: I9239b74b170a1d5a19b13caccecc732f9715de90
> Reviewed-on: https://chromium-review.googlesource.com/847816
> Reviewed-by: Ryan Sturm <ryansturm@chromium.org>
> Commit-Queue: Doug Arnett <dougarnett@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#526763}

Bug: 783237
Change-Id: I75cf52fbe3b471f81b3f18ae451d7daa0844190e
Reviewed-on: https://chromium-review.googlesource.com/849673
Commit-Queue: Doug Arnett <dougarnett@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#527094}
parent 94e94c2e
...@@ -48,6 +48,9 @@ std::unique_ptr<PreviewsOptimizationGuide::Hints> ...@@ -48,6 +48,9 @@ std::unique_ptr<PreviewsOptimizationGuide::Hints>
PreviewsOptimizationGuide::Hints::CreateFromConfig( PreviewsOptimizationGuide::Hints::CreateFromConfig(
const optimization_guide::proto::Configuration& config) { const optimization_guide::proto::Configuration& config) {
std::unique_ptr<Hints> hints(new Hints()); std::unique_ptr<Hints> hints(new Hints());
// The condition set ID is a simple increasing counter that matches the
// order of hints in the config (where earlier hints in the config take
// precendence over later hints in the config if there are multiple matches).
url_matcher::URLMatcherConditionSet::ID id = 0; url_matcher::URLMatcherConditionSet::ID id = 0;
url_matcher::URLMatcherConditionFactory* condition_factory = url_matcher::URLMatcherConditionFactory* condition_factory =
hints->url_matcher_.condition_factory(); hints->url_matcher_.condition_factory();
...@@ -75,18 +78,19 @@ PreviewsOptimizationGuide::Hints::CreateFromConfig( ...@@ -75,18 +78,19 @@ PreviewsOptimizationGuide::Hints::CreateFromConfig(
// Create whitelist condition set out of the optimizations that are // Create whitelist condition set out of the optimizations that are
// whitelisted for the host suffix. // whitelisted for the host suffix.
std::set<PreviewsType> whitelisted_optimizations;
for (const auto optimization : hint.whitelisted_optimizations()) { for (const auto optimization : hint.whitelisted_optimizations()) {
if (optimization.optimization_type() == if (optimization.optimization_type() ==
optimization_guide::proto::NOSCRIPT) { optimization_guide::proto::NOSCRIPT) {
url_matcher::URLMatcherCondition condition = whitelisted_optimizations.insert(PreviewsType::NOSCRIPT);
condition_factory->CreateHostSuffixCondition(hint.key());
all_conditions.push_back(new url_matcher::URLMatcherConditionSet(
id, std::set<url_matcher::URLMatcherCondition>{condition}));
hints->whitelist_[id] = std::set<PreviewsType>{PreviewsType::NOSCRIPT};
id++;
break;
} }
} }
url_matcher::URLMatcherCondition condition =
condition_factory->CreateHostSuffixCondition(hint.key());
all_conditions.push_back(new url_matcher::URLMatcherConditionSet(
id, std::set<url_matcher::URLMatcherCondition>{condition}));
hints->whitelist_[id] = whitelisted_optimizations;
id++;
} }
hints->url_matcher_.AddConditionSets(all_conditions); hints->url_matcher_.AddConditionSets(all_conditions);
return hints; return hints;
...@@ -96,18 +100,21 @@ bool PreviewsOptimizationGuide::Hints::IsWhitelisted(const GURL& url, ...@@ -96,18 +100,21 @@ bool PreviewsOptimizationGuide::Hints::IsWhitelisted(const GURL& url,
PreviewsType type) { PreviewsType type) {
std::set<url_matcher::URLMatcherConditionSet::ID> matches = std::set<url_matcher::URLMatcherConditionSet::ID> matches =
url_matcher_.MatchURL(url); url_matcher_.MatchURL(url);
for (const auto& match : matches) {
const auto whitelist_iter = whitelist_.find(match); // Only consider the first match in iteration order as it takes precendence
if (whitelist_iter == whitelist_.end()) { // if there are multiple matches.
continue; const auto& first_match = matches.begin();
} if (first_match == matches.end()) {
const auto& whitelisted_previews = whitelist_iter->second; return false;
std::set<PreviewsType>::iterator found = whitelisted_previews.find(type);
// TODO(crbug.com/783237): Make sure this checks for granularity of matched
// condition when more than one host suffix is matched.
return found != whitelisted_previews.end();
} }
return false;
const auto whitelist_iter = whitelist_.find(*first_match);
if (whitelist_iter == whitelist_.end()) {
return false;
}
const auto& whitelisted_previews = whitelist_iter->second;
return whitelisted_previews.find(type) != whitelisted_previews.end();
} }
PreviewsOptimizationGuide::PreviewsOptimizationGuide( PreviewsOptimizationGuide::PreviewsOptimizationGuide(
......
...@@ -212,6 +212,63 @@ TEST_F(PreviewsOptimizationGuideTest, ...@@ -212,6 +212,63 @@ TEST_F(PreviewsOptimizationGuideTest,
}); });
} }
TEST_F(PreviewsOptimizationGuideTest, IsWhitelistedWithMultipleHintMatches) {
optimization_guide::proto::Configuration config;
// Whitelist NoScript for indoor.sports.yahoo.com:
optimization_guide::proto::Hint* hint1 = config.add_hints();
hint1->set_key("indoor.sports.yahoo.com");
hint1->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
optimization_guide::proto::Optimization* optimization1 =
hint1->add_whitelisted_optimizations();
optimization1->set_optimization_type(optimization_guide::proto::NOSCRIPT);
// No optimizations for sports.yahoo.com:
optimization_guide::proto::Hint* hint2 = config.add_hints();
hint2->set_key("sports.yahoo.com");
hint2->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
// Whitelist NoScript for base domain yahoo.com:
optimization_guide::proto::Hint* hint3 = config.add_hints();
hint3->set_key("yahoo.com");
hint3->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
optimization_guide::proto::Optimization* optimization3 =
hint3->add_whitelisted_optimizations();
optimization3->set_optimization_type(optimization_guide::proto::NOSCRIPT);
// No optimizations for mail.yahoo.com:
optimization_guide::proto::Hint* hint4 = config.add_hints();
hint4->set_key("mail.yahoo.com");
hint4->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
ProcessHints(config);
RunUntilIdle();
std::unique_ptr<net::URLRequest> request1 =
CreateRequestWithURL(GURL("https://yahoo.com"));
EXPECT_TRUE(guide()->IsWhitelisted(*request1, PreviewsType::NOSCRIPT));
std::unique_ptr<net::URLRequest> request2 =
CreateRequestWithURL(GURL("https://sports.yahoo.com"));
// Uses "sports.yahoo.com" match before "yahoo.com" match.
EXPECT_FALSE(guide()->IsWhitelisted(*request2, PreviewsType::NOSCRIPT));
std::unique_ptr<net::URLRequest> request3 =
CreateRequestWithURL(GURL("https://mail.yahoo.com"));
// Uses "yahoo.com" match before "mail.yahoo.com" match.
EXPECT_TRUE(guide()->IsWhitelisted(*request3, PreviewsType::NOSCRIPT));
std::unique_ptr<net::URLRequest> request4 =
CreateRequestWithURL(GURL("https://indoor.sports.yahoo.com"));
// Uses "indoor.sports.yahoo.com" match before "sports.yahoo.com" match.
EXPECT_TRUE(guide()->IsWhitelisted(*request4, PreviewsType::NOSCRIPT));
std::unique_ptr<net::URLRequest> request5 =
CreateRequestWithURL(GURL("https://outdoor.sports.yahoo.com"));
// Uses "sports.yahoo.com" match before "yahoo.com" match.
EXPECT_FALSE(guide()->IsWhitelisted(*request5, PreviewsType::NOSCRIPT));
}
TEST_F(PreviewsOptimizationGuideTest, RemoveObserverCalledAtDestruction) { TEST_F(PreviewsOptimizationGuideTest, RemoveObserverCalledAtDestruction) {
EXPECT_FALSE(optimization_guide_service()->RemoveObserverCalled()); EXPECT_FALSE(optimization_guide_service()->RemoveObserverCalled());
......
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