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