xref: /aosp_15_r20/external/cronet/base/scoped_add_feature_flags.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <vector>
10 
11 #include "base/base_export.h"
12 #include "base/feature_list.h"
13 #include "base/memory/raw_ptr.h"
14 
15 namespace base {
16 
17 class CommandLine;
18 
19 // Helper class to enable and disable features if they are not already set in
20 // the command line. It reads the command line on construction, allows user to
21 // enable and disable features during its lifetime, and writes the modified
22 // --enable-features=... and --disable-features=... flags back to the command
23 // line on destruction.
24 class BASE_EXPORT ScopedAddFeatureFlags {
25  public:
26   explicit ScopedAddFeatureFlags(CommandLine* command_line);
27 
28   ScopedAddFeatureFlags(const ScopedAddFeatureFlags&) = delete;
29   ScopedAddFeatureFlags& operator=(const ScopedAddFeatureFlags&) = delete;
30 
31   ~ScopedAddFeatureFlags();
32 
33   // Any existing (user set) enable/disable takes precedence.
34   void EnableIfNotSet(const Feature& feature);
35   void DisableIfNotSet(const Feature& feature);
36   void EnableIfNotSetWithParameter(const Feature& feature,
37                                    StringPiece name,
38                                    StringPiece value);
39 
40   // Check if the feature is enabled from command line or functions above
41   bool IsEnabled(const Feature& feature);
42 
43   // Check if the feature with the given parameter name and value is enabled
44   // from command line or functions above. An empty parameter name means that we
45   // are checking if the feature is enabled without any parameter.
46   bool IsEnabledWithParameter(const Feature& feature,
47                               StringPiece parameter_name,
48                               StringPiece parameter_value);
49 
50  private:
51   void AddFeatureIfNotSet(const Feature& feature,
52                           StringPiece suffix,
53                           bool enable);
54 
55   const raw_ptr<CommandLine> command_line_;
56   std::vector<std::string> enabled_features_;
57   std::vector<std::string> disabled_features_;
58 };
59 
60 }  // namespace base
61 
62 #endif  // BASE_SCOPED_ADD_FEATURE_FLAGS_H_
63