Commit 1834d07f authored by Charlie Hu's avatar Charlie Hu Committed by Commit Bot

Refactor CreateFPHeader in site_per_process_browsertest

The renaming is because ParsedFeaturePolicy not necessarily represent
a feature policy header.

Previously the function is hardcoded to accept 2 declaration object as
params. The new version accepts a vector of declarations, which makes
it more generic.

Change-Id: I9e6dfa828f0579776c941734c50c09c0b54194de
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1836373Reviewed-by: default avatarAlex Moshchuk <alexmos@chromium.org>
Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Commit-Queue: Charlie Hu <chenleihu@google.com>
Cr-Commit-Position: refs/heads/master@{#703709}
parent 9bf3f4ff
...@@ -482,60 +482,52 @@ void OpenURLBlockUntilNavigationComplete(Shell* shell, const GURL& url) { ...@@ -482,60 +482,52 @@ void OpenURLBlockUntilNavigationComplete(Shell* shell, const GURL& url) {
} }
// Helper function to generate a feature policy for a single feature and a list // Helper function to generate a feature policy for a single feature and a list
// of origins. (Equivalent to the declared policy "feature origin1 origin2...".) // of origins.
void SetParsedFeaturePolicyDeclaration( // (Equivalent to the declared policy "feature origin1 origin2 ...".)
blink::ParsedFeaturePolicyDeclaration* declaration, // If the origins list is empty, it's treated as matches all origins
// (Equivalent to the declared policy "feature *")
blink::ParsedFeaturePolicyDeclaration CreateParsedFeaturePolicyDeclaration(
blink::mojom::FeaturePolicyFeature feature, blink::mojom::FeaturePolicyFeature feature,
const std::vector<GURL>& origins) { const std::vector<GURL>& origins) {
declaration->feature = feature; blink::ParsedFeaturePolicyDeclaration declaration;
const bool matches_all = origins.empty();
declaration.feature = feature;
blink::mojom::PolicyValueType feature_type = blink::mojom::PolicyValueType feature_type =
blink::FeaturePolicy::GetDefaultFeatureList().at(feature).second; blink::FeaturePolicy::GetDefaultFeatureList().at(feature).second;
declaration->fallback_value = declaration.fallback_value =
blink::PolicyValue::CreateMinPolicyValue(feature_type); matches_all ? blink::PolicyValue::CreateMaxPolicyValue(feature_type)
declaration->opaque_value = declaration->fallback_value; : blink::PolicyValue::CreateMinPolicyValue(feature_type);
if (feature == blink::mojom::FeaturePolicyFeature::kOversizedImages) { declaration.opaque_value = declaration.fallback_value;
declaration->fallback_value.SetDoubleValue(2.0);
declaration->opaque_value.SetDoubleValue(2.0); if (feature == blink::mojom::FeaturePolicyFeature::kOversizedImages &&
} !matches_all) {
DCHECK(!origins.empty()); declaration.fallback_value.SetDoubleValue(2.0);
declaration.opaque_value.SetDoubleValue(2.0);
}
for (const auto origin : origins) for (const auto origin : origins)
declaration->values.insert(std::pair<url::Origin, blink::PolicyValue>( declaration.values.insert(std::pair<url::Origin, blink::PolicyValue>(
url::Origin::Create(origin), url::Origin::Create(origin),
blink::PolicyValue::CreateMaxPolicyValue(feature_type))); blink::PolicyValue::CreateMaxPolicyValue(feature_type)));
return declaration;
} }
blink::ParsedFeaturePolicy CreateFPHeader( blink::ParsedFeaturePolicy CreateParsedFeaturePolicy(
blink::mojom::FeaturePolicyFeature feature1, const std::vector<blink::mojom::FeaturePolicyFeature>& features,
blink::mojom::FeaturePolicyFeature feature2,
const std::vector<GURL>& origins) { const std::vector<GURL>& origins) {
blink::ParsedFeaturePolicy result(2); blink::ParsedFeaturePolicy result;
SetParsedFeaturePolicyDeclaration(&(result[0]), feature1, origins); result.reserve(features.size());
SetParsedFeaturePolicyDeclaration(&(result[1]), feature2, origins); for (const auto& feature : features)
result.push_back(CreateParsedFeaturePolicyDeclaration(feature, origins));
return result; return result;
} }
// Helper function to generate a feature policy for a single feature which blink::ParsedFeaturePolicy CreateParsedFeaturePolicyMatchesAll(
// matches every origin. (Equivalent to the declared policy "feature1 *; const std::vector<blink::mojom::FeaturePolicyFeature>& features) {
// feature2 *".) return CreateParsedFeaturePolicy(features, {});
blink::ParsedFeaturePolicy CreateFPHeaderMatchesAll(
blink::mojom::FeaturePolicyFeature feature1,
blink::mojom::FeaturePolicyFeature feature2) {
blink::ParsedFeaturePolicy result(2);
blink::mojom::PolicyValueType feature_type1 =
blink::FeaturePolicy::GetDefaultFeatureList().at(feature1).second;
blink::mojom::PolicyValueType feature_type2 =
blink::FeaturePolicy::GetDefaultFeatureList().at(feature2).second;
blink::PolicyValue max_value1 =
blink::PolicyValue::CreateMaxPolicyValue(feature_type1);
blink::PolicyValue max_value2 =
blink::PolicyValue::CreateMaxPolicyValue(feature_type2);
result[0].feature = feature1;
result[0].fallback_value = max_value1;
result[0].opaque_value = max_value1;
result[1].feature = feature2;
result[1].fallback_value = max_value2;
result[1].opaque_value = max_value2;
return result;
} }
// Check frame depth on node, widget, and process all match expected depth. // Check frame depth on node, widget, and process all match expected depth.
...@@ -8753,17 +8745,18 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest, ...@@ -8753,17 +8745,18 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest,
EXPECT_TRUE(NavigateToURL(shell(), start_url)); EXPECT_TRUE(NavigateToURL(shell(), start_url));
FrameTreeNode* root = web_contents()->GetFrameTree()->root(); FrameTreeNode* root = web_contents()->GetFrameTree()->root();
EXPECT_EQ(CreateFPHeader(blink::mojom::FeaturePolicyFeature::kGeolocation, EXPECT_EQ(CreateParsedFeaturePolicy(
blink::mojom::FeaturePolicyFeature::kOversizedImages, {blink::mojom::FeaturePolicyFeature::kGeolocation,
{start_url.GetOrigin()}), blink::mojom::FeaturePolicyFeature::kOversizedImages},
{start_url.GetOrigin()}),
root->current_replication_state().feature_policy_header); root->current_replication_state().feature_policy_header);
// When the main frame navigates to a page with a new policy, it should // When the main frame navigates to a page with a new policy, it should
// overwrite the old one. // overwrite the old one.
EXPECT_TRUE(NavigateToURL(shell(), first_nav_url)); EXPECT_TRUE(NavigateToURL(shell(), first_nav_url));
EXPECT_EQ(CreateFPHeaderMatchesAll( EXPECT_EQ(CreateParsedFeaturePolicyMatchesAll(
blink::mojom::FeaturePolicyFeature::kGeolocation, {blink::mojom::FeaturePolicyFeature::kGeolocation,
blink::mojom::FeaturePolicyFeature::kOversizedImages), blink::mojom::FeaturePolicyFeature::kOversizedImages}),
root->current_replication_state().feature_policy_header); root->current_replication_state().feature_policy_header);
// When the main frame navigates to a page without a policy, the replicated // When the main frame navigates to a page without a policy, the replicated
...@@ -8783,17 +8776,18 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest, ...@@ -8783,17 +8776,18 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest,
EXPECT_TRUE(NavigateToURL(shell(), start_url)); EXPECT_TRUE(NavigateToURL(shell(), start_url));
FrameTreeNode* root = web_contents()->GetFrameTree()->root(); FrameTreeNode* root = web_contents()->GetFrameTree()->root();
EXPECT_EQ(CreateFPHeader(blink::mojom::FeaturePolicyFeature::kGeolocation, EXPECT_EQ(CreateParsedFeaturePolicy(
blink::mojom::FeaturePolicyFeature::kOversizedImages, {blink::mojom::FeaturePolicyFeature::kGeolocation,
{start_url.GetOrigin()}), blink::mojom::FeaturePolicyFeature::kOversizedImages},
{start_url.GetOrigin()}),
root->current_replication_state().feature_policy_header); root->current_replication_state().feature_policy_header);
// When the main frame navigates to a page with a new policy, it should // When the main frame navigates to a page with a new policy, it should
// overwrite the old one. // overwrite the old one.
EXPECT_TRUE(NavigateToURL(shell(), first_nav_url)); EXPECT_TRUE(NavigateToURL(shell(), first_nav_url));
EXPECT_EQ(CreateFPHeaderMatchesAll( EXPECT_EQ(CreateParsedFeaturePolicyMatchesAll(
blink::mojom::FeaturePolicyFeature::kGeolocation, {blink::mojom::FeaturePolicyFeature::kGeolocation,
blink::mojom::FeaturePolicyFeature::kOversizedImages), blink::mojom::FeaturePolicyFeature::kOversizedImages}),
root->current_replication_state().feature_policy_header); root->current_replication_state().feature_policy_header);
// When the main frame navigates to a page without a policy, the replicated // When the main frame navigates to a page without a policy, the replicated
...@@ -8815,23 +8809,25 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest, ...@@ -8815,23 +8809,25 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest,
EXPECT_TRUE(NavigateToURL(shell(), main_url)); EXPECT_TRUE(NavigateToURL(shell(), main_url));
FrameTreeNode* root = web_contents()->GetFrameTree()->root(); FrameTreeNode* root = web_contents()->GetFrameTree()->root();
EXPECT_EQ(CreateFPHeader(blink::mojom::FeaturePolicyFeature::kGeolocation, EXPECT_EQ(CreateParsedFeaturePolicy(
blink::mojom::FeaturePolicyFeature::kOversizedImages, {blink::mojom::FeaturePolicyFeature::kGeolocation,
{main_url.GetOrigin(), GURL("http://example.com/")}), blink::mojom::FeaturePolicyFeature::kOversizedImages},
{main_url.GetOrigin(), GURL("http://example.com/")}),
root->current_replication_state().feature_policy_header); root->current_replication_state().feature_policy_header);
EXPECT_EQ(1UL, root->child_count()); EXPECT_EQ(1UL, root->child_count());
EXPECT_EQ( EXPECT_EQ(
CreateFPHeader(blink::mojom::FeaturePolicyFeature::kGeolocation, CreateParsedFeaturePolicy(
blink::mojom::FeaturePolicyFeature::kOversizedImages, {blink::mojom::FeaturePolicyFeature::kGeolocation,
{main_url.GetOrigin()}), blink::mojom::FeaturePolicyFeature::kOversizedImages},
{main_url.GetOrigin()}),
root->child_at(0)->current_replication_state().feature_policy_header); root->child_at(0)->current_replication_state().feature_policy_header);
// Navigate the iframe cross-site. // Navigate the iframe cross-site.
NavigateFrameToURL(root->child_at(0), first_nav_url); NavigateFrameToURL(root->child_at(0), first_nav_url);
EXPECT_EQ( EXPECT_EQ(
CreateFPHeaderMatchesAll( CreateParsedFeaturePolicyMatchesAll(
blink::mojom::FeaturePolicyFeature::kGeolocation, {blink::mojom::FeaturePolicyFeature::kGeolocation,
blink::mojom::FeaturePolicyFeature::kOversizedImages), blink::mojom::FeaturePolicyFeature::kOversizedImages}),
root->child_at(0)->current_replication_state().feature_policy_header); root->child_at(0)->current_replication_state().feature_policy_header);
// Navigate the iframe to another location, this one with no policy header // Navigate the iframe to another location, this one with no policy header
...@@ -8843,9 +8839,9 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest, ...@@ -8843,9 +8839,9 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyBrowserTest,
// Navigate the iframe back to a page with a policy // Navigate the iframe back to a page with a policy
NavigateFrameToURL(root->child_at(0), first_nav_url); NavigateFrameToURL(root->child_at(0), first_nav_url);
EXPECT_EQ( EXPECT_EQ(
CreateFPHeaderMatchesAll( CreateParsedFeaturePolicyMatchesAll(
blink::mojom::FeaturePolicyFeature::kGeolocation, {blink::mojom::FeaturePolicyFeature::kGeolocation,
blink::mojom::FeaturePolicyFeature::kOversizedImages), blink::mojom::FeaturePolicyFeature::kOversizedImages}),
root->child_at(0)->current_replication_state().feature_policy_header); root->child_at(0)->current_replication_state().feature_policy_header);
} }
...@@ -8874,9 +8870,9 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyJavaScriptBrowserTest, ...@@ -8874,9 +8870,9 @@ IN_PROC_BROWSER_TEST_F(SitePerProcessFeaturePolicyJavaScriptBrowserTest,
// exists.) // exists.)
NavigateFrameToURL(root->child_at(1), first_nav_url); NavigateFrameToURL(root->child_at(1), first_nav_url);
EXPECT_EQ( EXPECT_EQ(
CreateFPHeaderMatchesAll( CreateParsedFeaturePolicyMatchesAll(
blink::mojom::FeaturePolicyFeature::kGeolocation, {blink::mojom::FeaturePolicyFeature::kGeolocation,
blink::mojom::FeaturePolicyFeature::kOversizedImages), blink::mojom::FeaturePolicyFeature::kOversizedImages}),
root->child_at(1)->current_replication_state().feature_policy_header); root->child_at(1)->current_replication_state().feature_policy_header);
EXPECT_EQ(1UL, root->child_at(1)->child_count()); EXPECT_EQ(1UL, root->child_at(1)->child_count());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment