1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/preloading/prefetch/prefetch_network_context.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/preloading/prefetch/prefetch_service.h"
#include "content/browser/preloading/prefetch/prefetch_test_util_internal.h"
#include "content/browser/preloading/prefetch/prefetch_type.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_features.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "net/base/isolation_info.h"
namespace content {
namespace {
// "arg" type is `url::Origin`.
// `url` type is `GURL`.
MATCHER_P(IsSameOriginWith, url, "") {
return arg.IsSameOriginWith(url);
}
// "arg" type is `net::IsolationInfo`.
MATCHER(IsEmptyIsolationInfo, "") {
return arg.IsEmpty();
}
class PrefetchNetworkContextTest : public RenderViewHostTestHarness {
public:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
test_content_browser_client_ = std::make_unique<
::testing::StrictMock<ScopedMockContentBrowserClient>>();
prefetch_service_ = std::make_unique<PrefetchService>(browser_context());
}
void TearDown() override {
prefetch_service_.reset();
test_content_browser_client_.reset();
RenderViewHostTestHarness::TearDown();
}
ScopedMockContentBrowserClient* test_content_browser_client() {
return test_content_browser_client_.get();
}
PrefetchService* prefetch_service() const { return prefetch_service_.get(); }
private:
std::unique_ptr<ScopedMockContentBrowserClient> test_content_browser_client_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<PrefetchService> prefetch_service_;
};
TEST_F(PrefetchNetworkContextTest, CreateIsolatedURLLoaderFactory) {
const GURL kReferringUrl = GURL("https://test.referring.origin.com");
NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
kReferringUrl);
EXPECT_CALL(*test_content_browser_client(),
WillCreateURLLoaderFactory(
testing::NotNull(), main_rfh(),
main_rfh()->GetProcess()->GetDeprecatedID(),
ContentBrowserClient::URLLoaderFactoryType::kPrefetch,
IsSameOriginWith(kReferringUrl), IsEmptyIsolationInfo(),
testing::Eq(std::nullopt),
ukm::SourceIdObj::FromInt64(main_rfh()->GetPageUkmSourceId()),
testing::_, testing::NotNull(), testing::NotNull(),
testing::IsNull(), testing::IsNull(), testing::IsNull()));
std::unique_ptr<PrefetchNetworkContext> prefetch_network_context =
std::make_unique<PrefetchNetworkContext>(
/*use_isolated_network_context=*/true,
PrefetchType(PreloadingTriggerType::kSpeculationRule,
/*use_prefetch_proxy=*/false,
blink::mojom::SpeculationEagerness::kEager),
main_rfh()->GetGlobalId(), main_rfh()->GetLastCommittedOrigin());
prefetch_network_context->GetURLLoaderFactory(prefetch_service());
}
TEST_F(PrefetchNetworkContextTest,
CreateURLLoaderFactoryInDefaultNetworkContext) {
const GURL kReferringUrl = GURL("https://test.referring.origin.com");
NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
kReferringUrl);
EXPECT_CALL(*test_content_browser_client(),
WillCreateURLLoaderFactory(
testing::NotNull(), main_rfh(),
main_rfh()->GetProcess()->GetDeprecatedID(),
ContentBrowserClient::URLLoaderFactoryType::kPrefetch,
IsSameOriginWith(kReferringUrl), IsEmptyIsolationInfo(),
testing::Eq(std::nullopt),
ukm::SourceIdObj::FromInt64(main_rfh()->GetPageUkmSourceId()),
testing::_, testing::NotNull(), testing::NotNull(),
testing::IsNull(), testing::IsNull(), testing::IsNull()));
std::unique_ptr<PrefetchNetworkContext> prefetch_network_context =
std::make_unique<PrefetchNetworkContext>(
/*use_isolated_network_context=*/false,
PrefetchType(PreloadingTriggerType::kSpeculationRule,
/*use_prefetch_proxy=*/false,
blink::mojom::SpeculationEagerness::kEager),
main_rfh()->GetGlobalId(), main_rfh()->GetLastCommittedOrigin());
prefetch_network_context->GetURLLoaderFactory(prefetch_service());
}
TEST_F(PrefetchNetworkContextTest,
CreateURLLoaderFactoryForBrowserInitiatedTriggersNetworkContext) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kPrefetchBrowserInitiatedTriggers);
const GURL kReferringUrl = GURL("https://test.referring.origin.com");
const url::Origin kReferringOrigin = url::Origin::Create(kReferringUrl);
EXPECT_CALL(
*test_content_browser_client(),
WillCreateURLLoaderFactory(
testing::NotNull(), testing::IsNull(),
testing::Eq(content::ChildProcessHost::kInvalidUniqueID),
ContentBrowserClient::URLLoaderFactoryType::kPrefetch,
IsSameOriginWith(kReferringUrl), IsEmptyIsolationInfo(),
testing::Eq(std::nullopt), testing::Eq(ukm::kInvalidSourceIdObj),
testing::_, testing::NotNull(), testing::NotNull(), testing::IsNull(),
testing::IsNull(), testing::IsNull()));
std::unique_ptr<PrefetchNetworkContext> prefetch_network_context =
std::make_unique<PrefetchNetworkContext>(
/*use_isolated_network_context=*/false,
PrefetchType(PreloadingTriggerType::kEmbedder,
/*use_prefetch_proxy=*/false),
/*referring_render_frame_host_id=*/GlobalRenderFrameHostId(),
kReferringOrigin);
prefetch_network_context->GetURLLoaderFactory(prefetch_service());
}
} // namespace
} // namespace content
|