scoped_add_feature_flags.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_SCOPED_ADD_FEATURE_FLAGS_H_
  5. #define BASE_SCOPED_ADD_FEATURE_FLAGS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/base_export.h"
  9. #include "base/feature_list.h"
  10. #include "base/memory/raw_ptr.h"
  11. namespace base {
  12. class CommandLine;
  13. // Helper class to enable and disable features if they are not already set in
  14. // the command line. It reads the command line on construction, allows user to
  15. // enable and disable features during its lifetime, and writes the modified
  16. // --enable-features=... and --disable-features=... flags back to the command
  17. // line on destruction.
  18. class BASE_EXPORT ScopedAddFeatureFlags {
  19. public:
  20. explicit ScopedAddFeatureFlags(CommandLine* command_line);
  21. ScopedAddFeatureFlags(const ScopedAddFeatureFlags&) = delete;
  22. ScopedAddFeatureFlags& operator=(const ScopedAddFeatureFlags&) = delete;
  23. ~ScopedAddFeatureFlags();
  24. // Any existing (user set) enable/disable takes precedence.
  25. void EnableIfNotSet(const Feature& feature);
  26. void DisableIfNotSet(const Feature& feature);
  27. void EnableIfNotSetWithParameter(const Feature& feature,
  28. StringPiece name,
  29. StringPiece value);
  30. // Check if the feature is enabled from command line or functions above
  31. bool IsEnabled(const Feature& feature);
  32. // Check if the feature with the given parameter name and value is enabled
  33. // from command line or functions above. An empty parameter name means that we
  34. // are checking if the feature is enabled without any parameter.
  35. bool IsEnabledWithParameter(const Feature& feature,
  36. StringPiece parameter_name,
  37. StringPiece parameter_value);
  38. private:
  39. void AddFeatureIfNotSet(const Feature& feature,
  40. StringPiece suffix,
  41. bool enable);
  42. const raw_ptr<CommandLine> command_line_;
  43. std::vector<std::string> enabled_features_;
  44. std::vector<std::string> disabled_features_;
  45. };
  46. } // namespace base
  47. #endif // BASE_SCOPED_ADD_FEATURE_FLAGS_H_