| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/flags_ui/flags_state.h" | 5 #include "components/flags_ui/flags_state.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 feature_trial_name, internal::kTrialGroupAboutFlags); | 221 feature_trial_name, internal::kTrialGroupAboutFlags); |
| 222 if (!trial) { | 222 if (!trial) { |
| 223 DLOG(WARNING) << "Could not create the trial " << feature_trial_name | 223 DLOG(WARNING) << "Could not create the trial " << feature_trial_name |
| 224 << " with group " << internal::kTrialGroupAboutFlags; | 224 << " with group " << internal::kTrialGroupAboutFlags; |
| 225 } | 225 } |
| 226 return trial; | 226 return trial; |
| 227 } | 227 } |
| 228 | 228 |
| 229 } // namespace | 229 } // namespace |
| 230 | 230 |
| 231 // Keeps track of affected switches for each FeatureEntry, based on which | 231 struct FlagsState::SwitchEntry { |
| 232 // choice is selected for it. | |
| 233 struct SwitchEntry { | |
| 234 // Corresponding base::Feature to toggle. | 232 // Corresponding base::Feature to toggle. |
| 235 std::string feature_name; | 233 std::string feature_name; |
| 236 | 234 |
| 237 // If |feature_name| is not empty, the state (enable/disabled) to set. | 235 // If |feature_name| is not empty, the state (enable/disabled) to set. |
| 238 bool feature_state; | 236 bool feature_state; |
| 239 | 237 |
| 240 // The name of the switch to add. | 238 // The name of the switch to add. |
| 241 std::string switch_name; | 239 std::string switch_name; |
| 242 | 240 |
| 243 // If |switch_name| is not empty, the value of the switch to set. | 241 // If |switch_name| is not empty, the value of the switch to set. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 254 | 252 |
| 255 FlagsState::~FlagsState() {} | 253 FlagsState::~FlagsState() {} |
| 256 | 254 |
| 257 void FlagsState::ConvertFlagsToSwitches( | 255 void FlagsState::ConvertFlagsToSwitches( |
| 258 FlagsStorage* flags_storage, | 256 FlagsStorage* flags_storage, |
| 259 base::CommandLine* command_line, | 257 base::CommandLine* command_line, |
| 260 SentinelsMode sentinels, | 258 SentinelsMode sentinels, |
| 261 const char* enable_features_flag_name, | 259 const char* enable_features_flag_name, |
| 262 const char* disable_features_flag_name) { | 260 const char* disable_features_flag_name) { |
| 263 std::set<std::string> enabled_entries; | 261 std::set<std::string> enabled_entries; |
| 264 | |
| 265 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, &enabled_entries); | |
| 266 | |
| 267 std::map<std::string, SwitchEntry> name_to_switch_map; | 262 std::map<std::string, SwitchEntry> name_to_switch_map; |
| 268 for (size_t i = 0; i < num_feature_entries_; ++i) { | 263 GenerateFlagsToSwitchesMapping(flags_storage, &enabled_entries, |
| 269 const FeatureEntry& e = feature_entries_[i]; | 264 &name_to_switch_map); |
| 270 switch (e.type) { | |
| 271 case FeatureEntry::SINGLE_VALUE: | |
| 272 case FeatureEntry::SINGLE_DISABLE_VALUE: | |
| 273 AddSwitchMapping(e.internal_name, e.command_line_switch, | |
| 274 e.command_line_value, &name_to_switch_map); | |
| 275 break; | |
| 276 case FeatureEntry::MULTI_VALUE: | |
| 277 for (int j = 0; j < e.num_options; ++j) { | |
| 278 AddSwitchMapping( | |
| 279 e.NameForOption(j), e.ChoiceForOption(j).command_line_switch, | |
| 280 e.ChoiceForOption(j).command_line_value, &name_to_switch_map); | |
| 281 } | |
| 282 break; | |
| 283 case FeatureEntry::ENABLE_DISABLE_VALUE: | |
| 284 AddSwitchMapping(e.NameForOption(0), std::string(), std::string(), | |
| 285 &name_to_switch_map); | |
| 286 AddSwitchMapping(e.NameForOption(1), e.command_line_switch, | |
| 287 e.command_line_value, &name_to_switch_map); | |
| 288 AddSwitchMapping(e.NameForOption(2), e.disable_command_line_switch, | |
| 289 e.disable_command_line_value, &name_to_switch_map); | |
| 290 break; | |
| 291 case FeatureEntry::FEATURE_VALUE: | |
| 292 case FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE: | |
| 293 for (int j = 0; j < e.num_options; ++j) { | |
| 294 FeatureEntry::FeatureState state = e.StateForOption(j); | |
| 295 if (state == FeatureEntry::FeatureState::DEFAULT) { | |
| 296 AddFeatureMapping(e.NameForOption(j), std::string(), false, | |
| 297 &name_to_switch_map); | |
| 298 } else { | |
| 299 AddFeatureMapping(e.NameForOption(j), e.feature->name, | |
| 300 state == FeatureEntry::FeatureState::ENABLED, | |
| 301 &name_to_switch_map); | |
| 302 } | |
| 303 } | |
| 304 break; | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 AddSwitchesToCommandLine(enabled_entries, name_to_switch_map, sentinels, | 265 AddSwitchesToCommandLine(enabled_entries, name_to_switch_map, sentinels, |
| 309 command_line, enable_features_flag_name, | 266 command_line, enable_features_flag_name, |
| 310 disable_features_flag_name); | 267 disable_features_flag_name); |
| 311 } | 268 } |
| 312 | 269 |
| 270 std::set<std::string> FlagsState::GetSwitchesFromFlags( |
| 271 FlagsStorage* flags_storage) { |
| 272 std::set<std::string> enabled_entries; |
| 273 std::map<std::string, SwitchEntry> name_to_switch_map; |
| 274 GenerateFlagsToSwitchesMapping(flags_storage, &enabled_entries, |
| 275 &name_to_switch_map); |
| 276 |
| 277 std::set<std::string> switches; |
| 278 for (const std::string& entry_name : enabled_entries) { |
| 279 const auto& entry_it = name_to_switch_map.find(entry_name); |
| 280 DCHECK(entry_it != name_to_switch_map.end()); |
| 281 |
| 282 const SwitchEntry& entry = entry_it->second; |
| 283 if (!entry.switch_name.empty()) |
| 284 switches.insert("--" + entry.switch_name); |
| 285 } |
| 286 return switches; |
| 287 } |
| 288 |
| 313 bool FlagsState::IsRestartNeededToCommitChanges() { | 289 bool FlagsState::IsRestartNeededToCommitChanges() { |
| 314 return needs_restart_; | 290 return needs_restart_; |
| 315 } | 291 } |
| 316 | 292 |
| 317 void FlagsState::SetFeatureEntryEnabled(FlagsStorage* flags_storage, | 293 void FlagsState::SetFeatureEntryEnabled(FlagsStorage* flags_storage, |
| 318 const std::string& internal_name, | 294 const std::string& internal_name, |
| 319 bool enable) { | 295 bool enable) { |
| 320 size_t at_index = internal_name.find(testing::kMultiSeparator); | 296 size_t at_index = internal_name.find(testing::kMultiSeparator); |
| 321 if (at_index != std::string::npos) { | 297 if (at_index != std::string::npos) { |
| 322 DCHECK(enable); | 298 DCHECK(enable); |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 #endif | 701 #endif |
| 726 } | 702 } |
| 727 | 703 |
| 728 std::set<std::string> new_enabled_entries = | 704 std::set<std::string> new_enabled_entries = |
| 729 base::STLSetIntersection<std::set<std::string>>(platform_entries, | 705 base::STLSetIntersection<std::set<std::string>>(platform_entries, |
| 730 *result); | 706 *result); |
| 731 | 707 |
| 732 result->swap(new_enabled_entries); | 708 result->swap(new_enabled_entries); |
| 733 } | 709 } |
| 734 | 710 |
| 711 void FlagsState::GenerateFlagsToSwitchesMapping( |
| 712 FlagsStorage* flags_storage, |
| 713 std::set<std::string>* enabled_entries, |
| 714 std::map<std::string, SwitchEntry>* name_to_switch_map) { |
| 715 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, enabled_entries); |
| 716 |
| 717 for (size_t i = 0; i < num_feature_entries_; ++i) { |
| 718 const FeatureEntry& e = feature_entries_[i]; |
| 719 switch (e.type) { |
| 720 case FeatureEntry::SINGLE_VALUE: |
| 721 case FeatureEntry::SINGLE_DISABLE_VALUE: |
| 722 AddSwitchMapping(e.internal_name, e.command_line_switch, |
| 723 e.command_line_value, name_to_switch_map); |
| 724 break; |
| 725 case FeatureEntry::MULTI_VALUE: |
| 726 for (int j = 0; j < e.num_options; ++j) { |
| 727 AddSwitchMapping( |
| 728 e.NameForOption(j), e.ChoiceForOption(j).command_line_switch, |
| 729 e.ChoiceForOption(j).command_line_value, name_to_switch_map); |
| 730 } |
| 731 break; |
| 732 case FeatureEntry::ENABLE_DISABLE_VALUE: |
| 733 AddSwitchMapping(e.NameForOption(0), std::string(), std::string(), |
| 734 name_to_switch_map); |
| 735 AddSwitchMapping(e.NameForOption(1), e.command_line_switch, |
| 736 e.command_line_value, name_to_switch_map); |
| 737 AddSwitchMapping(e.NameForOption(2), e.disable_command_line_switch, |
| 738 e.disable_command_line_value, name_to_switch_map); |
| 739 break; |
| 740 case FeatureEntry::FEATURE_VALUE: |
| 741 case FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE: |
| 742 for (int j = 0; j < e.num_options; ++j) { |
| 743 FeatureEntry::FeatureState state = e.StateForOption(j); |
| 744 if (state == FeatureEntry::FeatureState::DEFAULT) { |
| 745 AddFeatureMapping(e.NameForOption(j), std::string(), false, |
| 746 name_to_switch_map); |
| 747 } else { |
| 748 AddFeatureMapping(e.NameForOption(j), e.feature->name, |
| 749 state == FeatureEntry::FeatureState::ENABLED, |
| 750 name_to_switch_map); |
| 751 } |
| 752 } |
| 753 break; |
| 754 } |
| 755 } |
| 756 } |
| 757 |
| 735 } // namespace flags_ui | 758 } // namespace flags_ui |
| OLD | NEW |