#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/predictors/loading_predictor.h"
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_features.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_preconnect_client.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/predictors/lcp_critical_path_predictor/lcp_critical_path_predictor_util.h"
#include "chrome/browser/predictors/loading_predictor_factory.h"
#include "chrome/browser/predictors/loading_test_util.h"
#include "chrome/browser/predictors/preconnect_manager.h"
#include "chrome/browser/predictors/predictors_enums.h"
#include "chrome/browser/predictors/predictors_features.h"
#include "chrome/browser/predictors/predictors_switches.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_handle.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/referrer.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/simple_url_loader_test_helper.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "net/base/features.h"
#include "net/base/network_anonymization_key.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/connection_tracker.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/embedded_test_server_connection_listener.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/cors/cors_error_status.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/clear_data_filter.mojom.h"
#include "services/network/public/mojom/cors.mojom.h"
#include "services/network/public/mojom/ip_address_space.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "url/gurl.h"
#include "url/origin.h"
BrowserThread;
Optional;
SizeIs;
namespace predictors {
const char kChromiumUrl[] = …;
const char kHtmlSubresourcesPath[] = …;
const char* const kHtmlSubresourcesHosts[] = …;
std::string GetPathWithPortReplacement(const std::string& path, uint16_t port) { … }
GURL GetDataURLWithContent(const std::string& content) { … }
class PredictorInitializer : public TestObserver { … };
class LcpElementLearnWaiter : public TestObserver { … };
class TestPreconnectManagerObserver : public PreconnectManager::Observer { … };
struct PrefetchResult { … };
class TestPrefetchManagerObserver : public PrefetchManager::Observer { … };
class LoadingPredictorBrowserTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, SimpleNavigation) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, TwoConcurrentNavigations) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest,
TwoNavigationsToTheSameURL) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, NonHttpNavigation) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest,
PrepareForPageLoadNonHttpScheme) { … }
namespace {
class TestPrerenderStopObserver
: public prerender::NoStatePrefetchHandle::Observer { … };
}
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest,
PrepareForPageLoadWithoutPrediction) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, LearnFromNavigation) { … }
class LoadingPredictorBrowserTestLearnAllResources
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTestLearnAllResources,
LearnFromNavigation) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest,
LearnFromNavigationWithRedirect) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest,
PrepareForPageLoadWithPrediction) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, DnsPrefetch) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTest, PreconnectNonCors) { … }
class LCPCriticalPathPredictorBrowserTest : public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LCPCriticalPathPredictorBrowserTest,
LearnLCPPFromNavigation) { … }
IN_PROC_BROWSER_TEST_F(LCPCriticalPathPredictorBrowserTest, LearnLCPPFont) { … }
class SuppressesLoadingPredictorOnSlowNetworkBrowserTest
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(SuppressesLoadingPredictorOnSlowNetworkBrowserTest,
SuppressesOnSlowNetwork) { … }
enum class NetworkIsolationKeyMode { … };
class LoadingPredictorNetworkIsolationKeyBrowserTest
: public LoadingPredictorBrowserTest,
public testing::WithParamInterface<NetworkIsolationKeyMode> { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
LoadingPredictorNoRedirects) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
LoadingPredictorWithRedirects) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
LoadingPredictorWithRedirects2) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
LinkRelPreconnectMainFrame) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
LinkRelPreconnectSubFrame) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
PreconnectNonCorsAndFetchNonCors) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
PreconnectNonCorsAndFetchCors) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
PreconnectCorsAndFetchNonCors) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorNetworkIsolationKeyBrowserTest,
PreconnectCorsAndFetchCors) { … }
class LoadingPredictorBrowserTestWithProxy
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTestWithProxy,
PrepareForPageLoadWithoutPrediction) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTestWithProxy,
PrepareForPageLoadWithPrediction) { … }
class LoadingPredictorBrowserTestWithOptimizationGuide
: public ::testing::WithParamInterface<
std::tuple<bool, bool, bool, std::string>>,
public LoadingPredictorBrowserTest { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(LoadingPredictorBrowserTestWithOptimizationGuide,
NavigationHasLocalPredictionNoOptimizationHint) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorBrowserTestWithOptimizationGuide,
NavigationWithBothLocalPredictionAndOptimizationHint) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorBrowserTestWithOptimizationGuide,
NavigationWithNoLocalPredictionsButHasOptimizationHint) { … }
IN_PROC_BROWSER_TEST_P(
LoadingPredictorBrowserTestWithOptimizationGuide,
OptimizationGuidePredictionsNotAppliedForAlreadyCommittedNavigation) { … }
IN_PROC_BROWSER_TEST_P(LoadingPredictorBrowserTestWithOptimizationGuide,
OptimizationGuidePredictionsNotAppliedForRedirect) { … }
class LoadingPredictorBrowserTestWithNoLocalPredictions
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LoadingPredictorBrowserTestWithNoLocalPredictions,
ShouldNotActOnLocalPrediction) { … }
class LoadingPredictorPrefetchBrowserTest
: public LoadingPredictorBrowserTestWithOptimizationGuide { … };
IN_PROC_BROWSER_TEST_P(
LoadingPredictorPrefetchBrowserTest,
DISABLED_PrepareForPageLoadWithPredictionForPrefetchNoLocalHint) { … }
IN_PROC_BROWSER_TEST_P(
LoadingPredictorPrefetchBrowserTest,
PrepareForPageLoadWithPredictionForPrefetchHasLocalHint) { … }
class LoadingPredictorPrefetchBrowserTestWithBlockedLocalRequest
: public LoadingPredictorPrefetchBrowserTest { … };
IN_PROC_BROWSER_TEST_P(
LoadingPredictorPrefetchBrowserTestWithBlockedLocalRequest,
DISABLED_PrepareForPageLoadWithPredictionForPrefetch) { … }
class LoadingPredictorPrefetchCounterfactualBrowserTest
: public LoadingPredictorPrefetchBrowserTest { … };
IN_PROC_BROWSER_TEST_P(
LoadingPredictorPrefetchCounterfactualBrowserTest,
PrepareForPageLoadWithPredictionForPrefetchHasLocalHint) { … }
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
class LoadingPredictorMultiplePageBrowserTest
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(LoadingPredictorMultiplePageBrowserTest,
PrerenderNavigationNotObserved) { … }
IN_PROC_BROWSER_TEST_F(LoadingPredictorMultiplePageBrowserTest,
PrerenderActivationNotObserved) { … }
#if BUILDFLAG(IS_LINUX)
#define MAYBE_BackForwardCacheNavigationNotObserved …
#else
#define MAYBE_BackForwardCacheNavigationNotObserved …
#endif
IN_PROC_BROWSER_TEST_F(LoadingPredictorMultiplePageBrowserTest,
MAYBE_BackForwardCacheNavigationNotObserved) { … }
class FencedFrameLoadingPredictorBrowserTest
: public LoadingPredictorBrowserTest { … };
IN_PROC_BROWSER_TEST_F(FencedFrameLoadingPredictorBrowserTest, DnsPrefetch) { … }
IN_PROC_BROWSER_TEST_F(FencedFrameLoadingPredictorBrowserTest,
NetworkCutoffDisablesDnsPrefetch) { … }
IN_PROC_BROWSER_TEST_F(FencedFrameLoadingPredictorBrowserTest,
DnsPrefetchFromLinkHeader) { … }
IN_PROC_BROWSER_TEST_F(FencedFrameLoadingPredictorBrowserTest,
NetworkCutoffDisablesDnsPrefetchFromLinkHeader) { … }
}