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

{% from 'templates/macros.tmpl' import license, source_files_for_generated_file, platform_buildflag %}
{{license()}}

{{source_files_for_generated_file(template_file, input_files)}}

#include "third_party/blink/public/common/origin_trials/origin_trials.h"

#include <array>
#include <iterator>
#include <string_view>

#include "base/containers/contains.h"
#include "base/ranges/algorithm.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "third_party/blink/public/mojom/origin_trial_feature/origin_trial_feature.mojom-shared.h"

// For testing. See OriginTrialsSampleAPIInvalidOS.
#define BUILDFLAG_INTERNAL_IS_INVALID() (0)

namespace blink {

namespace {

static constexpr size_t kMaxFeaturesPerTrial = {{max_features_per_trial}};
static constexpr struct TrialToFeature {
  const char* trial_name;
  unsigned feature_count;
  std::array<mojom::OriginTrialFeature, kMaxFeaturesPerTrial> features;
} kTrialToFeaturesMap[] = {
    {% for trial_name, features_list in trial_to_features_map.items() %}
    { "{{trial_name}}", {{features_list|length}}, { {%- for trial_feature in features_list %}mojom::OriginTrialFeature::k{{trial_feature.name}}, {%- endfor %} } },
    {% endfor %}
    // For testing
    { "This trial does not exist", 1, { mojom::OriginTrialFeature::kNonExisting } },
};

} // namespace

bool origin_trials::IsTrialValid(std::string_view trial_name) {
  return base::Contains(kTrialToFeaturesMap, trial_name,
                        &TrialToFeature::trial_name);
}

bool origin_trials::IsTrialEnabledForInsecureContext(std::string_view trial_name) {
  static const char* const kEnabledForInsecureContext[] = {
  {% for trial in origin_trial_features|selectattr('origin_trial_allows_insecure')|map(attribute='origin_trial_feature_name')|unique %}
      "{{trial}}",
  {% endfor %}
  };
  return base::Contains(kEnabledForInsecureContext, trial_name);
}

bool origin_trials::IsTrialEnabledForThirdPartyOrigins(std::string_view trial_name) {
  static const char* const kEnabledForThirdPartyOrigins[] = {
  {% for trial in origin_trial_features|selectattr('origin_trial_allows_third_party')|map(attribute='origin_trial_feature_name')|unique %}
      "{{trial}}",
  {% endfor %}
  };
  return base::Contains(kEnabledForThirdPartyOrigins, trial_name);
}

bool origin_trials::IsTrialEnabledForBrowserProcessReadAccess(std::string_view trial_name) {
  // Select all features that represent origin trials and have
  // browser_process_read_write_access enabled. Determine if that list of
  // features contains the  `trial_name` provided.
  static const char* const kEnabledForBrowserProcessReadWriteAccess[] = {
  {% for trial in origin_trial_features %}
    {% if trial.browser_process_read_access or trial.browser_process_read_write_access  %}
      "{{trial.origin_trial_feature_name}}",
    {% endif %}
  {% endfor %}
  };
  return base::Contains(kEnabledForBrowserProcessReadWriteAccess, trial_name);
}

bool origin_trials::IsDeprecationTrial(std::string_view trial_name) {
  for (auto feature : FeaturesForTrial(trial_name)) {
    if (GetTrialType(feature) == OriginTrialType::kDeprecation) {
      return true;
    }
  }
  return false;
}

OriginTrialType origin_trials::GetTrialType(mojom::OriginTrialFeature feature) {
  switch (feature) {
    {% for feature in origin_trial_features if feature.origin_trial_type %}
    case mojom::OriginTrialFeature::k{{feature.name}}:
      return OriginTrialType::k{{feature.origin_trial_type}};
    {% endfor %}
    default:
      return OriginTrialType::kDefault;
  }
}


base::span<const mojom::OriginTrialFeature> origin_trials::FeaturesForTrial(
    std::string_view trial_name) {
  auto it = base::ranges::find(kTrialToFeaturesMap, trial_name,
                               &TrialToFeature::trial_name);
  DCHECK(it != std::end(kTrialToFeaturesMap));
  return {it->features.begin(), it->feature_count};
}

base::span<const mojom::OriginTrialFeature> origin_trials::GetImpliedFeatures(
    mojom::OriginTrialFeature feature) {
{% for implied_by_name, implied_set in implied_origin_trial_features.items() %}
  if (feature == mojom::OriginTrialFeature::k{{implied_by_name}}) {
    static constexpr mojom::OriginTrialFeature implied_features[] = {
{%- for implied_name in implied_set|sort %}
mojom::OriginTrialFeature::k{{implied_name}},
{%- endfor %}
};
    return implied_features;
  }
{% endfor %}
  return {};
}

bool origin_trials::FeatureEnabledForOS(mojom::OriginTrialFeature feature) {
  switch (feature) {
    {% for feature in features %}
    {% if feature.origin_trial_feature_name %}
    case mojom::OriginTrialFeature::k{{feature.name}}:
      {% if feature.origin_trial_os %}
#if
  {%- for os in feature.origin_trial_os %}
  {%- if not loop.first %} ||{% endif %}
 {{platform_buildflag(os)}}
  {%- endfor %}

      return true;
#else
      return false;
#endif
      {% else %}
      return true;
      {% endif %}
    {% endif %}
    {% endfor %}
    // For testing
    case mojom::OriginTrialFeature::kNonExisting:
      return true;
  }
}

} // namespace blink