[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 5 | #include "chrome/browser/about_flags.h" |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
| 9 | #include <map> |
| 10 | #include <set> |
| 11 | |
| 12 | #include "app/l10n_util.h" |
| 13 | #include "base/command_line.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 14 | #include "base/singleton.h" |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 16 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 17 | #include "chrome/common/chrome_switches.h" |
| 18 | #include "chrome/common/pref_names.h" |
| 19 | #include "grit/generated_resources.h" |
| 20 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 21 | namespace about_flags { |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 22 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 23 | namespace { |
| 24 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 25 | const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS; |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 26 | |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame^] | 27 | // Names for former Chrome OS Labs experiments, shared with prefs migration |
| 28 | // code. |
| 29 | const char kMediaPlayerExperimentName[] = "media-player"; |
| 30 | const char kAdvancedFileSystemExperimentName[] = "advanced-file-system"; |
| 31 | const char kVerticalTabsExperimentName[] = "vertical-tabs"; |
| 32 | |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 33 | const Experiment kExperiments[] = { |
| 34 | { |
[email protected] | 232448fb | 2010-08-27 18:48:24 | [diff] [blame] | 35 | "expose-for-tabs", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 36 | IDS_FLAGS_TABPOSE_NAME, |
| 37 | IDS_FLAGS_TABPOSE_DESCRIPTION, |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 38 | kOsMac, |
| 39 | #if defined(OS_MACOSX) |
| 40 | // The switch exists only on OS X. |
| 41 | switches::kEnableExposeForTabs |
| 42 | #else |
| 43 | "" |
| 44 | #endif |
| 45 | }, |
| 46 | { |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame^] | 47 | kMediaPlayerExperimentName, |
| 48 | IDS_FLAGS_MEDIA_PLAYER_NAME, |
| 49 | IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION, |
| 50 | kOsCrOS, |
| 51 | #if defined(OS_CHROMEOS) |
| 52 | // The switch exists only on Chrome OS. |
| 53 | switches::kEnableMediaPlayer |
| 54 | #else |
| 55 | "" |
| 56 | #endif |
| 57 | }, |
| 58 | { |
| 59 | kAdvancedFileSystemExperimentName, |
| 60 | IDS_FLAGS_ADVANCED_FS_NAME, |
| 61 | IDS_FLAGS_ADVANCED_FS_DESCRIPTION, |
| 62 | kOsCrOS, |
| 63 | #if defined(OS_CHROMEOS) |
| 64 | // The switch exists only on Chrome OS. |
| 65 | switches::kEnableAdvancedFileSystem |
| 66 | #else |
| 67 | "" |
| 68 | #endif |
| 69 | }, |
| 70 | { |
| 71 | kVerticalTabsExperimentName, |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 72 | IDS_FLAGS_SIDE_TABS_NAME, |
| 73 | IDS_FLAGS_SIDE_TABS_DESCRIPTION, |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame^] | 74 | kOsWin | kOsCrOS, |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 75 | switches::kEnableVerticalTabs |
[email protected] | 65415187 | 2010-09-13 22:43:05 | [diff] [blame] | 76 | }, |
| 77 | { |
| 78 | "tabbed-options", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 79 | IDS_FLAGS_TABBED_OPTIONS_NAME, |
| 80 | IDS_FLAGS_TABBED_OPTIONS_DESCRIPTION, |
[email protected] | 1af1dfe | 2010-10-19 23:49:14 | [diff] [blame] | 81 | kOsWin | kOsLinux | kOsMac, // Enabled by default on CrOS. |
[email protected] | 65415187 | 2010-09-13 22:43:05 | [diff] [blame] | 82 | switches::kEnableTabbedOptions |
| 83 | }, |
[email protected] | bcf9167 | 2010-09-16 15:40:21 | [diff] [blame] | 84 | { |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 85 | "remoting", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 86 | IDS_FLAGS_REMOTING_NAME, |
[email protected] | 4fb2f119 | 2010-09-25 14:56:31 | [diff] [blame] | 87 | #if defined(OS_WIN) |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 88 | // Windows only supports host functionality at the moment. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 89 | IDS_FLAGS_REMOTING_HOST_DESCRIPTION, |
[email protected] | 1af1dfe | 2010-10-19 23:49:14 | [diff] [blame] | 90 | #elif defined(OS_LINUX) // Also true for CrOS. |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 91 | // Linux only supports client functionality at the moment. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 92 | IDS_FLAGS_REMOTING_CLIENT_DESCRIPTION, |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 93 | #else |
[email protected] | a694085 | 2010-09-25 14:25:32 | [diff] [blame] | 94 | // On other platforms, this lab isn't available at all. |
| 95 | 0, |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 96 | #endif |
[email protected] | 1af1dfe | 2010-10-19 23:49:14 | [diff] [blame] | 97 | kOsWin | kOsLinux | kOsCrOS, |
[email protected] | 52fa2d5 | 2010-09-25 14:08:56 | [diff] [blame] | 98 | switches::kEnableRemoting |
| 99 | }, |
[email protected] | a6f0365 | 2010-09-28 15:59:07 | [diff] [blame] | 100 | { |
[email protected] | 57b66d0 | 2010-09-30 11:24:51 | [diff] [blame] | 101 | "disable-outdated-plugins", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 102 | IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_NAME, |
| 103 | IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_DESCRIPTION, |
[email protected] | 57b66d0 | 2010-09-30 11:24:51 | [diff] [blame] | 104 | kOsAll, |
| 105 | switches::kDisableOutdatedPlugins |
| 106 | }, |
[email protected] | b3ce30ea | 2010-10-01 09:33:53 | [diff] [blame] | 107 | { |
| 108 | "xss-auditor", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 109 | IDS_FLAGS_XSS_AUDITOR_NAME, |
| 110 | IDS_FLAGS_XSS_AUDITOR_DESCRIPTION, |
[email protected] | b3ce30ea | 2010-10-01 09:33:53 | [diff] [blame] | 111 | kOsAll, |
| 112 | switches::kEnableXSSAuditor |
| 113 | }, |
[email protected] | aea2ff4 | 2010-10-04 18:04:19 | [diff] [blame] | 114 | { |
| 115 | "background-webapps", // Do not change; see above |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 116 | IDS_FLAGS_BACKGROUND_WEBAPPS_NAME, |
| 117 | IDS_FLAGS_BACKGROUND_WEBAPPS_DESCRIPTION, |
[email protected] | aea2ff4 | 2010-10-04 18:04:19 | [diff] [blame] | 118 | kOsAll, |
| 119 | switches::kEnableBackgroundMode |
[email protected] | 7bee0b2 | 2010-10-05 17:00:47 | [diff] [blame] | 120 | }, |
| 121 | { |
[email protected] | c1bbaa8 | 2010-11-08 11:17:05 | [diff] [blame] | 122 | "conflicting-modules-check", // Do not change; see above. |
| 123 | IDS_FLAGS_CONFLICTS_CHECK_NAME, |
| 124 | IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION, |
| 125 | kOsWin, |
| 126 | switches::kConflictingModulesCheck |
| 127 | }, |
| 128 | { |
[email protected] | 7bee0b2 | 2010-10-05 17:00:47 | [diff] [blame] | 129 | "cloud-print-proxy", // Do not change; see above. |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 130 | IDS_FLAGS_CLOUD_PRINT_PROXY_NAME, |
| 131 | IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION, |
[email protected] | 7bee0b2 | 2010-10-05 17:00:47 | [diff] [blame] | 132 | kOsWin, |
| 133 | switches::kEnableCloudPrintProxy |
[email protected] | 8b6588a | 2010-10-12 02:39:42 | [diff] [blame] | 134 | }, |
[email protected] | 580939a | 2010-10-12 18:54:37 | [diff] [blame] | 135 | { |
| 136 | "match-preview", // Do not change; see above. |
[email protected] | fdf773c5 | 2010-11-01 20:58:19 | [diff] [blame] | 137 | IDS_FLAGS_PREDICTIVE_INSTANT_NAME, |
| 138 | IDS_FLAGS_PREDICTIVE_INSTANT_DESCRIPTION, |
[email protected] | cab03077 | 2010-11-03 21:37:36 | [diff] [blame] | 139 | kOsMac, |
[email protected] | fdf773c5 | 2010-11-01 20:58:19 | [diff] [blame] | 140 | switches::kEnablePredictiveInstant |
| 141 | }, |
[email protected] | 11c4c81 | 2010-10-22 19:50:12 | [diff] [blame] | 142 | // FIXME(scheib): Add Flags entry for accelerated Compositing, |
| 143 | // or pull it and the strings in generated_resources.grd by Dec 2010 |
| 144 | // { |
| 145 | // "gpu-compositing", // Do not change; see above |
| 146 | // IDS_FLAGS_ACCELERATED_COMPOSITING_NAME, |
| 147 | // IDS_FLAGS_ACCELERATED_COMPOSITING_DESCRIPTION, |
| 148 | // kOsAll, |
| 149 | // switches::kDisableAcceleratedCompositing |
| 150 | // }, |
[email protected] | 8b6588a | 2010-10-12 02:39:42 | [diff] [blame] | 151 | { |
| 152 | "gpu-canvas-2d", // Do not change; see above |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 153 | IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME, |
| 154 | IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION, |
[email protected] | 1af1dfe | 2010-10-19 23:49:14 | [diff] [blame] | 155 | kOsWin | kOsLinux | kOsCrOS, |
[email protected] | 8b6588a | 2010-10-12 02:39:42 | [diff] [blame] | 156 | switches::kEnableAccelerated2dCanvas |
[email protected] | 8d260e5 | 2010-10-13 01:03:05 | [diff] [blame] | 157 | }, |
[email protected] | 11c4c81 | 2010-10-22 19:50:12 | [diff] [blame] | 158 | // FIXME(scheib): Add Flags entry for WebGL, |
| 159 | // or pull it and the strings in generated_resources.grd by Dec 2010 |
| 160 | // { |
| 161 | // "webgl", // Do not change; see above |
| 162 | // IDS_FLAGS_WEBGL_NAME, |
| 163 | // IDS_FLAGS_WEBGL_DESCRIPTION, |
| 164 | // kOsAll, |
| 165 | // switches::kDisableExperimentalWebGL |
| 166 | // } |
[email protected] | 8d260e5 | 2010-10-13 01:03:05 | [diff] [blame] | 167 | { |
| 168 | "print-preview", // Do not change; see above |
[email protected] | 9486c1f | 2010-10-14 19:52:12 | [diff] [blame] | 169 | IDS_FLAGS_PRINT_PREVIEW_NAME, |
| 170 | IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION, |
[email protected] | 8d260e5 | 2010-10-13 01:03:05 | [diff] [blame] | 171 | kOsAll, |
| 172 | switches::kEnablePrintPreview |
[email protected] | 2fe15fcb | 2010-10-21 20:39:53 | [diff] [blame] | 173 | }, |
| 174 | { |
[email protected] | 4ff87d20 | 2010-11-06 01:28:40 | [diff] [blame] | 175 | "enable-nacl", // Do not change; see above. |
| 176 | IDS_FLAGS_ENABLE_NACL_NAME, |
| 177 | IDS_FLAGS_ENABLE_NACL_DESCRIPTION, |
| 178 | kOsAll, |
| 179 | switches::kEnableNaCl |
| 180 | }, |
| 181 | { |
[email protected] | 2fe15fcb | 2010-10-21 20:39:53 | [diff] [blame] | 182 | "dns-server", |
| 183 | IDS_FLAGS_DNS_SERVER_NAME, |
| 184 | IDS_FLAGS_DNS_SERVER_DESCRIPTION, |
| 185 | kOsLinux, |
| 186 | switches::kDnsServer |
| 187 | }, |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 188 | { |
[email protected] | 83d00d9 | 2010-11-04 19:20:21 | [diff] [blame] | 189 | "page-prerender", |
| 190 | IDS_FLAGS_PAGE_PRERENDER_NAME, |
| 191 | IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION, |
| 192 | kOsAll, |
| 193 | switches::kEnablePagePrerender |
| 194 | }, |
| 195 | { |
[email protected] | 177aceb | 2010-11-03 16:17:41 | [diff] [blame] | 196 | "confirm-to-quit", // Do not change; see above. |
| 197 | IDS_FLAGS_CONFIRM_TO_QUIT_NAME, |
| 198 | IDS_FLAGS_CONFIRM_TO_QUIT_DESCRIPTION, |
| 199 | kOsMac, |
| 200 | switches::kEnableConfirmToQuit |
| 201 | }, |
[email protected] | 9a40ebef | 2010-11-10 17:49:13 | [diff] [blame] | 202 | { |
| 203 | "snap-start", // Do not change; see above. |
| 204 | IDS_FLAGS_SNAP_START_NAME, |
| 205 | IDS_FLAGS_SNAP_START_DESCRIPTION, |
| 206 | kOsAll, |
| 207 | switches::kEnableSnapStart |
| 208 | }, |
[email protected] | 11dd68cd5 | 2010-11-12 01:15:32 | [diff] [blame] | 209 | { |
| 210 | "extension-apis", // Do not change; see above. |
| 211 | IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME, |
| 212 | IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION, |
| 213 | kOsAll, |
| 214 | switches::kEnableExperimentalExtensionApis |
| 215 | }, |
[email protected] | 3627b06d | 2010-11-12 16:36:16 | [diff] [blame] | 216 | { |
| 217 | "click-to-play", // Do not change; see above. |
| 218 | IDS_FLAGS_CLICK_TO_PLAY_NAME, |
| 219 | IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION, |
| 220 | kOsAll, |
| 221 | switches::kEnableClickToPlay |
| 222 | }, |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 223 | }; |
| 224 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 225 | const Experiment* experiments = kExperiments; |
| 226 | size_t num_experiments = arraysize(kExperiments); |
| 227 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 228 | // Stores and encapsulates the little state that about:flags has. |
| 229 | class FlagsState { |
| 230 | public: |
| 231 | FlagsState() : needs_restart_(false) {} |
| 232 | void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line); |
| 233 | bool IsRestartNeededToCommitChanges(); |
| 234 | void SetExperimentEnabled( |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 235 | PrefService* prefs, const std::string& internal_name, bool enable); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 236 | void RemoveFlagsSwitches( |
| 237 | std::map<std::string, CommandLine::StringType>* switch_list); |
| 238 | void reset(); |
| 239 | |
| 240 | // Returns the singleton instance of this class |
| 241 | static FlagsState* instance() { |
| 242 | return Singleton<FlagsState>::get(); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | bool needs_restart_; |
| 247 | std::set<std::string> flags_switches_; |
| 248 | |
| 249 | DISALLOW_COPY_AND_ASSIGN(FlagsState); |
| 250 | }; |
| 251 | |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame^] | 252 | #if defined(OS_CHROMEOS) |
| 253 | // Migrates Chrome OS Labs settings to experiments adding flags to enabled |
| 254 | // experiment list if the corresponding pref is on. |
| 255 | void MigrateChromeOSLabsPrefs(PrefService* prefs, |
| 256 | std::set<std::string>* result) { |
| 257 | DCHECK(prefs); |
| 258 | DCHECK(result); |
| 259 | if (prefs->GetBoolean(prefs::kLabsMediaplayerEnabled)) |
| 260 | result->insert(kMediaPlayerExperimentName); |
| 261 | if (prefs->GetBoolean(prefs::kLabsAdvancedFilesystemEnabled)) |
| 262 | result->insert(kAdvancedFileSystemExperimentName); |
| 263 | if (prefs->GetBoolean(prefs::kUseVerticalTabs)) |
| 264 | result->insert(kVerticalTabsExperimentName); |
| 265 | prefs->SetBoolean(prefs::kLabsMediaplayerEnabled, false); |
| 266 | prefs->SetBoolean(prefs::kLabsAdvancedFilesystemEnabled, false); |
| 267 | prefs->SetBoolean(prefs::kUseVerticalTabs, false); |
| 268 | } |
| 269 | #endif |
| 270 | |
[email protected] | c7b7800a | 2010-10-07 18:51:35 | [diff] [blame] | 271 | // Extracts the list of enabled lab experiments from preferences and stores them |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 272 | // in a set. |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 273 | void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) { |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 274 | const ListValue* enabled_experiments = prefs->GetList( |
| 275 | prefs::kEnabledLabsExperiments); |
| 276 | if (!enabled_experiments) |
| 277 | return; |
| 278 | |
| 279 | for (ListValue::const_iterator it = enabled_experiments->begin(); |
| 280 | it != enabled_experiments->end(); |
| 281 | ++it) { |
| 282 | std::string experiment_name; |
| 283 | if (!(*it)->GetAsString(&experiment_name)) { |
| 284 | LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments; |
| 285 | continue; |
| 286 | } |
| 287 | result->insert(experiment_name); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Takes a set of enabled lab experiments |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 292 | void SetEnabledFlags( |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 293 | PrefService* prefs, const std::set<std::string>& enabled_experiments) { |
| 294 | ListValue* experiments_list = prefs->GetMutableList( |
| 295 | prefs::kEnabledLabsExperiments); |
| 296 | if (!experiments_list) |
| 297 | return; |
| 298 | |
| 299 | experiments_list->Clear(); |
| 300 | for (std::set<std::string>::const_iterator it = enabled_experiments.begin(); |
| 301 | it != enabled_experiments.end(); |
| 302 | ++it) { |
| 303 | experiments_list->Append(new StringValue(*it)); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Removes all experiments from prefs::kEnabledLabsExperiments that are |
| 308 | // unknown, to prevent this list to become very long as experiments are added |
| 309 | // and removed. |
| 310 | void SanitizeList(PrefService* prefs) { |
| 311 | std::set<std::string> known_experiments; |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 312 | for (size_t i = 0; i < num_experiments; ++i) |
| 313 | known_experiments.insert(experiments[i].internal_name); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 314 | |
| 315 | std::set<std::string> enabled_experiments; |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 316 | GetEnabledFlags(prefs, &enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 317 | |
| 318 | std::set<std::string> new_enabled_experiments; |
| 319 | std::set_intersection( |
| 320 | known_experiments.begin(), known_experiments.end(), |
| 321 | enabled_experiments.begin(), enabled_experiments.end(), |
| 322 | std::inserter(new_enabled_experiments, new_enabled_experiments.begin())); |
| 323 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 324 | SetEnabledFlags(prefs, new_enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 325 | } |
| 326 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 327 | void GetSanitizedEnabledFlags( |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 328 | PrefService* prefs, std::set<std::string>* result) { |
| 329 | SanitizeList(prefs); |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 330 | GetEnabledFlags(prefs, result); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 333 | // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't |
| 334 | // enabled on the current platform. |
| 335 | void GetSanitizedEnabledFlagsForCurrentPlatform( |
| 336 | PrefService* prefs, std::set<std::string>* result) { |
| 337 | GetSanitizedEnabledFlags(prefs, result); |
| 338 | |
| 339 | // Filter out any experiments that aren't enabled on the current platform. We |
| 340 | // don't remove these from prefs else syncing to a platform with a different |
| 341 | // set of experiments would be lossy. |
| 342 | std::set<std::string> platform_experiments; |
| 343 | int current_platform = GetCurrentPlatform(); |
| 344 | for (size_t i = 0; i < num_experiments; ++i) { |
| 345 | if (experiments[i].supported_platforms & current_platform) |
| 346 | platform_experiments.insert(experiments[i].internal_name); |
| 347 | } |
| 348 | |
| 349 | std::set<std::string> new_enabled_experiments; |
| 350 | std::set_intersection( |
| 351 | platform_experiments.begin(), platform_experiments.end(), |
| 352 | result->begin(), result->end(), |
| 353 | std::inserter(new_enabled_experiments, new_enabled_experiments.begin())); |
| 354 | |
| 355 | result->swap(new_enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 358 | } // namespace |
| 359 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 360 | void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 361 | FlagsState::instance()->ConvertFlagsToSwitches(prefs, command_line); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 364 | ListValue* GetFlagsExperimentsData(PrefService* prefs) { |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 365 | std::set<std::string> enabled_experiments; |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 366 | GetSanitizedEnabledFlags(prefs, &enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 367 | |
| 368 | int current_platform = GetCurrentPlatform(); |
| 369 | |
| 370 | ListValue* experiments_data = new ListValue(); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 371 | for (size_t i = 0; i < num_experiments; ++i) { |
| 372 | const Experiment& experiment = experiments[i]; |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 373 | if (!(experiment.supported_platforms & current_platform)) |
| 374 | continue; |
| 375 | |
| 376 | DictionaryValue* data = new DictionaryValue(); |
| 377 | data->SetString("internal_name", experiment.internal_name); |
| 378 | data->SetString("name", |
| 379 | l10n_util::GetStringUTF16(experiment.visible_name_id)); |
| 380 | data->SetString("description", |
| 381 | l10n_util::GetStringUTF16( |
| 382 | experiment.visible_description_id)); |
| 383 | data->SetBoolean("enabled", |
| 384 | enabled_experiments.count(experiment.internal_name) > 0); |
| 385 | |
| 386 | experiments_data->Append(data); |
| 387 | } |
| 388 | return experiments_data; |
| 389 | } |
| 390 | |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 391 | bool IsRestartNeededToCommitChanges() { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 392 | return FlagsState::instance()->IsRestartNeededToCommitChanges(); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void SetExperimentEnabled( |
[email protected] | c7b7800a | 2010-10-07 18:51:35 | [diff] [blame] | 396 | PrefService* prefs, const std::string& internal_name, bool enable) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 397 | FlagsState::instance()->SetExperimentEnabled(prefs, internal_name, enable); |
| 398 | } |
| 399 | |
| 400 | void RemoveFlagsSwitches( |
| 401 | std::map<std::string, CommandLine::StringType>* switch_list) { |
| 402 | FlagsState::instance()->RemoveFlagsSwitches(switch_list); |
| 403 | } |
| 404 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 405 | int GetCurrentPlatform() { |
| 406 | #if defined(OS_MACOSX) |
| 407 | return kOsMac; |
| 408 | #elif defined(OS_WIN) |
| 409 | return kOsWin; |
| 410 | #elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check. |
| 411 | return kOsCrOS; |
| 412 | #elif defined(OS_LINUX) |
| 413 | return kOsLinux; |
| 414 | #else |
| 415 | #error Unknown platform |
| 416 | #endif |
| 417 | } |
| 418 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 419 | ////////////////////////////////////////////////////////////////////////////// |
| 420 | // FlagsState implementation. |
| 421 | |
| 422 | namespace { |
| 423 | |
| 424 | void FlagsState::ConvertFlagsToSwitches( |
| 425 | PrefService* prefs, CommandLine* command_line) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 426 | if (command_line->HasSwitch(switches::kNoExperiments)) |
| 427 | return; |
| 428 | |
| 429 | std::set<std::string> enabled_experiments; |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame^] | 430 | |
| 431 | #if defined(OS_CHROMEOS) |
| 432 | // Some experiments were implemented via prefs on Chrome OS and we want to |
| 433 | // seamlessly migrate these prefs to about:flags for updated users. |
| 434 | MigrateChromeOSLabsPrefs(prefs, &enabled_experiments); |
| 435 | #endif |
| 436 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 437 | GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 438 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 439 | std::map<std::string, const Experiment*> experiment_map; |
| 440 | for (size_t i = 0; i < num_experiments; ++i) |
| 441 | experiment_map[experiments[i].internal_name] = &experiments[i]; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 442 | |
| 443 | command_line->AppendSwitch(switches::kFlagSwitchesBegin); |
| 444 | flags_switches_.insert(switches::kFlagSwitchesBegin); |
| 445 | for (std::set<std::string>::iterator it = enabled_experiments.begin(); |
| 446 | it != enabled_experiments.end(); |
| 447 | ++it) { |
| 448 | const std::string& experiment_name = *it; |
| 449 | std::map<std::string, const Experiment*>::iterator experiment = |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 450 | experiment_map.find(experiment_name); |
| 451 | DCHECK(experiment != experiment_map.end()); |
| 452 | if (experiment == experiment_map.end()) |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 453 | continue; |
| 454 | |
| 455 | command_line->AppendSwitch(experiment->second->command_line); |
| 456 | flags_switches_.insert(experiment->second->command_line); |
| 457 | } |
| 458 | command_line->AppendSwitch(switches::kFlagSwitchesEnd); |
| 459 | flags_switches_.insert(switches::kFlagSwitchesEnd); |
| 460 | } |
| 461 | |
| 462 | bool FlagsState::IsRestartNeededToCommitChanges() { |
| 463 | return needs_restart_; |
| 464 | } |
| 465 | |
| 466 | void FlagsState::SetExperimentEnabled( |
| 467 | PrefService* prefs, const std::string& internal_name, bool enable) { |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 468 | needs_restart_ = true; |
| 469 | |
| 470 | std::set<std::string> enabled_experiments; |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 471 | GetSanitizedEnabledFlags(prefs, &enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 472 | |
| 473 | if (enable) |
| 474 | enabled_experiments.insert(internal_name); |
| 475 | else |
| 476 | enabled_experiments.erase(internal_name); |
| 477 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 478 | SetEnabledFlags(prefs, enabled_experiments); |
[email protected] | ad2a3ded | 2010-08-27 13:19:05 | [diff] [blame] | 479 | } |
| 480 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 481 | void FlagsState::RemoveFlagsSwitches( |
| 482 | std::map<std::string, CommandLine::StringType>* switch_list) { |
| 483 | for (std::set<std::string>::const_iterator it = flags_switches_.begin(); |
| 484 | it != flags_switches_.end(); |
| 485 | ++it) { |
| 486 | switch_list->erase(*it); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void FlagsState::reset() { |
| 491 | needs_restart_ = false; |
| 492 | flags_switches_.clear(); |
| 493 | } |
| 494 | |
| 495 | } // namespace |
| 496 | |
| 497 | namespace testing { |
| 498 | void ClearState() { |
| 499 | FlagsState::instance()->reset(); |
| 500 | } |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 501 | |
| 502 | void SetExperiments(const Experiment* e, size_t count) { |
| 503 | if (!e) { |
| 504 | experiments = kExperiments; |
| 505 | num_experiments = arraysize(kExperiments); |
| 506 | } else { |
| 507 | experiments = e; |
| 508 | num_experiments = count; |
| 509 | } |
| 510 | } |
| 511 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 512 | } // namespace testing |
| 513 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 514 | } // namespace about_flags |