Xiaocheng Hu | 30050af | 2022-06-09 21:51:59 | [diff] [blame^] | 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 | |
| 5 | #include "base/scoped_add_feature_flags.h" |
| 6 | |
| 7 | #include "base/base_switches.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/containers/contains.h" |
| 10 | #include "base/strings/strcat.h" |
| 11 | #include "base/strings/string_util.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | ScopedAddFeatureFlags::ScopedAddFeatureFlags(CommandLine* command_line) |
| 16 | : command_line_(command_line) { |
| 17 | std::string enabled_features = |
| 18 | command_line->GetSwitchValueASCII(switches::kEnableFeatures); |
| 19 | std::string disabled_features = |
| 20 | command_line->GetSwitchValueASCII(switches::kDisableFeatures); |
| 21 | for (const StringPiece& feature : |
| 22 | FeatureList::SplitFeatureListString(enabled_features)) { |
| 23 | enabled_features_.emplace_back(feature); |
| 24 | } |
| 25 | for (const StringPiece& feature : |
| 26 | FeatureList::SplitFeatureListString(disabled_features)) { |
| 27 | disabled_features_.emplace_back(feature); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | ScopedAddFeatureFlags::~ScopedAddFeatureFlags() { |
| 32 | command_line_->AppendSwitchASCII(switches::kEnableFeatures, |
| 33 | JoinString(enabled_features_, ",")); |
| 34 | command_line_->AppendSwitchASCII(switches::kDisableFeatures, |
| 35 | JoinString(disabled_features_, ",")); |
| 36 | } |
| 37 | |
| 38 | void ScopedAddFeatureFlags::EnableIfNotSet(const Feature& feature) { |
| 39 | AddFeatureIfNotSet(feature, /*suffix=*/"", /*enable=*/true); |
| 40 | } |
| 41 | |
| 42 | void ScopedAddFeatureFlags::EnableIfNotSetWithParameter(const Feature& feature, |
| 43 | StringPiece name, |
| 44 | StringPiece value) { |
| 45 | std::string suffix = StrCat({":", name, "/", value}); |
| 46 | AddFeatureIfNotSet(feature, suffix, true /* enable */); |
| 47 | } |
| 48 | |
| 49 | void ScopedAddFeatureFlags::DisableIfNotSet(const Feature& feature) { |
| 50 | AddFeatureIfNotSet(feature, /*suffix=*/"", /*enable=*/false); |
| 51 | } |
| 52 | |
| 53 | bool ScopedAddFeatureFlags::IsEnabled(const Feature& feature) { |
| 54 | return IsEnabledWithParameter(feature, /*parameter_name=*/"", |
| 55 | /*parameter_value=*/""); |
| 56 | } |
| 57 | |
| 58 | bool ScopedAddFeatureFlags::IsEnabledWithParameter( |
| 59 | const Feature& feature, |
| 60 | StringPiece parameter_name, |
| 61 | StringPiece parameter_value) { |
| 62 | std::string feature_name = feature.name; |
| 63 | if (!parameter_name.empty()) { |
| 64 | StrAppend(&feature_name, {":", parameter_name, "/", parameter_value}); |
| 65 | } |
| 66 | if (Contains(disabled_features_, feature_name)) |
| 67 | return false; |
| 68 | if (Contains(enabled_features_, feature_name)) |
| 69 | return true; |
| 70 | return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 71 | } |
| 72 | |
| 73 | void ScopedAddFeatureFlags::AddFeatureIfNotSet(const Feature& feature, |
| 74 | StringPiece suffix, |
| 75 | bool enable) { |
| 76 | std::string feature_name = StrCat({feature.name, suffix}); |
| 77 | if (Contains(enabled_features_, feature_name) || |
| 78 | Contains(disabled_features_, feature_name)) { |
| 79 | return; |
| 80 | } |
| 81 | if (enable) { |
| 82 | enabled_features_.emplace_back(feature_name); |
| 83 | } else { |
| 84 | disabled_features_.emplace_back(feature_name); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } // namespace base |