Commit 8985886d authored by Charlie Hu's avatar Charlie Hu Committed by Chromium LUCI CQ

[Document Policy] Supports runtime flag controlled default value

Example config in "document_policy_features.cc":
    {
      name: "DefaultValueFeatureForTest",
      document_policy_name: "_reserved",
      value_type: "Bool",
      default_value: "true",
      depends_on: [],
      default_value_behind_flag: [
        ["PaintHolding", "false"],
        ["ScriptStreaming", "true"],
      ]
    },

Generated output:
const DocumentPolicyFeatureInfoMap& GetDocumentPolicyFeatureInfoMap() {
  static const base::NoDestructor<DocumentPolicyFeatureInfoMap> feature_info_map([] {
    DocumentPolicyFeatureInfoMap map({        {
          mojom::DocumentPolicyFeature::kDefaultValueFeatureForTest,
          {
            "",
            PolicyValue::CreateBool(true)
          }
        }, ...});

        if (base::FeatureList::IsEnabled(features::kPaintHolding)) {
          map.insert_or_assign(
            mojom::DocumentPolicyFeature::kDefaultValueFeatureForTest,
            DocumentPolicyFeatureInfo {
              "",
              PolicyValue::CreateBool(false)
            }
          );
        } else if (base::FeatureList::IsEnabled(features::kScriptStreaming)) {
          map.insert_or_assign(
            mojom::DocumentPolicyFeature::kDefaultValueFeatureForTest,
            DocumentPolicyFeatureInfo {
              "",
              PolicyValue::CreateBool(true)
            }
          );
        }
    return map;
  }());
  return *feature_info_map;
}

