Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ | |
| 6 #define COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/macros.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class ListValue; | |
| 18 } | |
| 19 | |
| 20 namespace flags_ui { | |
| 21 | |
| 22 struct FeatureEntry; | |
| 23 class FlagsStorage; | |
| 24 struct SwitchEntry; | |
| 25 | |
| 26 // Enumeration of OSs. | |
| 27 enum { | |
| 28 kOsMac = 1 << 0, | |
| 29 kOsWin = 1 << 1, | |
| 30 kOsLinux = 1 << 2, | |
| 31 kOsCrOS = 1 << 3, | |
| 32 kOsAndroid = 1 << 4, | |
| 33 kOsCrOSOwnerOnly = 1 << 5 | |
| 34 }; | |
| 35 | |
| 36 // A flag controlling the behavior of the |ConvertFlagsToSwitches| function - | |
| 37 // whether it should add the sentinel switches around flags. | |
| 38 enum SentinelsMode { kNoSentinels, kAddSentinels }; | |
| 39 | |
| 40 // Differentiate between generic flags available on a per session base and flags | |
| 41 // that influence the whole machine and can be said by the admin only. This flag | |
| 42 // is relevant for ChromeOS for now only and dictates whether entries marked | |
| 43 // with the |kOsCrOSOwnerOnly| label should be enabled in the UI or not. | |
| 44 enum FlagAccess { kGeneralAccessFlagsOnly, kOwnerAccessToFlags }; | |
| 45 | |
| 46 // Stores and encapsulates the little state that about:flags has. | |
| 47 class FlagsState { | |
| 48 public: | |
| 49 FlagsState(const FeatureEntry* feature_entries, size_t num_feature_entries); | |
| 50 ~FlagsState(); | |
| 51 | |
| 52 void ConvertFlagsToSwitches(FlagsStorage* flags_storage, | |
| 53 base::CommandLine* command_line, | |
| 54 SentinelsMode sentinels, | |
| 55 const char* enable_features_flag_name, | |
| 56 const char* disable_features_flag_name); | |
| 57 bool IsRestartNeededToCommitChanges(); | |
| 58 void SetFeatureEntryEnabled(FlagsStorage* flags_storage, | |
| 59 const std::string& internal_name, | |
| 60 bool enable); | |
| 61 void RemoveFlagsSwitches( | |
| 62 std::map<std::string, base::CommandLine::StringType>* switch_list); | |
| 63 void ResetAllFlags(FlagsStorage* flags_storage); | |
| 64 void Reset(); | |
| 65 | |
| 66 // Gets the list of feature entries. Entries that are available for the | |
| 67 // current platform are appended to |supported_entries|; all other entries are | |
| 68 // appended to |unsupported_entries|. | |
| 69 void GetFlagFeatureEntries( | |
| 70 FlagsStorage* flags_storage, | |
| 71 FlagAccess access, | |
| 72 base::ListValue* supported_entries, | |
| 73 base::ListValue* unsupported_entries, | |
| 74 base::Callback<bool(const flags_ui::FeatureEntry&)> skip_feature_entry); | |
| 75 | |
| 76 private: | |
| 77 // Adds mapping to |name_to_switch_map| to set the given switch name/value. | |
| 78 void AddSwitchMapping(const std::string& key, | |
| 79 const std::string& switch_name, | |
| 80 const std::string& switch_value, | |
| 81 std::map<std::string, SwitchEntry>* name_to_switch_map); | |
| 82 | |
| 83 // Adds mapping to |name_to_switch_map| to toggle base::Feature |feature_name| | |
| 84 // to state |feature_state|. | |
| 85 void AddFeatureMapping( | |
| 86 const std::string& key, | |
| 87 const std::string& feature_name, | |
| 88 bool feature_state, | |
| 89 std::map<std::string, SwitchEntry>* name_to_switch_map); | |
| 90 | |
| 91 // Updates the switches in |command_line| by applying the modifications | |
| 92 // specified in |name_to_switch_map| for each entry in |enabled_entries|. | |
| 93 void AddSwitchesToCommandLine( | |
| 94 const std::set<std::string>& enabled_entries, | |
| 95 const std::map<std::string, SwitchEntry>& name_to_switch_map, | |
| 96 SentinelsMode sentinels, | |
| 97 base::CommandLine* command_line, | |
| 98 const char* enable_features_flag_name, | |
| 99 const char* disable_features_flag_name); | |
| 100 | |
| 101 // Updates |command_line| by merging the value of the --enable-features= or | |
| 102 // --disable-features= list (per the |switch_name| param) with corresponding | |
| 103 // entries in |feature_switches| that have value |feature_state|. Keeps track | |
| 104 // of the changes by updating |appended_switches|. | |
| 105 void MergeFeatureCommandLineSwitch( | |
| 106 const std::map<std::string, bool>& feature_switches, | |
| 107 const char* switch_name, | |
| 108 bool feature_state, | |
| 109 base::CommandLine* command_line); | |
| 110 | |
| 111 // Removes all entries from prefs::kEnabledLabsExperiments that are unknown, | |
| 112 // to prevent this list to become very long as entries are added and removed. | |
| 113 void SanitizeList(flags_ui::FlagsStorage* flags_storage); | |
| 114 | |
| 115 void GetSanitizedEnabledFlags(flags_ui::FlagsStorage* flags_storage, | |
| 116 std::set<std::string>* result); | |
| 117 | |
| 118 // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't | |
| 119 // enabled on the current platform. | |
| 120 void GetSanitizedEnabledFlagsForCurrentPlatform( | |
| 121 flags_ui::FlagsStorage* flags_storage, | |
| 122 std::set<std::string>* result); | |
| 123 | |
| 124 const FeatureEntry* feature_entries_; | |
| 125 size_t num_feature_entries_; | |
| 126 | |
| 127 bool needs_restart_; | |
| 128 std::map<std::string, std::string> flags_switches_; | |
| 129 | |
| 130 // Map from switch name to a set of string, that keeps track which strings | |
| 131 // were appended to existing (list value) switches. | |
| 132 std::map<std::string, std::set<std::string>> appended_switches_; | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(FlagsState); | |
| 135 }; | |
| 136 | |
| 137 // Returns the value for the current platform. This is one of the values defined | |
| 138 // by the OS enum above. | |
| 139 // This is exposed only for testing. | |
| 140 int GetCurrentPlatform(); | |
|
Alexei Svitkine (slow)
2015/11/06 21:26:01
It's kind of weird to have free-standing utility m
sdefresne
2015/11/12 11:37:25
Done.
| |
| 141 | |
| 142 // Compares a set of switches of the two provided command line objects and | |
| 143 // returns true if they are the same and false otherwise. | |
| 144 // If |out_difference| is not NULL, it's filled with set_symmetric_difference | |
| 145 // between sets. | |
| 146 bool AreSwitchesIdenticalToCurrentCommandLine( | |
| 147 const base::CommandLine& new_cmdline, | |
| 148 const base::CommandLine& active_cmdline, | |
| 149 std::set<base::CommandLine::StringType>* out_difference, | |
| 150 const char* extra_flag_sentinel_begin_flag_name, | |
| 151 const char* extra_flag_sentinel_end_flag_name); | |
| 152 | |
| 153 } // namespace flags_ui | |
| 154 | |
| 155 #endif // COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ | |
| OLD | NEW |