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 FeatureEntry&)> skip_feature_entry); |
| 75 |
| 76 // Returns the value for the current platform. This is one of the values |
| 77 // defined by the OS enum above. |
| 78 // This is exposed only for testing. |
| 79 static int GetCurrentPlatform(); |
| 80 |
| 81 // Compares a set of switches of the two provided command line objects and |
| 82 // returns true if they are the same and false otherwise. |
| 83 // If |out_difference| is not NULL, it's filled with set_symmetric_difference |
| 84 // between sets. |
| 85 // Only switches between --flag-switches-begin and --flag-switches-end are |
| 86 // compared. The embedder may use |extra_flag_sentinel_begin_flag_name| and |
| 87 // |extra_sentinel_end_flag_name| to specify other delimiters, if supported. |
| 88 static bool AreSwitchesIdenticalToCurrentCommandLine( |
| 89 const base::CommandLine& new_cmdline, |
| 90 const base::CommandLine& active_cmdline, |
| 91 std::set<base::CommandLine::StringType>* out_difference, |
| 92 const char* extra_flag_sentinel_begin_flag_name, |
| 93 const char* extra_flag_sentinel_end_flag_name); |
| 94 |
| 95 private: |
| 96 // Adds mapping to |name_to_switch_map| to set the given switch name/value. |
| 97 void AddSwitchMapping(const std::string& key, |
| 98 const std::string& switch_name, |
| 99 const std::string& switch_value, |
| 100 std::map<std::string, SwitchEntry>* name_to_switch_map); |
| 101 |
| 102 // Adds mapping to |name_to_switch_map| to toggle base::Feature |feature_name| |
| 103 // to state |feature_state|. |
| 104 void AddFeatureMapping( |
| 105 const std::string& key, |
| 106 const std::string& feature_name, |
| 107 bool feature_state, |
| 108 std::map<std::string, SwitchEntry>* name_to_switch_map); |
| 109 |
| 110 // Updates the switches in |command_line| by applying the modifications |
| 111 // specified in |name_to_switch_map| for each entry in |enabled_entries|. |
| 112 // |enable_features_flag_name| and |disable_features_flag_name| are switches |
| 113 // used by the embedder to enable/disable features respectively if supported. |
| 114 void AddSwitchesToCommandLine( |
| 115 const std::set<std::string>& enabled_entries, |
| 116 const std::map<std::string, SwitchEntry>& name_to_switch_map, |
| 117 SentinelsMode sentinels, |
| 118 base::CommandLine* command_line, |
| 119 const char* enable_features_flag_name, |
| 120 const char* disable_features_flag_name); |
| 121 |
| 122 // Updates |command_line| by merging the value of the --enable-features= or |
| 123 // --disable-features= list (per the |switch_name| param) with corresponding |
| 124 // entries in |feature_switches| that have value |feature_state|. Keeps track |
| 125 // of the changes by updating |appended_switches|. |
| 126 void MergeFeatureCommandLineSwitch( |
| 127 const std::map<std::string, bool>& feature_switches, |
| 128 const char* switch_name, |
| 129 bool feature_state, |
| 130 base::CommandLine* command_line); |
| 131 |
| 132 // Removes all entries from prefs::kEnabledLabsExperiments that are unknown, |
| 133 // to prevent this list to become very long as entries are added and removed. |
| 134 void SanitizeList(FlagsStorage* flags_storage); |
| 135 |
| 136 void GetSanitizedEnabledFlags(FlagsStorage* flags_storage, |
| 137 std::set<std::string>* result); |
| 138 |
| 139 // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't |
| 140 // enabled on the current platform. |
| 141 void GetSanitizedEnabledFlagsForCurrentPlatform( |
| 142 FlagsStorage* flags_storage, |
| 143 std::set<std::string>* result); |
| 144 |
| 145 const FeatureEntry* feature_entries_; |
| 146 size_t num_feature_entries_; |
| 147 |
| 148 bool needs_restart_; |
| 149 std::map<std::string, std::string> flags_switches_; |
| 150 |
| 151 // Map from switch name to a set of string, that keeps track which strings |
| 152 // were appended to existing (list value) switches. |
| 153 std::map<std::string, std::set<std::string>> appended_switches_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(FlagsState); |
| 156 }; |
| 157 |
| 158 } // namespace flags_ui |
| 159 |
| 160 #endif // COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ |
OLD | NEW |