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

Side by Side Diff: chrome/browser/about_flags.cc

Issue 1411453004: Componentize internal class FlagsState in flags_ui component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@feature_entry
Patch Set: Rebase Created 5 years 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
« no previous file with comments | « chrome/browser/about_flags.h ('k') | chrome/browser/about_flags_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/about_flags.h" 5 #include "chrome/browser/about_flags.h"
6 6
7 #include <iterator> 7 #include <iterator>
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h"
13 #include "base/callback.h"
12 #include "base/command_line.h" 14 #include "base/command_line.h"
13 #include "base/feature_list.h" 15 #include "base/feature_list.h"
14 #include "base/memory/singleton.h" 16 #include "base/memory/singleton.h"
15 #include "base/metrics/sparse_histogram.h" 17 #include "base/metrics/sparse_histogram.h"
16 #include "base/stl_util.h" 18 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h" 22 #include "base/values.h"
21 #include "cc/base/switches.h" 23 #include "cc/base/switches.h"
22 #include "chrome/common/channel_info.h" 24 #include "chrome/common/channel_info.h"
23 #include "chrome/common/chrome_content_client.h" 25 #include "chrome/common/chrome_content_client.h"
24 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
25 #include "chrome/grit/chromium_strings.h" 27 #include "chrome/grit/chromium_strings.h"
26 #include "chrome/grit/generated_resources.h" 28 #include "chrome/grit/generated_resources.h"
27 #include "chrome/grit/google_chrome_strings.h" 29 #include "chrome/grit/google_chrome_strings.h"
28 #include "components/autofill/core/common/autofill_switches.h" 30 #include "components/autofill/core/common/autofill_switches.h"
29 #include "components/browser_sync/common/browser_sync_switches.h" 31 #include "components/browser_sync/common/browser_sync_switches.h"
30 #include "components/cloud_devices/common/cloud_devices_switches.h" 32 #include "components/cloud_devices/common/cloud_devices_switches.h"
31 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" 33 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h"
32 #include "components/dom_distiller/core/dom_distiller_switches.h" 34 #include "components/dom_distiller/core/dom_distiller_switches.h"
33 #include "components/enhanced_bookmarks/enhanced_bookmark_switches.h" 35 #include "components/enhanced_bookmarks/enhanced_bookmark_switches.h"
34 #include "components/flags_ui/feature_entry_macros.h" 36 #include "components/flags_ui/feature_entry_macros.h"
35 #include "components/flags_ui/flags_storage.h" 37 #include "components/flags_ui/flags_storage.h"
38 #include "components/flags_ui/flags_ui_switches.h"
36 #include "components/metrics/metrics_hashes.h" 39 #include "components/metrics/metrics_hashes.h"
37 #include "components/nacl/common/nacl_switches.h" 40 #include "components/nacl/common/nacl_switches.h"
38 #include "components/offline_pages/offline_page_switches.h" 41 #include "components/offline_pages/offline_page_switches.h"
39 #include "components/omnibox/browser/omnibox_switches.h" 42 #include "components/omnibox/browser/omnibox_switches.h"
40 #include "components/password_manager/core/common/password_manager_switches.h" 43 #include "components/password_manager/core/common/password_manager_switches.h"
41 #include "components/proximity_auth/switches.h" 44 #include "components/proximity_auth/switches.h"
42 #include "components/search/search_switches.h" 45 #include "components/search/search_switches.h"
43 #include "components/signin/core/common/signin_switches.h" 46 #include "components/signin/core/common/signin_switches.h"
44 #include "components/sync_driver/sync_driver_switches.h" 47 #include "components/sync_driver/sync_driver_switches.h"
45 #include "components/tracing/tracing_switches.h" 48 #include "components/tracing/tracing_switches.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 85
83 #if defined(ENABLE_PRINT_PREVIEW) 86 #if defined(ENABLE_PRINT_PREVIEW)
84 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h" 87 #include "chrome/browser/ui/webui/print_preview/print_preview_distiller.h"
85 #endif 88 #endif
86 89
87 #if defined(USE_OZONE) 90 #if defined(USE_OZONE)
88 #include "ui/ozone/public/ozone_switches.h" 91 #include "ui/ozone/public/ozone_switches.h"
89 #endif 92 #endif
90 93
91 using flags_ui::FeatureEntry; 94 using flags_ui::FeatureEntry;
95 using flags_ui::kOsMac;
96 using flags_ui::kOsWin;
97 using flags_ui::kOsLinux;
98 using flags_ui::kOsCrOS;
99 using flags_ui::kOsAndroid;
100 using flags_ui::kOsCrOSOwnerOnly;
92 101
93 namespace about_flags { 102 namespace about_flags {
94 103
95 namespace { 104 namespace {
96 105
97 // Enumeration of OSs.
98 enum {
99 kOsMac = 1 << 0,
100 kOsWin = 1 << 1,
101 kOsLinux = 1 << 2,
102 kOsCrOS = 1 << 3,
103 kOsAndroid = 1 << 4,
104 kOsCrOSOwnerOnly = 1 << 5
105 };
106
107 const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS | kOsAndroid; 106 const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS | kOsAndroid;
108 const unsigned kOsDesktop = kOsMac | kOsWin | kOsLinux | kOsCrOS; 107 const unsigned kOsDesktop = kOsMac | kOsWin | kOsLinux | kOsCrOS;
109 108
110 // Adds a |StringValue| to |list| for each platform where |bitmask| indicates
111 // whether the entry is available on that platform.
112 void AddOsStrings(unsigned bitmask, base::ListValue* list) {
113 struct {
114 unsigned bit;
115 const char* const name;
116 } kBitsToOs[] = {
117 {kOsMac, "Mac"},
118 {kOsWin, "Windows"},
119 {kOsLinux, "Linux"},
120 {kOsCrOS, "Chrome OS"},
121 {kOsAndroid, "Android"},
122 {kOsCrOSOwnerOnly, "Chrome OS (owner only)"},
123 };
124 for (size_t i = 0; i < arraysize(kBitsToOs); ++i) {
125 if (bitmask & kBitsToOs[i].bit)
126 list->Append(new base::StringValue(kBitsToOs[i].name));
127 }
128 }
129
130 // Convert switch constants to proper CommandLine::StringType strings.
131 base::CommandLine::StringType GetSwitchString(const std::string& flag) {
132 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
133 cmd_line.AppendSwitch(flag);
134 DCHECK_EQ(2U, cmd_line.argv().size());
135 return cmd_line.argv()[1];
136 }
137
138 // Scoops flags from a command line.
139 std::set<base::CommandLine::StringType> ExtractFlagsFromCommandLine(
140 const base::CommandLine& cmdline) {
141 std::set<base::CommandLine::StringType> flags;
142 // First do the ones between --flag-switches-begin and --flag-switches-end.
143 base::CommandLine::StringVector::const_iterator first =
144 std::find(cmdline.argv().begin(), cmdline.argv().end(),
145 GetSwitchString(switches::kFlagSwitchesBegin));
146 base::CommandLine::StringVector::const_iterator last =
147 std::find(cmdline.argv().begin(), cmdline.argv().end(),
148 GetSwitchString(switches::kFlagSwitchesEnd));
149 if (first != cmdline.argv().end() && last != cmdline.argv().end())
150 flags.insert(first + 1, last);
151 #if defined(OS_CHROMEOS)
152 // Then add those between --policy-switches-begin and --policy-switches-end.
153 first = std::find(cmdline.argv().begin(), cmdline.argv().end(),
154 GetSwitchString(chromeos::switches::kPolicySwitchesBegin));
155 last = std::find(cmdline.argv().begin(), cmdline.argv().end(),
156 GetSwitchString(chromeos::switches::kPolicySwitchesEnd));
157 if (first != cmdline.argv().end() && last != cmdline.argv().end())
158 flags.insert(first + 1, last);
159 #endif
160 return flags;
161 }
162
163 const FeatureEntry::Choice kTouchEventsChoices[] = { 109 const FeatureEntry::Choice kTouchEventsChoices[] = {
164 { IDS_GENERIC_EXPERIMENT_CHOICE_AUTOMATIC, "", "" }, 110 { IDS_GENERIC_EXPERIMENT_CHOICE_AUTOMATIC, "", "" },
165 { IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED, 111 { IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED,
166 switches::kTouchEvents, 112 switches::kTouchEvents,
167 switches::kTouchEventsEnabled }, 113 switches::kTouchEventsEnabled },
168 { IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED, 114 { IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED,
169 switches::kTouchEvents, 115 switches::kTouchEvents,
170 switches::kTouchEventsDisabled } 116 switches::kTouchEventsDisabled }
171 }; 117 };
172 118
(...skipping 1931 matching lines...) Expand 10 before | Expand all | Expand 10 after
2104 kOsCrOS | kOsWin | kOsLinux, 2050 kOsCrOS | kOsWin | kOsLinux,
2105 ENABLE_DISABLE_VALUE_TYPE( 2051 ENABLE_DISABLE_VALUE_TYPE(
2106 autofill::switches::kEnableOfferUploadCreditCards, 2052 autofill::switches::kEnableOfferUploadCreditCards,
2107 autofill::switches::kDisableOfferUploadCreditCards)}, 2053 autofill::switches::kDisableOfferUploadCreditCards)},
2108 #endif // defined(TOOLKIT_VIEWS) 2054 #endif // defined(TOOLKIT_VIEWS)
2109 // NOTE: Adding new command-line switches requires adding corresponding 2055 // NOTE: Adding new command-line switches requires adding corresponding
2110 // entries to enum "LoginCustomFlags" in histograms.xml. See note in 2056 // entries to enum "LoginCustomFlags" in histograms.xml. See note in
2111 // histograms.xml and don't forget to run AboutFlagsHistogramTest unit test. 2057 // histograms.xml and don't forget to run AboutFlagsHistogramTest unit test.
2112 }; 2058 };
2113 2059
2114 // Stores and encapsulates the little state that about:flags has. 2060 class FlagsStateSingleton {
2115 class FlagsState {
2116 public: 2061 public:
2117 FlagsState() 2062 FlagsStateSingleton()
2118 : feature_entries(kFeatureEntries), 2063 : flags_state_(kFeatureEntries, arraysize(kFeatureEntries)) {}
2119 num_feature_entries(arraysize(kFeatureEntries)), 2064 ~FlagsStateSingleton() {}
2120 needs_restart_(false) {}
2121 void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
2122 base::CommandLine* command_line,
2123 SentinelsMode sentinels);
2124 bool IsRestartNeededToCommitChanges();
2125 void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
2126 const std::string& internal_name,
2127 bool enable);
2128 void RemoveFlagsSwitches(
2129 std::map<std::string, base::CommandLine::StringType>* switch_list);
2130 void ResetAllFlags(flags_ui::FlagsStorage* flags_storage);
2131 void Reset();
2132 2065
2133 // Gets the list of feature entries. Entries that are available for the 2066 static FlagsStateSingleton* GetInstance() {
2134 // current platform are appended to |supported_entries|; all other entries are 2067 return base::Singleton<FlagsStateSingleton>::get();
2135 // appended to |unsupported_entries|. 2068 }
2136 void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
2137 FlagAccess access,
2138 base::ListValue* supported_entries,
2139 base::ListValue* unsupported_entries);
2140 2069
2141 void SetFeatureEntries(const FeatureEntry* entries, size_t count); 2070 static flags_ui::FlagsState* GetFlagsState() {
2142 const FeatureEntry* GetFeatureEntries(size_t* count); 2071 return &GetInstance()->flags_state_;
2143
2144 // Returns the singleton instance of this class
2145 static FlagsState* GetInstance() {
2146 return base::Singleton<FlagsState>::get();
2147 } 2072 }
2148 2073
2149 private: 2074 private:
2150 // Keeps track of affected switches for each FeatureEntry, based on which 2075 flags_ui::FlagsState flags_state_;
2151 // choice is selected for it.
2152 struct SwitchEntry {
2153 // Corresponding base::Feature to toggle.
2154 std::string feature_name;
2155 2076
2156 // If |feature_name| is not empty, the state (enable/disabled) to set. 2077 DISALLOW_COPY_AND_ASSIGN(FlagsStateSingleton);
2157 bool feature_state;
2158
2159 // The name of the switch to add.
2160 std::string switch_name;
2161
2162 // If |switch_name| is not empty, the value of the switch to set.
2163 std::string switch_value;
2164
2165 SwitchEntry() : feature_state(false) {}
2166 };
2167
2168 // Adds mapping to |name_to_switch_map| to set the given switch name/value.
2169 void AddSwitchMapping(const std::string& key,
2170 const std::string& switch_name,
2171 const std::string& switch_value,
2172 std::map<std::string, SwitchEntry>* name_to_switch_map);
2173
2174 // Adds mapping to |name_to_switch_map| to toggle base::Feature |feature_name|
2175 // to state |feature_state|.
2176 void AddFeatureMapping(
2177 const std::string& key,
2178 const std::string& feature_name,
2179 bool feature_state,
2180 std::map<std::string, SwitchEntry>* name_to_switch_map);
2181
2182 // Updates the switches in |command_line| by applying the modifications
2183 // specified in |name_to_switch_map| for each entry in |enabled_entries|.
2184 void AddSwitchesToCommandLine(
2185 const std::set<std::string>& enabled_entries,
2186 const std::map<std::string, SwitchEntry>& name_to_switch_map,
2187 SentinelsMode sentinels,
2188 base::CommandLine* command_line);
2189
2190 // Updates |command_line| by merging the value of the --enable-features= or
2191 // --disable-features= list (per the |switch_name| param) with corresponding
2192 // entries in |feature_switches| that have value |feature_state|. Keeps track
2193 // of the changes by updating |appended_switches|.
2194 void MergeFeatureCommandLineSwitch(
2195 const std::map<std::string, bool>& feature_switches,
2196 const char* switch_name,
2197 bool feature_state,
2198 base::CommandLine* command_line);
2199
2200 // Removes all entries from prefs::kEnabledLabsExperiments that are unknown,
2201 // to prevent this list to become very long as entries are added and removed.
2202 void SanitizeList(flags_ui::FlagsStorage* flags_storage);
2203
2204 void GetSanitizedEnabledFlags(flags_ui::FlagsStorage* flags_storage,
2205 std::set<std::string>* result);
2206
2207 // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
2208 // enabled on the current platform.
2209 void GetSanitizedEnabledFlagsForCurrentPlatform(
2210 flags_ui::FlagsStorage* flags_storage,
2211 std::set<std::string>* result);
2212
2213 const FeatureEntry* feature_entries;
2214 size_t num_feature_entries;
2215
2216 bool needs_restart_;
2217 std::map<std::string, std::string> flags_switches_;
2218
2219 // Map from switch name to a set of string, that keeps track which strings
2220 // were appended to existing (list value) switches.
2221 std::map<std::string, std::set<std::string>> appended_switches_;
2222
2223 DISALLOW_COPY_AND_ASSIGN(FlagsState);
2224 }; 2078 };
2225 2079
2226 // Adds the internal names for the specified entry to |names|.
2227 void AddInternalName(const FeatureEntry& e, std::set<std::string>* names) {
2228 switch (e.type) {
2229 case FeatureEntry::SINGLE_VALUE:
2230 case FeatureEntry::SINGLE_DISABLE_VALUE:
2231 names->insert(e.internal_name);
2232 break;
2233 case FeatureEntry::MULTI_VALUE:
2234 case FeatureEntry::ENABLE_DISABLE_VALUE:
2235 case FeatureEntry::FEATURE_VALUE:
2236 for (int i = 0; i < e.num_choices; ++i)
2237 names->insert(e.NameForChoice(i));
2238 break;
2239 }
2240 }
2241
2242 // Confirms that an entry is valid, used in a DCHECK in
2243 // SanitizeList below.
2244 bool ValidateFeatureEntry(const FeatureEntry& e) {
2245 switch (e.type) {
2246 case FeatureEntry::SINGLE_VALUE:
2247 case FeatureEntry::SINGLE_DISABLE_VALUE:
2248 DCHECK_EQ(0, e.num_choices);
2249 DCHECK(!e.choices);
2250 return true;
2251 case FeatureEntry::MULTI_VALUE:
2252 DCHECK_GT(e.num_choices, 0);
2253 DCHECK(e.choices);
2254 DCHECK(e.choices[0].command_line_switch);
2255 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
2256 return true;
2257 case FeatureEntry::ENABLE_DISABLE_VALUE:
2258 DCHECK_EQ(3, e.num_choices);
2259 DCHECK(!e.choices);
2260 DCHECK(e.command_line_switch);
2261 DCHECK(e.command_line_value);
2262 DCHECK(e.disable_command_line_switch);
2263 DCHECK(e.disable_command_line_value);
2264 return true;
2265 case FeatureEntry::FEATURE_VALUE:
2266 DCHECK_EQ(3, e.num_choices);
2267 DCHECK(!e.choices);
2268 DCHECK(e.feature);
2269 return true;
2270 }
2271 NOTREACHED();
2272 return false;
2273 }
2274
2275 void FlagsState::SanitizeList(flags_ui::FlagsStorage* flags_storage) {
2276 std::set<std::string> known_entries;
2277 for (size_t i = 0; i < num_feature_entries; ++i) {
2278 DCHECK(ValidateFeatureEntry(feature_entries[i]));
2279 AddInternalName(feature_entries[i], &known_entries);
2280 }
2281
2282 std::set<std::string> enabled_entries = flags_storage->GetFlags();
2283
2284 std::set<std::string> new_enabled_entries =
2285 base::STLSetIntersection<std::set<std::string> >(
2286 known_entries, enabled_entries);
2287
2288 if (new_enabled_entries != enabled_entries)
2289 flags_storage->SetFlags(new_enabled_entries);
2290 }
2291
2292 void FlagsState::GetSanitizedEnabledFlags(flags_ui::FlagsStorage* flags_storage,
2293 std::set<std::string>* result) {
2294 SanitizeList(flags_storage);
2295 *result = flags_storage->GetFlags();
2296 }
2297
2298 bool SkipConditionalFeatureEntry(const FeatureEntry& entry) { 2080 bool SkipConditionalFeatureEntry(const FeatureEntry& entry) {
2299 version_info::Channel channel = chrome::GetChannel(); 2081 version_info::Channel channel = chrome::GetChannel();
2300
2301 #if defined(OS_ANDROID) 2082 #if defined(OS_ANDROID)
2302 // enable-data-reduction-proxy-dev is only available for the Dev/Beta channel. 2083 // enable-data-reduction-proxy-dev is only available for the Dev/Beta channel.
2303 if (!strcmp("enable-data-reduction-proxy-dev", entry.internal_name) && 2084 if (!strcmp("enable-data-reduction-proxy-dev", entry.internal_name) &&
2304 channel != version_info::Channel::BETA && 2085 channel != version_info::Channel::BETA &&
2305 channel != version_info::Channel::DEV) { 2086 channel != version_info::Channel::DEV) {
2306 return true; 2087 return true;
2307 } 2088 }
2308 // enable-data-reduction-proxy-alt is only available for the Dev channel. 2089 // enable-data-reduction-proxy-alt is only available for the Dev channel.
2309 if (!strcmp("enable-data-reduction-proxy-alt", entry.internal_name) && 2090 if (!strcmp("enable-data-reduction-proxy-alt", entry.internal_name) &&
2310 channel != version_info::Channel::DEV) { 2091 channel != version_info::Channel::DEV) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2349 channel != version_info::Channel::UNKNOWN && 2130 channel != version_info::Channel::UNKNOWN &&
2350 channel != version_info::Channel::CANARY && 2131 channel != version_info::Channel::CANARY &&
2351 channel != version_info::Channel::DEV) { 2132 channel != version_info::Channel::DEV) {
2352 return true; 2133 return true;
2353 } 2134 }
2354 #endif 2135 #endif
2355 2136
2356 return false; 2137 return false;
2357 } 2138 }
2358 2139
2359 void FlagsState::GetSanitizedEnabledFlagsForCurrentPlatform(
2360 flags_ui::FlagsStorage* flags_storage,
2361 std::set<std::string>* result) {
2362 GetSanitizedEnabledFlags(flags_storage, result);
2363
2364 // Filter out any entries that aren't enabled on the current platform. We
2365 // don't remove these from prefs else syncing to a platform with a different
2366 // set of entries would be lossy.
2367 std::set<std::string> platform_entries;
2368 int current_platform = GetCurrentPlatform();
2369 for (size_t i = 0; i < num_feature_entries; ++i) {
2370 const FeatureEntry& entry = feature_entries[i];
2371 if (entry.supported_platforms & current_platform)
2372 AddInternalName(entry, &platform_entries);
2373 #if defined(OS_CHROMEOS)
2374 if (feature_entries[i].supported_platforms & kOsCrOSOwnerOnly)
2375 AddInternalName(entry, &platform_entries);
2376 #endif
2377 }
2378
2379 std::set<std::string> new_enabled_entries =
2380 base::STLSetIntersection<std::set<std::string> >(
2381 platform_entries, *result);
2382
2383 result->swap(new_enabled_entries);
2384 }
2385
2386 // Returns true if none of this entry's options have been enabled.
2387 bool IsDefaultValue(
2388 const FeatureEntry& entry,
2389 const std::set<std::string>& enabled_entries) {
2390 switch (entry.type) {
2391 case FeatureEntry::SINGLE_VALUE:
2392 case FeatureEntry::SINGLE_DISABLE_VALUE:
2393 return enabled_entries.count(entry.internal_name) == 0;
2394 case FeatureEntry::MULTI_VALUE:
2395 case FeatureEntry::ENABLE_DISABLE_VALUE:
2396 case FeatureEntry::FEATURE_VALUE:
2397 for (int i = 0; i < entry.num_choices; ++i) {
2398 if (enabled_entries.count(entry.NameForChoice(i)) > 0)
2399 return false;
2400 }
2401 return true;
2402 }
2403 NOTREACHED();
2404 return true;
2405 }
2406
2407 // Returns the Value representing the choice data in the specified entry.
2408 base::Value* CreateChoiceData(
2409 const FeatureEntry& entry,
2410 const std::set<std::string>& enabled_entries) {
2411 DCHECK(entry.type == FeatureEntry::MULTI_VALUE ||
2412 entry.type == FeatureEntry::ENABLE_DISABLE_VALUE ||
2413 entry.type == FeatureEntry::FEATURE_VALUE);
2414 base::ListValue* result = new base::ListValue;
2415 for (int i = 0; i < entry.num_choices; ++i) {
2416 base::DictionaryValue* value = new base::DictionaryValue;
2417 const std::string name = entry.NameForChoice(i);
2418 value->SetString("internal_name", name);
2419 value->SetString("description", entry.DescriptionForChoice(i));
2420 value->SetBoolean("selected", enabled_entries.count(name) > 0);
2421 result->Append(value);
2422 }
2423 return result;
2424 }
2425
2426 } // namespace 2140 } // namespace
2427 2141
2428 void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage, 2142 void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
2429 base::CommandLine* command_line, 2143 base::CommandLine* command_line,
2430 SentinelsMode sentinels) { 2144 flags_ui::SentinelsMode sentinels) {
2431 FlagsState::GetInstance()->ConvertFlagsToSwitches(flags_storage, 2145 if (command_line->HasSwitch(switches::kNoExperiments))
2432 command_line, 2146 return;
2433 sentinels); 2147
2148 FlagsStateSingleton::GetFlagsState()->ConvertFlagsToSwitches(
2149 flags_storage, command_line, sentinels, switches::kEnableFeatures,
2150 switches::kDisableFeatures);
2434 } 2151 }
2435 2152
2436 bool AreSwitchesIdenticalToCurrentCommandLine( 2153 bool AreSwitchesIdenticalToCurrentCommandLine(
2437 const base::CommandLine& new_cmdline, 2154 const base::CommandLine& new_cmdline,
2438 const base::CommandLine& active_cmdline, 2155 const base::CommandLine& active_cmdline,
2439 std::set<base::CommandLine::StringType>* out_difference) { 2156 std::set<base::CommandLine::StringType>* out_difference) {
2440 std::set<base::CommandLine::StringType> new_flags = 2157 const char* extra_flag_sentinel_begin_flag_name = nullptr;
2441 ExtractFlagsFromCommandLine(new_cmdline); 2158 const char* extra_flag_sentinel_end_flag_name = nullptr;
2442 std::set<base::CommandLine::StringType> active_flags =
2443 ExtractFlagsFromCommandLine(active_cmdline);
2444
2445 bool result = false;
2446 // Needed because std::equal doesn't check if the 2nd set is empty.
2447 if (new_flags.size() == active_flags.size()) {
2448 result =
2449 std::equal(new_flags.begin(), new_flags.end(), active_flags.begin());
2450 }
2451
2452 if (out_difference && !result) {
2453 std::set_symmetric_difference(
2454 new_flags.begin(),
2455 new_flags.end(),
2456 active_flags.begin(),
2457 active_flags.end(),
2458 std::inserter(*out_difference, out_difference->begin()));
2459 }
2460
2461 return result;
2462 }
2463
2464 void FlagsState::GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
2465 FlagAccess access,
2466 base::ListValue* supported_entries,
2467 base::ListValue* unsupported_entries) {
2468 std::set<std::string> enabled_entries;
2469 GetSanitizedEnabledFlags(flags_storage, &enabled_entries);
2470
2471 int current_platform = GetCurrentPlatform();
2472
2473 for (size_t i = 0; i < num_feature_entries; ++i) {
2474 const FeatureEntry& entry = feature_entries[i];
2475 if (SkipConditionalFeatureEntry(entry))
2476 continue;
2477
2478 base::DictionaryValue* data = new base::DictionaryValue();
2479 data->SetString("internal_name", entry.internal_name);
2480 data->SetString("name",
2481 l10n_util::GetStringUTF16(entry.visible_name_id));
2482 data->SetString("description",
2483 l10n_util::GetStringUTF16(
2484 entry.visible_description_id));
2485
2486 base::ListValue* supported_platforms = new base::ListValue();
2487 AddOsStrings(entry.supported_platforms, supported_platforms);
2488 data->Set("supported_platforms", supported_platforms);
2489 // True if the switch is not currently passed.
2490 bool is_default_value = IsDefaultValue(entry, enabled_entries);
2491 data->SetBoolean("is_default", is_default_value);
2492
2493 switch (entry.type) {
2494 case FeatureEntry::SINGLE_VALUE:
2495 case FeatureEntry::SINGLE_DISABLE_VALUE:
2496 data->SetBoolean(
2497 "enabled",
2498 (!is_default_value &&
2499 entry.type == FeatureEntry::SINGLE_VALUE) ||
2500 (is_default_value &&
2501 entry.type == FeatureEntry::SINGLE_DISABLE_VALUE));
2502 break;
2503 case FeatureEntry::MULTI_VALUE:
2504 case FeatureEntry::ENABLE_DISABLE_VALUE:
2505 case FeatureEntry::FEATURE_VALUE:
2506 data->Set("choices", CreateChoiceData(entry, enabled_entries));
2507 break;
2508 }
2509
2510 bool supported = (entry.supported_platforms & current_platform) != 0;
2511 #if defined(OS_CHROMEOS) 2159 #if defined(OS_CHROMEOS)
2512 if (access == kOwnerAccessToFlags && 2160 // Put the flags between --policy-switches--begin and --policy-switches-end on
2513 (entry.supported_platforms & kOsCrOSOwnerOnly) != 0) { 2161 // ChromeOS.
2514 supported = true; 2162 extra_flag_sentinel_begin_flag_name =
2515 } 2163 chromeos::switches::kPolicySwitchesBegin;
2164 extra_flag_sentinel_end_flag_name = chromeos::switches::kPolicySwitchesEnd;
2516 #endif 2165 #endif
2517 if (supported) 2166 return flags_ui::FlagsState::AreSwitchesIdenticalToCurrentCommandLine(
2518 supported_entries->Append(data); 2167 new_cmdline, active_cmdline, out_difference,
2519 else 2168 extra_flag_sentinel_begin_flag_name, extra_flag_sentinel_end_flag_name);
2520 unsupported_entries->Append(data);
2521 }
2522 } 2169 }
2523 2170
2524 void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage, 2171 void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
2525 FlagAccess access, 2172 flags_ui::FlagAccess access,
2526 base::ListValue* supported_entries, 2173 base::ListValue* supported_entries,
2527 base::ListValue* unsupported_entries) { 2174 base::ListValue* unsupported_entries) {
2528 FlagsState::GetInstance()->GetFlagFeatureEntries(flags_storage, access, 2175 FlagsStateSingleton::GetFlagsState()->GetFlagFeatureEntries(
2529 supported_entries, 2176 flags_storage, access, supported_entries, unsupported_entries,
2530 unsupported_entries); 2177 base::Bind(&SkipConditionalFeatureEntry));
2531 } 2178 }
2532 2179
2533 bool IsRestartNeededToCommitChanges() { 2180 bool IsRestartNeededToCommitChanges() {
2534 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges(); 2181 return FlagsStateSingleton::GetFlagsState()->IsRestartNeededToCommitChanges();
2535 } 2182 }
2536 2183
2537 void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage, 2184 void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
2538 const std::string& internal_name, 2185 const std::string& internal_name,
2539 bool enable) { 2186 bool enable) {
2540 FlagsState::GetInstance()->SetFeatureEntryEnabled(flags_storage, 2187 FlagsStateSingleton::GetFlagsState()->SetFeatureEntryEnabled(
2541 internal_name, enable); 2188 flags_storage, internal_name, enable);
2542 } 2189 }
2543 2190
2544 void RemoveFlagsSwitches( 2191 void RemoveFlagsSwitches(
2545 std::map<std::string, base::CommandLine::StringType>* switch_list) { 2192 std::map<std::string, base::CommandLine::StringType>* switch_list) {
2546 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list); 2193 FlagsStateSingleton::GetFlagsState()->RemoveFlagsSwitches(switch_list);
2547 } 2194 }
2548 2195
2549 void ResetAllFlags(flags_ui::FlagsStorage* flags_storage) { 2196 void ResetAllFlags(flags_ui::FlagsStorage* flags_storage) {
2550 FlagsState::GetInstance()->ResetAllFlags(flags_storage); 2197 FlagsStateSingleton::GetFlagsState()->ResetAllFlags(flags_storage);
2551 }
2552
2553 int GetCurrentPlatform() {
2554 #if defined(OS_MACOSX)
2555 return kOsMac;
2556 #elif defined(OS_WIN)
2557 return kOsWin;
2558 #elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
2559 return kOsCrOS;
2560 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
2561 return kOsLinux;
2562 #elif defined(OS_ANDROID)
2563 return kOsAndroid;
2564 #else
2565 #error Unknown platform
2566 #endif
2567 } 2198 }
2568 2199
2569 void RecordUMAStatistics(flags_ui::FlagsStorage* flags_storage) { 2200 void RecordUMAStatistics(flags_ui::FlagsStorage* flags_storage) {
2570 std::set<std::string> flags = flags_storage->GetFlags(); 2201 std::set<std::string> flags = flags_storage->GetFlags();
2571 for (const std::string& flag : flags) { 2202 for (const std::string& flag : flags) {
2572 std::string action("AboutFlags_"); 2203 std::string action("AboutFlags_");
2573 action += flag; 2204 action += flag;
2574 content::RecordComputedAction(action); 2205 content::RecordComputedAction(action);
2575 } 2206 }
2576 // Since flag metrics are recorded every startup, add a tick so that the 2207 // Since flag metrics are recorded every startup, add a tick so that the
(...skipping 28 matching lines...) Expand all
2605 } 2236 }
2606 DVLOG(1) << "ReportCustomFlags(): histogram='" << uma_histogram_hame 2237 DVLOG(1) << "ReportCustomFlags(): histogram='" << uma_histogram_hame
2607 << "' '" << flag << "', uma_id=" << uma_id; 2238 << "' '" << flag << "', uma_id=" << uma_id;
2608 2239
2609 // Sparse histogram macro does not cache the histogram, so it's safe 2240 // Sparse histogram macro does not cache the histogram, so it's safe
2610 // to use macro with non-static histogram name here. 2241 // to use macro with non-static histogram name here.
2611 UMA_HISTOGRAM_SPARSE_SLOWLY(uma_histogram_hame, uma_id); 2242 UMA_HISTOGRAM_SPARSE_SLOWLY(uma_histogram_hame, uma_id);
2612 } 2243 }
2613 } 2244 }
2614 2245
2615 //////////////////////////////////////////////////////////////////////////////
2616 // FlagsState implementation.
2617
2618 namespace {
2619
2620 void FlagsState::ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
2621 base::CommandLine* command_line,
2622 SentinelsMode sentinels) {
2623 if (command_line->HasSwitch(switches::kNoExperiments))
2624 return;
2625
2626 std::set<std::string> enabled_entries;
2627
2628 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, &enabled_entries);
2629
2630 std::map<std::string, SwitchEntry> name_to_switch_map;
2631 for (size_t i = 0; i < num_feature_entries; ++i) {
2632 const FeatureEntry& e = feature_entries[i];
2633 switch (e.type) {
2634 case FeatureEntry::SINGLE_VALUE:
2635 case FeatureEntry::SINGLE_DISABLE_VALUE:
2636 AddSwitchMapping(e.internal_name, e.command_line_switch,
2637 e.command_line_value, &name_to_switch_map);
2638 break;
2639 case FeatureEntry::MULTI_VALUE:
2640 for (int j = 0; j < e.num_choices; ++j) {
2641 AddSwitchMapping(e.NameForChoice(j), e.choices[j].command_line_switch,
2642 e.choices[j].command_line_value,
2643 &name_to_switch_map);
2644 }
2645 break;
2646 case FeatureEntry::ENABLE_DISABLE_VALUE:
2647 AddSwitchMapping(e.NameForChoice(0), std::string(), std::string(),
2648 &name_to_switch_map);
2649 AddSwitchMapping(e.NameForChoice(1), e.command_line_switch,
2650 e.command_line_value, &name_to_switch_map);
2651 AddSwitchMapping(e.NameForChoice(2), e.disable_command_line_switch,
2652 e.disable_command_line_value, &name_to_switch_map);
2653 break;
2654 case FeatureEntry::FEATURE_VALUE:
2655 AddFeatureMapping(e.NameForChoice(0), std::string(), false,
2656 &name_to_switch_map);
2657 AddFeatureMapping(e.NameForChoice(1), e.feature->name, true,
2658 &name_to_switch_map);
2659 AddFeatureMapping(e.NameForChoice(2), e.feature->name, false,
2660 &name_to_switch_map);
2661 break;
2662 }
2663 }
2664
2665 AddSwitchesToCommandLine(enabled_entries, name_to_switch_map, sentinels,
2666 command_line);
2667 }
2668
2669 bool FlagsState::IsRestartNeededToCommitChanges() {
2670 return needs_restart_;
2671 }
2672
2673 void FlagsState::SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
2674 const std::string& internal_name,
2675 bool enable) {
2676 size_t at_index = internal_name.find(flags_ui::testing::kMultiSeparator);
2677 if (at_index != std::string::npos) {
2678 DCHECK(enable);
2679 // We're being asked to enable a multi-choice entry. Disable the
2680 // currently selected choice.
2681 DCHECK_NE(at_index, 0u);
2682 const std::string entry_name = internal_name.substr(0, at_index);
2683 SetFeatureEntryEnabled(flags_storage, entry_name, false);
2684
2685 // And enable the new choice, if it is not the default first choice.
2686 if (internal_name != entry_name + "@0") {
2687 std::set<std::string> enabled_entries;
2688 GetSanitizedEnabledFlags(flags_storage, &enabled_entries);
2689 needs_restart_ |= enabled_entries.insert(internal_name).second;
2690 flags_storage->SetFlags(enabled_entries);
2691 }
2692 return;
2693 }
2694
2695 std::set<std::string> enabled_entries;
2696 GetSanitizedEnabledFlags(flags_storage, &enabled_entries);
2697
2698 const FeatureEntry* e = nullptr;
2699 for (size_t i = 0; i < num_feature_entries; ++i) {
2700 if (feature_entries[i].internal_name == internal_name) {
2701 e = feature_entries + i;
2702 break;
2703 }
2704 }
2705 DCHECK(e);
2706
2707 if (e->type == FeatureEntry::SINGLE_VALUE) {
2708 if (enable)
2709 needs_restart_ |= enabled_entries.insert(internal_name).second;
2710 else
2711 needs_restart_ |= (enabled_entries.erase(internal_name) > 0);
2712 } else if (e->type == FeatureEntry::SINGLE_DISABLE_VALUE) {
2713 if (!enable)
2714 needs_restart_ |= enabled_entries.insert(internal_name).second;
2715 else
2716 needs_restart_ |= (enabled_entries.erase(internal_name) > 0);
2717 } else {
2718 if (enable) {
2719 // Enable the first choice.
2720 needs_restart_ |= enabled_entries.insert(e->NameForChoice(0)).second;
2721 } else {
2722 // Find the currently enabled choice and disable it.
2723 for (int i = 0; i < e->num_choices; ++i) {
2724 std::string choice_name = e->NameForChoice(i);
2725 if (enabled_entries.find(choice_name) !=
2726 enabled_entries.end()) {
2727 needs_restart_ = true;
2728 enabled_entries.erase(choice_name);
2729 // Continue on just in case there's a bug and more than one
2730 // entry for this choice was enabled.
2731 }
2732 }
2733 }
2734 }
2735
2736 flags_storage->SetFlags(enabled_entries);
2737 }
2738
2739 void FlagsState::RemoveFlagsSwitches(
2740 std::map<std::string, base::CommandLine::StringType>* switch_list) {
2741 for (const auto& entry : flags_switches_)
2742 switch_list->erase(entry.first);
2743
2744 // If feature entries were added to --enable-features= or --disable-features=
2745 // lists, remove them here while preserving existing values.
2746 for (const auto& entry : appended_switches_) {
2747 const auto& switch_name = entry.first;
2748 const auto& switch_added_values = entry.second;
2749
2750 // The below is either a std::string or a base::string16 based on platform.
2751 const auto& existing_value = (*switch_list)[switch_name];
2752 #if defined(OS_WIN)
2753 const std::string existing_value_utf8 = base::UTF16ToUTF8(existing_value);
2754 #else
2755 const std::string& existing_value_utf8 = existing_value;
2756 #endif
2757
2758 std::vector<std::string> features =
2759 base::FeatureList::SplitFeatureListString(existing_value_utf8);
2760 std::vector<std::string> remaining_features;
2761 // For any featrue name in |features| that is not in |switch_added_values| -
2762 // i.e. it wasn't added by about_flags code, add it to |remaining_features|.
2763 for (const std::string& feature : features) {
2764 if (!ContainsKey(switch_added_values, feature))
2765 remaining_features.push_back(feature);
2766 }
2767
2768 // Either remove the flag entirely if |remaining_features| is empty, or set
2769 // the new list.
2770 if (remaining_features.empty()) {
2771 switch_list->erase(switch_name);
2772 } else {
2773 std::string switch_value = base::JoinString(remaining_features, ",");
2774 #if defined(OS_WIN)
2775 (*switch_list)[switch_name] = base::UTF8ToUTF16(switch_value);
2776 #else
2777 (*switch_list)[switch_name] = switch_value;
2778 #endif
2779 }
2780 }
2781 }
2782
2783 void FlagsState::ResetAllFlags(flags_ui::FlagsStorage* flags_storage) {
2784 needs_restart_ = true;
2785
2786 std::set<std::string> no_entries;
2787 flags_storage->SetFlags(no_entries);
2788 }
2789
2790 void FlagsState::Reset() {
2791 needs_restart_ = false;
2792 flags_switches_.clear();
2793 appended_switches_.clear();
2794 }
2795
2796 void FlagsState::SetFeatureEntries(const FeatureEntry* entries, size_t count) {
2797 feature_entries = entries;
2798 num_feature_entries = count;
2799 }
2800
2801 const FeatureEntry* FlagsState::GetFeatureEntries(size_t* count) {
2802 *count = num_feature_entries;
2803 return feature_entries;
2804 }
2805
2806 void FlagsState::AddSwitchMapping(
2807 const std::string& key,
2808 const std::string& switch_name,
2809 const std::string& switch_value,
2810 std::map<std::string, SwitchEntry>* name_to_switch_map) {
2811 DCHECK(!ContainsKey(*name_to_switch_map, key));
2812
2813 SwitchEntry* entry = &(*name_to_switch_map)[key];
2814 entry->switch_name = switch_name;
2815 entry->switch_value = switch_value;
2816 }
2817
2818 void FlagsState::AddFeatureMapping(
2819 const std::string& key,
2820 const std::string& feature_name,
2821 bool feature_state,
2822 std::map<std::string, SwitchEntry>* name_to_switch_map) {
2823 DCHECK(!ContainsKey(*name_to_switch_map, key));
2824
2825 SwitchEntry* entry = &(*name_to_switch_map)[key];
2826 entry->feature_name = feature_name;
2827 entry->feature_state = feature_state;
2828 }
2829
2830 void FlagsState::AddSwitchesToCommandLine(
2831 const std::set<std::string>& enabled_entries,
2832 const std::map<std::string, SwitchEntry>& name_to_switch_map,
2833 SentinelsMode sentinels,
2834 base::CommandLine* command_line) {
2835 std::map<std::string, bool> feature_switches;
2836 if (sentinels == kAddSentinels) {
2837 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
2838 flags_switches_[switches::kFlagSwitchesBegin] = std::string();
2839 }
2840
2841 for (const std::string& entry_name : enabled_entries) {
2842 const auto& entry_it = name_to_switch_map.find(entry_name);
2843 if (entry_it == name_to_switch_map.end()) {
2844 NOTREACHED();
2845 continue;
2846 }
2847
2848 const SwitchEntry& entry = entry_it->second;
2849 if (!entry.feature_name.empty()) {
2850 feature_switches[entry.feature_name] = entry.feature_state;
2851 } else if (!entry.switch_name.empty()) {
2852 command_line->AppendSwitchASCII(entry.switch_name, entry.switch_value);
2853 flags_switches_[entry.switch_name] = entry.switch_value;
2854 }
2855 // If an entry doesn't match either of the above, then it is likely the
2856 // default entry for a FEATURE_VALUE entry. Safe to ignore.
2857 }
2858
2859 if (!feature_switches.empty()) {
2860 MergeFeatureCommandLineSwitch(feature_switches, switches::kEnableFeatures,
2861 true, command_line);
2862 MergeFeatureCommandLineSwitch(feature_switches, switches::kDisableFeatures,
2863 false, command_line);
2864 }
2865
2866 if (sentinels == kAddSentinels) {
2867 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
2868 flags_switches_[switches::kFlagSwitchesEnd] = std::string();
2869 }
2870 }
2871
2872 void FlagsState::MergeFeatureCommandLineSwitch(
2873 const std::map<std::string, bool>& feature_switches,
2874 const char* switch_name,
2875 bool feature_state,
2876 base::CommandLine* command_line) {
2877 std::string original_switch_value =
2878 command_line->GetSwitchValueASCII(switch_name);
2879 std::vector<std::string> features =
2880 base::FeatureList::SplitFeatureListString(original_switch_value);
2881 // Only add features that don't already exist in the lists.
2882 // Note: The ContainsValue() call results in O(n^2) performance, but in
2883 // practice n should be very small.
2884 for (const auto& entry : feature_switches) {
2885 if (entry.second == feature_state &&
2886 !ContainsValue(features, entry.first)) {
2887 features.push_back(entry.first);
2888 appended_switches_[switch_name].insert(entry.first);
2889 }
2890 }
2891 // Update the switch value only if it didn't change. This avoids setting an
2892 // empty list or duplicating the same list (since AppendSwitch() adds the
2893 // switch to the end but doesn't remove previous ones).
2894 std::string switch_value = base::JoinString(features, ",");
2895 if (switch_value != original_switch_value)
2896 command_line->AppendSwitchASCII(switch_name, switch_value);
2897 }
2898
2899 } // namespace
2900
2901 namespace testing { 2246 namespace testing {
2902 2247
2903 const base::HistogramBase::Sample kBadSwitchFormatHistogramId = 0; 2248 const base::HistogramBase::Sample kBadSwitchFormatHistogramId = 0;
2904 2249
2905 void ClearState() {
2906 FlagsState::GetInstance()->Reset();
2907 }
2908
2909 void SetFeatureEntries(const FeatureEntry* entries, size_t count) {
2910 if (!entries) {
2911 entries = kFeatureEntries;
2912 count = arraysize(kFeatureEntries);
2913 }
2914 FlagsState::GetInstance()->SetFeatureEntries(entries, count);
2915 }
2916
2917 const FeatureEntry* GetFeatureEntries(size_t* count) { 2250 const FeatureEntry* GetFeatureEntries(size_t* count) {
2918 return FlagsState::GetInstance()->GetFeatureEntries(count); 2251 *count = arraysize(kFeatureEntries);
2252 return kFeatureEntries;
2919 } 2253 }
2920 2254
2921 } // namespace testing 2255 } // namespace testing
2922 2256
2923 } // namespace about_flags 2257 } // namespace about_flags
OLDNEW
« no previous file with comments | « chrome/browser/about_flags.h ('k') | chrome/browser/about_flags_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698