summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/build/scripts/templates/feature_policy_helper.cc.tmpl
blob: 6a7d442645541ae4c084c2904f17062a63d4fe9c (plain)
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
{% from 'templates/macros.tmpl' import license, source_files_for_generated_file %}
{{license()}}

{{source_files_for_generated_file(template_file, input_files)}}

#include "third_party/blink/renderer/core/feature_policy/feature_policy_helper.h"

#include "third_party/blink/public/mojom/feature_policy/document_policy_feature.mojom-blink.h"
#include "third_party/blink/public/mojom/feature_policy/feature_policy.mojom-blink.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

namespace {

{% for feature in feature_policy_features %}
const char k{{feature.name}}PolicyName[] = "{{feature.feature_policy_name}}";
{% endfor %}

}  // namespace

// Features which depend on a flag also have the same flag controlling whether
// they are in this map. Features which are shipping as part of an origin trial
// add their feature names to this map unconditionally, as the trial token could
// be added after the HTTP header needs to be parsed. This also means that
// top-level documents which simply want to embed another page which uses an
// origin trial feature, without using the feature themselves, can use feature
// policy to allow use of the feature in subframes (The framed document will
// still require a valid origin trial token to use the feature in this scenario).
const FeatureNameMap& GetDefaultFeatureNameMap() {
  DEFINE_STATIC_LOCAL(FeatureNameMap, default_feature_name_map, ());
  if (default_feature_name_map.IsEmpty()) {
    {% for feature in feature_policy_features %}
    {% if not feature.depends_on or feature.name in fp_origin_trial_dependency_map %}
    default_feature_name_map.Set(k{{feature.name}}PolicyName,
                                 mojom::FeaturePolicyFeature::k{{feature.name}});
    {% endif %}
    {% endfor %}
    {% for runtime_feature_name, dependent_features in runtime_to_feature_policy_map.items() | sort %}
    if (RuntimeEnabledFeatures::{{runtime_feature_name}}Enabled()) {
      {% for feature in dependent_features %}
      default_feature_name_map.Set(k{{feature}}PolicyName,
                                   mojom::FeaturePolicyFeature::k{{feature}});
      {% endfor %}
    }
    {% endfor %}
  }
  return default_feature_name_map;
}

const DocumentPolicyFeatureSet& GetAvailableDocumentPolicyFeatures() {
  DEFINE_STATIC_LOCAL(DocumentPolicyFeatureSet, features, ());
  if (features.IsEmpty()) {
    {% for feature in document_policy_features %}
    {% if not feature.depends_on or feature.name in dp_origin_trial_dependency_map %}
    features.insert(mojom::DocumentPolicyFeature::k{{feature.name}});
    {% endif %}
    {% endfor %}
    {% for runtime_feature_name, dependent_features in runtime_to_document_policy_map.items() | sort %}
    if (RuntimeEnabledFeatures::{{runtime_feature_name}}Enabled()) {
      {% for feature in dependent_features %}
      features.insert(mojom::DocumentPolicyFeature::k{{feature}});
      {% endfor %}
    }
    {% endfor %}
  }
  return features;
}

// If any of the origin trial runtime feature is enabled, returns false,
// i.e. the feature is considered enabled by origin trial.
bool DisabledByOriginTrial(const String& feature_name,
                           FeatureContext* feature_context) {
  {% for feature_name, dependencies in fp_origin_trial_dependency_map.items() | sort %}
  if (feature_name == k{{feature_name}}PolicyName) {
    return
    {%- for dependency in dependencies %}
    {%- if not loop.first %} &&{% endif %}
 !RuntimeEnabledFeatures::{{dependency}}Enabled(feature_context)
    {%- endfor %};
  }
  {% endfor %}
  return false;
}

bool DisabledByOriginTrial(mojom::blink::DocumentPolicyFeature feature,
                           FeatureContext* feature_context) {
  {% for feature_name, dependencies in dp_origin_trial_dependency_map.items() | sort %}
  if (feature == mojom::DocumentPolicyFeature::k{{feature_name}}) {
    return
    {%- for dependency in dependencies %}
    {%- if not loop.first %} &&{% endif %}
 !RuntimeEnabledFeatures::{{dependency}}Enabled(feature_context)
    {%- endfor %};
  }
  {% endfor %}
  return false;
}

}  // namespace blink