chromium/third_party/blink/renderer/build/scripts/templates/permissions_policy_features_generated.cc.tmpl

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "third_party/blink/common/permissions_policy/permissions_policy_features_generated.h"

#include "base/no_destructor.h"
#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy_feature.mojom.h"

namespace blink {

// Applies new defaults based on flags.
void UpdatePermissionsPolicyFeatureListFlagDefaults(PermissionsPolicyFeatureList& mutable_feature_list) {
  {% 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}})) {
    mutable_feature_list.find(mojom::PermissionsPolicyFeature::k{{feature.name}})->second =
      PermissionsPolicyFeatureDefault::{{default_value}};
  }
    {%- endfor %}
 else {
    mutable_feature_list.find(mojom::PermissionsPolicyFeature::k{{feature.name}})->second =
      PermissionsPolicyFeatureDefault::{{feature.feature_default}};
  }

  {% endif %}
  {%- endfor %}
}

// The base list with no flags or special cases.
PermissionsPolicyFeatureList GetBasePermissionsPolicyFeatureList() {
  static const base::NoDestructor<PermissionsPolicyFeatureList> feature_list({
      {%- for feature in features %}
        {
          mojom::PermissionsPolicyFeature::k{{feature.name}},
          PermissionsPolicyFeatureDefault::{{feature.feature_default}}
        },
      {%- endfor %}
    });
  return *feature_list;
}

// Defaults with "unload" using EnabledForNone.
PermissionsPolicyFeatureList& GetPermissionsPolicyFeatureListUnloadNone() {
  static base::NoDestructor<PermissionsPolicyFeatureList> feature_list([] {
    PermissionsPolicyFeatureList map = GetBasePermissionsPolicyFeatureList();
    UpdatePermissionsPolicyFeatureListFlagDefaults(map);

{# Apply special case for unload if present. #}
{% for feature in features %}
  {% if feature.name.original is equalto "Unload" %}
    map.find(mojom::PermissionsPolicyFeature::kUnload)->second =
        PermissionsPolicyFeatureDefault::EnableForNone;
  {% endif %}
{% endfor %}

    return map;
  }());
  return *feature_list;
}

// Defaults with "unload" using EnabledForAll.
PermissionsPolicyFeatureList& GetPermissionsPolicyFeatureListUnloadAll() {
  static base::NoDestructor<PermissionsPolicyFeatureList> feature_list([] {
    PermissionsPolicyFeatureList map = GetBasePermissionsPolicyFeatureList();
    UpdatePermissionsPolicyFeatureListFlagDefaults(map);

{# Apply special case for unload if present. #}
{% for feature in features %}
  {% if feature.name.original is equalto "Unload" %}
    map.find(mojom::PermissionsPolicyFeature::kUnload)->second =
        PermissionsPolicyFeatureDefault::EnableForAll;
  {% endif %}
{% endfor %}

    return map;
  }());
  return *feature_list;
}

} // namespace blink