Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: components/flags_ui/flags_state.cc

Issue 2298223002: Fix logging of Launch.FlagsAtStartup histogram. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
232 // choice is selected for it.
233 struct SwitchEntry {
234 // Corresponding base::Feature to toggle.
235 std::string feature_name;
236
237 // If |feature_name| is not empty, the state (enable/disabled) to set.
238 bool feature_state;
239
240 // The name of the switch to add.
241 std::string switch_name;
242
243 // If |switch_name| is not empty, the value of the switch to set.
244 std::string switch_value;
245
246 SwitchEntry() : feature_state(false) {}
247 };
248
249 FlagsState::FlagsState(const FeatureEntry* feature_entries, 231 FlagsState::FlagsState(const FeatureEntry* feature_entries,
250 size_t num_feature_entries) 232 size_t num_feature_entries)
251 : feature_entries_(feature_entries), 233 : feature_entries_(feature_entries),
252 num_feature_entries_(num_feature_entries), 234 num_feature_entries_(num_feature_entries),
253 needs_restart_(false) {} 235 needs_restart_(false) {}
254 236
255 FlagsState::~FlagsState() {} 237 FlagsState::~FlagsState() {}
256 238
257 void FlagsState::ConvertFlagsToSwitches( 239 void FlagsState::ConvertFlagsToSwitches(
258 FlagsStorage* flags_storage, 240 FlagsStorage* flags_storage,
259 base::CommandLine* command_line, 241 base::CommandLine* command_line,
260 SentinelsMode sentinels, 242 SentinelsMode sentinels,
261 const char* enable_features_flag_name, 243 const char* enable_features_flag_name,
262 const char* disable_features_flag_name) { 244 const char* disable_features_flag_name) {
263 std::set<std::string> enabled_entries; 245 std::set<std::string> enabled_entries;
246 std::map<std::string, SwitchEntry> name_to_switch_map;
264 247
Ilya Sherman 2016/08/31 19:06:31 nit: I'd omit this newline.
Alexei Svitkine (slow) 2016/08/31 19:46:22 Done.
265 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, &enabled_entries); 248 GenerateFlagsToSwitchesMapping(flags_storage, &enabled_entries,
266 249 &name_to_switch_map);
267 std::map<std::string, SwitchEntry> name_to_switch_map;
268 for (size_t i = 0; i < num_feature_entries_; ++i) {
269 const FeatureEntry& e = feature_entries_[i];
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, 250 AddSwitchesToCommandLine(enabled_entries, name_to_switch_map, sentinels,
309 command_line, enable_features_flag_name, 251 command_line, enable_features_flag_name,
310 disable_features_flag_name); 252 disable_features_flag_name);
311 } 253 }
312 254
255 void FlagsState::GetSwitchesFromFlags(FlagsStorage* flags_storage,
256 std::set<std::string>* switches) {
257 std::set<std::string> enabled_entries;
258 std::map<std::string, SwitchEntry> name_to_switch_map;
259 GenerateFlagsToSwitchesMapping(flags_storage, &enabled_entries,
260 &name_to_switch_map);
261
262 for (const std::string& entry_name : enabled_entries) {
263 const auto& entry_it = name_to_switch_map.find(entry_name);
264 if (entry_it == name_to_switch_map.end()) {
265 NOTREACHED();
266 continue;
267 }
Ilya Sherman 2016/08/31 19:06:31 nit: Please replace these four lines with a single
Alexei Svitkine (slow) 2016/08/31 19:46:22 Done. DCHECK_NE didn't compile, so I just used a D
268
269 const SwitchEntry& entry = entry_it->second;
270 if (!entry.switch_name.empty())
271 switches->insert("--" + entry.switch_name);
272 }
273 }
274
313 bool FlagsState::IsRestartNeededToCommitChanges() { 275 bool FlagsState::IsRestartNeededToCommitChanges() {
314 return needs_restart_; 276 return needs_restart_;
315 } 277 }
316 278
317 void FlagsState::SetFeatureEntryEnabled(FlagsStorage* flags_storage, 279 void FlagsState::SetFeatureEntryEnabled(FlagsStorage* flags_storage,
318 const std::string& internal_name, 280 const std::string& internal_name,
319 bool enable) { 281 bool enable) {
320 size_t at_index = internal_name.find(testing::kMultiSeparator); 282 size_t at_index = internal_name.find(testing::kMultiSeparator);
321 if (at_index != std::string::npos) { 283 if (at_index != std::string::npos) {
322 DCHECK(enable); 284 DCHECK(enable);
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 if (out_difference && !result) { 542 if (out_difference && !result) {
581 std::set_symmetric_difference( 543 std::set_symmetric_difference(
582 new_flags.begin(), new_flags.end(), active_flags.begin(), 544 new_flags.begin(), new_flags.end(), active_flags.begin(),
583 active_flags.end(), 545 active_flags.end(),
584 std::inserter(*out_difference, out_difference->begin())); 546 std::inserter(*out_difference, out_difference->begin()));
585 } 547 }
586 548
587 return result; 549 return result;
588 } 550 }
589 551
552 FlagsState::SwitchEntry::SwitchEntry() : feature_state(false) {}
553 FlagsState::SwitchEntry::~SwitchEntry() {}
554
590 void FlagsState::AddSwitchMapping( 555 void FlagsState::AddSwitchMapping(
591 const std::string& key, 556 const std::string& key,
592 const std::string& switch_name, 557 const std::string& switch_name,
593 const std::string& switch_value, 558 const std::string& switch_value,
594 std::map<std::string, SwitchEntry>* name_to_switch_map) { 559 std::map<std::string, SwitchEntry>* name_to_switch_map) {
595 DCHECK(!base::ContainsKey(*name_to_switch_map, key)); 560 DCHECK(!base::ContainsKey(*name_to_switch_map, key));
596 561
597 SwitchEntry* entry = &(*name_to_switch_map)[key]; 562 SwitchEntry* entry = &(*name_to_switch_map)[key];
598 entry->switch_name = switch_name; 563 entry->switch_name = switch_name;
599 entry->switch_value = switch_value; 564 entry->switch_value = switch_value;
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 #endif 690 #endif
726 } 691 }
727 692
728 std::set<std::string> new_enabled_entries = 693 std::set<std::string> new_enabled_entries =
729 base::STLSetIntersection<std::set<std::string>>(platform_entries, 694 base::STLSetIntersection<std::set<std::string>>(platform_entries,
730 *result); 695 *result);
731 696
732 result->swap(new_enabled_entries); 697 result->swap(new_enabled_entries);
733 } 698 }
734 699
700 void FlagsState::GenerateFlagsToSwitchesMapping(
701 FlagsStorage* flags_storage,
702 std::set<std::string>* enabled_entries,
703 std::map<std::string, SwitchEntry>* name_to_switch_map) {
704 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, enabled_entries);
705
706 for (size_t i = 0; i < num_feature_entries_; ++i) {
707 const FeatureEntry& e = feature_entries_[i];
708 switch (e.type) {
709 case FeatureEntry::SINGLE_VALUE:
710 case FeatureEntry::SINGLE_DISABLE_VALUE:
711 AddSwitchMapping(e.internal_name, e.command_line_switch,
712 e.command_line_value, name_to_switch_map);
713 break;
714 case FeatureEntry::MULTI_VALUE:
715 for (int j = 0; j < e.num_options; ++j) {
716 AddSwitchMapping(
717 e.NameForOption(j), e.ChoiceForOption(j).command_line_switch,
718 e.ChoiceForOption(j).command_line_value, name_to_switch_map);
719 }
720 break;
721 case FeatureEntry::ENABLE_DISABLE_VALUE:
722 AddSwitchMapping(e.NameForOption(0), std::string(), std::string(),
723 name_to_switch_map);
724 AddSwitchMapping(e.NameForOption(1), e.command_line_switch,
725 e.command_line_value, name_to_switch_map);
726 AddSwitchMapping(e.NameForOption(2), e.disable_command_line_switch,
727 e.disable_command_line_value, name_to_switch_map);
728 break;
729 case FeatureEntry::FEATURE_VALUE:
730 case FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE:
731 for (int j = 0; j < e.num_options; ++j) {
732 FeatureEntry::FeatureState state = e.StateForOption(j);
733 if (state == FeatureEntry::FeatureState::DEFAULT) {
734 AddFeatureMapping(e.NameForOption(j), std::string(), false,
735 name_to_switch_map);
736 } else {
737 AddFeatureMapping(e.NameForOption(j), e.feature->name,
738 state == FeatureEntry::FeatureState::ENABLED,
739 name_to_switch_map);
740 }
741 }
742 break;
743 }
744 }
745 }
746
735 } // namespace flags_ui 747 } // namespace flags_ui
OLDNEW
« components/flags_ui/flags_state.h ('K') | « components/flags_ui/flags_state.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698