chromium/third_party/blink/renderer/build/scripts/templates/runtime_enabled_features_test_helpers.h.tmpl

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

{{source_files_for_generated_file(template_file, input_files)}}

#ifndef {{header_guard}}
#define {{header_guard}}

#include "base/check_op.h"
#include "base/memory/protected_memory.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

// Don't use this class directly. Use Scoped*ForTest instead.
class RuntimeEnabledFeaturesTestHelpers {
 public:
  template <bool& data_member>
  class ScopedRuntimeEnabledFeature {
   public:
    ScopedRuntimeEnabledFeature(bool enabled)
        : enabled_(enabled), original_(data_member) { data_member = enabled; }
    ~ScopedRuntimeEnabledFeature() {
      CHECK_EQ(enabled_, data_member);
      data_member = original_;
    }
   private:
    bool enabled_;
    bool original_;
  };

  template <base::ProtectedMemory<bool>& data_member>
  class ScopedRuntimeEnabledProtectedFeature {
   public:
    ScopedRuntimeEnabledProtectedFeature(bool enabled)
        : enabled_(enabled), original_(*data_member) {
      base::AutoWritableMemory data_member_writer(data_member);
      data_member_writer.GetProtectedData() = enabled;
    }
    ~ScopedRuntimeEnabledProtectedFeature() {
      CHECK_EQ(enabled_, *data_member);
      base::AutoWritableMemory data_member_writer(data_member);
      data_member_writer.GetProtectedData() = original_;
    }
   private:
    bool enabled_;
    bool original_;
  };

  {% for feature in features %}
  {% if feature.is_protected_feature %}
  using Scoped{{feature.name}} = ScopedRuntimeEnabledProtectedFeature<
    RuntimeEnabledFeaturesBase::{{feature.data_member_name}}>;
  {% else %}
  using Scoped{{feature.name}} = ScopedRuntimeEnabledFeature<
      RuntimeEnabledFeaturesBase::{{feature.data_member_name}}>;
  {% endif %}
  {% endfor %}
};

{% for feature in features %}
using Scoped{{feature.name}}ForTest =
    RuntimeEnabledFeaturesTestHelpers::Scoped{{feature.name}};
{% endfor %}
}  // namespace blink

#endif  // {{header_guard}}