blob: 2828e70177aa0a2a4dcc0ffb2144b998dbf649cb [file] [log] [blame]
[email protected]3828d6f2011-02-24 18:32:211// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]ad2a3ded2010-08-27 13:19:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1a47d7e2010-10-15 00:37:245#include "chrome/browser/about_flags.h"
[email protected]ad2a3ded2010-08-27 13:19:056
7#include <algorithm>
8#include <iterator>
9#include <map>
10#include <set>
11
[email protected]ad2a3ded2010-08-27 13:19:0512#include "base/command_line.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/singleton.h"
[email protected]8a6ff28d2010-12-02 16:35:1914#include "base/string_number_conversions.h"
[email protected]d208f4d82011-05-23 21:52:0315#include "base/utf_string_conversions.h"
[email protected]ad2a3ded2010-08-27 13:19:0516#include "base/values.h"
[email protected]d208f4d82011-05-23 21:52:0317#include "chrome/browser/plugin_updater.h"
[email protected]ad2a3ded2010-08-27 13:19:0518#include "chrome/browser/prefs/pref_service.h"
[email protected]1bc78422011-03-31 08:41:3819#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]d208f4d82011-05-23 21:52:0320#include "chrome/common/chrome_content_client.h"
[email protected]ad2a3ded2010-08-27 13:19:0521#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
[email protected]afd1e522011-04-27 23:29:5923#include "content/browser/user_metrics.h"
[email protected]ad2a3ded2010-08-27 13:19:0524#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1725#include "ui/base/l10n/l10n_util.h"
[email protected]8ff7f342011-05-25 01:49:4726#include "ui/gfx/gl/gl_switches.h"
[email protected]ad2a3ded2010-08-27 13:19:0527
[email protected]1a47d7e2010-10-15 00:37:2428namespace about_flags {
[email protected]ad2a3ded2010-08-27 13:19:0529
[email protected]8a6ff28d2010-12-02 16:35:1930// Macros to simplify specifying the type.
[email protected]a82744532011-02-11 16:15:5331#define SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, switch_value) \
32 Experiment::SINGLE_VALUE, command_line_switch, switch_value, NULL, 0
33#define SINGLE_VALUE_TYPE(command_line_switch) \
34 SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, "")
35#define MULTI_VALUE_TYPE(choices) \
[email protected]0e6f56d2011-02-12 23:45:1536 Experiment::MULTI_VALUE, "", "", choices, arraysize(choices)
[email protected]8a6ff28d2010-12-02 16:35:1937
[email protected]e2ddbc92010-10-15 20:02:0738namespace {
39
[email protected]a314ee5a2010-10-26 21:23:2840const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0541
[email protected]ba8164242010-11-16 21:31:0042// Names for former Chrome OS Labs experiments, shared with prefs migration
43// code.
44const char kMediaPlayerExperimentName[] = "media-player";
45const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
46const char kVerticalTabsExperimentName[] = "vertical-tabs";
47
[email protected]4bc5050c2010-11-18 17:55:5448// RECORDING USER METRICS FOR FLAGS:
49// -----------------------------------------------------------------------------
50// The first line of the experiment is the internal name. If you'd like to
51// gather statistics about the usage of your flag, you should append a marker
52// comment to the end of the feature name, like so:
53// "my-special-feature", // FLAGS:RECORD_UMA
54//
55// After doing that, run //chrome/tools/extract_actions.py (see instructions at
56// the top of that file for details) to update the chromeactions.txt file, which
57// will enable UMA to record your feature flag.
58//
59// After your feature has shipped under a flag, you can locate the metrics
60// under the action name AboutFlags_internal-action-name. Actions are recorded
61// once per startup, so you should divide this number by AboutFlags_StartupTick
62// to get a sense of usage. Note that this will not be the same as number of
63// users with a given feature enabled because users can quit and relaunch
64// the application multiple times over a given time interval.
65// TODO(rsesek): See if there's a way to count per-user, rather than
66// per-startup.
67
[email protected]8a6ff28d2010-12-02 16:35:1968// To add a new experiment add to the end of kExperiments. There are two
69// distinct types of experiments:
70// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
71// macro for this type supplying the command line to the macro.
[email protected]28e35af2011-02-09 12:56:2272// . MULTI_VALUE: a list of choices, the first of which should correspond to a
73// deactivated state for this lab (i.e. no command line option). To specify
74// this type of experiment use the macro MULTI_VALUE_TYPE supplying it the
75// array of choices.
[email protected]8a6ff28d2010-12-02 16:35:1976// See the documentation of Experiment for details on the fields.
77//
78// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0579const Experiment kExperiments[] = {
80 {
[email protected]aac169d2011-03-18 19:53:0381 "expose-for-tabs", // FLAGS:RECORD_UMA
82 IDS_FLAGS_TABPOSE_NAME,
83 IDS_FLAGS_TABPOSE_DESCRIPTION,
84 kOsMac,
85#if defined(OS_MACOSX)
86 // The switch exists only on OS X.
87 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
88#else
89 SINGLE_VALUE_TYPE("")
90#endif
91 },
92 {
[email protected]4bc5050c2010-11-18 17:55:5493 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:1294 IDS_FLAGS_SIDE_TABS_NAME,
95 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:0096 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:1997 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:0598 },
99 {
[email protected]4bc5050c2010-11-18 17:55:54100 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05101 IDS_FLAGS_CONFLICTS_CHECK_NAME,
102 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
103 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19104 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05105 },
106 {
[email protected]4bc5050c2010-11-18 17:55:54107 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12108 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
109 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]5d10d57d2011-07-22 22:16:31110#if defined(GOOGLE_CHROME_BUILD)
[email protected]9f8872b32011-03-04 19:44:45111 // For a Chrome build, we know we have a PDF plug-in on Windows, so it's
[email protected]5d10d57d2011-07-22 22:16:31112 // fully enabled. Linux still need some final polish.
113 kOsLinux,
[email protected]e4798602011-07-22 19:35:11114#else
[email protected]5d10d57d2011-07-22 22:16:31115 // Otherwise, where we know Windows could be working if a viable PDF
116 // plug-in could be supplied, we'll keep the lab enabled. Mac always has
117 // PDF rasterization available, so no flag needed there.
118 kOsWin | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19119#endif
[email protected]8a6ff28d2010-12-02 16:35:19120 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42121 },
[email protected]580939a2010-10-12 18:54:37122 {
[email protected]bb461532010-11-26 21:50:23123 "crxless-web-apps",
124 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
125 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
126 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19127 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23128 },
129 {
[email protected]96c6f4c2011-05-18 19:36:22130 "ignore-gpu-blacklist",
131 IDS_FLAGS_IGNORE_GPU_BLACKLIST_NAME,
132 IDS_FLAGS_IGNORE_GPU_BLACKLIST_DESCRIPTION,
133 kOsAll,
134 SINGLE_VALUE_TYPE(switches::kIgnoreGpuBlacklist)
135 },
136 {
[email protected]0110cf112011-07-02 00:39:43137 "force-compositing-mode-2",
[email protected]96c6f4c2011-05-18 19:36:22138 IDS_FLAGS_FORCE_COMPOSITING_MODE_NAME,
139 IDS_FLAGS_FORCE_COMPOSITING_MODE_DESCRIPTION,
140 kOsAll,
141 SINGLE_VALUE_TYPE(switches::kForceCompositingMode)
142 },
143 {
[email protected]5963b772011-02-09 22:55:38144 "composited-layer-borders",
145 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
146 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
147 kOsAll,
148 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
149 },
150 {
[email protected]a8f1eaa2011-03-07 19:00:58151 "show-fps-counter",
152 IDS_FLAGS_SHOW_FPS_COUNTER,
153 IDS_FLAGS_SHOW_FPS_COUNTER_DESCRIPTION,
154 kOsAll,
155 SINGLE_VALUE_TYPE(switches::kShowFPSCounter)
156 },
[email protected]8ff7f342011-05-25 01:49:47157 {
158 "disable-gpu-vsync",
159 IDS_FLAGS_DISABLE_GPU_VSYNC_NAME,
160 IDS_FLAGS_DISABLE_GPU_VSYNC_DESCRIPTION,
161 kOsAll,
162 SINGLE_VALUE_TYPE(switches::kDisableGpuVsync)
163 },
[email protected]b4291ae2011-07-22 08:29:03164#if !defined(GOOGLE_CHROME_BUILD)
[email protected]38e46812011-05-09 20:49:22165 // Only expose this for Chromium builds where users may not have the PDF
[email protected]b4291ae2011-07-22 08:29:03166 // plugin. Do not give Google Chrome users the option to disable it here.
[email protected]8d260e52010-10-13 01:03:05167 {
[email protected]4bc5050c2010-11-18 17:55:54168 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12169 IDS_FLAGS_PRINT_PREVIEW_NAME,
170 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]b579afd2011-07-13 00:01:27171 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19172 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53173 },
[email protected]38e46812011-05-09 20:49:22174#endif
[email protected]d208f4d82011-05-23 21:52:03175 // TODO(dspringer): When NaCl is on by default, remove this flag entry.
[email protected]2fe15fcb2010-10-21 20:39:53176 {
[email protected]d208f4d82011-05-23 21:52:03177 switches::kEnableNaCl, // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40178 IDS_FLAGS_ENABLE_NACL_NAME,
179 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
180 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19181 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40182 },
183 {
[email protected]4bc5050c2010-11-18 17:55:54184 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32185 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
186 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
187 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19188 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32189 },
[email protected]3627b06d2010-11-12 16:36:16190 {
[email protected]cbe224d2011-08-04 22:12:49191 "apps-new-install-bubble",
192 IDS_FLAGS_APPS_NEW_INSTALL_BUBBLE_NAME,
193 IDS_FLAGS_APPS_NEW_INSTALL_BUBBLE_DESCRIPTION,
194 kOsAll,
195 SINGLE_VALUE_TYPE(switches::kAppsNewInstallBubble)
196 },
197 {
[email protected]4bc5050c2010-11-18 17:55:54198 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16199 IDS_FLAGS_CLICK_TO_PLAY_NAME,
200 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
201 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19202 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16203 },
[email protected]80bd24e2010-11-30 09:34:38204 {
205 "disable-hyperlink-auditing",
206 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
207 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
208 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19209 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51210 },
211 {
212 "experimental-location-features", // FLAGS:RECORD_UMA
213 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
214 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
215 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19216 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
217 },
218 {
[email protected]4c6452d2011-01-12 08:47:57219 "block-reading-third-party-cookies",
220 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
221 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
222 kOsAll,
223 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
224 },
[email protected]04227962011-01-20 02:03:09225 {
226 "disable-interactive-form-validation",
227 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
228 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
229 kOsAll,
230 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
231 },
[email protected]8df51192011-01-22 20:05:03232 {
[email protected]07d490bc2011-03-07 17:05:26233 "focus-existing-tab-on-open", // FLAGS:RECORD_UMA
234 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_NAME,
235 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_DESCRIPTION,
236 kOsAll,
237 SINGLE_VALUE_TYPE(switches::kFocusExistingTabOnOpen)
238 },
[email protected]d5dcfb32011-03-19 00:49:24239 {
240 "new-tab-page-4",
241 IDS_FLAGS_NEW_TAB_PAGE_4_NAME,
242 IDS_FLAGS_NEW_TAB_PAGE_4_DESCRIPTION,
243 kOsAll,
244 SINGLE_VALUE_TYPE(switches::kNewTabPage4)
245 },
[email protected]5aea1862011-03-23 23:55:39246 {
247 "tab-groups-context-menu",
248 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_NAME,
249 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_DESCRIPTION,
250 kOsWin,
251 SINGLE_VALUE_TYPE(switches::kEnableTabGroupsContextMenu)
252 },
[email protected]e9bf9d92011-03-31 20:57:15253 {
254 "ppapi-flash-in-process",
255 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_NAME,
256 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_DESCRIPTION,
[email protected]f2d3ce0d2011-04-01 18:19:30257 kOsAll,
[email protected]e9bf9d92011-03-31 20:57:15258 SINGLE_VALUE_TYPE(switches::kPpapiFlashInProcess)
259 },
[email protected]ce5737142011-04-14 23:35:09260 {
[email protected]20b5df962011-04-18 22:07:43261 "multi-profiles",
262 IDS_FLAGS_MULTI_PROFILES_NAME,
263 IDS_FLAGS_MULTI_PROFILES_DESCRIPTION,
[email protected]0d76f6a2011-07-26 17:10:21264 kOsWin | kOsMac | kOsLinux, // This switch is not available in CrOS.
[email protected]20b5df962011-04-18 22:07:43265 SINGLE_VALUE_TYPE(switches::kMultiProfiles)
266 },
[email protected]cc057b02011-04-21 17:19:04267 {
[email protected]08338b862011-05-05 20:22:23268 "restrict-instant-to-search",
269 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_NAME,
270 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_DESCRIPTION,
271 kOsAll,
272 SINGLE_VALUE_TYPE(switches::kRestrictInstantToSearch)
273 },
[email protected]b652fc82011-05-23 07:53:05274 {
[email protected]f2557bd2011-06-01 02:33:07275 "preload-instant-search",
276 IDS_FLAGS_PRELOAD_INSTANT_SEARCH_NAME,
277 IDS_FLAGS_PRELOAD_INSTANT_SEARCH_DESCRIPTION,
278 kOsAll,
279 SINGLE_VALUE_TYPE(switches::kPreloadInstantSearch)
280 },
[email protected]354e33b2011-06-15 00:29:10281 {
282 "static-ip-config",
283 IDS_FLAGS_STATIC_IP_CONFIG_NAME,
284 IDS_FLAGS_STATIC_IP_CONFIG_DESCRIPTION,
285 kOsCrOS,
286#if defined(OS_CHROMEOS)
287 // This switch exists only on Chrome OS.
288 SINGLE_VALUE_TYPE(switches::kEnableStaticIPConfig)
289#else
290 SINGLE_VALUE_TYPE("")
291#endif
292 },
[email protected]3eb5728c2011-06-20 22:32:24293 {
294 "show-autofill-type-predictions",
295 IDS_FLAGS_SHOW_AUTOFILL_TYPE_PREDICTIONS_NAME,
296 IDS_FLAGS_SHOW_AUTOFILL_TYPE_PREDICTIONS_DESCRIPTION,
297 kOsAll,
298 SINGLE_VALUE_TYPE(switches::kShowAutofillTypePredictions)
299 },
[email protected]bff4d3e2011-06-21 23:58:52300 {
301 "sync-typed-urls",
302 IDS_FLAGS_SYNC_TYPED_URLS_NAME,
303 IDS_FLAGS_SYNC_TYPED_URLS_DESCRIPTION,
304 kOsAll,
305 SINGLE_VALUE_TYPE(switches::kEnableSyncTypedUrls)
306 },
[email protected]a22ebd812011-06-23 00:05:39307 {
308 "enable-smooth-scrolling", // FLAGS:RECORD_UMA
309 IDS_FLAGS_ENABLE_SMOOTH_SCROLLING_NAME,
310 IDS_FLAGS_ENABLE_SMOOTH_SCROLLING_DESCRIPTION,
311 // Can't expose the switch unless the code is compiled in.
312#if defined(ENABLE_SMOOTH_SCROLLING)
313 kOsAll,
314#else
315 0,
316#endif
317 SINGLE_VALUE_TYPE(switches::kEnableSmoothScrolling)
318 },
[email protected]81a6b0b2011-06-24 17:55:40319 {
320 "prerender-from-omnibox",
321 IDS_FLAGS_PRERENDER_FROM_OMNIBOX_NAME,
322 IDS_FLAGS_PRERENDER_FROM_OMNIBOX_DESCRIPTION,
323 kOsAll,
324 SINGLE_VALUE_TYPE(switches::kPrerenderFromOmnibox)
325 },
[email protected]40916632011-07-02 01:11:01326 {
[email protected]f6f1d942011-07-29 00:21:11327 "enable-autofill-feedback",
328 IDS_FLAGS_ENABLE_ADDITIONAL_AUTOFILL_FEEDBACK_NAME,
329 IDS_FLAGS_ENABLE_ADDITIONAL_AUTOFILL_FEEDBACK_DESCRIPTION,
330 kOsAll,
331 SINGLE_VALUE_TYPE(switches::kEnableAutofillFeedback)
332 },
333 {
[email protected]73fb1fc52011-07-09 00:06:54334 "panels",
335 IDS_FLAGS_ENABLE_PANELS_NAME,
336 IDS_FLAGS_ENABLE_PANELS_DESCRIPTION,
337 kOsAll,
338 SINGLE_VALUE_TYPE(switches::kEnablePanels)
339 },
[email protected]e3749d12011-07-25 22:22:12340 {
341 "enable-shortcuts-provider",
342 IDS_FLAGS_ENABLE_SHORTCUTS_PROVIDER,
343 IDS_FLAGS_ENABLE_SHORTCUTS_PROVIDER_DESCRIPTION,
344 kOsAll,
345 SINGLE_VALUE_TYPE(switches::kEnableShortcutsProvider)
346 },
[email protected]80eb4262011-08-03 16:10:41347 {
348 "memory-widget",
349 IDS_FLAGS_MEMORY_WIDGET_NAME,
350 IDS_FLAGS_MEMORY_WIDGET_DESCRIPTION,
351 kOsCrOS,
352#if defined(OS_CHROMEOS)
353 // This switch exists only on Chrome OS.
354 SINGLE_VALUE_TYPE(switches::kMemoryWidget)
355#else
356 SINGLE_VALUE_TYPE("")
357#endif
358 },};
[email protected]ad2a3ded2010-08-27 13:19:05359
[email protected]a314ee5a2010-10-26 21:23:28360const Experiment* experiments = kExperiments;
361size_t num_experiments = arraysize(kExperiments);
362
[email protected]e2ddbc92010-10-15 20:02:07363// Stores and encapsulates the little state that about:flags has.
364class FlagsState {
365 public:
366 FlagsState() : needs_restart_(false) {}
367 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
368 bool IsRestartNeededToCommitChanges();
369 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28370 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07371 void RemoveFlagsSwitches(
372 std::map<std::string, CommandLine::StringType>* switch_list);
373 void reset();
374
375 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55376 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07377 return Singleton<FlagsState>::get();
378 }
379
380 private:
381 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53382 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07383
384 DISALLOW_COPY_AND_ASSIGN(FlagsState);
385};
386
[email protected]c7b7800a2010-10-07 18:51:35387// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05388// in a set.
[email protected]1a47d7e2010-10-15 00:37:24389void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05390 const ListValue* enabled_experiments = prefs->GetList(
391 prefs::kEnabledLabsExperiments);
392 if (!enabled_experiments)
393 return;
394
395 for (ListValue::const_iterator it = enabled_experiments->begin();
396 it != enabled_experiments->end();
397 ++it) {
398 std::string experiment_name;
399 if (!(*it)->GetAsString(&experiment_name)) {
400 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
401 continue;
402 }
403 result->insert(experiment_name);
404 }
405}
406
407// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24408void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05409 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
[email protected]1bc78422011-03-31 08:41:38410 ListPrefUpdate update(prefs, prefs::kEnabledLabsExperiments);
411 ListValue* experiments_list = update.Get();
[email protected]ad2a3ded2010-08-27 13:19:05412
413 experiments_list->Clear();
414 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
415 it != enabled_experiments.end();
416 ++it) {
417 experiments_list->Append(new StringValue(*it));
418 }
419}
420
[email protected]8a6ff28d2010-12-02 16:35:19421// Returns the name used in prefs for the choice at the specified index.
422std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34423 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
424 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19425 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
426 base::IntToString(index);
427}
428
429// Adds the internal names for the specified experiment to |names|.
430void AddInternalName(const Experiment& e, std::set<std::string>* names) {
431 if (e.type == Experiment::SINGLE_VALUE) {
432 names->insert(e.internal_name);
433 } else {
[email protected]2ce9c89752011-02-25 18:24:34434 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19435 for (int i = 0; i < e.num_choices; ++i)
436 names->insert(NameForChoice(e, i));
437 }
438}
439
[email protected]28e35af2011-02-09 12:56:22440// Confirms that an experiment is valid, used in a DCHECK in
441// SanitizeList below.
442bool ValidateExperiment(const Experiment& e) {
443 switch (e.type) {
444 case Experiment::SINGLE_VALUE:
445 DCHECK_EQ(0, e.num_choices);
446 DCHECK(!e.choices);
447 break;
448 case Experiment::MULTI_VALUE:
449 DCHECK_GT(e.num_choices, 0);
450 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53451 DCHECK(e.choices[0].command_line_switch);
452 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22453 break;
454 default:
455 NOTREACHED();
456 }
457 return true;
458}
459
[email protected]ad2a3ded2010-08-27 13:19:05460// Removes all experiments from prefs::kEnabledLabsExperiments that are
461// unknown, to prevent this list to become very long as experiments are added
462// and removed.
463void SanitizeList(PrefService* prefs) {
464 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22465 for (size_t i = 0; i < num_experiments; ++i) {
466 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19467 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22468 }
[email protected]ad2a3ded2010-08-27 13:19:05469
470 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24471 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05472
473 std::set<std::string> new_enabled_experiments;
474 std::set_intersection(
475 known_experiments.begin(), known_experiments.end(),
476 enabled_experiments.begin(), enabled_experiments.end(),
477 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
478
[email protected]1a47d7e2010-10-15 00:37:24479 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05480}
481
[email protected]1a47d7e2010-10-15 00:37:24482void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05483 PrefService* prefs, std::set<std::string>* result) {
484 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24485 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05486}
487
[email protected]a314ee5a2010-10-26 21:23:28488// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
489// enabled on the current platform.
490void GetSanitizedEnabledFlagsForCurrentPlatform(
491 PrefService* prefs, std::set<std::string>* result) {
492 GetSanitizedEnabledFlags(prefs, result);
493
494 // Filter out any experiments that aren't enabled on the current platform. We
495 // don't remove these from prefs else syncing to a platform with a different
496 // set of experiments would be lossy.
497 std::set<std::string> platform_experiments;
498 int current_platform = GetCurrentPlatform();
499 for (size_t i = 0; i < num_experiments; ++i) {
500 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19501 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28502 }
503
504 std::set<std::string> new_enabled_experiments;
505 std::set_intersection(
506 platform_experiments.begin(), platform_experiments.end(),
507 result->begin(), result->end(),
508 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
509
510 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05511}
512
[email protected]8a6ff28d2010-12-02 16:35:19513// Returns the Value representing the choice data in the specified experiment.
[email protected]8a6ff28d2010-12-02 16:35:19514Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22515 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34516 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19517 ListValue* result = new ListValue;
518 for (int i = 0; i < experiment.num_choices; ++i) {
519 const Experiment::Choice& choice = experiment.choices[i];
520 DictionaryValue* value = new DictionaryValue;
521 std::string name = NameForChoice(experiment, i);
522 value->SetString("description",
523 l10n_util::GetStringUTF16(choice.description_id));
524 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22525 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19526 result->Append(value);
527 }
528 return result;
529}
530
[email protected]e2ddbc92010-10-15 20:02:07531} // namespace
532
[email protected]1a47d7e2010-10-15 00:37:24533void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55534 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05535}
536
[email protected]1a47d7e2010-10-15 00:37:24537ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05538 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24539 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05540
541 int current_platform = GetCurrentPlatform();
542
543 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28544 for (size_t i = 0; i < num_experiments; ++i) {
545 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05546 if (!(experiment.supported_platforms & current_platform))
547 continue;
548
549 DictionaryValue* data = new DictionaryValue();
550 data->SetString("internal_name", experiment.internal_name);
551 data->SetString("name",
552 l10n_util::GetStringUTF16(experiment.visible_name_id));
553 data->SetString("description",
554 l10n_util::GetStringUTF16(
555 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05556
[email protected]28e35af2011-02-09 12:56:22557 switch (experiment.type) {
558 case Experiment::SINGLE_VALUE:
559 data->SetBoolean(
560 "enabled",
561 enabled_experiments.count(experiment.internal_name) > 0);
562 break;
563 case Experiment::MULTI_VALUE:
564 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
565 break;
566 default:
567 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19568 }
569
[email protected]ad2a3ded2010-08-27 13:19:05570 experiments_data->Append(data);
571 }
572 return experiments_data;
573}
574
[email protected]ad2a3ded2010-08-27 13:19:05575bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55576 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05577}
578
579void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35580 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55581 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07582}
583
584void RemoveFlagsSwitches(
585 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55586 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07587}
588
[email protected]a314ee5a2010-10-26 21:23:28589int GetCurrentPlatform() {
590#if defined(OS_MACOSX)
591 return kOsMac;
592#elif defined(OS_WIN)
593 return kOsWin;
594#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
595 return kOsCrOS;
596#elif defined(OS_LINUX)
597 return kOsLinux;
598#else
599#error Unknown platform
600#endif
601}
602
[email protected]4bc5050c2010-11-18 17:55:54603void RecordUMAStatistics(const PrefService* prefs) {
604 std::set<std::string> flags;
605 GetEnabledFlags(prefs, &flags);
606 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
607 ++it) {
608 std::string action("AboutFlags_");
609 action += *it;
610 UserMetrics::RecordComputedAction(action);
611 }
612 // Since flag metrics are recorded every startup, add a tick so that the
613 // stats can be made meaningful.
614 if (flags.size())
615 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
[email protected]970b2fd2011-01-22 18:06:45616 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54617}
618
[email protected]e2ddbc92010-10-15 20:02:07619//////////////////////////////////////////////////////////////////////////////
620// FlagsState implementation.
621
622namespace {
623
624void FlagsState::ConvertFlagsToSwitches(
625 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07626 if (command_line->HasSwitch(switches::kNoExperiments))
627 return;
628
629 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00630
[email protected]a314ee5a2010-10-26 21:23:28631 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07632
[email protected]a82744532011-02-11 16:15:53633 typedef std::map<std::string, std::pair<std::string, std::string> >
634 NameToSwitchAndValueMap;
635 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19636 for (size_t i = 0; i < num_experiments; ++i) {
637 const Experiment& e = experiments[i];
638 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53639 name_to_switch_map[e.internal_name] =
640 std::pair<std::string, std::string>(e.command_line_switch,
641 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19642 } else {
643 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53644 name_to_switch_map[NameForChoice(e, j)] =
645 std::pair<std::string, std::string>(
646 e.choices[j].command_line_switch,
647 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19648 }
649 }
[email protected]e2ddbc92010-10-15 20:02:07650
651 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53652 flags_switches_.insert(
653 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
654 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07655 for (std::set<std::string>::iterator it = enabled_experiments.begin();
656 it != enabled_experiments.end();
657 ++it) {
658 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53659 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19660 name_to_switch_map.find(experiment_name);
661 if (name_to_switch_it == name_to_switch_map.end()) {
662 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07663 continue;
[email protected]8a6ff28d2010-12-02 16:35:19664 }
[email protected]e2ddbc92010-10-15 20:02:07665
[email protected]a82744532011-02-11 16:15:53666 const std::pair<std::string, std::string>&
667 switch_and_value_pair = name_to_switch_it->second;
668
669 command_line->AppendSwitchASCII(switch_and_value_pair.first,
670 switch_and_value_pair.second);
671 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07672 }
673 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53674 flags_switches_.insert(
675 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
676 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07677}
678
679bool FlagsState::IsRestartNeededToCommitChanges() {
680 return needs_restart_;
681}
682
683void FlagsState::SetExperimentEnabled(
684 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05685 needs_restart_ = true;
686
[email protected]8a6ff28d2010-12-02 16:35:19687 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
688 if (at_index != std::string::npos) {
689 DCHECK(enable);
690 // We're being asked to enable a multi-choice experiment. Disable the
691 // currently selected choice.
692 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22693 const std::string experiment_name = internal_name.substr(0, at_index);
694 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19695
[email protected]28e35af2011-02-09 12:56:22696 // And enable the new choice, if it is not the default first choice.
697 if (internal_name != experiment_name + "@0") {
698 std::set<std::string> enabled_experiments;
699 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
700 enabled_experiments.insert(internal_name);
701 SetEnabledFlags(prefs, enabled_experiments);
702 }
[email protected]8a6ff28d2010-12-02 16:35:19703 return;
704 }
705
[email protected]ad2a3ded2010-08-27 13:19:05706 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24707 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05708
[email protected]8a6ff28d2010-12-02 16:35:19709 const Experiment* e = NULL;
710 for (size_t i = 0; i < num_experiments; ++i) {
711 if (experiments[i].internal_name == internal_name) {
712 e = experiments + i;
713 break;
714 }
715 }
716 DCHECK(e);
717
718 if (e->type == Experiment::SINGLE_VALUE) {
[email protected]d208f4d82011-05-23 21:52:03719 if (enable) {
[email protected]8a6ff28d2010-12-02 16:35:19720 enabled_experiments.insert(internal_name);
[email protected]d208f4d82011-05-23 21:52:03721 // If enabling NaCl, make sure the plugin is also enabled. See bug
722 // http://code.google.com/p/chromium/issues/detail?id=81010 for more
723 // information.
724 // TODO(dspringer): When NaCl is on by default, remove this code.
725 if (internal_name == switches::kEnableNaCl) {
726 PluginUpdater* plugin_updater = PluginUpdater::GetInstance();
727 string16 nacl_plugin_name =
728 ASCIIToUTF16(chrome::ChromeContentClient::kNaClPluginName);
729 plugin_updater->EnablePluginGroup(true, nacl_plugin_name);
730 }
731 } else {
[email protected]8a6ff28d2010-12-02 16:35:19732 enabled_experiments.erase(internal_name);
[email protected]d208f4d82011-05-23 21:52:03733 }
[email protected]8a6ff28d2010-12-02 16:35:19734 } else {
735 if (enable) {
736 // Enable the first choice.
737 enabled_experiments.insert(NameForChoice(*e, 0));
738 } else {
739 // Find the currently enabled choice and disable it.
740 for (int i = 0; i < e->num_choices; ++i) {
741 std::string choice_name = NameForChoice(*e, i);
742 if (enabled_experiments.find(choice_name) !=
743 enabled_experiments.end()) {
744 enabled_experiments.erase(choice_name);
745 // Continue on just in case there's a bug and more than one
746 // experiment for this choice was enabled.
747 }
748 }
749 }
750 }
[email protected]ad2a3ded2010-08-27 13:19:05751
[email protected]1a47d7e2010-10-15 00:37:24752 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05753}
754
[email protected]e2ddbc92010-10-15 20:02:07755void FlagsState::RemoveFlagsSwitches(
756 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53757 for (std::map<std::string, std::string>::const_iterator
758 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
759 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07760 }
761}
762
763void FlagsState::reset() {
764 needs_restart_ = false;
765 flags_switches_.clear();
766}
767
[email protected]38e46812011-05-09 20:49:22768} // namespace
[email protected]e2ddbc92010-10-15 20:02:07769
770namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19771
772// WARNING: '@' is also used in the html file. If you update this constant you
773// also need to update the html file.
774const char kMultiSeparator[] = "@";
775
[email protected]e2ddbc92010-10-15 20:02:07776void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55777 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07778}
[email protected]a314ee5a2010-10-26 21:23:28779
780void SetExperiments(const Experiment* e, size_t count) {
781 if (!e) {
782 experiments = kExperiments;
783 num_experiments = arraysize(kExperiments);
784 } else {
785 experiments = e;
786 num_experiments = count;
787 }
788}
789
[email protected]8a6ff28d2010-12-02 16:35:19790const Experiment* GetExperiments(size_t* count) {
791 *count = num_experiments;
792 return experiments;
793}
794
[email protected]e2ddbc92010-10-15 20:02:07795} // namespace testing
796
[email protected]1a47d7e2010-10-15 00:37:24797} // namespace about_flags