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

{% 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/public/common/features.h"
#include "third_party/blink/public/mojom/origin_trial_feature/origin_trial_feature.mojom-shared.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/origin_trial_state/origin_trial_state_host.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/blink/renderer/platform/runtime_feature_state/runtime_feature_state_override_context.h"

namespace blink {

void RuntimeFeatureStateOverrideContext::ApplyOverrideValuesFromParams(
    const base::flat_map<mojom::RuntimeFeature, bool>&
        values_from_params) {
  for(const auto& override_value : values_from_params) {
    override_values_[override_value.first] = override_value.second;
  }
}

{% for feature in overridable_features %}
bool RuntimeFeatureStateOverrideContext::
    Is{{feature.name}}ForceDisabled() const {
  auto it = override_values_.find(
      mojom::RuntimeFeature::k{{feature.name}});
  if (it != override_values_.end() && it->second == false)
    return true;

  return false;
}

bool RuntimeFeatureStateOverrideContext::
    Is{{feature.name}}ForceEnabled() const {
  auto it = override_values_.find(mojom::RuntimeFeature::k{{feature.name}});
  if(it != override_values_.end() && it->second == true)
    return true;

  return false;
}

void RuntimeFeatureStateOverrideContext::Set{{feature.name}}ForceDisabled() {
  override_values_[mojom::RuntimeFeature::k{{feature.name}}] = false;
}

void RuntimeFeatureStateOverrideContext::Set{{feature.name}}ForceEnabled() {
  override_values_[mojom::RuntimeFeature::k{{feature.name}}] = true;
}

{% if feature in origin_trial_controlled_features %}
bool RuntimeFeatureStateOverrideContext::
    Set{{feature.name}}Enabled(const WTF::Vector<WTF::String>& tokens) {
{% if (feature.name)|string == 'DisableThirdPartyStoragePartitioning2' %}
  UseCounter::Count(use_counter_,
                          WebFeature::kDisableThirdPartyStoragePartitioning2);
  // If the base::Feature for the deprecation trial is disabled, we should
  // not send any overrides to the browser, so we return false immediately,
  // skipping any mojom messaging.
  if(!base::FeatureList::IsEnabled(
    blink::features::kDisableThirdPartyStoragePartitioningDeprecationTrial2)){
    return false;
  }
{% endif %}
  // Origin Trial feature values should not override any sent from
  // the browser, so we only add a value if has not already been set
  // in the override_values_ map with an identical token set. Return
  // whether the override was successful.
  auto value = mojom::blink::OriginTrialFeatureState::New(true, tokens);
  if(!override_values_.contains(
      mojom::RuntimeFeature::k{{feature.name}}) ||
     origin_trial_overrides_[
      mojom::RuntimeFeature::k{{feature.name}}] != value) {
    // Update the state of all feature overrides in this class.
    override_values_
      [mojom::RuntimeFeature::k{{feature.name}}] = true;
    // Add successful overrides to the list of features we sent back
    // to the browser process.
    origin_trial_overrides_[
      mojom::RuntimeFeature::k{{feature.name}}] = std::move(value);
    return true;
  }
  return false;
}
{% endif %}
{% endfor %}

void RuntimeFeatureStateOverrideContext::ApplyOriginTrialOverride(
    const blink::mojom::blink::OriginTrialFeature& origin_trial_feature,
    const WTF::Vector<WTF::String>& tokens) {
  // Determine which of the overrideable features has been set
  // via Origin Trial.
  bool overridden = false;
{% for feature in overridable_features %}
{% if origin_trial_controlled_features.count(feature) > 0 %}
  if(origin_trial_feature == blink::mojom::blink::OriginTrialFeature::k{{feature.name}}) {
    overridden = Set{{feature.name}}Enabled(tokens);
  }
{% endif %}
{% endfor %}
  // Send the newly overriden values to the browser via IPC.
  if(overridden){
    // We must convert from std:: types to WTF:: types.
    // TODO(https://crbug.com/1377000): make origin_trial_overrides_ a
    // WTF:: type in a follow-up CL.
    WTF::HashMap<mojom::blink::RuntimeFeature,
                 mojom::blink::OriginTrialFeatureStatePtr>
        blink_overrides;
    for (auto& o : origin_trial_overrides_){
      blink_overrides.insert(o.first, std::move(o.second));
    }
    // Feature overrides and origin trial information should only be
    // sent to the browser process if the corresponding base::Feature
    // is enabled.
    if (base::FeatureList::IsEnabled(
            blink::features::kOriginTrialStateHostApplyFeatureDiff)) {
      GetOrBindOriginTrialStateHost()->ApplyFeatureDiffForOriginTrial(
          std::move(blink_overrides));
    }
    // TODO(https://crbug.com/1377000): when we convert to WTF:: types,
    // we use std::move, so we have to do some cleanup.
    origin_trial_overrides_.clear();
  }
}

void RuntimeFeatureStateOverrideContext::EnablePersistentTrial(
      const WTF::String& raw_token,
      const WTF::Vector<scoped_refptr<const blink::SecurityOrigin>>&
          script_origins) {
    GetOrBindOriginTrialStateHost()->EnablePersistentTrial(
        raw_token, script_origins);
  }

mojom::blink::OriginTrialStateHost*
RuntimeFeatureStateOverrideContext::GetOrBindOriginTrialStateHost(){
  // It's possible that the prior interface we bound is no longer available
  // in which case we should reset the connection and re-bind a new one.
  if (origin_trial_state_host_remote_.is_bound() &&
      !origin_trial_state_host_remote_.is_connected()) {
    origin_trial_state_host_remote_.reset();
  }
  // TODO(https://crbug.com/1410817): add support for workers/non-frames that
  // are enabling origin trials to send their information to the browser too.
  if (!origin_trial_state_host_remote_.is_bound()) {
      CHECK(send_runtime_features_to_browser_);
      binding_context_->GetBrowserInterfaceBroker().GetInterface(
          origin_trial_state_host_remote_.BindNewPipeAndPassReceiver(
              binding_context_->GetTaskRunner(TaskType::kInternalDefault)));
    }
  return origin_trial_state_host_remote_.get();
}

void RuntimeFeatureStateOverrideContext::Trace(Visitor* visitor) const{
  visitor->Trace(binding_context_);
  visitor->Trace(use_counter_);
  visitor->Trace(origin_trial_state_host_remote_);
}

}  // namespace blink