Bug: 1166698
Change-Id: Ib0bf5905ddcccface2673486181616662c6e2b7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2534612
Commit-Queue: Charlie Hu <chenleihu@google.com>
Reviewed-by: default avatarAlex Moshchuk <alexmos@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843716}
parent ec7c48cc
......@@ -6767,6 +6767,70 @@ IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
EXPECT_EQ("POST", root_frame_host()->last_http_method());
}
struct DocumentPolicyDefaultValueBrowserTestCase {
std::initializer_list<base::Feature> enabled_features;
std::initializer_list<base::Feature> disabled_features;
bool expected_feature_state;
};
// The test on Document Policy default value control must be browsertest
// as the initialization of default value in
// |blink::GetDocumentPolicyFeatureInfoMap| only happens once per browser
// instance.
class DocumentPolicyDefaultValueBrowserTest
: public RenderFrameHostImplBrowserTest,
public ::testing::WithParamInterface<
DocumentPolicyDefaultValueBrowserTestCase> {
public:
static const DocumentPolicyDefaultValueBrowserTestCase kCases[];
DocumentPolicyDefaultValueBrowserTest() {
const auto& test_case = GetParam();
feature_list_.InitWithFeatures(test_case.enabled_features,
test_case.disabled_features);
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Following flag control logic is defined in
// blink/renderer/core/feature_policy/document_policy_features.json5.
const DocumentPolicyDefaultValueBrowserTestCase
DocumentPolicyDefaultValueBrowserTest::kCases[] = {
{{},
{blink::features::kDocumentPolicyRuntimeFlag1ForTest,
blink::features::kDocumentPolicyRuntimeFlag2ForTest},
true},
{{blink::features::kDocumentPolicyRuntimeFlag1ForTest},
{blink::features::kDocumentPolicyRuntimeFlag2ForTest},
false},
{{blink::features::kDocumentPolicyRuntimeFlag2ForTest},
{blink::features::kDocumentPolicyRuntimeFlag1ForTest},
true},
{{blink::features::kDocumentPolicyRuntimeFlag2ForTest,
blink::features::kDocumentPolicyRuntimeFlag1ForTest},
{},
false},
};
INSTANTIATE_TEST_SUITE_P(
All,
DocumentPolicyDefaultValueBrowserTest,
testing::ValuesIn(DocumentPolicyDefaultValueBrowserTest::kCases));
IN_PROC_BROWSER_TEST_P(DocumentPolicyDefaultValueBrowserTest,
DocumentPolicyDefaultValueBehindFlag) {
const auto& test_case = GetParam();
std::unique_ptr<blink::DocumentPolicy> document_policy =
blink::DocumentPolicy::CreateWithHeaderPolicy({});
EXPECT_EQ(
document_policy->IsFeatureEnabled(
blink::mojom::DocumentPolicyFeature::kDefaultValueFeatureForTest),
test_case.expected_feature_state);
}
// Check Chrome won't attempt automatically loading the /favicon.ico if it would
// be blocked by CSP.
IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
......
......@@ -866,5 +866,11 @@ const base::Feature kInterestCohortAPIOriginTrial{
const base::Feature kInterestCohortFeaturePolicy{
"InterestCohortFeaturePolicy", base::FEATURE_DISABLED_BY_DEFAULT};
// Flags only used for testing purposes. No effect when enabled.
const base::Feature kDocumentPolicyRuntimeFlag1ForTest{
"kDocumentPolicyRuntimeFlag1ForTest", base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kDocumentPolicyRuntimeFlag2ForTest{
"kDocumentPolicyRuntimeFlag2ForTest", base::FEATURE_DISABLED_BY_DEFAULT};
} // namespace features
} // namespace blink
......@@ -356,6 +356,13 @@ BLINK_COMMON_EXPORT extern const base::Feature kInterestCohortAPIOriginTrial;
BLINK_COMMON_EXPORT extern const base::Feature kInterestCohortFeaturePolicy;
// Flags used to test DocumentPolicy's default value being controlled by
// different runtime flag states.
BLINK_COMMON_EXPORT extern const base::Feature
kDocumentPolicyRuntimeFlag1ForTest;
BLINK_COMMON_EXPORT extern const base::Feature
kDocumentPolicyRuntimeFlag2ForTest;
} // namespace features
} // namespace blink
......
......@@ -36,6 +36,9 @@ enum DocumentPolicyFeature {
kJSProfiling = 11,
// Controls use of synchronous XMLHTTPRequest API.
kSyncXHR = 12,
// Feature for testing 'default_value_behind_flag' field in
// document_policy_features.json5.
kDefaultValueFeatureForTest = 13,
// Don't change assigned numbers of any item, and don't reuse removed slots.
// Add new features at the end of the enum.
// Also, run update_document_policy_enum.py in
......
......@@ -4,6 +4,8 @@
{{ source_files_for_generated_file(template_file, input_files) }}
#include "base/no_destructor.h"
#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/feature_policy/document_policy_features.h"
#include "third_party/blink/public/common/feature_policy/policy_value.h"
#include "third_party/blink/public/mojom/feature_policy/policy_value.mojom.h"
......@@ -13,17 +15,38 @@
namespace blink {
const DocumentPolicyFeatureInfoMap& GetDocumentPolicyFeatureInfoMap() {
static const base::NoDestructor<DocumentPolicyFeatureInfoMap> feature_info_map({
{%- for feature in features %}
{
mojom::DocumentPolicyFeature::k{{feature.name}},
static const base::NoDestructor<DocumentPolicyFeatureInfoMap> feature_info_map([] {
DocumentPolicyFeatureInfoMap map({
{%- for feature in features %}
{
"{{feature.document_policy_name}}",
{{parse_default_value(feature.default_value, feature.value_type)}}
mojom::DocumentPolicyFeature::k{{feature.name}},
{
"{{feature.document_policy_name}}",
{{parse_default_value(feature.default_value, feature.value_type)}}
}
},
{%- endfor %}
});
{%- for feature in features %}
{%- if feature.default_value_behind_flag %}
{%- for flag, default_value in feature.default_value_behind_flag %}
{%- if not loop.first %} else {% endif %}
if (base::FeatureList::IsEnabled(features::k{{flag}})) {
map.insert_or_assign(
mojom::DocumentPolicyFeature::k{{feature.name}},
DocumentPolicyFeatureInfo {
"{{feature.document_policy_name}}",
{{parse_default_value(default_value, feature.value_type)}}
}
);
}
},
{%- endfor %}
{%- endif %}
{%- endfor %}
});
return map;
}());
return *feature_info_map;
}
......@@ -38,5 +61,3 @@ const DocumentPolicyNameFeatureMap& GetDocumentPolicyNameFeatureMap() {
}
} // namespace blink
......@@ -12,7 +12,6 @@
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
namespace {
{% for feature in feature_policy_features %}
......
{
// All document policy (https://w3c.github.io/webappsec-feature-policy/document-policy.html)
// features are defined here.
// All Features have to be defined in FeaturePolicyFeature enum as well
// (defined in third_party/blink/public/mojom/feature_policy/feature_policy.mojom).
// All Features have to be defined in DocumentPolicyFeature enum as well
// (defined in third_party/blink/public/mojom/feature_policy/document_policy_feature.mojom).
// The enum value has to have the same name as the feature name here.
parameters: {
......@@ -26,9 +26,42 @@
default: [],
valid_type: "list",
},
// "default_value_behind_flag" specifies default_value override values
// based on different runtime flags set.
//
// When multiple flags are set, default_value correspond to the first
// flag in the list will be used, e.g.
// default_value_behind_flag: [
// ["A", 1.0],
// ["B", 2.0],
// ]
// 1.0 will be used as default value when both flag A and B are set.
//
// Note: the runtime flags here refer to features defined in
// "third_party/blink/public/common/features.h", instead of those defined in
// "runtime_enabled_features.json5" because the latter is only available
// on renderer side, while default_value is needed from browser side as
// well.
default_value_behind_flag: {
default: [],
valid_type: "list",
}
},
data: [
{
name: "DefaultValueFeatureForTest",
// Setting document_policy_name to "", so that it will not be recognized
// by the parser, as structured header token cannot be empty.
document_policy_name: "",
value_type: "Bool",
default_value: "true",
depends_on: [],
default_value_behind_flag: [
["DocumentPolicyRuntimeFlag1ForTest", "false"],
["DocumentPolicyRuntimeFlag2ForTest", "true"],
]
},
{
name: "Default",
document_policy_name: "*",
......
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