diff options
Diffstat (limited to 'chromium/components/previews/content')
17 files changed, 2110 insertions, 1095 deletions
diff --git a/chromium/components/previews/content/BUILD.gn b/chromium/components/previews/content/BUILD.gn index 4e6d9992fec..461aae9fbf2 100644 --- a/chromium/components/previews/content/BUILD.gn +++ b/chromium/components/previews/content/BUILD.gn @@ -4,12 +4,16 @@ static_library("content") { sources = [ + "previews_decider.h", "previews_decider_impl.cc", "previews_decider_impl.h", "previews_hints.cc", "previews_hints.h", - "previews_optimization_guide.cc", "previews_optimization_guide.h", + "previews_optimization_guide_decider.cc", + "previews_optimization_guide_decider.h", + "previews_optimization_guide_impl.cc", + "previews_optimization_guide_impl.h", "previews_ui_service.cc", "previews_ui_service.h", "previews_user_data.cc", @@ -26,6 +30,7 @@ static_library("content") { "//components/previews/core", "//components/url_matcher", "//components/variations", + "//content/public/browser", "//content/public/common", "//net", "//services/network:network_service", @@ -37,7 +42,8 @@ source_set("unit_tests") { sources = [ "previews_decider_impl_unittest.cc", "previews_hints_unittest.cc", - "previews_optimization_guide_unittest.cc", + "previews_optimization_guide_decider_unittest.cc", + "previews_optimization_guide_impl_unittest.cc", "previews_ui_service_unittest.cc", "previews_user_data_unittest.cc", ] @@ -54,7 +60,9 @@ source_set("unit_tests") { "//components/prefs:test_support", "//components/previews/core", "//components/variations", + "//content/public/browser", "//content/public/common", + "//content/test:test_support", "//net:test_support", "//services/network:network_service", "//services/network:test_support", diff --git a/chromium/components/previews/content/DEPS b/chromium/components/previews/content/DEPS index b525d9da6c9..ce31fe01b24 100644 --- a/chromium/components/previews/content/DEPS +++ b/chromium/components/previews/content/DEPS @@ -8,7 +8,9 @@ include_rules = [ "+components/previews/core", "+components/url_matcher", "+components/variations", + "+content/public/browser", "+content/public/common", + "+content/public/test", "+net", "+services/network", ] diff --git a/chromium/components/previews/content/previews_decider.h b/chromium/components/previews/content/previews_decider.h new file mode 100644 index 00000000000..886492ad336 --- /dev/null +++ b/chromium/components/previews/content/previews_decider.h @@ -0,0 +1,68 @@ +// Copyright 2016 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. + +#ifndef COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_DECIDER_H_ +#define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_DECIDER_H_ + +#include <string> +#include <vector> + +#include "components/previews/core/previews_experiments.h" + +namespace content { +class NavigationHandle; +} // namespace content + +class GURL; + +namespace previews { +class PreviewsUserData; + +class PreviewsDecider { + public: + // Whether the URL for |navigation_handle| is allowed to show a preview of + // |type| as can be determined at the start of a navigation (or start of a + // redirection). This can be further checked at navigation commit time via + // |ShouldCommitPreview|. Some types of previews will be checked for an + // applicable network quality threshold - these are client previews that do + // not have optimization hint support. Previews with optimization hint support + // can have variable network quality thresholds based on the committed URL. + // Data Reduction Proxy previews (i.e., LITE_PAGE) perform a network quality + // check on the server. + virtual bool ShouldAllowPreviewAtNavigationStart( + PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + bool is_reload, + PreviewsType type) const = 0; + + // Whether the URL for |navigation_handle| is allowed to show a preview of + // |type|. + virtual bool ShouldCommitPreview(PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) const = 0; + + // Requests that any applicable detailed page hints be loaded. Returns + // whether client knows that it has hints for the host of the URL for + // |navigation_handle| (that may need to be loaded from persistent storage). + virtual bool LoadPageHints(content::NavigationHandle* navigation_handle) = 0; + + // Whether |url| has loaded resource loading hints and, if it does, populates + // |out_resource_patterns_to_block| with the resource patterns to block. + virtual bool GetResourceLoadingHints( + const GURL& url, + std::vector<std::string>* out_resource_patterns_to_block) const = 0; + + // Logs UMA for whether the OptimizationGuide HintCache has a matching Hint + // guidance for |url|. This is useful for measuring the effectiveness of the + // page hints provided by Cacao. + virtual void LogHintCacheMatch(const GURL& url, bool is_committed) const = 0; + + protected: + PreviewsDecider() {} + virtual ~PreviewsDecider() {} +}; + +} // namespace previews + +#endif // COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_DECIDER_H_ diff --git a/chromium/components/previews/content/previews_decider_impl.cc b/chromium/components/previews/content/previews_decider_impl.cc index 04c52a6ff69..68c8e42f939 100644 --- a/chromium/components/previews/content/previews_decider_impl.cc +++ b/chromium/components/previews/content/previews_decider_impl.cc @@ -26,6 +26,7 @@ #include "components/previews/core/previews_experiments.h" #include "components/previews/core/previews_features.h" #include "components/previews/core/previews_switches.h" +#include "content/public/browser/navigation_handle.h" #include "net/nqe/network_quality_estimator.h" namespace previews { @@ -204,9 +205,11 @@ void PreviewsDeciderImpl::SetIgnorePreviewsBlacklistDecision(bool ignored) { bool PreviewsDeciderImpl::ShouldAllowPreviewAtNavigationStart( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, bool is_reload, PreviewsType type) const { + const GURL url = navigation_handle->GetURL(); + if (!ShouldConsiderPreview(type, url, previews_data)) { // Don't capture metrics since preview is either disabled or url is local. return false; @@ -215,8 +218,8 @@ bool PreviewsDeciderImpl::ShouldAllowPreviewAtNavigationStart( bool is_drp_server_preview = (type == PreviewsType::LITE_PAGE); std::vector<PreviewsEligibilityReason> passed_reasons; PreviewsEligibilityReason eligibility = - DeterminePreviewEligibility(previews_data, url, is_reload, type, - is_drp_server_preview, &passed_reasons); + DeterminePreviewEligibility(previews_data, navigation_handle, is_reload, + type, is_drp_server_preview, &passed_reasons); LogPreviewDecisionMade(eligibility, url, clock_->Now(), type, std::move(passed_reasons), previews_data); return eligibility == PreviewsEligibilityReason::ALLOWED; @@ -233,13 +236,14 @@ bool PreviewsDeciderImpl::ShouldConsiderPreview( PreviewsEligibilityReason PreviewsDeciderImpl::DeterminePreviewEligibility( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, bool is_reload, PreviewsType type, bool is_drp_server_preview, std::vector<PreviewsEligibilityReason>* passed_reasons) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(previews::params::ArePreviewsAllowed()); + const GURL url = navigation_handle->GetURL(); DCHECK(url.has_host()); DCHECK(previews_data); @@ -312,39 +316,49 @@ PreviewsEligibilityReason PreviewsDeciderImpl::DeterminePreviewEligibility( } passed_reasons->push_back(PreviewsEligibilityReason::DEVICE_OFFLINE); - if (effective_connection_type_ > - previews::params::GetECTThresholdForPreview(type)) { - return PreviewsEligibilityReason::NETWORK_NOT_SLOW; + // If the optimization type is not a commit-time preview, determine + // the ECT network triggering condition here. + if (!CheckECTOnlyAtCommitTime(type)) { + if (effective_connection_type_ > + previews::params::GetECTThresholdForPreview(type)) { + return PreviewsEligibilityReason::NETWORK_NOT_SLOW; + } + passed_reasons->push_back(PreviewsEligibilityReason::NETWORK_NOT_SLOW); + + if (effective_connection_type_ > params::GetSessionMaxECTThreshold()) { + return PreviewsEligibilityReason::NETWORK_NOT_SLOW_FOR_SESSION; + } + passed_reasons->push_back( + PreviewsEligibilityReason::NETWORK_NOT_SLOW_FOR_SESSION); } - passed_reasons->push_back(PreviewsEligibilityReason::NETWORK_NOT_SLOW); } if (is_reload) { return PreviewsEligibilityReason::RELOAD_DISALLOWED; } - passed_reasons->push_back(PreviewsEligibilityReason::RELOAD_DISALLOWED); - // Check server whitelist/blacklist, if provided. + // Check optimization hints, if provided. if (ShouldCheckOptimizationHints(type)) { if (optimization_guide::features::IsOptimizationHintsEnabled()) { // Optimization hints are configured, so determine if those hints // allow the optimization type (as of start-of-navigation time anyway). - return ShouldAllowPreviewPerOptimizationHints(previews_data, url, type, - passed_reasons); + return ShouldAllowPreviewPerOptimizationHints( + previews_data, navigation_handle, type, passed_reasons); } else if (type == PreviewsType::RESOURCE_LOADING_HINTS || type == PreviewsType::NOSCRIPT || type == PreviewsType::DEFER_ALL_SCRIPT) { - return PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER; + return PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE; } } return PreviewsEligibilityReason::ALLOWED; } -bool PreviewsDeciderImpl::LoadPageHints(const GURL& url) { +bool PreviewsDeciderImpl::LoadPageHints( + content::NavigationHandle* navigation_handle) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return previews_opt_guide_->MaybeLoadOptimizationHints(url, + return previews_opt_guide_->MaybeLoadOptimizationHints(navigation_handle, base::DoNothing()); } @@ -365,17 +379,20 @@ void PreviewsDeciderImpl::LogHintCacheMatch(const GURL& url, if (!previews_opt_guide_) return; - previews_opt_guide_->LogHintCacheMatch(url, is_committed, - effective_connection_type_); + previews_opt_guide_->LogHintCacheMatch(url, is_committed); } -bool PreviewsDeciderImpl::ShouldCommitPreview(PreviewsUserData* previews_data, - const GURL& committed_url, - PreviewsType type) const { +bool PreviewsDeciderImpl::ShouldCommitPreview( + PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(PreviewsType::NOSCRIPT == type || PreviewsType::RESOURCE_LOADING_HINTS == type || PreviewsType::DEFER_ALL_SCRIPT == type); + + const GURL committed_url = navigation_handle->GetURL(); + if (previews_black_list_ && !blacklist_ignored_) { std::vector<PreviewsEligibilityReason> passed_reasons; // The blacklist will disallow certain hosts for periods of time based on @@ -394,7 +411,7 @@ bool PreviewsDeciderImpl::ShouldCommitPreview(PreviewsUserData* previews_data, optimization_guide::features::IsOptimizationHintsEnabled()) { std::vector<PreviewsEligibilityReason> passed_reasons; PreviewsEligibilityReason status = ShouldCommitPreviewPerOptimizationHints( - previews_data, committed_url, type, &passed_reasons); + previews_data, navigation_handle, type, &passed_reasons); if (status != PreviewsEligibilityReason::ALLOWED) { LogPreviewDecisionMade(status, committed_url, clock_->Now(), type, std::move(passed_reasons), previews_data); @@ -408,7 +425,7 @@ bool PreviewsDeciderImpl::ShouldCommitPreview(PreviewsUserData* previews_data, PreviewsEligibilityReason PreviewsDeciderImpl::ShouldAllowPreviewPerOptimizationHints( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, PreviewsType type, std::vector<PreviewsEligibilityReason>* passed_reasons) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -424,18 +441,26 @@ PreviewsDeciderImpl::ShouldAllowPreviewPerOptimizationHints( if (type == PreviewsType::LITE_PAGE_REDIRECT) { if (base::CommandLine::ForCurrentProcess()->HasSwitch( switches::kIgnoreLitePageRedirectOptimizationBlacklist)) { + // Make sure to also check the ECT threshold for the Preview if we are + // bypassing the optimization guide. + if (effective_connection_type_ > + params::GetECTThresholdForPreview(type)) { + return PreviewsEligibilityReason::NETWORK_NOT_SLOW; + } return PreviewsEligibilityReason::ALLOWED; } - if (!previews_opt_guide_ || !previews_opt_guide_->has_hints()) + if (!previews_opt_guide_ || !previews_opt_guide_->IsReady()) return PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE; passed_reasons->push_back( PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE); - if (previews_opt_guide_->IsBlacklisted(url, type)) - return PreviewsEligibilityReason::HOST_BLACKLISTED_BY_SERVER; + if (!previews_opt_guide_->CanApplyPreview(previews_data, navigation_handle, + type)) { + return PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE; + } passed_reasons->push_back( - PreviewsEligibilityReason::HOST_BLACKLISTED_BY_SERVER); + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE); } return PreviewsEligibilityReason::ALLOWED; @@ -444,7 +469,7 @@ PreviewsDeciderImpl::ShouldAllowPreviewPerOptimizationHints( PreviewsEligibilityReason PreviewsDeciderImpl::ShouldCommitPreviewPerOptimizationHints( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, PreviewsType type, std::vector<PreviewsEligibilityReason>* passed_reasons) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -461,22 +486,22 @@ PreviewsDeciderImpl::ShouldCommitPreviewPerOptimizationHints( return PreviewsEligibilityReason::ALLOWED; } - if (!previews_opt_guide_ || !previews_opt_guide_->has_hints()) + if (!previews_opt_guide_ || !previews_opt_guide_->IsReady()) return PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE; passed_reasons->push_back( PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE); // Check if request URL is whitelisted by the optimization guide. - net::EffectiveConnectionType ect_threshold = - net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; - if (!previews_opt_guide_->IsWhitelisted(previews_data, url, type, - &ect_threshold)) { - return PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER; + if (!previews_opt_guide_->CanApplyPreview(previews_data, navigation_handle, + type)) { + return PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE; } passed_reasons->push_back( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER); + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE); + + // The url is whitelisted, now check some additional cases of the effective + // network condition. - // The url is whitelisted, now check the ECT threshold for it. // Note: the network quality estimator may sometimes return effective // connection type as offline when the Android APIs incorrectly return device // connectivity as null. See https://crbug.com/838969. So, we do not trigger @@ -500,11 +525,6 @@ PreviewsDeciderImpl::ShouldCommitPreviewPerOptimizationHints( } passed_reasons->push_back(PreviewsEligibilityReason::DEVICE_OFFLINE); - if (ect > ect_threshold) { - return PreviewsEligibilityReason::NETWORK_NOT_SLOW; - } - passed_reasons->push_back(PreviewsEligibilityReason::NETWORK_NOT_SLOW); - if (ect > params::GetSessionMaxECTThreshold()) { return PreviewsEligibilityReason::NETWORK_NOT_SLOW_FOR_SESSION; } diff --git a/chromium/components/previews/content/previews_decider_impl.h b/chromium/components/previews/content/previews_decider_impl.h index f99311fe1ab..c2b073f82e6 100644 --- a/chromium/components/previews/content/previews_decider_impl.h +++ b/chromium/components/previews/content/previews_decider_impl.h @@ -20,9 +20,9 @@ #include "base/time/time.h" #include "components/blacklist/opt_out_blacklist/opt_out_blacklist_data.h" #include "components/blacklist/opt_out_blacklist/opt_out_blacklist_delegate.h" +#include "components/previews/content/previews_decider.h" #include "components/previews/content/previews_optimization_guide.h" #include "components/previews/core/previews_black_list.h" -#include "components/previews/core/previews_decider.h" #include "components/previews/core/previews_experiments.h" #include "components/previews/core/previews_logger.h" #include "net/nqe/effective_connection_type.h" @@ -36,6 +36,10 @@ namespace blacklist { class OptOutStore; } +namespace content { +class NavigationHandle; +} + namespace previews { class PreviewsUIService; @@ -106,19 +110,20 @@ class PreviewsDeciderImpl : public PreviewsDecider, PreviewsBlackList* black_list() const { return previews_black_list_.get(); } // PreviewsDecider implementation: - bool ShouldAllowPreviewAtNavigationStart(PreviewsUserData* previews_data, - const GURL& url, - bool is_reload, - PreviewsType type) const override; + bool ShouldAllowPreviewAtNavigationStart( + PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + bool is_reload, + PreviewsType type) const override; bool ShouldCommitPreview(PreviewsUserData* previews_data, - const GURL& committed_url, + content::NavigationHandle* navigation_handle, PreviewsType type) const override; // Set whether to ignore the long term blacklist rules for server previews. void SetIgnoreLongTermBlackListForServerPreviews( bool ignore_long_term_blacklist_for_server_previews); - bool LoadPageHints(const GURL& url) override; + bool LoadPageHints(content::NavigationHandle* navigation_handle) override; bool GetResourceLoadingHints( const GURL& url, @@ -157,7 +162,7 @@ class PreviewsDeciderImpl : public PreviewsDecider, // Determines the eligibility of the preview |type| for |url|. PreviewsEligibilityReason DeterminePreviewEligibility( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, bool is_reload, PreviewsType type, bool is_drp_server_preview, @@ -169,7 +174,7 @@ class PreviewsDeciderImpl : public PreviewsDecider, // to deny the preview for consideration. PreviewsEligibilityReason ShouldAllowPreviewPerOptimizationHints( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, PreviewsType type, std::vector<PreviewsEligibilityReason>* passed_reasons) const; @@ -178,7 +183,7 @@ class PreviewsDeciderImpl : public PreviewsDecider, // navigation URL against any specific hint details. PreviewsEligibilityReason ShouldCommitPreviewPerOptimizationHints( PreviewsUserData* previews_data, - const GURL& url, + content::NavigationHandle* navigation_handle, PreviewsType type, std::vector<PreviewsEligibilityReason>* passed_reasons) const; diff --git a/chromium/components/previews/content/previews_decider_impl_unittest.cc b/chromium/components/previews/content/previews_decider_impl_unittest.cc index c1651508faf..38ae4312742 100644 --- a/chromium/components/previews/content/previews_decider_impl_unittest.cc +++ b/chromium/components/previews/content/previews_decider_impl_unittest.cc @@ -8,6 +8,7 @@ #include <map> #include <memory> #include <string> +#include <unordered_set> #include <utility> #include <vector> @@ -26,8 +27,8 @@ #include "base/test/metrics/histogram_tester.h" #include "base/test/scoped_command_line.h" #include "base/test/scoped_feature_list.h" -#include "base/test/scoped_task_environment.h" #include "base/test/simple_test_clock.h" +#include "base/test/task_environment.h" #include "base/threading/thread_task_runner_handle.h" #include "base/time/default_clock.h" #include "base/time/time.h" @@ -39,6 +40,7 @@ #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" #include "components/optimization_guide/hint_cache_store.h" #include "components/optimization_guide/optimization_guide_features.h" +#include "components/optimization_guide/optimization_guide_prefs.h" #include "components/optimization_guide/optimization_guide_service.h" #include "components/optimization_guide/proto_database_provider_test_base.h" #include "components/optimization_guide/top_host_provider.h" @@ -52,6 +54,7 @@ #include "components/previews/core/previews_logger.h" #include "components/previews/core/previews_switches.h" #include "components/variations/variations_associated_data.h" +#include "content/public/test/mock_navigation_handle.h" #include "net/nqe/effective_connection_type.h" #include "net/nqe/network_quality_estimator_test_util.h" #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" @@ -165,38 +168,46 @@ class TestTopHostProvider : public optimization_guide::TopHostProvider { // Stub class of PreviewsOptimizationGuide to control IsWhitelisted and // IsBlacklisted outcomes when testing PreviewsDeciderImpl. -class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuide { +class TestPreviewsOptimizationGuide + : public PreviewsOptimizationGuide, + public network::NetworkQualityTracker::EffectiveConnectionTypeObserver { public: TestPreviewsOptimizationGuide( - optimization_guide::OptimizationGuideService* optimization_guide_service, - const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, - const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, - const base::FilePath& test_path, - PrefService* pref_service, - leveldb_proto::ProtoDatabaseProvider* database_provider, - optimization_guide::TopHostProvider* optimization_guide_top_host_provider, - scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) - : PreviewsOptimizationGuide(optimization_guide_service, - ui_task_runner, - background_task_runner, - test_path, - pref_service, - database_provider, - optimization_guide_top_host_provider, - url_loader_factory) {} - ~TestPreviewsOptimizationGuide() override {} + network::NetworkQualityTracker* network_quality_tracker) + : network_quality_tracker_(network_quality_tracker) { + network_quality_tracker_->AddEffectiveConnectionTypeObserver(this); + } + + ~TestPreviewsOptimizationGuide() override { + network_quality_tracker_->RemoveEffectiveConnectionTypeObserver(this); + } + + bool IsReady() const override { return is_ready_; } + + void SetIsReady(bool is_ready) { is_ready_ = is_ready; } + + void OnEffectiveConnectionTypeChanged( + net::EffectiveConnectionType ect) override { + current_ect_ = ect; + } // PreviewsOptimizationGuide: - bool IsWhitelisted( - PreviewsUserData* previews_user_data, - const GURL& url, - PreviewsType type, - net::EffectiveConnectionType* ect_threshold) const override { + bool CanApplyPreview(PreviewsUserData* previews_user_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) override { EXPECT_TRUE(type == PreviewsType::NOSCRIPT || type == PreviewsType::RESOURCE_LOADING_HINTS || - type == PreviewsType::DEFER_ALL_SCRIPT); + type == PreviewsType::DEFER_ALL_SCRIPT || + type == PreviewsType::LITE_PAGE_REDIRECT); + // Use default ect trigger threshold for the preview type. - *ect_threshold = previews::params::GetECTThresholdForPreview(type); + if (current_ect_ > previews::params::GetECTThresholdForPreview(type)) + return false; + + if (type == PreviewsType::LITE_PAGE_REDIRECT) + return !IsBlacklisted(navigation_handle, type); + + const GURL url = navigation_handle->GetURL(); if (type == PreviewsType::NOSCRIPT) { return url.host().compare("whitelisted.example.com") == 0 || url.host().compare("noscript_only_whitelisted.example.com") == 0; @@ -208,14 +219,42 @@ class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuide { return false; } - // PreviewsOptimizationGuide: - bool IsBlacklisted(const GURL& url, PreviewsType type) const override { + // Returns whether the URL associated with |navigation_handle| should be + // blacklisted from |type|. + bool IsBlacklisted(content::NavigationHandle* navigation_handle, + PreviewsType type) const { EXPECT_TRUE(type == PreviewsType::LITE_PAGE_REDIRECT); + const GURL url = navigation_handle->GetURL(); if (type == PreviewsType::LITE_PAGE_REDIRECT) { return url.host().compare("blacklisted.example.com") == 0; } return false; } + + // PreviewsOptimizationGuide: + bool MaybeLoadOptimizationHints(content::NavigationHandle* navigation_handle, + base::OnceClosure callback) override { + return false; + } + + // PreviewsOptimizationGuide: + bool GetResourceLoadingHints( + const GURL& url, + std::vector<std::string>* out_resource_patterns_to_block) override { + return false; + } + + // PreviewsOptimizationGuide: + void LogHintCacheMatch(const GURL& url, bool is_committed) const override {} + + // PreviewsOptimizationGuide: + void ClearFetchedHints() override {} + + private: + network::NetworkQualityTracker* network_quality_tracker_; + bool is_ready_ = false; + net::EffectiveConnectionType current_ect_ = + net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; }; // Stub class of PreviewsUIService to test logging functionalities in @@ -352,7 +391,8 @@ class TestPreviewsUIService : public PreviewsUIService { class TestPreviewsDeciderImpl : public PreviewsDeciderImpl { public: - TestPreviewsDeciderImpl(base::Clock* clock) : PreviewsDeciderImpl(clock) {} + explicit TestPreviewsDeciderImpl(base::Clock* clock) + : PreviewsDeciderImpl(clock) {} ~TestPreviewsDeciderImpl() override {} // Expose the injecting blacklist method from PreviewsDeciderImpl, and inject @@ -396,7 +436,7 @@ class PreviewsDeciderImplTest : field_trial_list_(nullptr), previews_decider_impl_(nullptr), optimization_guide_service_( - scoped_task_environment_.GetMainThreadTaskRunner()) { + task_environment_.GetMainThreadTaskRunner()) { clock_.SetNow(base::Time::Now()); network_quality_tracker_.ReportEffectiveConnectionTypeForTesting( @@ -434,15 +474,14 @@ class PreviewsDeciderImplTest std::make_unique<TestPreviewsDeciderImpl>(&clock_); previews_decider_impl_ = previews_decider_impl.get(); pref_service_ = std::make_unique<TestingPrefServiceSimple>(); + optimization_guide::prefs::RegisterProfilePrefs(pref_service_->registry()); + std::unique_ptr<TestPreviewsOptimizationGuide> previews_opt_guide = + std::make_unique<TestPreviewsOptimizationGuide>( + &network_quality_tracker_); + previews_opt_guide_ = previews_opt_guide.get(); ui_service_.reset(new TestPreviewsUIService( std::move(previews_decider_impl), std::make_unique<TestOptOutStore>(), - std::make_unique<TestPreviewsOptimizationGuide>( - &optimization_guide_service_, - scoped_task_environment_.GetMainThreadTaskRunner(), - base::CreateSequencedTaskRunnerWithTraits( - {base::MayBlock(), base::TaskPriority::BEST_EFFORT}), - temp_dir_.GetPath(), pref_service_.get(), db_provider_.get(), - &optimization_guide_top_host_provider_, url_loader_factory_), + std::move(previews_opt_guide), base::BindRepeating(&IsPreviewFieldTrialEnabled), std::make_unique<PreviewsLogger>(), std::move(allowed_types), &network_quality_tracker_)); @@ -450,17 +489,12 @@ class PreviewsDeciderImplTest void InitializeUIService() { InitializeUIServiceWithoutWaitingForBlackList(); - scoped_task_environment_.RunUntilIdle(); + task_environment_.RunUntilIdle(); base::RunLoop().RunUntilIdle(); } void InitializeOptimizationGuideHints() { - std::unique_ptr<optimization_guide::proto::Configuration> config = - std::make_unique<optimization_guide::proto::Configuration>(); - std::unique_ptr<PreviewsHints> hints = - PreviewsHints::CreateFromHintsConfiguration(std::move(config), nullptr); - previews_decider_impl()->previews_opt_guide()->UpdateHints( - base::DoNothing(), std::move(hints)); + previews_opt_guide_->SetIsReady(true); } TestPreviewsDeciderImpl* previews_decider_impl() { @@ -479,10 +513,11 @@ class PreviewsDeciderImplTest base::SimpleTestClock clock_; private: - base::test::ScopedTaskEnvironment scoped_task_environment_; + base::test::TaskEnvironment task_environment_; base::FieldTrialList field_trial_list_; TestPreviewsDeciderImpl* previews_decider_impl_; optimization_guide::OptimizationGuideService optimization_guide_service_; + TestPreviewsOptimizationGuide* previews_opt_guide_; TestTopHostProvider optimization_guide_top_host_provider_; std::unique_ptr<TestPreviewsUIService> ui_service_; network::TestNetworkQualityTracker network_quality_tracker_; @@ -500,9 +535,10 @@ TEST_F(PreviewsDeciderImplTest, AllPreviewsDisabledByFeature) { PreviewsUserData user_data(kDefaultPageId); PreviewsUserData user_data2(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data2, GURL("https://www.google.com"), false, - PreviewsType::NOSCRIPT)); + &user_data2, &navigation_handle, false, PreviewsType::NOSCRIPT)); } TEST_F(PreviewsDeciderImplTest, TestDisallowBasicAuthentication) { @@ -514,9 +550,10 @@ TEST_F(PreviewsDeciderImplTest, TestDisallowBasicAuthentication) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://user:pass@www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://user:pass@www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason", static_cast<int>(PreviewsEligibilityReason::URL_HAS_BASIC_AUTH), 1); @@ -535,13 +572,14 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); InitializeUIServiceWithoutWaitingForBlackList(); // The blacklist is not loaded yet. EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason", static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), @@ -561,8 +599,7 @@ TEST_F(PreviewsDeciderImplTest, previews_decider_impl()->ClearBlackList(now, now); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), @@ -581,6 +618,8 @@ TEST_F(PreviewsDeciderImplTest, MAYBE_TestSetBlacklistBoolDueToBlackListState) { scoped_feature_list.InitAndEnableFeature(features::kPreviews); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; InitializeUIServiceWithoutWaitingForBlackList(); @@ -590,8 +629,7 @@ TEST_F(PreviewsDeciderImplTest, MAYBE_TestSetBlacklistBoolDueToBlackListState) { EXPECT_FALSE(user_data.black_listed_for_lite_page()); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE)); EXPECT_TRUE(user_data.black_listed_for_lite_page()); } @@ -602,13 +640,14 @@ TEST_F(PreviewsDeciderImplTest, InitializeUIService(); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), @@ -625,13 +664,15 @@ TEST_F(PreviewsDeciderImplTest, TestAllowLitePageWhenNetworkQualityFast) { InitializeUIService(); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); + // LitePage check NQE on its own. ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_4G); base::HistogramTester histogram_tester; EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.LitePage", static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); @@ -643,12 +684,13 @@ TEST_F(PreviewsDeciderImplTest, TestDisallowOfflineWhenNetworkQualityFast) { InitializeUIService(); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_3G); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); @@ -660,12 +702,13 @@ TEST_F(PreviewsDeciderImplTest, TestDisallowOfflineWhenNetworkOffline) { InitializeUIService(); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::DEVICE_OFFLINE), 1); @@ -679,10 +722,12 @@ TEST_F(PreviewsDeciderImplTest, TestDisallowOfflineOnReload) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), true, PreviewsType::OFFLINE)); + &user_data, &navigation_handle, true, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason", static_cast<int>(PreviewsEligibilityReason::RELOAD_DISALLOWED), 1); @@ -700,10 +745,12 @@ TEST_F(PreviewsDeciderImplTest, TestDisallowOfflineOnReloadWithExperiment) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), true, PreviewsType::OFFLINE)); + &user_data, &navigation_handle, true, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason", static_cast<int>(PreviewsEligibilityReason::RELOAD_DISALLOWED), 1); @@ -731,11 +778,12 @@ TEST_F(PreviewsDeciderImplTest, TestAllowOffline) { ReportEffectiveConnectionType(test.effective_connection_type); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_EQ(test.expected_offline_allowed, previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)) + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)) << " effective_connection_type=" << test.effective_connection_type; if (test.expected_offline_allowed) { histogram_tester.ExpectUniqueSample( @@ -760,10 +808,11 @@ TEST_F(PreviewsDeciderImplTest, OfflineDisallowedWhenFeatureDisabled) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 0); } @@ -777,10 +826,11 @@ TEST_F(PreviewsDeciderImplTest, ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), @@ -799,9 +849,10 @@ TEST_F(PreviewsDeciderImplTest, OfflineDisallowedWhenNetworkFast) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); @@ -818,8 +869,10 @@ TEST_F(PreviewsDeciderImplTest, MissingHostDisallowed) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("file:///sdcard")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("file:///sdcard"), false, PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); } TEST_F(PreviewsDeciderImplTest, OfflineDisallowedOnReload) { @@ -833,10 +886,12 @@ TEST_F(PreviewsDeciderImplTest, OfflineDisallowedOnReload) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), true, PreviewsType::OFFLINE)); + &user_data, &navigation_handle, true, PreviewsType::OFFLINE)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.Offline", static_cast<int>(PreviewsEligibilityReason::RELOAD_DISALLOWED), 1); @@ -849,13 +904,14 @@ TEST_F(PreviewsDeciderImplTest, NoScriptFeatureDefaultBehavior) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); #if defined(OS_ANDROID) // Enabled by default on Android. NOSCRIPT always allowed at navigation start // to handle asynchronous loading of page hints; non-whitelisted ones are // later blocked on commit. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); histogram_tester.ExpectTotalCount("Previews.EligibilityReason.NoScript", 1); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.NoScript", @@ -863,8 +919,7 @@ TEST_F(PreviewsDeciderImplTest, NoScriptFeatureDefaultBehavior) { #else // !defined(OS_ANDROID) // Disabled by default on non-Android. EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); histogram_tester.ExpectTotalCount("Previews.EligibilityReason.NoScript", 0); #endif // defined(OS_ANDROID) } @@ -883,14 +938,15 @@ TEST_F(PreviewsDeciderImplTest, NoScriptNotAllowedWithoutOptimizationHints) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); EXPECT_EQ(test_ect, user_data.navigation_ect()); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE), 1); } } @@ -907,16 +963,18 @@ TEST_F(PreviewsDeciderImplTest, NoScriptAllowedByFeatureWithWhitelist) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // First verify preview allowed for non-whitelisted url; they're always // allowed at navigation start to enable asynchronous loading of page hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); // Now verify preview allowed for whitelisted url. + content::MockNavigationHandle whitelisted_navigation_handle; + whitelisted_navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.NoScript", @@ -934,27 +992,30 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { // First verify not allowed for non-whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://www.google.com"), PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), 1); } // Now verify preview for whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); // Expect no eligibility logging. histogram_tester.ExpectTotalCount("Previews.EligibilityReason.NoScript", 0); @@ -962,16 +1023,19 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { // Verify preview not allowed for whitelisted url when network is not slow. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_3G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_3G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", - static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), + 1); } // Verify preview not allowed for whitelisted url for unknown network quality. @@ -979,10 +1043,11 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", @@ -993,16 +1058,17 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { // Verify preview not allowed for session limited ECT threshold. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::test::ScopedFeatureList nested_scoped_list; nested_scoped_list.InitAndEnableFeatureWithParameters( features::kSlowPageTriggering, {{"session_max_ect_trigger", "Slow-2G"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", @@ -1021,10 +1087,11 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { {{"session_max_ect_trigger", "Slow-2G"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", @@ -1043,10 +1110,11 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { {{"session_max_ect_trigger", "Slow-2G"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectTotalCount("Previews.EligibilityReason.NoScript", 0); } @@ -1061,14 +1129,17 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { {{"session_max_ect_trigger", "Slow-2G"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.NoScript", - static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), + 1); } // Verify preview allowed for session when navigation ECT is unknown but max @@ -1080,10 +1151,11 @@ TEST_F(PreviewsDeciderImplTest, NoScriptCommitTimeWhitelistCheck) { features::kSlowPageTriggering, {{"session_max_ect_trigger", "4G"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, PreviewsType::NOSCRIPT)); histogram_tester.ExpectTotalCount("Previews.EligibilityReason.NoScript", 0); } @@ -1099,9 +1171,10 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason", @@ -1123,27 +1196,33 @@ TEST_F(PreviewsDeciderImplTest, LitePageRedirectDisallowedByServerBlacklist) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // First verify preview allowed for non-whitelisted url. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.LitePageRedirect", static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); // Now verify no preview for blacklisted url. + content::MockNavigationHandle blacklisted_navigation_handle; + blacklisted_navigation_handle.set_url( + GURL("https://blacklisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://blacklisted.example.com"), false, + &user_data, &blacklisted_navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason", - static_cast<int>(PreviewsEligibilityReason::HOST_BLACKLISTED_BY_SERVER), + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), 1); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.LitePageRedirect", - static_cast<int>(PreviewsEligibilityReason::HOST_BLACKLISTED_BY_SERVER), + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), 1); } @@ -1167,18 +1246,21 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // First verify preview allowed for non-whitelisted url. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.LitePageRedirect", static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); + content::MockNavigationHandle blacklisted_navigation_handle; + blacklisted_navigation_handle.set_url( + GURL("https://blacklisted.example.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://blacklisted.example.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.LitePageRedirect", @@ -1197,26 +1279,25 @@ TEST_F(PreviewsDeciderImplTest, OptimizationGuidePreviewsAllowedWithoutHints) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); // NoScript is allowed before commit without hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); // ResourceLoading is allowed before commit without hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); // DeferAllScript is allowed before commit without hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); // LitePageRedirect is not allowed before commit without hints. EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.LitePageRedirect", static_cast<int>( @@ -1226,17 +1307,14 @@ TEST_F(PreviewsDeciderImplTest, OptimizationGuidePreviewsAllowedWithoutHints) { // Load hints and make sure everything is allowed. InitializeOptimizationGuideHints(); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::NOSCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::NOSCRIPT)); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::LITE_PAGE_REDIRECT)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE_REDIRECT)); } TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsAllowedByDefault) { @@ -1249,6 +1327,8 @@ TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsAllowedByDefault) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); #if defined(OS_ANDROID) bool expected = true; @@ -1257,7 +1337,7 @@ TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsAllowedByDefault) { #endif // defined(OS_ANDROID) EXPECT_EQ(expected, previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); } @@ -1271,18 +1351,20 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE), 1); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.ResourceLoadingHints", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE), 1); } @@ -1303,10 +1385,12 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); // Check whitelisted URL. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); EXPECT_EQ(test_ect, user_data.navigation_ect()); histogram_tester.ExpectUniqueSample( @@ -1327,10 +1411,12 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // Verify preview allowed initially for url without known hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, + &user_data, &navigation_handle, false, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectBucketCount( @@ -1349,28 +1435,30 @@ TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsCommitTimeWhitelistCheck) { // First verify not allowed for non-whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://www.google.com"), - PreviewsType::RESOURCE_LOADING_HINTS)); + &user_data, &navigation_handle, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.ResourceLoadingHints", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), 1); } // Now verify preview for whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::RESOURCE_LOADING_HINTS)); + &user_data, &navigation_handle, PreviewsType::RESOURCE_LOADING_HINTS)); // Expect no eligibility logging. histogram_tester.ExpectTotalCount( @@ -1379,26 +1467,30 @@ TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsCommitTimeWhitelistCheck) { // Verify preview not allowed for whitelisted url when network is not slow. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_4G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_4G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::RESOURCE_LOADING_HINTS)); + &user_data, &navigation_handle, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.ResourceLoadingHints", - static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), + 1); } // Verify preview not allowed for whitelisted url for offline network quality. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::RESOURCE_LOADING_HINTS)); + &user_data, &navigation_handle, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.ResourceLoadingHints", @@ -1407,16 +1499,17 @@ TEST_F(PreviewsDeciderImplTest, ResourceLoadingHintsCommitTimeWhitelistCheck) { // Verify preview not allowed for session limited ECT threshold. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::test::ScopedFeatureList nested_scoped_list; nested_scoped_list.InitAndEnableFeatureWithParameters( features::kSlowPageTriggering, {{"session_max_ect_trigger", "Offline"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::RESOURCE_LOADING_HINTS)); + &user_data, &navigation_handle, PreviewsType::RESOURCE_LOADING_HINTS)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.ResourceLoadingHints", @@ -1436,10 +1529,11 @@ TEST_F(PreviewsDeciderImplTest, DeferAllScriptNotAllowedByDefault) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); } TEST_F(PreviewsDeciderImplTest, @@ -1452,18 +1546,19 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE), 1); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::OPTIMIZATION_HINTS_NOT_AVAILABLE), 1); } @@ -1483,11 +1578,12 @@ TEST_F(PreviewsDeciderImplTest, DeferAllScriptAllowedByFeatureAndWhitelist) { base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); // Check whitelisted URL. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://whitelisted.example.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); EXPECT_EQ(test_ect, user_data.navigation_ect()); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", @@ -1507,11 +1603,12 @@ TEST_F(PreviewsDeciderImplTest, base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // Verify preview allowed initially for url without known hints. EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, false, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectBucketCount( "Previews.EligibilityReason.DeferAllScript", @@ -1529,28 +1626,30 @@ TEST_F(PreviewsDeciderImplTest, DeferAllScriptCommitTimeWhitelistCheck) { // First verify not allowed for non-whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://www.google.com"), - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", static_cast<int>( - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER), + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), 1); } // Now verify preview for whitelisted url. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, PreviewsType::DEFER_ALL_SCRIPT)); // Expect no eligibility logging. histogram_tester.ExpectTotalCount( @@ -1559,26 +1658,30 @@ TEST_F(PreviewsDeciderImplTest, DeferAllScriptCommitTimeWhitelistCheck) { // Verify preview not allowed for whitelisted url when network is not slow. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_4G); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_4G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", - static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); + static_cast<int>( + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE), + 1); } // Verify preview not allowed for whitelisted url for offline network quality. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", @@ -1587,16 +1690,17 @@ TEST_F(PreviewsDeciderImplTest, DeferAllScriptCommitTimeWhitelistCheck) { // Verify preview not allowed for session limited ECT threshold. { + ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); base::test::ScopedFeatureList nested_scoped_list; nested_scoped_list.InitAndEnableFeatureWithParameters( features::kSlowPageTriggering, {{"session_max_ect_trigger", "Offline"}}); base::HistogramTester histogram_tester; PreviewsUserData user_data(kDefaultPageId); - user_data.set_navigation_ect(net::EFFECTIVE_CONNECTION_TYPE_2G); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("https://whitelisted.example.com"), - PreviewsType::DEFER_ALL_SCRIPT)); + &user_data, &navigation_handle, PreviewsType::DEFER_ALL_SCRIPT)); histogram_tester.ExpectUniqueSample( "Previews.EligibilityReason.DeferAllScript", @@ -1678,8 +1782,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeBlacklistNotAvailable) { previews_decider_impl()->InjectTestBlacklist(nullptr /* blacklist */); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. EXPECT_THAT(ui_service()->decision_reasons(), @@ -1713,8 +1819,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeBlacklistStatusesDefault) { previews_decider_impl()->InjectTestBlacklist(std::move(blacklist)); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. // Check for all decision upto current decision is logged. @@ -1735,9 +1843,11 @@ TEST_F(PreviewsDeciderImplTest, ShouldCommitPreviewBlacklistStatuses) { InitializeUIService(); auto expected_type = PreviewsType::NOSCRIPT; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); // First verify URL is allowed for no blacklist status. EXPECT_TRUE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("http://www.google.com"), expected_type)); + &user_data, &navigation_handle, expected_type)); PreviewsEligibilityReason expected_reasons[] = { PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, @@ -1756,8 +1866,10 @@ TEST_F(PreviewsDeciderImplTest, ShouldCommitPreviewBlacklistStatuses) { previews_decider_impl()); previews_decider_impl()->InjectTestBlacklist(std::move(blacklist)); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_FALSE(previews_decider_impl()->ShouldCommitPreview( - &user_data, GURL("http://www.google.com"), expected_type)); + &user_data, &navigation_handle, expected_type)); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. // Check for all decision upto current decision is logged. @@ -1794,8 +1906,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeBlacklistStatusesIgnore) { previews_decider_impl()); previews_decider_impl()->InjectTestBlacklist(std::move(blacklist)); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -1826,9 +1940,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeMediaSuffixesAreExcluded) { previews_decider_impl()); previews_decider_impl()->InjectTestBlacklist(std::move(blacklist)); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com/video.mp4")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com/video.mp4"), false, - expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -1849,15 +1964,15 @@ TEST_F(PreviewsDeciderImplTest, IgnoreFlagDoesNotCheckBlacklist) { previews_decider_impl()->SetIgnorePreviewsBlacklistDecision( true /* ignored */); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); previews_decider_impl()->AddPreviewReload(); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); } TEST_F(PreviewsDeciderImplTest, ReloadsTriggerFiveMinuteRule) { @@ -1868,9 +1983,10 @@ TEST_F(PreviewsDeciderImplTest, ReloadsTriggerFiveMinuteRule) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); previews_decider_impl()->AddPreviewNavigation( GURL("http://wwww.somedomain.com"), false, PreviewsType::OFFLINE, 1); @@ -1878,16 +1994,14 @@ TEST_F(PreviewsDeciderImplTest, ReloadsTriggerFiveMinuteRule) { previews_decider_impl()->AddPreviewReload(); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); EXPECT_EQ(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT, ui_service()->decision_reasons().back()); clock_.Advance(base::TimeDelta::FromMinutes(6)); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); EXPECT_THAT( ui_service()->decision_passed_reasons().back(), ::testing::Contains(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT)); @@ -1901,7 +2015,6 @@ TEST_F(PreviewsDeciderImplTest, optimization_guide::features::kOptimizationHints}, {}); InitializeUIService(); - InitializeOptimizationGuideHints(); std::unique_ptr<TestPreviewsBlackList> blacklist = std::make_unique<TestPreviewsBlackList>( @@ -1924,8 +2037,10 @@ TEST_F(PreviewsDeciderImplTest, ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -1970,8 +2085,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeNetworkNotSlow) { PreviewsEligibilityReason::DEVICE_OFFLINE, }; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. EXPECT_THAT(ui_service()->decision_reasons(), @@ -1998,6 +2115,8 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeReloadDisallowed) { ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); auto expected_reason = PreviewsEligibilityReason::RELOAD_DISALLOWED; auto expected_type = PreviewsType::OFFLINE; @@ -2013,10 +2132,11 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeReloadDisallowed) { PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, PreviewsEligibilityReason::DEVICE_OFFLINE, PreviewsEligibilityReason::NETWORK_NOT_SLOW, + PreviewsEligibilityReason::NETWORK_NOT_SLOW_FOR_SESSION, }; previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), true, expected_type); + &user_data, &navigation_handle, true, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -2050,10 +2170,11 @@ TEST_F(PreviewsDeciderImplTest, IgnoreBlacklistEnabledViaFlag) { previews_decider_impl()->InjectTestBlacklist(std::move(blacklist)); ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); auto expected_reason = PreviewsEligibilityReason::ALLOWED; EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::OFFLINE)); + &user_data, &navigation_handle, false, PreviewsType::OFFLINE)); base::RunLoop().RunUntilIdle(); EXPECT_THAT(ui_service()->decision_reasons(), @@ -2088,11 +2209,14 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeAllowClientPreviewsWithECT) { PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, PreviewsEligibilityReason::DEVICE_OFFLINE, PreviewsEligibilityReason::NETWORK_NOT_SLOW, + PreviewsEligibilityReason::NETWORK_NOT_SLOW_FOR_SESSION, PreviewsEligibilityReason::RELOAD_DISALLOWED, }; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://www.google.com"), false, expected_type); + &user_data, &navigation_handle, false, expected_type); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -2116,7 +2240,6 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeAllowHintPreviewWithoutECT) { optimization_guide::features::kOptimizationHints}, {}); InitializeUIService(); - InitializeOptimizationGuideHints(); std::unique_ptr<TestPreviewsBlackList> blacklist = std::make_unique<TestPreviewsBlackList>( @@ -2140,9 +2263,10 @@ TEST_F(PreviewsDeciderImplTest, LogDecisionMadeAllowHintPreviewWithoutECT) { PreviewsEligibilityReason::RELOAD_DISALLOWED, }; PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whitelisted.example.com")); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("http://whitelisted.example.com"), false, - expected_type)); + &user_data, &navigation_handle, false, expected_type)); base::RunLoop().RunUntilIdle(); // Testing correct log method is called. @@ -2234,16 +2358,16 @@ TEST_F(PreviewsDeciderImplTest, TestIgnoreLongTermRule) { // DataReductionProxy LitePage checks NQE on their own. ReportEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_3G); PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://www.google.com")); base::HistogramTester histogram_tester; previews_decider_impl()->SetIgnoreLongTermBlackListForServerPreviews(false); EXPECT_FALSE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE)); previews_decider_impl()->SetIgnoreLongTermBlackListForServerPreviews(true); EXPECT_TRUE(previews_decider_impl()->ShouldAllowPreviewAtNavigationStart( - &user_data, GURL("https://www.google.com"), false, - PreviewsType::LITE_PAGE)); + &user_data, &navigation_handle, false, PreviewsType::LITE_PAGE)); } } // namespace diff --git a/chromium/components/previews/content/previews_hints.cc b/chromium/components/previews/content/previews_hints.cc index 6b72e76037c..21612a21f18 100644 --- a/chromium/components/previews/content/previews_hints.cc +++ b/chromium/components/previews/content/previews_hints.cc @@ -5,6 +5,7 @@ #include "components/previews/content/previews_hints.h" #include <unordered_set> +#include <utility> #include "base/files/file.h" #include "base/files/file_util.h" @@ -122,30 +123,6 @@ bool IsEnabledOptimizationType( } } -net::EffectiveConnectionType ConvertProtoEffectiveConnectionType( - optimization_guide::proto::EffectiveConnectionType proto_ect) { - switch (proto_ect) { - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_UNKNOWN: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_OFFLINE: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE; - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_SLOW_2G: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_2G: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G; - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_3G: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G; - case optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_4G: - return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G; - } -} - } // namespace PreviewsHints::PreviewsHints( @@ -241,8 +218,7 @@ void PreviewsHints::ParseOptimizationFilters( ConvertProtoOptimizationTypeToPreviewsType( blacklist.optimization_type()); if (previews_type == PreviewsType::LITE_PAGE_REDIRECT && - previews::params::IsLitePageServerPreviewsEnabled() && - blacklist.has_bloom_filter()) { + previews::params::IsLitePageServerPreviewsEnabled()) { RecordOptimizationFilterStatus( blacklist.optimization_type(), optimization_guide::OptimizationFilterStatus:: @@ -256,46 +232,11 @@ void PreviewsHints::ParseOptimizationFilters( kFailedServerBlacklistDuplicateConfig); continue; } - const auto& bloom_filter_proto = blacklist.bloom_filter(); - DCHECK_GT(bloom_filter_proto.num_hash_functions(), 0u); - DCHECK_GT(bloom_filter_proto.num_bits(), 0u); - DCHECK(bloom_filter_proto.has_data()); - if (!bloom_filter_proto.has_data() || - bloom_filter_proto.num_bits() <= 0 || - bloom_filter_proto.num_bits() > - bloom_filter_proto.data().size() * 8) { - DLOG(ERROR) << "Bloom filter config issue"; - RecordOptimizationFilterStatus( - blacklist.optimization_type(), - optimization_guide::OptimizationFilterStatus:: - kFailedServerBlacklistBadConfig); - continue; - } - if (static_cast<int>(bloom_filter_proto.num_bits()) > - previews::params:: - LitePageRedirectPreviewMaxServerBlacklistByteSize() * - 8) { - DLOG(ERROR) << "Bloom filter data exceeds maximum size of " - << previews::params:: - LitePageRedirectPreviewMaxServerBlacklistByteSize() - << " bytes"; - RecordOptimizationFilterStatus( - blacklist.optimization_type(), - optimization_guide::OptimizationFilterStatus:: - kFailedServerBlacklistTooBig); - continue; - } - std::unique_ptr<optimization_guide::BloomFilter> bloom_filter = - std::make_unique<optimization_guide::BloomFilter>( - bloom_filter_proto.num_hash_functions(), - bloom_filter_proto.num_bits(), bloom_filter_proto.data()); + + optimization_guide::OptimizationFilterStatus status; lite_page_redirect_blacklist_ = - std::make_unique<optimization_guide::HostFilter>( - std::move(bloom_filter)); - RecordOptimizationFilterStatus( - blacklist.optimization_type(), - optimization_guide::OptimizationFilterStatus:: - kCreatedServerBlacklist); + optimization_guide::ProcessOptimizationFilter(blacklist, &status); + RecordOptimizationFilterStatus(blacklist.optimization_type(), status); } } } @@ -353,13 +294,17 @@ bool PreviewsHints::IsWhitelisted( if (optimization.has_previews_metadata()) { *out_inflation_percent = optimization.previews_metadata().inflation_percent(); - *out_ect_threshold = ConvertProtoEffectiveConnectionType( - optimization.previews_metadata().max_ect_trigger()); + if (out_ect_threshold) { + *out_ect_threshold = + optimization_guide::ConvertProtoEffectiveConnectionType( + optimization.previews_metadata().max_ect_trigger()); + } } else { *out_inflation_percent = optimization.inflation_percent(); - if (matched_page_hint->has_max_ect_trigger()) { - *out_ect_threshold = ConvertProtoEffectiveConnectionType( - matched_page_hint->max_ect_trigger()); + if (out_ect_threshold && matched_page_hint->has_max_ect_trigger()) { + *out_ect_threshold = + optimization_guide::ConvertProtoEffectiveConnectionType( + matched_page_hint->max_ect_trigger()); } } @@ -387,7 +332,7 @@ bool PreviewsHints::IsBlacklisted(const GURL& url, PreviewsType type) const { return true; } - return lite_page_redirect_blacklist_->ContainsHostSuffix(url); + return lite_page_redirect_blacklist_->Matches(url); } return false; @@ -460,31 +405,26 @@ bool PreviewsHints::GetResourceLoadingHints( } void PreviewsHints::LogHintCacheMatch(const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect) const { + bool is_committed) const { DCHECK(hint_cache_); - if (hint_cache_->HasHint(url.host())) { - if (!is_committed) { - UMA_HISTOGRAM_ENUMERATION( - "Previews.OptimizationGuide.HintCache.HasHint.BeforeCommit", ect, - net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_LAST); - } else { - UMA_HISTOGRAM_ENUMERATION( - "Previews.OptimizationGuide.HintCache.HasHint.AtCommit", ect, - net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_LAST); - const optimization_guide::proto::Hint* hint = - hint_cache_->GetHintIfLoaded(url.host()); - if (hint) { - UMA_HISTOGRAM_ENUMERATION( - "Previews.OptimizationGuide.HintCache.HostMatch.AtCommit", ect, - net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_LAST); - if (optimization_guide::FindPageHintForURL(url, hint)) { - UMA_HISTOGRAM_ENUMERATION( - "Previews.OptimizationGuide.HintCache.PageMatch.AtCommit", ect, - net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_LAST); - } - } + bool has_hint = hint_cache_->HasHint(url.host()); + if (!is_committed) { + UMA_HISTOGRAM_BOOLEAN("OptimizationGuide.HintCache.HasHint.BeforeCommit", + has_hint); + return; + } + + UMA_HISTOGRAM_BOOLEAN("OptimizationGuide.HintCache.HasHint.AtCommit", + has_hint); + if (has_hint) { + const optimization_guide::proto::Hint* hint = + hint_cache_->GetHintIfLoaded(url.host()); + UMA_HISTOGRAM_BOOLEAN("OptimizationGuide.HintCache.HostMatch.AtCommit", + hint); + if (hint) { + UMA_HISTOGRAM_BOOLEAN("OptimizationGuide.HintCache.PageMatch.AtCommit", + optimization_guide::FindPageHintForURL(url, hint)); } } } diff --git a/chromium/components/previews/content/previews_hints.h b/chromium/components/previews/content/previews_hints.h index aa01f64a046..997a40732ab 100644 --- a/chromium/components/previews/content/previews_hints.h +++ b/chromium/components/previews/content/previews_hints.h @@ -13,7 +13,7 @@ #include "base/sequence_checker.h" #include "components/optimization_guide/hint_cache.h" #include "components/optimization_guide/hints_processing_util.h" -#include "components/optimization_guide/host_filter.h" +#include "components/optimization_guide/optimization_filter.h" #include "components/optimization_guide/proto/hints.pb.h" #include "components/previews/content/previews_user_data.h" #include "net/nqe/effective_connection_type.h" @@ -85,11 +85,9 @@ class PreviewsHints { std::vector<std::string>* out_resource_patterns_to_block) const; // Logs UMA for whether the HintCache has a matching Hint and also a matching - // PageHint for |url|. Records the client's current |ect| as well. This is - // useful for measuring the effectiveness of the page hints provided by Cacao. - void LogHintCacheMatch(const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect) const; + // PageHint for |url|. This is useful for measuring the effectiveness of the + // page hints provided by Cacao. + void LogHintCacheMatch(const GURL& url, bool is_committed) const; private: friend class PreviewsHintsTest; @@ -114,7 +112,8 @@ class PreviewsHints { std::unique_ptr<optimization_guide::HintUpdateData> component_update_data_; // Blacklist of host suffixes for LITE_PAGE_REDIRECT Previews. - std::unique_ptr<optimization_guide::HostFilter> lite_page_redirect_blacklist_; + std::unique_ptr<optimization_guide::OptimizationFilter> + lite_page_redirect_blacklist_; SEQUENCE_CHECKER(sequence_checker_); diff --git a/chromium/components/previews/content/previews_hints_unittest.cc b/chromium/components/previews/content/previews_hints_unittest.cc index 06bce379df6..91fecdf1841 100644 --- a/chromium/components/previews/content/previews_hints_unittest.cc +++ b/chromium/components/previews/content/previews_hints_unittest.cc @@ -5,6 +5,7 @@ #include "components/previews/content/previews_hints.h" #include <string> +#include <vector> #include "base/bind.h" #include "base/command_line.h" @@ -13,14 +14,13 @@ #include "base/run_loop.h" #include "base/test/metrics/histogram_tester.h" #include "base/test/scoped_feature_list.h" -#include "base/test/scoped_task_environment.h" +#include "base/test/task_environment.h" #include "components/optimization_guide/bloom_filter.h" #include "components/optimization_guide/hint_cache.h" #include "components/optimization_guide/hint_cache_store.h" #include "components/optimization_guide/hint_update_data.h" #include "components/optimization_guide/hints_component_info.h" #include "components/optimization_guide/hints_component_util.h" -#include "components/optimization_guide/host_filter.h" #include "components/optimization_guide/optimization_guide_features.h" #include "components/optimization_guide/proto/hints.pb.h" #include "components/optimization_guide/proto_database_provider_test_base.h" @@ -59,20 +59,23 @@ void AddBlacklistBloomFilterToConfig( blacklist_proto->set_allocated_bloom_filter(bloom_filter_proto.release()); } -} // namespace - -class TestHostFilter : public optimization_guide::HostFilter { - public: - explicit TestHostFilter(std::string single_host_match) - : HostFilter(nullptr), single_host_match_(single_host_match) {} +void AddRegexpsFilterToConfig(optimization_guide::proto::Configuration* config, + const std::vector<std::string>& regexps) { + optimization_guide::proto::OptimizationFilter* blacklist_proto; + if (config->optimization_blacklists_size() > 0) { + blacklist_proto = config->mutable_optimization_blacklists(0); + } else { + blacklist_proto = config->add_optimization_blacklists(); + blacklist_proto->set_optimization_type( + optimization_guide::proto::LITE_PAGE_REDIRECT); + } - bool ContainsHostSuffix(const GURL& url) const override { - return single_host_match_ == url.host(); + for (const std::string& regexp : regexps) { + blacklist_proto->add_regexps(regexp); } +} - private: - std::string single_host_match_; -}; +} // namespace class PreviewsHintsTest : public optimization_guide::ProtoDatabaseProviderTestBase { @@ -85,8 +88,8 @@ class PreviewsHintsTest ProtoDatabaseProviderTestBase::SetUp(); hint_cache_ = std::make_unique<optimization_guide::HintCache>( std::make_unique<optimization_guide::HintCacheStore>( - db_provider_.get(), temp_dir_.GetPath(), nullptr /* pref_service */, - scoped_task_environment_.GetMainThreadTaskRunner())); + db_provider_.get(), temp_dir_.GetPath(), + task_environment_.GetMainThreadTaskRunner())); is_store_initialized_ = false; hint_cache_->Initialize( @@ -141,12 +144,10 @@ class PreviewsHintsTest net::EffectiveConnectionType* out_ect_threshold, std::string* serialized_hint_version_string); - void MaybeLoadHintAndLogHintCacheMatch(const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect); + void MaybeLoadHintAndLogHintCacheMatch(const GURL& url, bool is_committed); void RunUntilIdle() { - scoped_task_environment_.RunUntilIdle(); + task_environment_.RunUntilIdle(); base::RunLoop().RunUntilIdle(); } @@ -168,7 +169,7 @@ class PreviewsHintsTest void MaybeLoadHint(const GURL& url); - base::test::ScopedTaskEnvironment scoped_task_environment_; + base::test::TaskEnvironment task_environment_; bool is_store_initialized_; bool are_previews_hints_initialized_; @@ -190,12 +191,10 @@ bool PreviewsHintsTest::MaybeLoadHintAndCheckIsWhitelisted( out_serialized_hint_version_string); } -void PreviewsHintsTest::MaybeLoadHintAndLogHintCacheMatch( - const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect) { +void PreviewsHintsTest::MaybeLoadHintAndLogHintCacheMatch(const GURL& url, + bool is_committed) { MaybeLoadHint(url); - previews_hints_->LogHintCacheMatch(url, is_committed, ect); + previews_hints_->LogHintCacheMatch(url, is_committed); } void PreviewsHintsTest::MaybeLoadHint(const GURL& url) { @@ -233,43 +232,99 @@ TEST_F(PreviewsHintsTest, LogHintCacheMatch) { resource_loading_hint1->set_resource_pattern("news_cruft.js"); ParseConfig(config); - base::HistogramTester histogram_tester; + // Verify histogram counts for non-matching URL host prior to commit. + { + base::HistogramTester histogram_tester; - // First verify no histogram counts for non-matching URL host. - MaybeLoadHintAndLogHintCacheMatch( - GURL("https://someotherdomain.com/news/story.html"), - false /* is_committed */, net::EFFECTIVE_CONNECTION_TYPE_3G); - MaybeLoadHintAndLogHintCacheMatch( - GURL("https://someotherdomain.com/news/story2.html"), - true /* is_committed */, net::EFFECTIVE_CONNECTION_TYPE_4G); - histogram_tester.ExpectTotalCount( - "Previews.OptimizationGuide.HintCache.HasHint.BeforeCommit", 0); - histogram_tester.ExpectTotalCount( - "Previews.OptimizationGuide.HintCache.HasHint.AtCommit", 0); - histogram_tester.ExpectTotalCount( - "Previews.OptimizationGuide.HintCache.HintLoaded.AtCommit", 0); - histogram_tester.ExpectTotalCount( - "Previews.OptimizationGuide.HintCache.PageMatch.AtCommit", 0); - - // Now verify do have histogram counts for matching URL host. - MaybeLoadHintAndLogHintCacheMatch( - GURL("https://somedomain.org/news/story.html"), false /* is_committed */, - net::EFFECTIVE_CONNECTION_TYPE_3G); - MaybeLoadHintAndLogHintCacheMatch( - GURL("https://somedomain.org/news/story2.html"), true /* is_committed */, - net::EFFECTIVE_CONNECTION_TYPE_4G); - histogram_tester.ExpectBucketCount( - "Previews.OptimizationGuide.HintCache.HasHint.BeforeCommit", - 4 /* EFFECTIVE_CONNECTION_TYPE_3G */, 1); - histogram_tester.ExpectBucketCount( - "Previews.OptimizationGuide.HintCache.HasHint.AtCommit", - 5 /* EFFECTIVE_CONNECTION_TYPE_4G */, 1); - histogram_tester.ExpectBucketCount( - "Previews.OptimizationGuide.HintCache.HostMatch.AtCommit", - 5 /* EFFECTIVE_CONNECTION_TYPE_4G */, 1); - histogram_tester.ExpectBucketCount( - "Previews.OptimizationGuide.HintCache.PageMatch.AtCommit", - 5 /* EFFECTIVE_CONNECTION_TYPE_4G */, 1); + MaybeLoadHintAndLogHintCacheMatch( + GURL("https://someotherdomain.com/news/story.html"), + false /* is_committed */); + + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HasHint.BeforeCommit", false, 1); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HasHint.AtCommit", 0); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HintLoaded.AtCommit", 0); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.PageMatch.AtCommit", 0); + } + + // Verify histogram counts for non-matching URL host after commit. + { + base::HistogramTester histogram_tester; + + MaybeLoadHintAndLogHintCacheMatch( + GURL("https://someotherdomain.com/news/story2.html"), + true /* is_committed */); + + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HasHint.BeforeCommit", 0); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HasHint.AtCommit", false, 1); + // We don't have a hint for this host so we do not expect to check if the + // hint is loaded and we have a page hint. + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HintLoaded.AtCommit", 0); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.PageMatch.AtCommit", 0); + } + + // Verify do have histogram counts for matching URL host before commit. + { + base::HistogramTester histogram_tester; + + MaybeLoadHintAndLogHintCacheMatch( + GURL("https://somedomain.org/news/story.html"), + false /* is_committed */); + + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HasHint.BeforeCommit", true, 1); + // None of the AfterCommit histograms should be recorded. + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HasHint.AtCommit", 0); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HostMatch.AtCommit", 0); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.PageMatch.AtCommit", 0); + } + + // Verify do have histogram counts for matching URL host after commit. + { + base::HistogramTester histogram_tester; + + MaybeLoadHintAndLogHintCacheMatch( + GURL("https://somedomain.org/news/story2.html"), + true /* is_committed */); + + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HasHint.BeforeCommit", 0); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HasHint.AtCommit", true, 1); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HostMatch.AtCommit", true, 1); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.PageMatch.AtCommit", true, 1); + } + + // Verify do have histogram counts for matching URL host but no matching page + // hint after commit. + { + base::HistogramTester histogram_tester; + + MaybeLoadHintAndLogHintCacheMatch( + GURL("https://somedomain.org/nopagehint/story2.html"), + true /* is_committed */); + + histogram_tester.ExpectTotalCount( + "OptimizationGuide.HintCache.HasHint.BeforeCommit", 0); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HasHint.AtCommit", true, 1); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.HostMatch.AtCommit", true, 1); + histogram_tester.ExpectUniqueSample( + "OptimizationGuide.HintCache.PageMatch.AtCommit", false, 1); + } } TEST_F(PreviewsHintsTest, IsBlacklistedReturnsTrueIfNoBloomFilter) { @@ -292,7 +347,36 @@ TEST_F(PreviewsHintsTest, IsBlacklistedReturnsTrueIfNoBloomFilter) { GURL("https://nonblack.com"), PreviewsType::LITE_PAGE_REDIRECT)); } -TEST_F(PreviewsHintsTest, IsBlacklisted) { +TEST_F(PreviewsHintsTest, IsBlacklisted_BloomFilterAndRegexps) { + base::test::ScopedFeatureList scoped_list; + scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); + + optimization_guide::BloomFilter blacklist_bloom_filter( + kBlackBlacklistBloomFilterNumHashFunctions, + kBlackBlacklistBloomFilterNumBits); + PopulateBlackBlacklistBloomFilter(&blacklist_bloom_filter); + + optimization_guide::proto::Configuration config; + AddBlacklistBloomFilterToConfig(blacklist_bloom_filter, + kBlackBlacklistBloomFilterNumHashFunctions, + kBlackBlacklistBloomFilterNumBits, &config); + AddRegexpsFilterToConfig(&config, {"blackpath"}); + ParseConfig(config); + + EXPECT_TRUE(HasLitePageRedirectBlacklist()); + EXPECT_FALSE(previews_hints()->IsBlacklisted(GURL("https://black.com/path"), + PreviewsType::OFFLINE)); + EXPECT_TRUE(previews_hints()->IsBlacklisted( + GURL("https://black.com/path"), PreviewsType::LITE_PAGE_REDIRECT)); + EXPECT_TRUE(previews_hints()->IsBlacklisted( + GURL("https://joe.black.com/path"), PreviewsType::LITE_PAGE_REDIRECT)); + EXPECT_FALSE(previews_hints()->IsBlacklisted( + GURL("https://ok-host.com/"), PreviewsType::LITE_PAGE_REDIRECT)); + EXPECT_TRUE(previews_hints()->IsBlacklisted( + GURL("https://ok-host.com/blackpath"), PreviewsType::LITE_PAGE_REDIRECT)); +} + +TEST_F(PreviewsHintsTest, IsBlacklisted_BloomFilter) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); @@ -318,6 +402,47 @@ TEST_F(PreviewsHintsTest, IsBlacklisted) { GURL("https://nonblack.com"), PreviewsType::LITE_PAGE_REDIRECT)); } +TEST_F(PreviewsHintsTest, IsBlacklisted_Regexps) { + base::test::ScopedFeatureList scoped_list; + scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); + + optimization_guide::proto::Configuration config; + AddRegexpsFilterToConfig(&config, {"black\\.com"}); + ParseConfig(config); + + EXPECT_TRUE(HasLitePageRedirectBlacklist()); + EXPECT_FALSE(previews_hints()->IsBlacklisted(GURL("https://black.com/path"), + PreviewsType::OFFLINE)); + EXPECT_TRUE(previews_hints()->IsBlacklisted( + GURL("https://black.com/path"), PreviewsType::LITE_PAGE_REDIRECT)); + EXPECT_TRUE(previews_hints()->IsBlacklisted( + GURL("https://joe.black.com/path"), PreviewsType::LITE_PAGE_REDIRECT)); + EXPECT_FALSE(previews_hints()->IsBlacklisted( + GURL("https://blackish.com"), PreviewsType::LITE_PAGE_REDIRECT)); +} + +TEST_F(PreviewsHintsTest, ParseConfigWithBadRegexp) { + base::HistogramTester histogram_tester; + base::test::ScopedFeatureList scoped_list; + scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); + + optimization_guide::proto::Configuration config; + AddRegexpsFilterToConfig(&config, {"["}); + ParseConfig(config); + + histogram_tester.ExpectBucketCount( + "OptimizationGuide.OptimizationFilterStatus.LitePageRedirect", + optimization_guide::OptimizationFilterStatus::kFoundServerBlacklistConfig, + 1); + histogram_tester.ExpectBucketCount( + "OptimizationGuide.OptimizationFilterStatus.LitePageRedirect", + optimization_guide::OptimizationFilterStatus::kInvalidRegexp, 1); + histogram_tester.ExpectTotalCount( + "OptimizationGuide.OptimizationFilterStatus.LitePageRedirect", 2); + + EXPECT_FALSE(HasLitePageRedirectBlacklist()); +} + TEST_F(PreviewsHintsTest, ParseConfigWithInsufficientConfigDetails) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; @@ -358,9 +483,7 @@ TEST_F(PreviewsHintsTest, ParseConfigWithTooLargeBlacklist) { scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); int too_many_bits = - previews::params::LitePageRedirectPreviewMaxServerBlacklistByteSize() * - 8 + - 1; + optimization_guide::features::MaxServerBloomFilterByteSize() * 8 + 1; optimization_guide::BloomFilter blacklist_bloom_filter( kBlackBlacklistBloomFilterNumHashFunctions, too_many_bits); diff --git a/chromium/components/previews/content/previews_optimization_guide.h b/chromium/components/previews/content/previews_optimization_guide.h index 403f94be302..40c138cf24a 100644 --- a/chromium/components/previews/content/previews_optimization_guide.h +++ b/chromium/components/previews/content/previews_optimization_guide.h @@ -5,222 +5,62 @@ #ifndef COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_H_ #define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_H_ -#include <memory> #include <string> #include <vector> #include "base/callback_forward.h" -#include "base/macros.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_refptr.h" -#include "base/memory/weak_ptr.h" -#include "base/sequenced_task_runner.h" -#include "base/single_thread_task_runner.h" -#include "base/time/clock.h" -#include "base/timer/timer.h" -#include "components/optimization_guide/hint_cache.h" -#include "components/optimization_guide/optimization_guide_service_observer.h" #include "components/previews/core/previews_experiments.h" -#include "url/gurl.h" -class PrefService; +namespace content { +class NavigationHandle; +} // namespace content -namespace base { -class FilePath; -} // namespace base -namespace network { -class SharedURLLoaderFactory; -} // namespace network -namespace optimization_guide { -struct HintsComponentInfo; -class HintsFetcher; -class OptimizationGuideService; -class TopHostProvider; -namespace proto { -class Hint; -} // namespace proto -} // namespace optimization_guide +class GURL; namespace previews { - -class PreviewsHints; class PreviewsUserData; // A Previews optimization guide that makes decisions guided by hints received // from the OptimizationGuideService. -class PreviewsOptimizationGuide - : public optimization_guide::OptimizationGuideServiceObserver { +class PreviewsOptimizationGuide { public: - // The embedder guarantees |optimization_guide_service| outlives |this|. - // The embedder guarantees that |previews_top_host_provider_| outlives |this|. - PreviewsOptimizationGuide( - optimization_guide::OptimizationGuideService* optimization_guide_service, - const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, - const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, - const base::FilePath& profile_path, - PrefService* pref_service, - leveldb_proto::ProtoDatabaseProvider* database_provider, - optimization_guide::TopHostProvider* top_host_provider, - scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); - - ~PreviewsOptimizationGuide() override; - - // Returns whether |type| is whitelisted for |url|. If so |out_ect_threshold| - // provides the maximum effective connection type to trigger the preview for. - // |previews_data| can be modified (for further details provided by hints). - // Virtual so it can be mocked in tests. - virtual bool IsWhitelisted( - PreviewsUserData* previews_data, - const GURL& url, - PreviewsType type, - net::EffectiveConnectionType* out_ect_threshold) const; - - // Returns whether |type| is blacklisted for |url|. - // Virtual so it can be mocked in tests. - virtual bool IsBlacklisted(const GURL& url, PreviewsType type) const; - - // Returns whether |request| may have associated optimization hints + PreviewsOptimizationGuide() {} + virtual ~PreviewsOptimizationGuide() {} + + // Returns whether the optimization guide is ready to receive requests. + virtual bool IsReady() const = 0; + + // Returns whether |type| is allowed for the URL associated with + // |navigation_handle| and the current conditions. |previews_data| can be + // modified (for further details provided by hints). Note that this will + // return false if a hint is needed to determine if the preview is allowed but + // we do not have everything we need to make that determination in memory. + virtual bool CanApplyPreview(PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) = 0; + + // Returns whether |navigation_handle| may have associated optimization hints // (specifically, PageHints). If so, but the hints are not available // synchronously, this method will request that they be loaded (from disk or // network). The callback is run after the hint is loaded and can be used as // a signal during tests. - bool MaybeLoadOptimizationHints(const GURL& url, base::OnceClosure callback); + virtual bool MaybeLoadOptimizationHints( + content::NavigationHandle* navigation_handle, + base::OnceClosure callback) = 0; // Whether |url| has loaded resource loading hints and, if it does, populates // |out_resource_patterns_to_block| with the resource patterns to block. - bool GetResourceLoadingHints( + virtual bool GetResourceLoadingHints( const GURL& url, - std::vector<std::string>* out_resource_patterns_to_block) const; + std::vector<std::string>* out_resource_patterns_to_block) = 0; // Logs UMA for whether the OptimizationGuide HintCache has a matching Hint // guidance for |url|. This is useful for measuring the effectiveness of the // page hints provided by Cacao. - void LogHintCacheMatch(const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect) const; - - // optimization_guide::OptimizationGuideServiceObserver implementation: - // Called by OptimizationGuideService when a new component is available for - // processing. - void OnHintsComponentAvailable( - const optimization_guide::HintsComponentInfo& info) override; - - PreviewsHints* GetHintsForTesting() { return hints_.get(); } - - // |next_update_closure| is called the next time OnHintsComponentAvailable is - // called and the corresponding hints have been updated. - void ListenForNextUpdateForTesting(base::OnceClosure next_update_closure); - - // Updates the hints to the latest hints sent by the Component Updater. - // |update_closure| is called once the hints are updated. Public for testing. - void UpdateHints(base::OnceClosure update_closure, - std::unique_ptr<PreviewsHints> hints); - - // Clear all fetched hints known to |this|, including those persisted on disk. - void ClearFetchedHints(); - - bool has_hints() const { return !!hints_; } - - // Set |time_clock_| for testing. - void SetTimeClockForTesting(const base::Clock* time_clock); - - // Set |hints_fetcher_| for testing. - void SetHintsFetcherForTesting( - std::unique_ptr<optimization_guide::HintsFetcher> hints_fetcher); - - optimization_guide::HintsFetcher* GetHintsFetcherForTesting(); - - // Called when the hints store is initialized to determine when hints - // should be fetched and schedules the |hints_fetch_timer_| to fire based on: - // 1. The update time for the fetched hints in the store and - // 2. The last time a fetch attempt was made, |last_fetch_attempt_|. - void ScheduleHintsFetch(); - - protected: - // Callback executed after remote hints have been fetched and returned from - // the remote Optimization Guide Service. At this point, the hints response - // is ready to be processed and stored for use. Virtual to be mocked in - // testing. - virtual void OnHintsFetched( - base::Optional< - std::unique_ptr<optimization_guide::proto::GetHintsResponse>> - get_hints_response); - - // Callback executed after the Hints have been successfully stored in the - // store. Virtual to be mocked in tests. - virtual void OnFetchedHintsStored(); - - private: - // Callback run after the hint cache is fully initialized. At this point, the - // PreviewsOptimizationGuide is ready to process components from the - // OptimizationGuideService and registers as an observer with it. - void OnHintCacheInitialized(); - - // Called when the hints have been fully updated with the latest hints from - // the Component Updater. This is used as a signal during tests. - // |update_closure| is called immediately if not null. - void OnHintsUpdated(base::OnceClosure update_closure); - - // Callback when a hint is loaded. - void OnLoadedHint(base::OnceClosure callback, - const GURL& document_url, - const optimization_guide::proto::Hint* loaded_hint) const; - - // Method to request new hints for user's sites based on - // engagement scores using |hints_fetcher_|. - void FetchHints(); - - // Return the time when a hints fetch request was last attempted. - base::Time GetLastHintsFetchAttemptTime() const; - - // Set the time when a hints fetch was last attempted to |last_attempt_time|. - void SetLastHintsFetchAttemptTime(base::Time last_attempt_time); - - // The OptimizationGuideService that this guide is listening to. Not owned. - optimization_guide::OptimizationGuideService* optimization_guide_service_; - - // Runner for UI thread tasks. - scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; - - // Background thread where hints processing should be performed. - scoped_refptr<base::SequencedTaskRunner> background_task_runner_; - - // The hint cache used by PreviewsHints. It is owned by - // PreviewsOptimizationGuide so that the existing hint cache can be reused on - // component updates. Otherwise, a new cache and store would need to be - // created during each component update. - std::unique_ptr<optimization_guide::HintCache> hint_cache_; - - // The current hints used for this optimization guide. - std::unique_ptr<PreviewsHints> hints_; - - // Used in testing to subscribe to an update event in this class. - base::OnceClosure next_update_closure_; - - // HintsFetcher handles making the request for updated hints from the remote - // Optimization Guide Service. - std::unique_ptr<optimization_guide::HintsFetcher> hints_fetcher_; - - // Timer to schedule when to fetch hints from the remote Optimization Guide - // Service. - base::OneShotTimer hints_fetch_timer_; - - // TopHostProvider that this guide can query. Not owned. - optimization_guide::TopHostProvider* top_host_provider_ = nullptr; - - // Clock used for scheduling the |hints_fetch_timer_|. - const base::Clock* time_clock_; - - // A reference to the PrefService for this profile. Not owned. - PrefService* pref_service_ = nullptr; - - // Used for fetching Hints by the Hints Fetcher. - scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; - - // Used to get |weak_ptr_| to self on the UI thread. - base::WeakPtrFactory<PreviewsOptimizationGuide> ui_weak_ptr_factory_{this}; + virtual void LogHintCacheMatch(const GURL& url, bool is_committed) const = 0; - DISALLOW_COPY_AND_ASSIGN(PreviewsOptimizationGuide); + // Clears all fetched hints from its store. + virtual void ClearFetchedHints() = 0; }; } // namespace previews diff --git a/chromium/components/previews/content/previews_optimization_guide_decider.cc b/chromium/components/previews/content/previews_optimization_guide_decider.cc new file mode 100644 index 00000000000..fc446b83540 --- /dev/null +++ b/chromium/components/previews/content/previews_optimization_guide_decider.cc @@ -0,0 +1,211 @@ +// Copyright 2019 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 "components/previews/content/previews_optimization_guide_decider.h" + +#include <utility> + +#include "components/optimization_guide/hints_processing_util.h" +#include "components/optimization_guide/optimization_guide_decider.h" +#include "components/optimization_guide/proto/hints.pb.h" +#include "components/previews/content/previews_user_data.h" +#include "components/previews/core/previews_experiments.h" +#include "components/previews/core/previews_switches.h" +#include "content/public/browser/navigation_handle.h" + +namespace previews { + +namespace { + +// The default max size of the cache holding resource loading hints by URL. +size_t kDefaultMaxResourceLoadingHintsCacheSize = 10; + +// Returns base::nullopt if |previews_type| can't be converted. +base::Optional<optimization_guide::proto::OptimizationType> +ConvertPreviewsTypeToOptimizationType(PreviewsType previews_type) { + switch (previews_type) { + case PreviewsType::NONE: + return optimization_guide::proto::OPTIMIZATION_NONE; + case PreviewsType::NOSCRIPT: + return optimization_guide::proto::NOSCRIPT; + case PreviewsType::UNSPECIFIED: + return optimization_guide::proto::TYPE_UNSPECIFIED; + case PreviewsType::RESOURCE_LOADING_HINTS: + return optimization_guide::proto::RESOURCE_LOADING; + case PreviewsType::LITE_PAGE_REDIRECT: + return optimization_guide::proto::LITE_PAGE_REDIRECT; + case PreviewsType::DEFER_ALL_SCRIPT: + return optimization_guide::proto::DEFER_ALL_SCRIPT; + default: + return base::nullopt; + } +} + +// Returns the optimization types to register with the Optimization Guide +// Decider based on which Previews are enabled for the session. +std::unordered_set<optimization_guide::proto::OptimizationType> +GetOptimizationTypesToRegister() { + std::unordered_set<optimization_guide::proto::OptimizationType> + optimization_types; + + if (params::IsNoScriptPreviewsEnabled()) + optimization_types.insert(optimization_guide::proto::NOSCRIPT); + if (params::IsResourceLoadingHintsEnabled()) + optimization_types.insert(optimization_guide::proto::RESOURCE_LOADING); + if (params::IsLitePageServerPreviewsEnabled()) + optimization_types.insert(optimization_guide::proto::LITE_PAGE_REDIRECT); + if (params::IsDeferAllScriptPreviewsEnabled()) + optimization_types.insert(optimization_guide::proto::DEFER_ALL_SCRIPT); + + return optimization_types; +} + +// Parses |resource_loading_hints| and returns a vector of resource patterns +// that can be blocked. +std::vector<std::string> GetResourcePatternsToBlock( + const google::protobuf::RepeatedPtrField< + optimization_guide::proto::ResourceLoadingHint>& + resource_loading_hints) { + std::vector<std::string> resource_patterns_to_block; + for (const auto& resource_loading_hint : resource_loading_hints) { + if (!resource_loading_hint.resource_pattern().empty() && + resource_loading_hint.loading_optimization_type() == + optimization_guide::proto::LOADING_BLOCK_RESOURCE) { + resource_patterns_to_block.push_back( + resource_loading_hint.resource_pattern()); + } + } + return resource_patterns_to_block; +} + +} // namespace + +PreviewsOptimizationGuideDecider::PreviewsOptimizationGuideDecider( + optimization_guide::OptimizationGuideDecider* optimization_guide_decider) + : optimization_guide_decider_(optimization_guide_decider), + resource_loading_hints_cache_(kDefaultMaxResourceLoadingHintsCacheSize), + registered_optimization_types_(GetOptimizationTypesToRegister()) { + DCHECK(optimization_guide_decider_); + + optimization_guide_decider_->RegisterOptimizationTypes( + std::vector<optimization_guide::proto::OptimizationType>( + registered_optimization_types_.begin(), + registered_optimization_types_.end())); +} + +PreviewsOptimizationGuideDecider::~PreviewsOptimizationGuideDecider() = default; + +bool PreviewsOptimizationGuideDecider::IsReady() const { + // TODO(crbug/969558): Figure out what to do here. The behavior in the old + // version was to check if the hints were initialized, but that detail is + // opaque to us at this level. + return true; +} + +bool PreviewsOptimizationGuideDecider::CanApplyPreview( + PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) { + // See if we need to bypass the lite page redirect blacklist. + if (type == PreviewsType::LITE_PAGE_REDIRECT && + params::LitePageRedirectPreviewIgnoresOptimizationGuideFilter()) { + return true; + } + + base::Optional<optimization_guide::proto::OptimizationType> + optimization_type = ConvertPreviewsTypeToOptimizationType(type); + if (!optimization_type.has_value()) + return false; + + // See if we can apply the optimization. Note that + // |optimization_guide_decider_| also ensures that the current browser + // conditions match a painful page load as a prerequisite for returning true. + optimization_guide::OptimizationMetadata optimization_metadata; + optimization_guide::OptimizationGuideDecision decision = + optimization_guide_decider_->CanApplyOptimization( + navigation_handle, + optimization_guide::OptimizationTarget::kPainfulPageLoad, + *optimization_type, &optimization_metadata); + + // Return false if we are even unsure if we can apply the optimization (i.e. + // hint not loaded yet or just not applicable). + if (decision != optimization_guide::OptimizationGuideDecision::kTrue) + return false; + + // If we can apply it, populate information from metadata. + if (previews_data && + optimization_metadata.previews_metadata.has_inflation_percent()) { + previews_data->set_data_savings_inflation_percent( + optimization_metadata.previews_metadata.inflation_percent()); + } + if (optimization_metadata.previews_metadata.resource_loading_hints_size() > + 0) { + resource_loading_hints_cache_.Put( + navigation_handle->GetURL(), + GetResourcePatternsToBlock( + optimization_metadata.previews_metadata.resource_loading_hints())); + } + + return true; +} + +bool PreviewsOptimizationGuideDecider::MaybeLoadOptimizationHints( + content::NavigationHandle* navigation_handle, + base::OnceClosure callback) { + // We use this method as a way of enforcing some sort of preview ordering. + // Thus, we check if we can potentially apply any of the client-side previews, + // and if any of them potentially can be applied, then we return true. + const std::vector<optimization_guide::proto::OptimizationType> + optimization_types_to_check = { + optimization_guide::proto::DEFER_ALL_SCRIPT, + optimization_guide::proto::RESOURCE_LOADING, + optimization_guide::proto::NOSCRIPT}; + + bool might_have_hint = false; + for (const auto optimization_type : optimization_types_to_check) { + // Don't check for the hint if the optimization type is not enabled. + if (registered_optimization_types_.find(optimization_type) == + registered_optimization_types_.end()) { + continue; + } + + if (optimization_guide_decider_->CanApplyOptimization( + navigation_handle, + optimization_guide::OptimizationTarget::kPainfulPageLoad, + optimization_type, + /*optimization_metadata=*/nullptr) != + optimization_guide::OptimizationGuideDecision::kFalse) { + might_have_hint = true; + break; + } + } + + std::move(callback).Run(); + return might_have_hint; +} + +bool PreviewsOptimizationGuideDecider::GetResourceLoadingHints( + const GURL& url, + std::vector<std::string>* out_resource_patterns_to_block) { + auto rlh_it = resource_loading_hints_cache_.Get(url); + if (rlh_it == resource_loading_hints_cache_.end()) + return false; + + *out_resource_patterns_to_block = rlh_it->second; + return true; +} + +void PreviewsOptimizationGuideDecider::LogHintCacheMatch( + const GURL& url, + bool is_committed) const { + // Intentionally unimplemented: This is handled directly by the + // OptimizationGuideDecider. +} + +void PreviewsOptimizationGuideDecider::ClearFetchedHints() { + // Intentionally unimplemented: This is handled directly by the + // OptimizationGuideDecider. +} + +} // namespace previews diff --git a/chromium/components/previews/content/previews_optimization_guide_decider.h b/chromium/components/previews/content/previews_optimization_guide_decider.h new file mode 100644 index 00000000000..4fccbbd39ff --- /dev/null +++ b/chromium/components/previews/content/previews_optimization_guide_decider.h @@ -0,0 +1,62 @@ +// Copyright 2019 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. + +#ifndef COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_DECIDER_H_ +#define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_DECIDER_H_ + +#include "components/previews/content/previews_optimization_guide.h" + +#include <string> +#include <unordered_set> +#include <vector> + +#include "base/containers/mru_cache.h" +#include "components/optimization_guide/proto/hints.pb.h" + +namespace optimization_guide { +class OptimizationGuideDecider; +} // namespace optimization_guide + +namespace previews { + +// An |optimization_guide::OptimizationDecider| backed implementation of +// |PreviewsOptimizationGuide|. +class PreviewsOptimizationGuideDecider : public PreviewsOptimizationGuide { + public: + explicit PreviewsOptimizationGuideDecider( + optimization_guide::OptimizationGuideDecider* optimization_guide_decider); + ~PreviewsOptimizationGuideDecider() override; + + // PreviewsOptimizationGuide implementation: + bool IsReady() const override; + bool CanApplyPreview(PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) override; + bool MaybeLoadOptimizationHints(content::NavigationHandle* navigation_handle, + base::OnceClosure callback) override; + bool GetResourceLoadingHints( + const GURL& url, + std::vector<std::string>* out_resource_patterns_to_block) override; + void LogHintCacheMatch(const GURL& url, bool is_committed) const override; + void ClearFetchedHints() override; + + private: + // The Optimization Guide Decider to consult for whether an optimization can + // be applied. Not owned. + optimization_guide::OptimizationGuideDecider* optimization_guide_decider_; + + // An in-memory cache of resource loading hints keyed by the URL. This allows + // us to avoid making too many calls to |optimization_guide_decider_|. + base::MRUCache<GURL, std::vector<std::string>> resource_loading_hints_cache_; + + // The optimization types registered with |optimization_guide_decider_|. + const std::unordered_set<optimization_guide::proto::OptimizationType> + registered_optimization_types_; + + DISALLOW_COPY_AND_ASSIGN(PreviewsOptimizationGuideDecider); +}; + +} // namespace previews + +#endif // COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_DECIDER_H_ diff --git a/chromium/components/previews/content/previews_optimization_guide_decider_unittest.cc b/chromium/components/previews/content/previews_optimization_guide_decider_unittest.cc new file mode 100644 index 00000000000..2dc85bcb282 --- /dev/null +++ b/chromium/components/previews/content/previews_optimization_guide_decider_unittest.cc @@ -0,0 +1,355 @@ +// Copyright 2019 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 "components/previews/content/previews_optimization_guide_decider.h" + +#include <map> +#include <memory> +#include <string> +#include <tuple> +#include <unordered_set> +#include <vector> + +#include "base/command_line.h" +#include "base/test/scoped_feature_list.h" +#include "components/optimization_guide/optimization_guide_decider.h" +#include "components/previews/content/previews_user_data.h" +#include "components/previews/core/previews_features.h" +#include "components/previews/core/previews_switches.h" +#include "content/public/browser/navigation_handle.h" +#include "content/public/test/mock_navigation_handle.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace previews { + +class TestOptimizationGuideDecider + : public optimization_guide::OptimizationGuideDecider { + public: + TestOptimizationGuideDecider() = default; + ~TestOptimizationGuideDecider() override = default; + + void RegisterOptimizationTypes( + std::vector<optimization_guide::proto::OptimizationType> + optimization_types) override { + registered_optimization_types_ = + std::unordered_set<optimization_guide::proto::OptimizationType>( + optimization_types.begin(), optimization_types.end()); + } + + // Returns the optimization types registered with the Optimization Guide + // Decider. + std::unordered_set<optimization_guide::proto::OptimizationType> + registered_optimization_types() { + return registered_optimization_types_; + } + + optimization_guide::OptimizationGuideDecision CanApplyOptimization( + content::NavigationHandle* navigation_handle, + optimization_guide::OptimizationTarget optimization_target, + optimization_guide::proto::OptimizationType optimization_type, + optimization_guide::OptimizationMetadata* optimization_metadata) + override { + // Previews should always call this method with painful page load as the + // target. + DCHECK(optimization_target == + optimization_guide::OptimizationTarget::kPainfulPageLoad); + + auto response_iter = responses_.find( + std::make_tuple(navigation_handle->GetURL(), optimization_type)); + if (response_iter == responses_.end()) + return optimization_guide::OptimizationGuideDecision::kFalse; + + auto response = response_iter->second; + if (optimization_metadata) + *optimization_metadata = std::get<1>(response); + + return std::get<0>(response); + } + + void SetResponses( + std::map<std::tuple<GURL, optimization_guide::proto::OptimizationType>, + std::tuple<optimization_guide::OptimizationGuideDecision, + optimization_guide::OptimizationMetadata>> + responses) { + responses_ = responses; + } + + private: + // The optimization types that were registered with the Optimization Guide + // Decider. + std::unordered_set<optimization_guide::proto::OptimizationType> + registered_optimization_types_; + + std::map<std::tuple<GURL, optimization_guide::proto::OptimizationType>, + std::tuple<optimization_guide::OptimizationGuideDecision, + optimization_guide::OptimizationMetadata>> + responses_; + + DISALLOW_COPY_AND_ASSIGN(TestOptimizationGuideDecider); +}; + +class PreviewsOptimizationGuideDeciderTest : public testing::Test { + public: + void SetUp() override { + optimization_guide_decider_.reset(new TestOptimizationGuideDecider); + } + + TestOptimizationGuideDecider* optimization_guide_decider() { + return optimization_guide_decider_.get(); + } + + void SeedOptimizationGuideDeciderWithDefaultResponses() { + optimization_guide::OptimizationMetadata default_metadata; + + optimization_guide::OptimizationMetadata rlh_metadata; + rlh_metadata.previews_metadata.set_inflation_percent(123); + rlh_metadata.previews_metadata.set_max_ect_trigger( + optimization_guide::proto::EFFECTIVE_CONNECTION_TYPE_3G); + auto* rlh1 = rlh_metadata.previews_metadata.add_resource_loading_hints(); + rlh1->set_resource_pattern("resource1"); + rlh1->set_loading_optimization_type( + optimization_guide::proto::LOADING_BLOCK_RESOURCE); + auto* rlh2 = rlh_metadata.previews_metadata.add_resource_loading_hints(); + rlh2->set_resource_pattern("resource2"); + rlh2->set_loading_optimization_type( + optimization_guide::proto::LOADING_BLOCK_RESOURCE); + rlh_metadata.previews_metadata.add_resource_loading_hints() + ->set_resource_pattern("shouldbeskipped"); + // Should also be skipped since the resource pattern is empty. + rlh_metadata.previews_metadata.add_resource_loading_hints() + ->set_loading_optimization_type( + optimization_guide::proto::LOADING_BLOCK_RESOURCE); + + std::map<std::tuple<GURL, optimization_guide::proto::OptimizationType>, + std::tuple<optimization_guide::OptimizationGuideDecision, + optimization_guide::OptimizationMetadata>> + responses = { + {std::make_tuple(blacklisted_lpr_url(), + optimization_guide::proto::LITE_PAGE_REDIRECT), + std::make_tuple( + optimization_guide::OptimizationGuideDecision::kFalse, + default_metadata)}, + {std::make_tuple(hint_not_loaded_url(), + optimization_guide::proto::NOSCRIPT), + std::make_tuple( + optimization_guide::OptimizationGuideDecision::kUnknown, + default_metadata)}, + {std::make_tuple(resource_loading_hints_url(), + optimization_guide::proto::RESOURCE_LOADING), + std::make_tuple( + optimization_guide::OptimizationGuideDecision::kTrue, + rlh_metadata)}, + }; + + optimization_guide_decider()->SetResponses(responses); + } + + GURL blacklisted_lpr_url() { return GURL("https://blacklistedlpr.com/123"); } + + GURL hint_not_loaded_url() { return GURL("https://hintnotloaded.com/123"); } + + GURL resource_loading_hints_url() { + return GURL("https://hasresourceloadinghints.com/123"); + } + + private: + std::unique_ptr<TestOptimizationGuideDecider> optimization_guide_decider_; +}; + +TEST_F(PreviewsOptimizationGuideDeciderTest, + InitializationRegistersCorrectOptimizationTypes) { + base::test::ScopedFeatureList scoped_feature_list; + scoped_feature_list.InitWithFeatures( + {previews::features::kLitePageServerPreviews, + previews::features::kDeferAllScriptPreviews, + previews::features::kNoScriptPreviews, + previews::features::kResourceLoadingHints}, + {}); + + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + + std::unordered_set<optimization_guide::proto::OptimizationType> + registered_optimization_types = + optimization_guide_decider()->registered_optimization_types(); + EXPECT_EQ(4u, registered_optimization_types.size()); + // We expect for LITE_PAGE_REDIRECT, DEFER_ALL_SCRIPT, NOSCRIPT, and + // RESOURCE_LOADING to be registered. + EXPECT_TRUE(registered_optimization_types.find( + optimization_guide::proto::LITE_PAGE_REDIRECT) != + registered_optimization_types.end()); + EXPECT_TRUE(registered_optimization_types.find( + optimization_guide::proto::DEFER_ALL_SCRIPT) != + registered_optimization_types.end()); + EXPECT_TRUE( + registered_optimization_types.find(optimization_guide::proto::NOSCRIPT) != + registered_optimization_types.end()); + EXPECT_TRUE(registered_optimization_types.find( + optimization_guide::proto::RESOURCE_LOADING) != + registered_optimization_types.end()); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + InitializationRegistersOnlyEnabledTypes) { + base::test::ScopedFeatureList scoped_feature_list; + scoped_feature_list.InitWithFeatures( + {previews::features::kLitePageServerPreviews}, + {previews::features::kDeferAllScriptPreviews, + previews::features::kNoScriptPreviews, + previews::features::kResourceLoadingHints}); + + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + + std::unordered_set<optimization_guide::proto::OptimizationType> + registered_optimization_types = + optimization_guide_decider()->registered_optimization_types(); + EXPECT_EQ(1u, registered_optimization_types.size()); + // We only expect for LITE_PAGE_REDIRECT to be registered, as it's the only + // type that is enabled. + EXPECT_TRUE(registered_optimization_types.find( + optimization_guide::proto::LITE_PAGE_REDIRECT) != + registered_optimization_types.end()); + EXPECT_EQ(registered_optimization_types.find( + optimization_guide::proto::DEFER_ALL_SCRIPT), + registered_optimization_types.end()); + EXPECT_EQ( + registered_optimization_types.find(optimization_guide::proto::NOSCRIPT), + registered_optimization_types.end()); + EXPECT_EQ(registered_optimization_types.find( + optimization_guide::proto::RESOURCE_LOADING), + registered_optimization_types.end()); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + PreviewsTypeWithoutCorrespondingOptimizationTypeReturnsFalse) { + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://whatever.com/")); + + EXPECT_FALSE(decider.CanApplyPreview( + /*previews_data=*/nullptr, &navigation_handle, + PreviewsType::DEPRECATED_LOFI)); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + LitePageRedirectConvertsToOptimizationTypeCorrectly) { + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(blacklisted_lpr_url()); + + EXPECT_FALSE(decider.CanApplyPreview( + /*previews_data=*/nullptr, &navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + LitePageRedirectSwitchOverridesDecisionForCanApplyPreview) { + base::CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kIgnoreLitePageRedirectOptimizationBlacklist); + + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(blacklisted_lpr_url()); + + EXPECT_TRUE(decider.CanApplyPreview( + /*previews_data=*/nullptr, &navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + CanApplyPreviewPopulatesResourceLoadingHintsCache) { + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + // Make sure resource loading hints not cached. + std::vector<std::string> resource_loading_hints; + EXPECT_FALSE(decider.GetResourceLoadingHints(resource_loading_hints_url(), + &resource_loading_hints)); + EXPECT_TRUE(resource_loading_hints.empty()); + + // Check if we can apply it and metadata is properly applied. + PreviewsUserData data(/*page_id=*/1); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(resource_loading_hints_url()); + EXPECT_TRUE(decider.CanApplyPreview(&data, &navigation_handle, + PreviewsType::RESOURCE_LOADING_HINTS)); + EXPECT_EQ(123, data.data_savings_inflation_percent()); + + // Make sure resource loading hints are validated and cached. + EXPECT_TRUE(decider.GetResourceLoadingHints(resource_loading_hints_url(), + &resource_loading_hints)); + EXPECT_EQ(2u, resource_loading_hints.size()); + EXPECT_EQ("resource1", resource_loading_hints[0]); + EXPECT_EQ("resource2", resource_loading_hints[1]); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + CanApplyPreviewWithUnknownDecisionReturnsFalse) { + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(hint_not_loaded_url()); + + EXPECT_FALSE(decider.CanApplyPreview( + /*previews_data=*/nullptr, &navigation_handle, PreviewsType::NOSCRIPT)); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + MaybeLoadOptimizationHintsWithAtLeastOneNonFalseDecisionReturnsTrue) { + base::test::ScopedFeatureList scoped_feature_list; + scoped_feature_list.InitWithFeatures( + {previews::features::kLitePageServerPreviews, + previews::features::kDeferAllScriptPreviews, + previews::features::kNoScriptPreviews, + previews::features::kResourceLoadingHints}, + {}); + + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(hint_not_loaded_url()); + + EXPECT_TRUE(decider.MaybeLoadOptimizationHints(&navigation_handle, + base::DoNothing())); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + MaybeLoadOptimizationHintsReturnsFalseIfNoClientSidePreviewsEnabled) { + base::test::ScopedFeatureList scoped_feature_list; + scoped_feature_list.InitWithFeatures( + {previews::features::kLitePageServerPreviews}, + {previews::features::kDeferAllScriptPreviews, + previews::features::kNoScriptPreviews, + previews::features::kResourceLoadingHints}); + + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(hint_not_loaded_url()); + + EXPECT_FALSE(decider.MaybeLoadOptimizationHints(&navigation_handle, + base::DoNothing())); +} + +TEST_F(PreviewsOptimizationGuideDeciderTest, + MaybeLoadOptimizationHintsWithAllFalseDecisionsReturnsFalse) { + PreviewsOptimizationGuideDecider decider(optimization_guide_decider()); + SeedOptimizationGuideDeciderWithDefaultResponses(); + + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://nohints.com")); + + EXPECT_FALSE(decider.MaybeLoadOptimizationHints(&navigation_handle, + base::DoNothing())); +} + +} // namespace previews diff --git a/chromium/components/previews/content/previews_optimization_guide.cc b/chromium/components/previews/content/previews_optimization_guide_impl.cc index 49a8a9edbc2..49e7cd1eaab 100644 --- a/chromium/components/previews/content/previews_optimization_guide.cc +++ b/chromium/components/previews/content/previews_optimization_guide_impl.cc @@ -2,7 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/previews/content/previews_optimization_guide.h" +#include "components/previews/content/previews_optimization_guide_impl.h" + +#include <utility> #include "base/bind.h" #include "base/command_line.h" @@ -18,6 +20,7 @@ #include "components/optimization_guide/hints_component_info.h" #include "components/optimization_guide/hints_component_util.h" #include "components/optimization_guide/hints_fetcher.h" +#include "components/optimization_guide/optimization_guide_constants.h" #include "components/optimization_guide/optimization_guide_features.h" #include "components/optimization_guide/optimization_guide_prefs.h" #include "components/optimization_guide/optimization_guide_service.h" @@ -27,8 +30,8 @@ #include "components/prefs/pref_service.h" #include "components/previews/content/previews_hints.h" #include "components/previews/content/previews_user_data.h" -#include "components/previews/core/previews_constants.h" #include "components/previews/core/previews_switches.h" +#include "content/public/browser/navigation_handle.h" #include "services/network/public/cpp/shared_url_loader_factory.h" #include "url/gurl.h" @@ -61,7 +64,7 @@ base::TimeDelta RandomFetchDelay() { } // namespace -PreviewsOptimizationGuide::PreviewsOptimizationGuide( +PreviewsOptimizationGuideImpl::PreviewsOptimizationGuideImpl( optimization_guide::OptimizationGuideService* optimization_guide_service, const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, @@ -69,7 +72,8 @@ PreviewsOptimizationGuide::PreviewsOptimizationGuide( PrefService* pref_service, leveldb_proto::ProtoDatabaseProvider* database_provider, optimization_guide::TopHostProvider* top_host_provider, - scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, + network::NetworkQualityTracker* network_quality_tracker) : optimization_guide_service_(optimization_guide_service), ui_task_runner_(ui_task_runner), background_task_runner_(background_task_runner), @@ -77,41 +81,69 @@ PreviewsOptimizationGuide::PreviewsOptimizationGuide( std::make_unique<optimization_guide::HintCacheStore>( database_provider, profile_path, - pref_service, background_task_runner_))), top_host_provider_(top_host_provider), time_clock_(base::DefaultClock::GetInstance()), pref_service_(pref_service), - url_loader_factory_(url_loader_factory) { + url_loader_factory_(url_loader_factory), + network_quality_tracker_(network_quality_tracker) { DCHECK(optimization_guide_service_); + network_quality_tracker_->AddEffectiveConnectionTypeObserver(this); hint_cache_->Initialize( optimization_guide::switches::ShouldPurgeHintCacheStoreOnStartup(), - base::BindOnce(&PreviewsOptimizationGuide::OnHintCacheInitialized, + base::BindOnce(&PreviewsOptimizationGuideImpl::OnHintCacheInitialized, ui_weak_ptr_factory_.GetWeakPtr())); } -PreviewsOptimizationGuide::~PreviewsOptimizationGuide() { +PreviewsOptimizationGuideImpl::~PreviewsOptimizationGuideImpl() { DCHECK(ui_task_runner_->BelongsToCurrentThread()); optimization_guide_service_->RemoveObserver(this); + network_quality_tracker_->RemoveEffectiveConnectionTypeObserver(this); +} + +bool PreviewsOptimizationGuideImpl::IsReady() const { + return !!hints_; } -bool PreviewsOptimizationGuide::IsWhitelisted( +bool PreviewsOptimizationGuideImpl::CanApplyPreview( PreviewsUserData* previews_data, - const GURL& url, - PreviewsType type, - net::EffectiveConnectionType* out_ect_threshold) const { + content::NavigationHandle* navigation_handle, + PreviewsType type) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); if (!hints_) { return false; } - *out_ect_threshold = params::GetECTThresholdForPreview(type); + // Check if LITE_PAGE_REDIRECT is blacklisted or not. + if (type == PreviewsType::LITE_PAGE_REDIRECT) { + if (current_effective_connection_type_ > + params::GetECTThresholdForPreview(type)) { + return false; + } + + if (params::LitePageRedirectPreviewIgnoresOptimizationGuideFilter()) { + return true; + } + + return !hints_->IsBlacklisted(navigation_handle->GetURL(), + PreviewsType::LITE_PAGE_REDIRECT); + } + + // Check other previews. int inflation_percent = 0; + net::EffectiveConnectionType out_ect_threshold = + params::GetECTThresholdForPreview(type); std::string serialized_hint_version_string; - if (!hints_->IsWhitelisted(url, type, &inflation_percent, out_ect_threshold, + if (!hints_->IsWhitelisted(navigation_handle->GetURL(), type, + &inflation_percent, &out_ect_threshold, &serialized_hint_version_string)) { return false; } + // Also check the hint's ECT threshold against the current ECT if the type + // can be applied. + if (current_effective_connection_type_ > out_ect_threshold) { + return false; + } if (inflation_percent != 0 && previews_data) { previews_data->set_data_savings_inflation_percent(inflation_percent); @@ -125,28 +157,7 @@ bool PreviewsOptimizationGuide::IsWhitelisted( return true; } -bool PreviewsOptimizationGuide::IsBlacklisted(const GURL& url, - PreviewsType type) const { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); - - if (type == PreviewsType::LITE_PAGE_REDIRECT) { - if (base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kIgnoreLitePageRedirectOptimizationBlacklist)) { - return false; - } - - if (!hints_) - return true; - - return hints_->IsBlacklisted(url, PreviewsType::LITE_PAGE_REDIRECT); - } - - // This function is only used by lite page redirect. - NOTREACHED(); - return false; -} - -void PreviewsOptimizationGuide::OnLoadedHint( +void PreviewsOptimizationGuideImpl::OnLoadedHint( base::OnceClosure callback, const GURL& document_url, const optimization_guide::proto::Hint* loaded_hint) const { @@ -154,16 +165,16 @@ void PreviewsOptimizationGuide::OnLoadedHint( // Record that the hint finished loading. This is used as a signal during // tests. - LOCAL_HISTOGRAM_BOOLEAN( - kPreviewsOptimizationGuideOnLoadedHintResultHistogramString, loaded_hint); + LOCAL_HISTOGRAM_BOOLEAN(optimization_guide::kLoadedHintLocalHistogramString, + loaded_hint); // Run the callback now that the hint is loaded. This is used as a signal by // tests. std::move(callback).Run(); } -bool PreviewsOptimizationGuide::MaybeLoadOptimizationHints( - const GURL& url, +bool PreviewsOptimizationGuideImpl::MaybeLoadOptimizationHints( + content::NavigationHandle* navigation_handle, base::OnceClosure callback) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); @@ -171,15 +182,16 @@ bool PreviewsOptimizationGuide::MaybeLoadOptimizationHints( return false; } + const GURL& url = navigation_handle->GetURL(); return hints_->MaybeLoadOptimizationHints( - url, base::BindOnce(&PreviewsOptimizationGuide::OnLoadedHint, + url, base::BindOnce(&PreviewsOptimizationGuideImpl::OnLoadedHint, ui_weak_ptr_factory_.GetWeakPtr(), std::move(callback), url)); } -bool PreviewsOptimizationGuide::GetResourceLoadingHints( +bool PreviewsOptimizationGuideImpl::GetResourceLoadingHints( const GURL& url, - std::vector<std::string>* out_resource_patterns_to_block) const { + std::vector<std::string>* out_resource_patterns_to_block) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); if (!hints_) @@ -188,18 +200,16 @@ bool PreviewsOptimizationGuide::GetResourceLoadingHints( return hints_->GetResourceLoadingHints(url, out_resource_patterns_to_block); } -void PreviewsOptimizationGuide::LogHintCacheMatch( - const GURL& url, - bool is_committed, - net::EffectiveConnectionType ect) const { +void PreviewsOptimizationGuideImpl::LogHintCacheMatch(const GURL& url, + bool is_committed) const { if (!hints_) { return; } - hints_->LogHintCacheMatch(url, is_committed, ect); + hints_->LogHintCacheMatch(url, is_committed); } -void PreviewsOptimizationGuide::OnHintCacheInitialized() { +void PreviewsOptimizationGuideImpl::OnHintCacheInitialized() { DCHECK(ui_task_runner_->BelongsToCurrentThread()); // Check if there is a valid hint proto given on the command line first. We // don't normally expect one, but if one is provided then use that and do not @@ -216,14 +226,12 @@ void PreviewsOptimizationGuide::OnHintCacheInitialized() { base::Version(kManualConfigComponentVersion)))); } - - // Register as an observer regardless of hint proto override usage. This is // needed as a signal during testing. optimization_guide_service_->AddObserver(this); } -void PreviewsOptimizationGuide::OnHintsComponentAvailable( +void PreviewsOptimizationGuideImpl::OnHintsComponentAvailable( const optimization_guide::HintsComponentInfo& info) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); @@ -246,12 +254,12 @@ void PreviewsOptimizationGuide::OnHintsComponentAvailable( base::BindOnce( &PreviewsHints::CreateFromHintsComponent, info, hint_cache_->MaybeCreateUpdateDataForComponentHints(info.version)), - base::BindOnce(&PreviewsOptimizationGuide::UpdateHints, + base::BindOnce(&PreviewsOptimizationGuideImpl::UpdateHints, ui_weak_ptr_factory_.GetWeakPtr(), std::move(next_update_closure_))); } -void PreviewsOptimizationGuide::FetchHints() { +void PreviewsOptimizationGuideImpl::FetchHints() { base::Optional<std::vector<std::string>> top_hosts = optimization_guide::switches::ParseHintsFetchOverrideFromCommandLine(); if (!top_hosts) { @@ -266,17 +274,19 @@ void PreviewsOptimizationGuide::FetchHints() { if (!hints_fetcher_) { hints_fetcher_ = std::make_unique<optimization_guide::HintsFetcher>( url_loader_factory_, - optimization_guide::features::GetOptimizationGuideServiceURL()); + optimization_guide::features::GetOptimizationGuideServiceURL(), + pref_service_); } if (top_hosts->size() > 0) { hints_fetcher_->FetchOptimizationGuideServiceHints( - *top_hosts, base::BindOnce(&PreviewsOptimizationGuide::OnHintsFetched, - ui_weak_ptr_factory_.GetWeakPtr())); + *top_hosts, + base::BindOnce(&PreviewsOptimizationGuideImpl::OnHintsFetched, + ui_weak_ptr_factory_.GetWeakPtr())); } } -void PreviewsOptimizationGuide::OnHintsFetched( +void PreviewsOptimizationGuideImpl::OnHintsFetched( base::Optional<std::unique_ptr<optimization_guide::proto::GetHintsResponse>> get_hints_response) { // TODO(mcrouse): this will be dropped into a backgroundtask as it will likely @@ -285,27 +295,28 @@ void PreviewsOptimizationGuide::OnHintsFetched( hint_cache_->UpdateFetchedHints( std::move(*get_hints_response), time_clock_->Now() + kUpdateFetchedHintsDelay, - base::BindOnce(&PreviewsOptimizationGuide::OnFetchedHintsStored, + base::BindOnce(&PreviewsOptimizationGuideImpl::OnFetchedHintsStored, ui_weak_ptr_factory_.GetWeakPtr())); } else { // The fetch did not succeed so we will schedule to retry the fetch in // after delaying for |kFetchRetryDelay| // TODO(mcrouse): When the store is refactored from closures, the timer will // be scheduled on failure of the store instead. - hints_fetch_timer_.Start(FROM_HERE, kFetchRetryDelay, this, - &PreviewsOptimizationGuide::ScheduleHintsFetch); + hints_fetch_timer_.Start( + FROM_HERE, kFetchRetryDelay, this, + &PreviewsOptimizationGuideImpl::ScheduleHintsFetch); } } -void PreviewsOptimizationGuide::OnFetchedHintsStored() { +void PreviewsOptimizationGuideImpl::OnFetchedHintsStored() { hints_fetch_timer_.Stop(); hints_fetch_timer_.Start( FROM_HERE, hint_cache_->FetchedHintsUpdateTime() - time_clock_->Now(), - this, &PreviewsOptimizationGuide::ScheduleHintsFetch); + this, &PreviewsOptimizationGuideImpl::ScheduleHintsFetch); // TODO(mcrouse): Purge hints now that new fetched hints have been stored. } -void PreviewsOptimizationGuide::UpdateHints( +void PreviewsOptimizationGuideImpl::UpdateHints( base::OnceClosure update_closure, std::unique_ptr<PreviewsHints> hints) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); @@ -313,7 +324,7 @@ void PreviewsOptimizationGuide::UpdateHints( if (hints_) { hints_->Initialize( hint_cache_.get(), - base::BindOnce(&PreviewsOptimizationGuide::OnHintsUpdated, + base::BindOnce(&PreviewsOptimizationGuideImpl::OnHintsUpdated, ui_weak_ptr_factory_.GetWeakPtr(), std::move(update_closure))); } else { @@ -321,12 +332,14 @@ void PreviewsOptimizationGuide::UpdateHints( } } -void PreviewsOptimizationGuide::ClearFetchedHints() { +void PreviewsOptimizationGuideImpl::ClearFetchedHints() { DCHECK(hint_cache_); hint_cache_->ClearFetchedHints(); + optimization_guide::HintsFetcher::ClearHostsSuccessfullyFetched( + pref_service_); } -void PreviewsOptimizationGuide::OnHintsUpdated( +void PreviewsOptimizationGuideImpl::OnHintsUpdated( base::OnceClosure update_closure) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); DCHECK(pref_service_); @@ -343,9 +356,6 @@ void PreviewsOptimizationGuide::OnHintsUpdated( // If the client is eligible to fetch hints, currently controlled by a feature // flag |kOptimizationHintsFetching|, fetch hints from the remote Optimization // Guide Service. - // - // TODO(mcrouse): Add a check if Infobar notification needs to be shown to the - // user. if (!data_reduction_proxy::DataReductionProxySettings:: IsDataSaverEnabledByUser(pref_service_)) { return; @@ -365,7 +375,7 @@ void PreviewsOptimizationGuide::OnHintsUpdated( } } -void PreviewsOptimizationGuide::SetLastHintsFetchAttemptTime( +void PreviewsOptimizationGuideImpl::SetLastHintsFetchAttemptTime( base::Time last_attempt_time) { DCHECK(pref_service_); pref_service_->SetInt64( @@ -373,14 +383,14 @@ void PreviewsOptimizationGuide::SetLastHintsFetchAttemptTime( last_attempt_time.ToDeltaSinceWindowsEpoch().InMicroseconds()); } -base::Time PreviewsOptimizationGuide::GetLastHintsFetchAttemptTime() const { +base::Time PreviewsOptimizationGuideImpl::GetLastHintsFetchAttemptTime() const { DCHECK(pref_service_); return base::Time::FromDeltaSinceWindowsEpoch( base::TimeDelta::FromMicroseconds(pref_service_->GetInt64( optimization_guide::prefs::kHintsFetcherLastFetchAttempt))); } -void PreviewsOptimizationGuide::ScheduleHintsFetch() { +void PreviewsOptimizationGuideImpl::ScheduleHintsFetch() { DCHECK(!hints_fetch_timer_.IsRunning()); DCHECK(pref_service_); @@ -399,7 +409,7 @@ void PreviewsOptimizationGuide::ScheduleHintsFetch() { // been made in last |kFetchRetryDelay|. SetLastHintsFetchAttemptTime(time_clock_->Now()); hints_fetch_timer_.Start(FROM_HERE, RandomFetchDelay(), this, - &PreviewsOptimizationGuide::FetchHints); + &PreviewsOptimizationGuideImpl::FetchHints); } else { if (time_until_update_time >= base::TimeDelta()) { // If the fetched hints in the store are still up-to-date, set a timer @@ -411,31 +421,37 @@ void PreviewsOptimizationGuide::ScheduleHintsFetch() { // delay. fetcher_delay = time_until_retry; } - hints_fetch_timer_.Start(FROM_HERE, fetcher_delay, this, - &PreviewsOptimizationGuide::ScheduleHintsFetch); + hints_fetch_timer_.Start( + FROM_HERE, fetcher_delay, this, + &PreviewsOptimizationGuideImpl::ScheduleHintsFetch); } } -void PreviewsOptimizationGuide::SetTimeClockForTesting( +void PreviewsOptimizationGuideImpl::SetTimeClockForTesting( const base::Clock* time_clock) { time_clock_ = time_clock; } -void PreviewsOptimizationGuide::SetHintsFetcherForTesting( +void PreviewsOptimizationGuideImpl::SetHintsFetcherForTesting( std::unique_ptr<optimization_guide::HintsFetcher> hints_fetcher) { hints_fetcher_ = std::move(hints_fetcher); } optimization_guide::HintsFetcher* -PreviewsOptimizationGuide::GetHintsFetcherForTesting() { +PreviewsOptimizationGuideImpl::GetHintsFetcherForTesting() { return hints_fetcher_.get(); } -void PreviewsOptimizationGuide::ListenForNextUpdateForTesting( +void PreviewsOptimizationGuideImpl::ListenForNextUpdateForTesting( base::OnceClosure next_update_closure) { DCHECK(next_update_closure_.is_null()) << "Only one update closure is supported at a time"; next_update_closure_ = std::move(next_update_closure); } +void PreviewsOptimizationGuideImpl::OnEffectiveConnectionTypeChanged( + net::EffectiveConnectionType effective_connection_type) { + current_effective_connection_type_ = effective_connection_type; +} + } // namespace previews diff --git a/chromium/components/previews/content/previews_optimization_guide_impl.h b/chromium/components/previews/content/previews_optimization_guide_impl.h new file mode 100644 index 00000000000..b36b65bd3cc --- /dev/null +++ b/chromium/components/previews/content/previews_optimization_guide_impl.h @@ -0,0 +1,223 @@ +// Copyright 2017 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. + +#ifndef COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_IMPL_H_ +#define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_IMPL_H_ + +#include <memory> +#include <string> +#include <vector> + +#include "base/callback_forward.h" +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_refptr.h" +#include "base/memory/weak_ptr.h" +#include "base/sequenced_task_runner.h" +#include "base/single_thread_task_runner.h" +#include "base/time/clock.h" +#include "base/timer/timer.h" +#include "components/optimization_guide/hint_cache.h" +#include "components/optimization_guide/optimization_guide_service_observer.h" +#include "components/previews/content/previews_optimization_guide.h" +#include "components/previews/core/previews_experiments.h" +#include "services/network/public/cpp/network_quality_tracker.h" +#include "url/gurl.h" + +class PrefService; + +namespace base { +class FilePath; +} // namespace base +namespace content { +class NavigationHandle; +} // namespace content +namespace network { +class SharedURLLoaderFactory; +} // namespace network +namespace optimization_guide { +struct HintsComponentInfo; +class HintsFetcher; +class OptimizationGuideService; +class TopHostProvider; +namespace proto { +class Hint; +} // namespace proto +} // namespace optimization_guide + +namespace previews { + +class PreviewsHints; +class PreviewsUserData; + +// A Previews optimization guide that makes decisions guided by hints received +// from the OptimizationGuideService. +class PreviewsOptimizationGuideImpl + : public PreviewsOptimizationGuide, + public optimization_guide::OptimizationGuideServiceObserver, + public network::NetworkQualityTracker::EffectiveConnectionTypeObserver { + public: + // The embedder guarantees |optimization_guide_service| outlives |this|. + // The embedder guarantees that |previews_top_host_provider_| outlives |this|. + PreviewsOptimizationGuideImpl( + optimization_guide::OptimizationGuideService* optimization_guide_service, + const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, + const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, + const base::FilePath& profile_path, + PrefService* pref_service, + leveldb_proto::ProtoDatabaseProvider* database_provider, + optimization_guide::TopHostProvider* top_host_provider, + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, + network::NetworkQualityTracker* network_quality_tracker); + + ~PreviewsOptimizationGuideImpl() override; + + // PreviewsOptimizationGuide implementation: + bool IsReady() const override; + bool CanApplyPreview(PreviewsUserData* previews_data, + content::NavigationHandle* navigation_handle, + PreviewsType type) override; + bool MaybeLoadOptimizationHints(content::NavigationHandle* navigation_handle, + base::OnceClosure callback) override; + bool GetResourceLoadingHints( + const GURL& url, + std::vector<std::string>* out_resource_patterns_to_block) override; + void LogHintCacheMatch(const GURL& url, bool is_committed) const override; + void ClearFetchedHints() override; + + // optimization_guide::OptimizationGuideServiceObserver implementation: + // Called by OptimizationGuideService when a new component is available for + // processing. + void OnHintsComponentAvailable( + const optimization_guide::HintsComponentInfo& info) override; + + // network::NetworkQualityTracker::EffectiveConnectionTypeObserver + // implementation: + void OnEffectiveConnectionTypeChanged( + net::EffectiveConnectionType effective_connection_type) override; + + PreviewsHints* GetHintsForTesting() { return hints_.get(); } + + // |next_update_closure| is called the next time OnHintsComponentAvailable is + // called and the corresponding hints have been updated. + void ListenForNextUpdateForTesting(base::OnceClosure next_update_closure); + + // Set |time_clock_| for testing. + void SetTimeClockForTesting(const base::Clock* time_clock); + + // Set |hints_fetcher_| for testing. + void SetHintsFetcherForTesting( + std::unique_ptr<optimization_guide::HintsFetcher> hints_fetcher); + + optimization_guide::HintsFetcher* GetHintsFetcherForTesting(); + + // Called when the hints store is initialized to determine when hints + // should be fetched and schedules the |hints_fetch_timer_| to fire based on: + // 1. The update time for the fetched hints in the store and + // 2. The last time a fetch attempt was made, |last_fetch_attempt_|. + void ScheduleHintsFetch(); + + protected: + // Callback executed after remote hints have been fetched and returned from + // the remote Optimization Guide Service. At this point, the hints response + // is ready to be processed and stored for use. Virtual to be mocked in + // testing. + virtual void OnHintsFetched( + base::Optional< + std::unique_ptr<optimization_guide::proto::GetHintsResponse>> + get_hints_response); + + // Callback executed after the Hints have been successfully stored in the + // store. Virtual to be mocked in tests. + virtual void OnFetchedHintsStored(); + + private: + // Callback run after the hint cache is fully initialized. At this point, the + // PreviewsOptimizationGuide is ready to process components from the + // OptimizationGuideService and registers as an observer with it. + void OnHintCacheInitialized(); + + // Updates the hints to the latest hints sent by the Component Updater. + // |update_closure| is called once the hints are updated. + void UpdateHints(base::OnceClosure update_closure, + std::unique_ptr<PreviewsHints> hints); + + // Called when the hints have been fully updated with the latest hints from + // the Component Updater. This is used as a signal during tests. + // |update_closure| is called immediately if not null. + void OnHintsUpdated(base::OnceClosure update_closure); + + // Callback when a hint is loaded. + void OnLoadedHint(base::OnceClosure callback, + const GURL& document_url, + const optimization_guide::proto::Hint* loaded_hint) const; + + // Method to request new hints for user's sites based on + // engagement scores using |hints_fetcher_|. + void FetchHints(); + + // Return the time when a hints fetch request was last attempted. + base::Time GetLastHintsFetchAttemptTime() const; + + // Set the time when a hints fetch was last attempted to |last_attempt_time|. + void SetLastHintsFetchAttemptTime(base::Time last_attempt_time); + + // The OptimizationGuideService that this guide is listening to. Not owned. + optimization_guide::OptimizationGuideService* optimization_guide_service_; + + // Runner for UI thread tasks. + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; + + // Background thread where hints processing should be performed. + scoped_refptr<base::SequencedTaskRunner> background_task_runner_; + + // The hint cache used by PreviewsHints. It is owned by + // PreviewsOptimizationGuide so that the existing hint cache can be reused on + // component updates. Otherwise, a new cache and store would need to be + // created during each component update. + std::unique_ptr<optimization_guide::HintCache> hint_cache_; + + // The current hints used for this optimization guide. + std::unique_ptr<PreviewsHints> hints_; + + // Used in testing to subscribe to an update event in this class. + base::OnceClosure next_update_closure_; + + // HintsFetcher handles making the request for updated hints from the remote + // Optimization Guide Service. + std::unique_ptr<optimization_guide::HintsFetcher> hints_fetcher_; + + // Timer to schedule when to fetch hints from the remote Optimization Guide + // Service. + base::OneShotTimer hints_fetch_timer_; + + // TopHostProvider that this guide can query. Not owned. + optimization_guide::TopHostProvider* top_host_provider_ = nullptr; + + // Clock used for scheduling the |hints_fetch_timer_|. + const base::Clock* time_clock_; + + // A reference to the PrefService for this profile. Not owned. + PrefService* pref_service_ = nullptr; + + // Used for fetching Hints by the Hints Fetcher. + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; + + // The network quality tracker that this guide is listening to. Not owned. + network::NetworkQualityTracker* network_quality_tracker_; + + // The current estimate of the effective connection type. + net::EffectiveConnectionType current_effective_connection_type_ = + net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; + + // Used to get |weak_ptr_| to self on the UI thread. + base::WeakPtrFactory<PreviewsOptimizationGuideImpl> ui_weak_ptr_factory_{ + this}; + + DISALLOW_COPY_AND_ASSIGN(PreviewsOptimizationGuideImpl); +}; + +} // namespace previews + +#endif // COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_OPTIMIZATION_GUIDE_IMPL_H_ diff --git a/chromium/components/previews/content/previews_optimization_guide_unittest.cc b/chromium/components/previews/content/previews_optimization_guide_impl_unittest.cc index 6f7a58c498c..22188299815 100644 --- a/chromium/components/previews/content/previews_optimization_guide_unittest.cc +++ b/chromium/components/previews/content/previews_optimization_guide_impl_unittest.cc @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/previews/content/previews_optimization_guide.h" +#include "components/previews/content/previews_optimization_guide_impl.h" #include <memory> +#include <utility> #include "base/base64.h" #include "base/bind.h" @@ -20,8 +21,8 @@ #include "base/test/gtest_util.h" #include "base/test/metrics/histogram_tester.h" #include "base/test/scoped_feature_list.h" -#include "base/test/scoped_task_environment.h" #include "base/test/simple_test_clock.h" +#include "base/test/task_environment.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" #include "components/optimization_guide/bloom_filter.h" @@ -41,8 +42,10 @@ #include "components/previews/content/previews_user_data.h" #include "components/previews/core/previews_experiments.h" #include "components/previews/core/previews_features.h" +#include "content/public/test/mock_navigation_handle.h" #include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" +#include "services/network/test/test_network_quality_tracker.h" #include "services/network/test/test_url_loader_factory.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -120,22 +123,21 @@ std::unique_ptr<optimization_guide::proto::GetHintsResponse> BuildHintsResponse( // A mock class implementation of HintsFetcher for unittesting // previews_optimization_guide. class TestHintsFetcher : public optimization_guide::HintsFetcher { - using HintsFetchedCallback = base::OnceCallback<void( - base::Optional< - std::unique_ptr<optimization_guide::proto::GetHintsResponse>>)>; - using HintsFetcher::FetchOptimizationGuideServiceHints; - public: TestHintsFetcher( scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, GURL optimization_guide_service_url, - HintsFetcherEndState fetch_state) - : HintsFetcher(url_loader_factory, optimization_guide_service_url), + HintsFetcherEndState fetch_state, + PrefService* pref_service) + : HintsFetcher(url_loader_factory, + optimization_guide_service_url, + pref_service), fetch_state_(fetch_state) {} bool FetchOptimizationGuideServiceHints( const std::vector<std::string>& hosts, - HintsFetchedCallback hints_fetched_callback) override { + optimization_guide::HintsFetchedCallback hints_fetched_callback) + override { switch (fetch_state_) { case HintsFetcherEndState::kFetchFailed: std::move(hints_fetched_callback).Run(base::nullopt); @@ -161,7 +163,7 @@ class TestHintsFetcher : public optimization_guide::HintsFetcher { // A Test PreviewsOptimizationGuide to observe and record when callbacks // from hints fetching and storing occur. -class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuide { +class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuideImpl { public: TestPreviewsOptimizationGuide( optimization_guide::OptimizationGuideService* optimization_guide_service, @@ -171,15 +173,17 @@ class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuide { PrefService* pref_service, leveldb_proto::ProtoDatabaseProvider* database_provider, optimization_guide::TopHostProvider* optimization_guide_top_host_provider, - scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) - : PreviewsOptimizationGuide(optimization_guide_service, - ui_task_runner, - background_task_runner, - profile_path, - pref_service, - database_provider, - optimization_guide_top_host_provider, - url_loader_factory) {} + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, + network::TestNetworkQualityTracker* network_quality_tracker) + : PreviewsOptimizationGuideImpl(optimization_guide_service, + ui_task_runner, + background_task_runner, + profile_path, + pref_service, + database_provider, + optimization_guide_top_host_provider, + url_loader_factory, + network_quality_tracker) {} bool fetched_hints_stored() { return fetched_hints_stored_; } @@ -189,26 +193,26 @@ class TestPreviewsOptimizationGuide : public PreviewsOptimizationGuide { std::unique_ptr<optimization_guide::proto::GetHintsResponse>> get_hints_response) override { fetched_hints_stored_ = false; - PreviewsOptimizationGuide::OnHintsFetched(std::move(get_hints_response)); + PreviewsOptimizationGuideImpl::OnHintsFetched( + std::move(get_hints_response)); } void OnFetchedHintsStored() override { fetched_hints_stored_ = true; - PreviewsOptimizationGuide::OnFetchedHintsStored(); + PreviewsOptimizationGuideImpl::OnFetchedHintsStored(); } bool fetched_hints_stored_ = false; }; -class PreviewsOptimizationGuideTest +class PreviewsOptimizationGuideImplTest : public optimization_guide::ProtoDatabaseProviderTestBase { public: - PreviewsOptimizationGuideTest() - : scoped_task_environment_( - base::test::ScopedTaskEnvironment::MainThreadType::UI, - base::test::ScopedTaskEnvironment::TimeSource::MOCK_TIME) {} + PreviewsOptimizationGuideImplTest() + : task_environment_(base::test::TaskEnvironment::MainThreadType::UI, + base::test::TaskEnvironment::TimeSource::MOCK_TIME) {} - ~PreviewsOptimizationGuideTest() override {} + ~PreviewsOptimizationGuideImplTest() override {} void SetUp() override { ProtoDatabaseProviderTestBase::SetUp(); @@ -222,7 +226,7 @@ class PreviewsOptimizationGuideTest ResetGuide(); } - PreviewsOptimizationGuide* guide() { return guide_.get(); } + PreviewsOptimizationGuideImpl* guide() { return guide_.get(); } MockTopHostProvider* top_host_provider() { return optimization_guide_top_host_provider_.get(); @@ -242,6 +246,10 @@ class PreviewsOptimizationGuideTest return static_cast<TestHintsFetcher*>(guide_->GetHintsFetcherForTesting()); } + network::TestNetworkQualityTracker* network_quality_tracker() { + return network_quality_tracker_.get(); + } + void ProcessHints(const optimization_guide::proto::Configuration& config, const std::string& version) { base::HistogramTester histogram_tester; @@ -284,7 +292,7 @@ class PreviewsOptimizationGuideTest } optimization_guide_service_ = std::make_unique<TestOptimizationGuideService>( - scoped_task_environment_.GetMainThreadTaskRunner()); + task_environment_.GetMainThreadTaskRunner()); pref_service_ = std::make_unique<TestingPrefServiceSimple>(); // Registry pref for DataSaver with default off. @@ -292,14 +300,18 @@ class PreviewsOptimizationGuideTest data_reduction_proxy::prefs::kDataSaverEnabled, false); optimization_guide::prefs::RegisterProfilePrefs(pref_service_->registry()); + network_quality_tracker_ = + std::make_unique<network::TestNetworkQualityTracker>(); + guide_ = std::make_unique<TestPreviewsOptimizationGuide>( optimization_guide_service_.get(), - scoped_task_environment_.GetMainThreadTaskRunner(), - scoped_task_environment_.GetMainThreadTaskRunner(), temp_dir(), + task_environment_.GetMainThreadTaskRunner(), + task_environment_.GetMainThreadTaskRunner(), temp_dir(), pref_service_.get(), db_provider_.get(), - optimization_guide_top_host_provider_.get(), url_loader_factory_); + optimization_guide_top_host_provider_.get(), url_loader_factory_, + network_quality_tracker_.get()); - guide_->SetTimeClockForTesting(scoped_task_environment_.GetMockClock()); + guide_->SetTimeClockForTesting(task_environment_.GetMockClock()); base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -313,7 +325,7 @@ class PreviewsOptimizationGuideTest } const base::Clock* GetMockClock() const { - return scoped_task_environment_.GetMockClock(); + return task_environment_.GetMockClock(); } void ResetGuide() { @@ -324,23 +336,24 @@ class PreviewsOptimizationGuideTest std::unique_ptr<TestHintsFetcher> BuildTestHintsFetcher( HintsFetcherEndState end_state) { std::unique_ptr<TestHintsFetcher> hints_fetcher = - std::make_unique<TestHintsFetcher>( - url_loader_factory_, GURL("https://hintsserver.com"), end_state); + std::make_unique<TestHintsFetcher>(url_loader_factory_, + GURL("https://hintsserver.com"), + end_state, pref_service()); return hints_fetcher; } base::FilePath temp_dir() const { return temp_dir_.GetPath(); } protected: - base::test::ScopedTaskEnvironment scoped_task_environment_; + base::test::TaskEnvironment task_environment_; void MoveClockForwardBy(base::TimeDelta time_delta) { - scoped_task_environment_.FastForwardBy(time_delta); + task_environment_.FastForwardBy(time_delta); base::RunLoop().RunUntilIdle(); } void RunUntilIdle() { - scoped_task_environment_.RunUntilIdle(); + task_environment_.RunUntilIdle(); base::RunLoop().RunUntilIdle(); } @@ -366,15 +379,20 @@ class PreviewsOptimizationGuideTest // server blacklist. void InitializeWithLitePageRedirectBlacklist(); + // This is a wrapper around MaybeLoadOptimizationHints that wraps |url| in a + // navigation handle to call the method. + bool CallMaybeLoadOptimizationHints(const GURL& url); + // This function guarantees that all of the asynchronous processing required - // to load the specified hint has occurred prior to calling IsWhitelisted. - // It accomplishes this by calling MaybeLoadOptimizationHints() and waiting - // until OnLoadOptimizationHints runs before calling IsWhitelisted(). - bool MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + // to load the specified hint has occurred prior to calling + // CanApplyPreview. It accomplishes this by calling + // MaybeLoadOptimizationHints() and waiting until OnLoadOptimizationHints runs + // before calling CanApplyPreview(). + bool MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( PreviewsUserData* previews_data, const GURL& url, PreviewsType type, - net::EffectiveConnectionType* out_ect_threshold); + net::EffectiveConnectionType ect); void RunUntilFetchedHintsStored() { while (!guide_->fetched_hints_stored()) { @@ -408,18 +426,19 @@ class PreviewsOptimizationGuideTest std::unique_ptr<TestOptimizationGuideService> optimization_guide_service_; std::unique_ptr<MockTopHostProvider> optimization_guide_top_host_provider_; std::unique_ptr<TestingPrefServiceSimple> pref_service_; + std::unique_ptr<network::TestNetworkQualityTracker> network_quality_tracker_; scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_; - GURL loaded_hints_document_gurl_; std::vector<std::string> loaded_hints_resource_patterns_; - DISALLOW_COPY_AND_ASSIGN(PreviewsOptimizationGuideTest); + DISALLOW_COPY_AND_ASSIGN(PreviewsOptimizationGuideImplTest); }; -void PreviewsOptimizationGuideTest::InitializeFixedCountResourceLoadingHints() { +void PreviewsOptimizationGuideImplTest:: + InitializeFixedCountResourceLoadingHints() { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint1 = config.add_hints(); hint1->set_key("somedomain.org"); @@ -461,7 +480,7 @@ void PreviewsOptimizationGuideTest::InitializeFixedCountResourceLoadingHints() { ProcessHints(config, "2.0.0"); } -void PreviewsOptimizationGuideTest:: +void PreviewsOptimizationGuideImplTest:: InitializeFixedCountResourceLoadingHintsWithTwoExperiments() { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint1 = config.add_hints(); @@ -497,7 +516,7 @@ void PreviewsOptimizationGuideTest:: ProcessHints(config, "2.0.0"); } -void PreviewsOptimizationGuideTest::InitializeMultipleResourceLoadingHints( +void PreviewsOptimizationGuideImplTest::InitializeMultipleResourceLoadingHints( size_t key_count, size_t page_patterns_per_key) { optimization_guide::proto::Configuration config; @@ -535,7 +554,8 @@ void PreviewsOptimizationGuideTest::InitializeMultipleResourceLoadingHints( ProcessHints(config, "2.0.0"); } -void PreviewsOptimizationGuideTest::InitializeWithLitePageRedirectBlacklist() { +void PreviewsOptimizationGuideImplTest:: + InitializeWithLitePageRedirectBlacklist() { optimization_guide::BloomFilter blacklist_bloom_filter(7, 511); blacklist_bloom_filter.Add("blacklisteddomain.com"); blacklist_bloom_filter.Add("blacklistedsubdomain.maindomain.co.in"); @@ -555,42 +575,54 @@ void PreviewsOptimizationGuideTest::InitializeWithLitePageRedirectBlacklist() { ProcessHints(config, "2.0.0"); } -bool PreviewsOptimizationGuideTest:: - MaybeLoadOptimizationHintsAndCheckIsWhitelisted( +bool PreviewsOptimizationGuideImplTest::CallMaybeLoadOptimizationHints( + const GURL& url) { + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(url); + return guide()->MaybeLoadOptimizationHints(&navigation_handle, + base::DoNothing()); +} + +bool PreviewsOptimizationGuideImplTest:: + MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( PreviewsUserData* previews_data, const GURL& url, PreviewsType type, - net::EffectiveConnectionType* out_ect_threshold) { + net::EffectiveConnectionType ect) { + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(url); + // Ensure that all asynchronous MaybeLoadOptimizationHints processing - // finishes prior to calling IsWhitelisted. This is accomplished by waiting - // for the OnLoadOptimizationHints callback to set |requested_hints_loaded_| - // to true. + // finishes prior to calling CanApplyPreview. This is accomplished by + // waiting for the OnLoadOptimizationHints callback to set + // |requested_hints_loaded_| to true. requested_hints_loaded_ = false; if (guide()->MaybeLoadOptimizationHints( - url, base::BindOnce( - &PreviewsOptimizationGuideTest::OnLoadOptimizationHints, - base::Unretained(this)))) { + &navigation_handle, + base::BindOnce( + &PreviewsOptimizationGuideImplTest::OnLoadOptimizationHints, + base::Unretained(this)))) { while (!requested_hints_loaded_) { RunUntilIdle(); } } - return guide()->IsWhitelisted(previews_data, url, type, out_ect_threshold); + network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(ect); + return guide()->CanApplyPreview(previews_data, &navigation_handle, type); } -void PreviewsOptimizationGuideTest::OnLoadOptimizationHints() { +void PreviewsOptimizationGuideImplTest::OnLoadOptimizationHints() { requested_hints_loaded_ = true; } -TEST_F(PreviewsOptimizationGuideTest, IsWhitelistedWithoutHints) { +TEST_F(PreviewsOptimizationGuideImplTest, CanApplyPreviewWithoutHints) { PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsForNoScriptPageHintsPopulatedCorrectly) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -609,8 +641,7 @@ TEST_F(PreviewsOptimizationGuideTest, optimization_guide::proto::PageHint* page_hint2 = hint1->add_page_hints(); page_hint2->set_page_pattern("noscript_3g"); page_hint2->set_max_ect_trigger( - optimization_guide::proto::EffectiveConnectionType:: - EFFECTIVE_CONNECTION_TYPE_3G); + optimization_guide::proto::EFFECTIVE_CONNECTION_TYPE_3G); optimization_guide::proto::Optimization* optimization2 = page_hint2->add_whitelisted_optimizations(); optimization2->set_optimization_type(optimization_guide::proto::NOSCRIPT); @@ -627,34 +658,31 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify page matches and ECT thresholds. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://somedomain.org/noscript_default_2g"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://somedomain.org/noscript_3g"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G, ect_threshold); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_3G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://somedomain.org/no_pattern_match"), - PreviewsType::NOSCRIPT, &ect_threshold)); + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); // Verify * matches any page. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://anypage.com/noscript_for_all"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://anypage.com/"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://anypage.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithValidCommandLineOverride) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -680,16 +708,14 @@ TEST_F(PreviewsOptimizationGuideTest, // Verify page matches and ECT thresholds. PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; EXPECT_TRUE(guide()->GetHintsForTesting()); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://somedomain.org/noscript_default_2g"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithValidCommandLineOverrideForDeferAllScript) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures({features::kDeferAllScriptPreviews}, {}); @@ -715,16 +741,14 @@ TEST_F(PreviewsOptimizationGuideTest, // Verify page matches and ECT thresholds. PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; EXPECT_TRUE(guide()->GetHintsForTesting()); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://somedomain.org/defer_default_2g"), - PreviewsType::DEFER_ALL_SCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::DEFER_ALL_SCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithValidCommandLineOverrideAndPreexistingData) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -732,8 +756,8 @@ TEST_F(PreviewsOptimizationGuideTest, InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint = config.add_hints(); @@ -755,19 +779,17 @@ TEST_F(PreviewsOptimizationGuideTest, // Verify page matches and ECT thresholds. PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; EXPECT_TRUE(guide()->GetHintsForTesting()); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://otherdomain.org/noscript_default_2g"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_FALSE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithInvalidCommandLineOverride) { base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( optimization_guide::switches::kHintsProtoOverride, "this-is-not-a-proto"); @@ -776,53 +798,48 @@ TEST_F(PreviewsOptimizationGuideTest, EXPECT_FALSE(guide()->GetHintsForTesting()); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithPurgeHintCacheStoreCommandLineAndNoPreexistingData) { base::CommandLine::ForCurrentProcess()->AppendSwitch( optimization_guide::switches::kPurgeHintCacheStore); CreateServiceAndGuide(); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithPurgeHintCacheStoreCommandLineAndPreexistingData) { InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); base::CommandLine::ForCurrentProcess()->AppendSwitch( optimization_guide::switches::kPurgeHintCacheStore); CreateServiceAndGuide(); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); } // Test when resource loading hints are enabled. -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsForResourceLoadingHintsPopulatedCorrectly) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -871,23 +888,24 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Twitter and Facebook should be whitelisted but not Google. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.twitter.com/example"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://google.com"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } // Test when both NoScript and resource loading hints are enabled. TEST_F( - PreviewsOptimizationGuideTest, + PreviewsOptimizationGuideImplTest, ProcessHintsWhitelistForNoScriptAndResourceLoadingHintsPopulatedCorrectly) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -935,23 +953,24 @@ TEST_F( ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Twitter and Facebook should be whitelisted but not Google. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com/example.html"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::NOSCRIPT, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.twitter.com/example"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://google.com"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsForResourceLoadingHintsWithSlowPageTriggering) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -999,19 +1018,16 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://3g.com"), PreviewsType::RESOURCE_LOADING_HINTS, - &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G, ect_threshold); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_3G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://4g.com/example"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_4G, ect_threshold); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, net::EFFECTIVE_CONNECTION_TYPE_4G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://default2g.com"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } // This is a helper function for testing the experiment flags on the config for @@ -1019,7 +1035,7 @@ TEST_F(PreviewsOptimizationGuideTest, // multiple optimizations. The optimization under test will be marked with an // experiment name if one is provided in |experiment_name|. It will then be // tested to see if it's enabled, the expectation found in |expect_enabled|. -void PreviewsOptimizationGuideTest::DoExperimentFlagTest( +void PreviewsOptimizationGuideImplTest::DoExperimentFlagTest( base::Optional<std::string> experiment_name, bool expect_enabled) { base::test::ScopedFeatureList scoped_list; @@ -1065,36 +1081,32 @@ void PreviewsOptimizationGuideTest::DoExperimentFlagTest( ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Check to ensure the optimization under test (facebook noscript) is either // enabled or disabled, depending on what the caller told us to expect. - EXPECT_EQ(expect_enabled, MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_EQ(expect_enabled, MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), - PreviewsType::NOSCRIPT, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::NOSCRIPT, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); // RESOURCE_LOADING_HINTS for facebook should always be enabled. - ect_threshold = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; EXPECT_EQ(!expect_enabled, - MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); // Twitter's NOSCRIPT should always be enabled; RESOURCE_LOADING_HINTS is not // configured and should be disabled. - ect_threshold = net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.twitter.com/example"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); // Google (which is not configured at all) should always have both NOSCRIPT // and RESOURCE_LOADING_HINTS disabled. - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HandlesExperimentalFlagWithNoExperimentFlaggedOrEnabled) { // With the optimization NOT flagged as experimental and no experiment // enabled, the optimization should be enabled. @@ -1104,7 +1116,7 @@ TEST_F(PreviewsOptimizationGuideTest, DoExperimentFlagTest(base::nullopt, true); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HandlesExperimentalFlagWithEmptyExperimentName) { // Empty experiment names should be equivalent to no experiment flag set. base::test::ScopedFeatureList scoped_list; @@ -1113,7 +1125,7 @@ TEST_F(PreviewsOptimizationGuideTest, DoExperimentFlagTest("", true); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HandlesExperimentalFlagWithExperimentConfiguredAndNotRunning) { // With the optimization flagged as experimental and no experiment // enabled, the optimization should be disabled. @@ -1123,7 +1135,7 @@ TEST_F(PreviewsOptimizationGuideTest, DoExperimentFlagTest("foo_experiment", false); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HandlesExperimentalFlagWithExperimentConfiguredAndSameOneRunning) { // With the optimization flagged as experimental and an experiment with that // name running, the optimization should be enabled. @@ -1134,7 +1146,7 @@ TEST_F(PreviewsOptimizationGuideTest, DoExperimentFlagTest("foo_experiment", true); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HandlesExperimentalFlagWithExperimentConfiguredAndDifferentOneRunning) { // With the optimization flagged as experimental and a *different* experiment // enabled, the optimization should be disabled. @@ -1145,14 +1157,15 @@ TEST_F(PreviewsOptimizationGuideTest, DoExperimentFlagTest("foo_experiment", false); } -TEST_F(PreviewsOptimizationGuideTest, EnsureExperimentsDisabledByDefault) { +TEST_F(PreviewsOptimizationGuideImplTest, EnsureExperimentsDisabledByDefault) { // Mark an optimization as experiment, and ensure it's disabled even though we // don't explicitly enable or disable the feature as part of the test. This // ensures the experiments feature is disabled by default. DoExperimentFlagTest("foo_experiment", false); } -TEST_F(PreviewsOptimizationGuideTest, ProcessHintsUnsupportedKeyRepIsIgnored) { +TEST_F(PreviewsOptimizationGuideImplTest, + ProcessHintsUnsupportedKeyRepIsIgnored) { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint = config.add_hints(); hint->set_key("facebook.com"); @@ -1166,13 +1179,12 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsUnsupportedKeyRepIsIgnored) { ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsUnsupportedOptimizationIsIgnored) { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint = config.add_hints(); @@ -1188,13 +1200,12 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithExistingSentinel) { +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithExistingSentinel) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( {features::kNoScriptPreviews, features::kResourceLoadingHints}, {}); @@ -1220,10 +1231,9 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithExistingSentinel) { ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); EXPECT_TRUE(base::PathExists(sentinel_path)); histogram_tester.ExpectUniqueSample( "OptimizationGuide.ProcessHintsResult", @@ -1234,10 +1244,9 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithExistingSentinel) { // Now verify config is processed for different version and sentinel cleared. ProcessHints(config, "3.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, ect_threshold); + net::EFFECTIVE_CONNECTION_TYPE_2G)); EXPECT_FALSE(base::PathExists(sentinel_path)); histogram_tester.ExpectBucketCount( "OptimizationGuide.ProcessHintsResult", @@ -1246,7 +1255,7 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithExistingSentinel) { 1); } -TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithInvalidSentinelFile) { +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsWithInvalidSentinelFile) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( {features::kNoScriptPreviews, features::kResourceLoadingHints}, {}); @@ -1273,10 +1282,9 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithInvalidSentinelFile) { ProcessHints(config, "2.0.0"); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); EXPECT_FALSE(base::PathExists(sentinel_path)); histogram_tester.ExpectUniqueSample( "OptimizationGuide.ProcessHintsResult", @@ -1287,9 +1295,9 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithInvalidSentinelFile) { // Now verify config is processed with sentinel cleared. ProcessHints(config, "2.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); EXPECT_FALSE(base::PathExists(sentinel_path)); histogram_tester.ExpectBucketCount( "OptimizationGuide.ProcessHintsResult", @@ -1298,7 +1306,8 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintsWithInvalidSentinelFile) { 1); } -TEST_F(PreviewsOptimizationGuideTest, SkipHintProcessingForSameConfigVersion) { +TEST_F(PreviewsOptimizationGuideImplTest, + SkipHintProcessingForSameConfigVersion) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( {features::kNoScriptPreviews, features::kResourceLoadingHints}, {}); @@ -1325,17 +1334,16 @@ TEST_F(PreviewsOptimizationGuideTest, SkipHintProcessingForSameConfigVersion) { optimization2->set_optimization_type(optimization_guide::proto::NOSCRIPT); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Process the new hints config and verify that they are available. ProcessHints(config1, "2.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectUniqueSample( "OptimizationGuide.ProcessHintsResult", static_cast<int>( @@ -1346,12 +1354,12 @@ TEST_F(PreviewsOptimizationGuideTest, SkipHintProcessingForSameConfigVersion) { // is skipped. ProcessHints(config2, "2.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectBucketCount( "OptimizationGuide.ProcessHintsResult", static_cast<int>(optimization_guide::ProcessHintsComponentResult:: @@ -1359,7 +1367,7 @@ TEST_F(PreviewsOptimizationGuideTest, SkipHintProcessingForSameConfigVersion) { 1); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, SkipHintProcessingForEarlierConfigVersion) { base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -1387,17 +1395,16 @@ TEST_F(PreviewsOptimizationGuideTest, optimization2->set_optimization_type(optimization_guide::proto::NOSCRIPT); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Process the new hints config and verify that they are available. ProcessHints(config1, "2.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectUniqueSample( "OptimizationGuide.ProcessHintsResult", static_cast<int>( @@ -1408,12 +1415,12 @@ TEST_F(PreviewsOptimizationGuideTest, // one is skipped. ProcessHints(config2, "1.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectBucketCount( "OptimizationGuide.ProcessHintsResult", static_cast<int>(optimization_guide::ProcessHintsComponentResult:: @@ -1421,7 +1428,7 @@ TEST_F(PreviewsOptimizationGuideTest, 1); } -TEST_F(PreviewsOptimizationGuideTest, ProcessMultipleNewConfigs) { +TEST_F(PreviewsOptimizationGuideImplTest, ProcessMultipleNewConfigs) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitWithFeatures( @@ -1448,17 +1455,16 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessMultipleNewConfigs) { optimization2->set_optimization_type(optimization_guide::proto::NOSCRIPT); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Process the new hints config and verify that they are available. ProcessHints(config1, "2.0.0"); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectUniqueSample( "OptimizationGuide.ProcessHintsResult", static_cast<int>( @@ -1469,12 +1475,12 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessMultipleNewConfigs) { // is processed. ProcessHints(config2, "3.0.0"); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.facebook.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://m.google.com"), PreviewsType::NOSCRIPT, - &ect_threshold)); + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); histogram_tester.ExpectBucketCount( "OptimizationGuide.ProcessHintsResult", static_cast<int>( @@ -1482,7 +1488,8 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessMultipleNewConfigs) { 2); } -TEST_F(PreviewsOptimizationGuideTest, ProcessHintConfigWithNoKeyFailsDcheck) { +TEST_F(PreviewsOptimizationGuideImplTest, + ProcessHintConfigWithNoKeyFailsDcheck) { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint = config.add_hints(); hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX); @@ -1492,12 +1499,10 @@ TEST_F(PreviewsOptimizationGuideTest, ProcessHintConfigWithNoKeyFailsDcheck) { page_hint1->add_whitelisted_optimizations(); optimization1->set_optimization_type(optimization_guide::proto::NOSCRIPT); - EXPECT_DCHECK_DEATH({ - ProcessHints(config, "2.0.0"); - }); + EXPECT_DCHECK_DEATH({ ProcessHints(config, "2.0.0"); }); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, ProcessHintsConfigWithDuplicateKeysFailsDcheck) { optimization_guide::proto::Configuration config; optimization_guide::proto::Hint* hint1 = config.add_hints(); @@ -1517,75 +1522,73 @@ TEST_F(PreviewsOptimizationGuideTest, page_hint2->add_whitelisted_optimizations(); optimization2->set_optimization_type(optimization_guide::proto::NOSCRIPT); - EXPECT_DCHECK_DEATH({ - ProcessHints(config, "2.0.0"); - }); + EXPECT_DCHECK_DEATH({ ProcessHints(config, "2.0.0"); }); } -TEST_F(PreviewsOptimizationGuideTest, MaybeLoadOptimizationHints) { +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadOptimizationHints) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/football/seahawksrebuildingyear"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadPageHintsWithTwoExperimentsDisabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); InitializeFixedCountResourceLoadingHintsWithTwoExperiments(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/football/seahawksrebuildingyear"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadPageHintsWithFirstExperimentEnabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -1597,32 +1600,32 @@ TEST_F(PreviewsOptimizationGuideTest, InitializeFixedCountResourceLoadingHintsWithTwoExperiments(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/football/seahawksrebuildingyear"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadPageHintsWithSecondExperimentEnabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -1634,32 +1637,32 @@ TEST_F(PreviewsOptimizationGuideTest, InitializeFixedCountResourceLoadingHintsWithTwoExperiments(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/football/seahawksrebuildingyear"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadPageHintsWithBothExperimentEnabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -1672,34 +1675,34 @@ TEST_F(PreviewsOptimizationGuideTest, InitializeFixedCountResourceLoadingHintsWithTwoExperiments(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/football/seahawksrebuildingyear"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } // Test that optimization hints with multiple page patterns is processed // correctly. -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, LoadManyResourceLoadingOptimizationHints) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); @@ -1708,61 +1711,66 @@ TEST_F(PreviewsOptimizationGuideTest, const size_t page_patterns_per_key = 25; PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; InitializeMultipleResourceLoadingHints(key_count, page_patterns_per_key); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain0.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain0.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain0.org/news0/football"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain49.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain49.org/news0/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain50.org/"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain50.org/news0/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); - - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain0.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain0.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://somedomain0.org/news0/football"))); + EXPECT_TRUE( + CallMaybeLoadOptimizationHints(GURL("https://somedomain49.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://somedomain49.org/news0/football"))); + EXPECT_FALSE( + CallMaybeLoadOptimizationHints(GURL("https://somedomain50.org/"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints( + GURL("https://somedomain50.org/news0/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); + + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain0.org/news0/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain0.org/news24/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain0.org/news25/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain49.org/news0/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain49.org/news24/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain49.org/news25/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain50.org/news0/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain50.org/news24/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain50.org/news25/football"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); RunUntilIdle(); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, MaybeLoadOptimizationHintsWithoutEnabledPageHintsFeature) { // Without PageHints-oriented feature enabled, never see // enabled, the optimization should be disabled. @@ -1771,85 +1779,96 @@ TEST_F(PreviewsOptimizationGuideTest, InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org"), base::DoNothing())); + EXPECT_TRUE( + CallMaybeLoadOptimizationHints(GURL("https://www.somedomain.org"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); } -TEST_F(PreviewsOptimizationGuideTest, PreviewsUserDataPopulatedCorrectly) { +TEST_F(PreviewsOptimizationGuideImplTest, PreviewsUserDataPopulatedCorrectly) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kResourceLoadingHints); InitializeFixedCountResourceLoadingHints(); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://somedomain.org/"), base::DoNothing())); - EXPECT_TRUE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.somedomain.org/news/football"), base::DoNothing())); - EXPECT_FALSE(guide()->MaybeLoadOptimizationHints( - GURL("https://www.unknown.com"), base::DoNothing())); + EXPECT_TRUE(CallMaybeLoadOptimizationHints(GURL("https://somedomain.org/"))); + EXPECT_TRUE(CallMaybeLoadOptimizationHints( + GURL("https://www.somedomain.org/news/football"))); + EXPECT_FALSE(CallMaybeLoadOptimizationHints(GURL("https://www.unknown.com"))); RunUntilIdle(); PreviewsUserData user_data(kDefaultPageId); - net::EffectiveConnectionType ect_threshold; // Verify whitelisting from loaded page hints. - EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_FALSE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/unhinted"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); EXPECT_EQ(base::nullopt, user_data.serialized_hint_version_string()); - EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIsWhitelisted( + EXPECT_TRUE(MaybeLoadOptimizationHintsAndCheckIfCanApplyPreview( &user_data, GURL("https://www.somedomain.org/news/weather/raininginseattle"), - PreviewsType::RESOURCE_LOADING_HINTS, &ect_threshold)); + PreviewsType::RESOURCE_LOADING_HINTS, + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); EXPECT_EQ("someversion", user_data.serialized_hint_version_string()); } -TEST_F(PreviewsOptimizationGuideTest, IsBlacklisted) { +TEST_F(PreviewsOptimizationGuideImplTest, + CanApplyPreviewWithLitePageServerPreviewsEnabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature(features::kLitePageServerPreviews); + network_quality_tracker()->ReportEffectiveConnectionTypeForTesting( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); - EXPECT_TRUE( - guide()->IsBlacklisted(GURL("https://m.blacklisteddomain.com/path"), - PreviewsType::LITE_PAGE_REDIRECT)); + PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://m.blacklisteddomain.com/path")); + EXPECT_FALSE(guide()->CanApplyPreview(&user_data, &navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); InitializeWithLitePageRedirectBlacklist(); - EXPECT_TRUE( - guide()->IsBlacklisted(GURL("https://m.blacklisteddomain.com/path"), - PreviewsType::LITE_PAGE_REDIRECT)); - EXPECT_DCHECK_DEATH(guide()->IsBlacklisted( - GURL("https://m.blacklisteddomain.com/path"), PreviewsType::NOSCRIPT)); + EXPECT_FALSE(guide()->CanApplyPreview(&user_data, &navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); - EXPECT_TRUE(guide()->IsBlacklisted( - GURL("https://blacklistedsubdomain.maindomain.co.in"), + content::MockNavigationHandle blacklisted_subdomain_navigation_handle; + blacklisted_subdomain_navigation_handle.set_url( + GURL("https://blacklistedsubdomain.maindomain.co.in")); + EXPECT_FALSE(guide()->CanApplyPreview( + &user_data, &blacklisted_subdomain_navigation_handle, PreviewsType::LITE_PAGE_REDIRECT)); - EXPECT_FALSE(guide()->IsBlacklisted(GURL("https://maindomain.co.in"), - PreviewsType::LITE_PAGE_REDIRECT)); + content::MockNavigationHandle main_domain_navigation_handle; + main_domain_navigation_handle.set_url(GURL("https://maindomain.co.in")); + EXPECT_TRUE(guide()->CanApplyPreview(&user_data, + &main_domain_navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); } -TEST_F(PreviewsOptimizationGuideTest, - IsBlacklistedWithLitePageServerPreviewsDisabled) { +TEST_F(PreviewsOptimizationGuideImplTest, + CanApplyPreviewWithLitePageServerPreviewsDisabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndDisableFeature(features::kLitePageServerPreviews); + network_quality_tracker()->ReportEffectiveConnectionTypeForTesting( + net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); InitializeWithLitePageRedirectBlacklist(); - EXPECT_TRUE( - guide()->IsBlacklisted(GURL("https://m.blacklisteddomain.com/path"), - PreviewsType::LITE_PAGE_REDIRECT)); + PreviewsUserData user_data(kDefaultPageId); + content::MockNavigationHandle navigation_handle; + navigation_handle.set_url(GURL("https://m.blacklisteddomain.com/path")); + EXPECT_FALSE(guide()->CanApplyPreview(&user_data, &navigation_handle, + PreviewsType::LITE_PAGE_REDIRECT)); } -TEST_F(PreviewsOptimizationGuideTest, RemoveObserverCalledAtDestruction) { +TEST_F(PreviewsOptimizationGuideImplTest, RemoveObserverCalledAtDestruction) { EXPECT_FALSE(optimization_guide_service()->RemoveObserverCalled()); ResetGuide(); @@ -1857,7 +1876,7 @@ TEST_F(PreviewsOptimizationGuideTest, RemoveObserverCalledAtDestruction) { EXPECT_TRUE(optimization_guide_service()->RemoveObserverCalled()); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherEnabledNoHosts) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherEnabledNoHosts) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -1876,7 +1895,7 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherEnabledNoHosts) { EXPECT_FALSE(hints_fetcher()->hints_fetched()); } -TEST_F(PreviewsOptimizationGuideTest, +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherEnabledWithHostsNoHintsInResponse) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -1906,7 +1925,7 @@ TEST_F(PreviewsOptimizationGuideTest, EXPECT_CALL(*top_host_provider(), GetTopHosts(testing::_)).Times(0); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherEnabledWithHosts) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherEnabledWithHosts) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -1929,7 +1948,7 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherEnabledWithHosts) { EXPECT_TRUE(hints_fetcher()->hints_fetched()); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherTimerRetryDelay) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherTimerRetryDelay) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -1959,7 +1978,7 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherTimerRetryDelay) { EXPECT_TRUE(hints_fetcher()->hints_fetched()); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherTimerFetchSucceeds) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherTimerFetchSucceeds) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -1993,7 +2012,7 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherTimerFetchSucceeds) { EXPECT_TRUE(hints_fetcher()->hints_fetched()); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherDisabled) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherDisabled) { base::test::ScopedFeatureList scoped_list; scoped_list.InitAndDisableFeature( optimization_guide::features::kOptimizationHintsFetching); @@ -2006,7 +2025,7 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherDisabled) { InitializeFixedCountResourceLoadingHints(); } -TEST_F(PreviewsOptimizationGuideTest, HintsFetcherLastFetchAtttempt) { +TEST_F(PreviewsOptimizationGuideImplTest, HintsFetcherLastFetchAtttempt) { base::HistogramTester histogram_tester; base::test::ScopedFeatureList scoped_list; scoped_list.InitAndEnableFeature( @@ -2045,14 +2064,14 @@ TEST_F(PreviewsOptimizationGuideTest, HintsFetcherLastFetchAtttempt) { } class PreviewsOptimizationGuideDataSaverOffTest - : public PreviewsOptimizationGuideTest { + : public PreviewsOptimizationGuideImplTest { public: PreviewsOptimizationGuideDataSaverOffTest() {} ~PreviewsOptimizationGuideDataSaverOffTest() override {} void SetUp() override { - PreviewsOptimizationGuideTest::SetUp(); + PreviewsOptimizationGuideImplTest::SetUp(); DisableDataSaver(); } diff --git a/chromium/components/previews/content/previews_ui_service_unittest.cc b/chromium/components/previews/content/previews_ui_service_unittest.cc index eede77b0d6b..a5ae8be8ea4 100644 --- a/chromium/components/previews/content/previews_ui_service_unittest.cc +++ b/chromium/components/previews/content/previews_ui_service_unittest.cc @@ -10,7 +10,7 @@ #include "base/memory/ref_counted.h" #include "base/run_loop.h" #include "base/single_thread_task_runner.h" -#include "base/test/scoped_task_environment.h" +#include "base/test/task_environment.h" #include "base/time/default_clock.h" #include "components/blacklist/opt_out_blacklist/opt_out_blacklist_data.h" #include "components/previews/content/previews_decider_impl.h" @@ -214,7 +214,7 @@ class PreviewsUIServiceTest : public testing::Test { protected: // Run this test on a single thread. - base::test::ScopedTaskEnvironment task_environment_; + base::test::TaskEnvironment task_environment_; TestPreviewsLogger* logger_ptr_; network::TestNetworkQualityTracker test_network_quality_tracker_; @@ -300,7 +300,7 @@ TEST_F(PreviewsUIServiceTest, TestLogPreviewDecisionMadePassesCorrectParams) { const base::Time time_b = base::Time::Now(); PreviewsType type_b = PreviewsType::OFFLINE; std::vector<PreviewsEligibilityReason> passed_reasons_b = { - PreviewsEligibilityReason::HOST_NOT_WHITELISTED_BY_SERVER, + PreviewsEligibilityReason::NOT_ALLOWED_BY_OPTIMIZATION_GUIDE, PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, }; const std::vector<PreviewsEligibilityReason> expected_passed_reasons_b( |