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