blob: fec86c439e8bf6273706172909f7e56f6a677fce [file] [log] [blame]
[email protected]ad2a3ded2010-08-27 13:19:051// 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]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
12#include "app/l10n_util.h"
13#include "base/command_line.h"
[email protected]e2ddbc92010-10-15 20:02:0714#include "base/singleton.h"
[email protected]8a6ff28d2010-12-02 16:35:1915#include "base/string_number_conversions.h"
[email protected]ad2a3ded2010-08-27 13:19:0516#include "base/values.h"
[email protected]4bc5050c2010-11-18 17:55:5417#include "chrome/browser/metrics/user_metrics.h"
[email protected]ad2a3ded2010-08-27 13:19:0518#include "chrome/browser/prefs/pref_service.h"
[email protected]ad2a3ded2010-08-27 13:19:0519#include "chrome/common/chrome_switches.h"
20#include "chrome/common/pref_names.h"
21#include "grit/generated_resources.h"
22
[email protected]1a47d7e2010-10-15 00:37:2423namespace about_flags {
[email protected]ad2a3ded2010-08-27 13:19:0524
[email protected]8a6ff28d2010-12-02 16:35:1925// Macros to simplify specifying the type.
26#define SINGLE_VALUE_TYPE(command_line) Experiment::SINGLE_VALUE, \
27 command_line, NULL, 0
28#define MULTI_VALUE_TYPE(choices) Experiment::MULTI_VALUE, "", choices, \
29 arraysize(choices)
30
[email protected]e2ddbc92010-10-15 20:02:0731namespace {
32
[email protected]a314ee5a2010-10-26 21:23:2833const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0534
[email protected]ba8164242010-11-16 21:31:0035// Names for former Chrome OS Labs experiments, shared with prefs migration
36// code.
37const char kMediaPlayerExperimentName[] = "media-player";
38const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
39const char kVerticalTabsExperimentName[] = "vertical-tabs";
40
[email protected]8a6ff28d2010-12-02 16:35:1941// If adding a new choice, add it to the end of the list.
42const Experiment::Choice kInstantChoices[] = {
43 { IDS_FLAGS_INSTANT_TYPE_VERBATIM, switches::kEnableVerbatimInstant },
44 { IDS_FLAGS_INSTANT_TYPE_PREDICTIVE, switches::kEnablePredictiveInstant },
45 { IDS_FLAGS_INSTANT_TYPE_PREDICTIVE_NO_AUTO_COMPLETE,
46 switches::kEnablePredictiveNoAutoCompleteInstant
47 },
48};
49
[email protected]4bc5050c2010-11-18 17:55:5450// RECORDING USER METRICS FOR FLAGS:
51// -----------------------------------------------------------------------------
52// The first line of the experiment is the internal name. If you'd like to
53// gather statistics about the usage of your flag, you should append a marker
54// comment to the end of the feature name, like so:
55// "my-special-feature", // FLAGS:RECORD_UMA
56//
57// After doing that, run //chrome/tools/extract_actions.py (see instructions at
58// the top of that file for details) to update the chromeactions.txt file, which
59// will enable UMA to record your feature flag.
60//
61// After your feature has shipped under a flag, you can locate the metrics
62// under the action name AboutFlags_internal-action-name. Actions are recorded
63// once per startup, so you should divide this number by AboutFlags_StartupTick
64// to get a sense of usage. Note that this will not be the same as number of
65// users with a given feature enabled because users can quit and relaunch
66// the application multiple times over a given time interval.
67// TODO(rsesek): See if there's a way to count per-user, rather than
68// per-startup.
69
[email protected]8a6ff28d2010-12-02 16:35:1970// To add a new experiment add to the end of kExperiments. There are two
71// distinct types of experiments:
72// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
73// macro for this type supplying the command line to the macro.
74// . MULTI_VALUE: if enabled the command line of the selected choice is enabled.
75// To specify this type of experiment use the macro MULTI_VALUE_TYPE supplying
76// it the array of choices.
77// See the documentation of Experiment for details on the fields.
78//
79// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0580const Experiment kExperiments[] = {
81 {
[email protected]4bc5050c2010-11-18 17:55:5482 "expose-for-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:1283 IDS_FLAGS_TABPOSE_NAME,
84 IDS_FLAGS_TABPOSE_DESCRIPTION,
[email protected]ad2a3ded2010-08-27 13:19:0585 kOsMac,
86#if defined(OS_MACOSX)
87 // The switch exists only on OS X.
[email protected]8a6ff28d2010-12-02 16:35:1988 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
[email protected]ad2a3ded2010-08-27 13:19:0589#else
[email protected]8a6ff28d2010-12-02 16:35:1990 SINGLE_VALUE_TYPE("")
[email protected]ad2a3ded2010-08-27 13:19:0591#endif
92 },
93 {
[email protected]ba8164242010-11-16 21:31:0094 kMediaPlayerExperimentName,
95 IDS_FLAGS_MEDIA_PLAYER_NAME,
96 IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION,
97 kOsCrOS,
98#if defined(OS_CHROMEOS)
99 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19100 SINGLE_VALUE_TYPE(switches::kEnableMediaPlayer)
[email protected]ba8164242010-11-16 21:31:00101#else
[email protected]8a6ff28d2010-12-02 16:35:19102 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00103#endif
104 },
105 {
106 kAdvancedFileSystemExperimentName,
107 IDS_FLAGS_ADVANCED_FS_NAME,
108 IDS_FLAGS_ADVANCED_FS_DESCRIPTION,
109 kOsCrOS,
110#if defined(OS_CHROMEOS)
111 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19112 SINGLE_VALUE_TYPE(switches::kEnableAdvancedFileSystem)
[email protected]ba8164242010-11-16 21:31:00113#else
[email protected]8a6ff28d2010-12-02 16:35:19114 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00115#endif
116 },
117 {
[email protected]4bc5050c2010-11-18 17:55:54118 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12119 IDS_FLAGS_SIDE_TABS_NAME,
120 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:00121 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19122 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:05123 },
124 {
[email protected]4bc5050c2010-11-18 17:55:54125 "tabbed-options", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12126 IDS_FLAGS_TABBED_OPTIONS_NAME,
127 IDS_FLAGS_TABBED_OPTIONS_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14128 kOsWin | kOsLinux | kOsMac, // Enabled by default on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19129 SINGLE_VALUE_TYPE(switches::kEnableTabbedOptions)
[email protected]654151872010-09-13 22:43:05130 },
[email protected]bcf91672010-09-16 15:40:21131 {
[email protected]4bc5050c2010-11-18 17:55:54132 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12133 IDS_FLAGS_REMOTING_NAME,
[email protected]4fb2f1192010-09-25 14:56:31134#if defined(OS_WIN)
[email protected]52fa2d52010-09-25 14:08:56135 // Windows only supports host functionality at the moment.
[email protected]9486c1f2010-10-14 19:52:12136 IDS_FLAGS_REMOTING_HOST_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14137#elif defined(OS_LINUX) // Also true for CrOS.
[email protected]52fa2d52010-09-25 14:08:56138 // Linux only supports client functionality at the moment.
[email protected]9486c1f2010-10-14 19:52:12139 IDS_FLAGS_REMOTING_CLIENT_DESCRIPTION,
[email protected]52fa2d52010-09-25 14:08:56140#else
[email protected]a6940852010-09-25 14:25:32141 // On other platforms, this lab isn't available at all.
142 0,
[email protected]52fa2d52010-09-25 14:08:56143#endif
[email protected]1af1dfe2010-10-19 23:49:14144 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19145 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56146 },
[email protected]a6f03652010-09-28 15:59:07147 {
[email protected]4bc5050c2010-11-18 17:55:54148 "disable-outdated-plugins", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12149 IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_NAME,
150 IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_DESCRIPTION,
[email protected]57b66d02010-09-30 11:24:51151 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19152 SINGLE_VALUE_TYPE(switches::kDisableOutdatedPlugins)
[email protected]57b66d02010-09-30 11:24:51153 },
[email protected]b3ce30ea2010-10-01 09:33:53154 {
[email protected]4bc5050c2010-11-18 17:55:54155 "xss-auditor", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12156 IDS_FLAGS_XSS_AUDITOR_NAME,
157 IDS_FLAGS_XSS_AUDITOR_DESCRIPTION,
[email protected]b3ce30ea2010-10-01 09:33:53158 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19159 SINGLE_VALUE_TYPE(switches::kEnableXSSAuditor)
[email protected]b3ce30ea2010-10-01 09:33:53160 },
[email protected]aea2ff42010-10-04 18:04:19161 {
[email protected]4bc5050c2010-11-18 17:55:54162 "background-webapps", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12163 IDS_FLAGS_BACKGROUND_WEBAPPS_NAME,
164 IDS_FLAGS_BACKGROUND_WEBAPPS_DESCRIPTION,
[email protected]186c8af2010-11-24 07:01:10165 kOsMac | kOsLinux | kOsCrOS, // Enabled by default on windows
[email protected]8a6ff28d2010-12-02 16:35:19166 SINGLE_VALUE_TYPE(switches::kEnableBackgroundMode)
[email protected]7bee0b22010-10-05 17:00:47167 },
168 {
[email protected]4bc5050c2010-11-18 17:55:54169 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05170 IDS_FLAGS_CONFLICTS_CHECK_NAME,
171 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
172 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19173 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05174 },
175 {
[email protected]4bc5050c2010-11-18 17:55:54176 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12177 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
178 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19179#if defined(GOOGLE_CHROME_BUILD)
180 // For a Chrome build, we know we have a PDF plug-in, and so we'll
181 // enable by platform as we get things working.
182 0,
183#else
184 // Otherwise, where we know it could be working if a viable PDF
185 // plug-in could be supplied, we'll keep the lab enabled.
[email protected]7bee0b22010-10-05 17:00:47186 kOsWin,
[email protected]fa6d2a2f2010-11-30 21:47:19187#endif
[email protected]8a6ff28d2010-12-02 16:35:19188 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42189 },
[email protected]580939a2010-10-12 18:54:37190 {
[email protected]bb461532010-11-26 21:50:23191 "crxless-web-apps",
192 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
193 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
194 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19195 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23196 },
197 {
[email protected]4bc5050c2010-11-18 17:55:54198 "match-preview", // FLAGS:RECORD_UMA
[email protected]fdf773c52010-11-01 20:58:19199 IDS_FLAGS_PREDICTIVE_INSTANT_NAME,
200 IDS_FLAGS_PREDICTIVE_INSTANT_DESCRIPTION,
[email protected]cab030772010-11-03 21:37:36201 kOsMac,
[email protected]8a6ff28d2010-12-02 16:35:19202 SINGLE_VALUE_TYPE(switches::kEnablePredictiveInstant)
[email protected]fdf773c52010-11-01 20:58:19203 },
[email protected]11c4c812010-10-22 19:50:12204 // FIXME(scheib): Add Flags entry for accelerated Compositing,
205 // or pull it and the strings in generated_resources.grd by Dec 2010
206 // {
[email protected]4bc5050c2010-11-18 17:55:54207 // "gpu-compositing",
[email protected]11c4c812010-10-22 19:50:12208 // IDS_FLAGS_ACCELERATED_COMPOSITING_NAME,
209 // IDS_FLAGS_ACCELERATED_COMPOSITING_DESCRIPTION,
210 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19211 // SINGLE_VALUE_TYPE(switches::kDisableAcceleratedCompositing)
[email protected]11c4c812010-10-22 19:50:12212 // },
[email protected]8b6588a2010-10-12 02:39:42213 {
[email protected]4bc5050c2010-11-18 17:55:54214 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12215 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
216 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14217 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19218 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05219 },
[email protected]11c4c812010-10-22 19:50:12220 // FIXME(scheib): Add Flags entry for WebGL,
221 // or pull it and the strings in generated_resources.grd by Dec 2010
222 // {
[email protected]4bc5050c2010-11-18 17:55:54223 // "webgl",
[email protected]11c4c812010-10-22 19:50:12224 // IDS_FLAGS_WEBGL_NAME,
225 // IDS_FLAGS_WEBGL_DESCRIPTION,
226 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19227 // SINGLE_VALUE_TYPE(switches::kDisableExperimentalWebGL)
[email protected]11c4c812010-10-22 19:50:12228 // }
[email protected]8d260e52010-10-13 01:03:05229 {
[email protected]4bc5050c2010-11-18 17:55:54230 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12231 IDS_FLAGS_PRINT_PREVIEW_NAME,
232 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]8d260e52010-10-13 01:03:05233 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19234 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53235 },
236 {
[email protected]4bc5050c2010-11-18 17:55:54237 "enable-nacl", // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40238 IDS_FLAGS_ENABLE_NACL_NAME,
239 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
240 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19241 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40242 },
243 {
[email protected]4bc5050c2010-11-18 17:55:54244 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53245 IDS_FLAGS_DNS_SERVER_NAME,
246 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
247 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19248 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53249 },
[email protected]177aceb2010-11-03 16:17:41250 {
[email protected]4bc5050c2010-11-18 17:55:54251 "page-prerender", // FLAGS:RECORD_UMA
[email protected]83d00d92010-11-04 19:20:21252 IDS_FLAGS_PAGE_PRERENDER_NAME,
253 IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION,
254 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19255 SINGLE_VALUE_TYPE(switches::kEnablePagePrerender)
[email protected]83d00d92010-11-04 19:20:21256 },
257 {
[email protected]4bc5050c2010-11-18 17:55:54258 "confirm-to-quit", // FLAGS:RECORD_UMA
[email protected]177aceb2010-11-03 16:17:41259 IDS_FLAGS_CONFIRM_TO_QUIT_NAME,
260 IDS_FLAGS_CONFIRM_TO_QUIT_DESCRIPTION,
261 kOsMac,
[email protected]8a6ff28d2010-12-02 16:35:19262 SINGLE_VALUE_TYPE(switches::kEnableConfirmToQuit)
[email protected]177aceb2010-11-03 16:17:41263 },
[email protected]9a40ebef2010-11-10 17:49:13264 {
[email protected]4bc5050c2010-11-18 17:55:54265 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32266 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
267 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
268 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19269 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32270 },
[email protected]3627b06d2010-11-12 16:36:16271 {
[email protected]4bc5050c2010-11-18 17:55:54272 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16273 IDS_FLAGS_CLICK_TO_PLAY_NAME,
274 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
275 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19276 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16277 },
[email protected]80bd24e2010-11-30 09:34:38278 {
279 "disable-hyperlink-auditing",
280 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
281 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
282 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19283 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51284 },
285 {
286 "experimental-location-features", // FLAGS:RECORD_UMA
287 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
288 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
289 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19290 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
291 },
292 {
293 "instant-type", // FLAGS:RECORD_UMA
294 IDS_FLAGS_INSTANT_TYPE_NAME,
295 IDS_FLAGS_INSTANT_TYPE_DESCRIPTION,
296 kOsWin,
297 MULTI_VALUE_TYPE(kInstantChoices)
298 },
[email protected]ad2a3ded2010-08-27 13:19:05299};
300
[email protected]a314ee5a2010-10-26 21:23:28301const Experiment* experiments = kExperiments;
302size_t num_experiments = arraysize(kExperiments);
303
[email protected]e2ddbc92010-10-15 20:02:07304// Stores and encapsulates the little state that about:flags has.
305class FlagsState {
306 public:
307 FlagsState() : needs_restart_(false) {}
308 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
309 bool IsRestartNeededToCommitChanges();
310 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28311 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07312 void RemoveFlagsSwitches(
313 std::map<std::string, CommandLine::StringType>* switch_list);
314 void reset();
315
316 // Returns the singleton instance of this class
317 static FlagsState* instance() {
318 return Singleton<FlagsState>::get();
319 }
320
321 private:
322 bool needs_restart_;
323 std::set<std::string> flags_switches_;
324
325 DISALLOW_COPY_AND_ASSIGN(FlagsState);
326};
327
[email protected]ba8164242010-11-16 21:31:00328#if defined(OS_CHROMEOS)
329// Migrates Chrome OS Labs settings to experiments adding flags to enabled
330// experiment list if the corresponding pref is on.
331void MigrateChromeOSLabsPrefs(PrefService* prefs,
332 std::set<std::string>* result) {
333 DCHECK(prefs);
334 DCHECK(result);
335 if (prefs->GetBoolean(prefs::kLabsMediaplayerEnabled))
336 result->insert(kMediaPlayerExperimentName);
337 if (prefs->GetBoolean(prefs::kLabsAdvancedFilesystemEnabled))
338 result->insert(kAdvancedFileSystemExperimentName);
339 if (prefs->GetBoolean(prefs::kUseVerticalTabs))
340 result->insert(kVerticalTabsExperimentName);
341 prefs->SetBoolean(prefs::kLabsMediaplayerEnabled, false);
342 prefs->SetBoolean(prefs::kLabsAdvancedFilesystemEnabled, false);
343 prefs->SetBoolean(prefs::kUseVerticalTabs, false);
344}
345#endif
346
[email protected]c7b7800a2010-10-07 18:51:35347// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05348// in a set.
[email protected]1a47d7e2010-10-15 00:37:24349void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05350 const ListValue* enabled_experiments = prefs->GetList(
351 prefs::kEnabledLabsExperiments);
352 if (!enabled_experiments)
353 return;
354
355 for (ListValue::const_iterator it = enabled_experiments->begin();
356 it != enabled_experiments->end();
357 ++it) {
358 std::string experiment_name;
359 if (!(*it)->GetAsString(&experiment_name)) {
360 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
361 continue;
362 }
363 result->insert(experiment_name);
364 }
365}
366
367// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24368void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05369 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
370 ListValue* experiments_list = prefs->GetMutableList(
371 prefs::kEnabledLabsExperiments);
372 if (!experiments_list)
373 return;
374
375 experiments_list->Clear();
376 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
377 it != enabled_experiments.end();
378 ++it) {
379 experiments_list->Append(new StringValue(*it));
380 }
381}
382
[email protected]8a6ff28d2010-12-02 16:35:19383// Returns the name used in prefs for the choice at the specified index.
384std::string NameForChoice(const Experiment& e, int index) {
385 DCHECK(e.type == Experiment::MULTI_VALUE);
386 DCHECK(index < e.num_choices);
387 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
388 base::IntToString(index);
389}
390
391// Adds the internal names for the specified experiment to |names|.
392void AddInternalName(const Experiment& e, std::set<std::string>* names) {
393 if (e.type == Experiment::SINGLE_VALUE) {
394 names->insert(e.internal_name);
395 } else {
396 DCHECK(e.type == Experiment::MULTI_VALUE);
397 for (int i = 0; i < e.num_choices; ++i)
398 names->insert(NameForChoice(e, i));
399 }
400}
401
[email protected]ad2a3ded2010-08-27 13:19:05402// Removes all experiments from prefs::kEnabledLabsExperiments that are
403// unknown, to prevent this list to become very long as experiments are added
404// and removed.
405void SanitizeList(PrefService* prefs) {
406 std::set<std::string> known_experiments;
[email protected]a314ee5a2010-10-26 21:23:28407 for (size_t i = 0; i < num_experiments; ++i)
[email protected]8a6ff28d2010-12-02 16:35:19408 AddInternalName(experiments[i], &known_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05409
410 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24411 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05412
413 std::set<std::string> new_enabled_experiments;
414 std::set_intersection(
415 known_experiments.begin(), known_experiments.end(),
416 enabled_experiments.begin(), enabled_experiments.end(),
417 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
418
[email protected]1a47d7e2010-10-15 00:37:24419 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05420}
421
[email protected]1a47d7e2010-10-15 00:37:24422void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05423 PrefService* prefs, std::set<std::string>* result) {
424 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24425 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05426}
427
[email protected]a314ee5a2010-10-26 21:23:28428// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
429// enabled on the current platform.
430void GetSanitizedEnabledFlagsForCurrentPlatform(
431 PrefService* prefs, std::set<std::string>* result) {
432 GetSanitizedEnabledFlags(prefs, result);
433
434 // Filter out any experiments that aren't enabled on the current platform. We
435 // don't remove these from prefs else syncing to a platform with a different
436 // set of experiments would be lossy.
437 std::set<std::string> platform_experiments;
438 int current_platform = GetCurrentPlatform();
439 for (size_t i = 0; i < num_experiments; ++i) {
440 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19441 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28442 }
443
444 std::set<std::string> new_enabled_experiments;
445 std::set_intersection(
446 platform_experiments.begin(), platform_experiments.end(),
447 result->begin(), result->end(),
448 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
449
450 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05451}
452
[email protected]8a6ff28d2010-12-02 16:35:19453// Returns the Value representing the choice data in the specified experiment.
454// If one of the choices is enabled |is_one_selected| is set to true.
455Value* CreateChoiceData(const Experiment& experiment,
456 const std::set<std::string>& enabled_experiments,
457 bool* is_one_selected) {
458 DCHECK(experiment.type == Experiment::MULTI_VALUE);
459 ListValue* result = new ListValue;
460 for (int i = 0; i < experiment.num_choices; ++i) {
461 const Experiment::Choice& choice = experiment.choices[i];
462 DictionaryValue* value = new DictionaryValue;
463 std::string name = NameForChoice(experiment, i);
464 value->SetString("description",
465 l10n_util::GetStringUTF16(choice.description_id));
466 value->SetString("internal_name", name);
467 bool is_selected = enabled_experiments.count(name) > 0;
468 if (is_selected)
469 *is_one_selected = true;
470 value->SetBoolean("selected", is_selected);
471 result->Append(value);
472 }
473 return result;
474}
475
[email protected]e2ddbc92010-10-15 20:02:07476} // namespace
477
[email protected]1a47d7e2010-10-15 00:37:24478void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07479 FlagsState::instance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05480}
481
[email protected]1a47d7e2010-10-15 00:37:24482ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05483 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24484 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05485
486 int current_platform = GetCurrentPlatform();
487
488 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28489 for (size_t i = 0; i < num_experiments; ++i) {
490 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05491 if (!(experiment.supported_platforms & current_platform))
492 continue;
493
494 DictionaryValue* data = new DictionaryValue();
495 data->SetString("internal_name", experiment.internal_name);
496 data->SetString("name",
497 l10n_util::GetStringUTF16(experiment.visible_name_id));
498 data->SetString("description",
499 l10n_util::GetStringUTF16(
500 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05501
[email protected]8a6ff28d2010-12-02 16:35:19502 bool enabled = enabled_experiments.count(experiment.internal_name) > 0;
503
504 if (experiment.type == Experiment::MULTI_VALUE) {
505 data->Set("choices", CreateChoiceData(experiment, enabled_experiments,
506 &enabled));
507 }
508
509 data->SetBoolean("enabled", enabled);
[email protected]ad2a3ded2010-08-27 13:19:05510 experiments_data->Append(data);
511 }
512 return experiments_data;
513}
514
[email protected]ad2a3ded2010-08-27 13:19:05515bool IsRestartNeededToCommitChanges() {
[email protected]e2ddbc92010-10-15 20:02:07516 return FlagsState::instance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05517}
518
519void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35520 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]e2ddbc92010-10-15 20:02:07521 FlagsState::instance()->SetExperimentEnabled(prefs, internal_name, enable);
522}
523
524void RemoveFlagsSwitches(
525 std::map<std::string, CommandLine::StringType>* switch_list) {
526 FlagsState::instance()->RemoveFlagsSwitches(switch_list);
527}
528
[email protected]a314ee5a2010-10-26 21:23:28529int GetCurrentPlatform() {
530#if defined(OS_MACOSX)
531 return kOsMac;
532#elif defined(OS_WIN)
533 return kOsWin;
534#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
535 return kOsCrOS;
536#elif defined(OS_LINUX)
537 return kOsLinux;
538#else
539#error Unknown platform
540#endif
541}
542
[email protected]4bc5050c2010-11-18 17:55:54543void RecordUMAStatistics(const PrefService* prefs) {
544 std::set<std::string> flags;
545 GetEnabledFlags(prefs, &flags);
546 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
547 ++it) {
548 std::string action("AboutFlags_");
549 action += *it;
550 UserMetrics::RecordComputedAction(action);
551 }
552 // Since flag metrics are recorded every startup, add a tick so that the
553 // stats can be made meaningful.
554 if (flags.size())
555 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
556}
557
[email protected]e2ddbc92010-10-15 20:02:07558//////////////////////////////////////////////////////////////////////////////
559// FlagsState implementation.
560
561namespace {
562
563void FlagsState::ConvertFlagsToSwitches(
564 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07565 if (command_line->HasSwitch(switches::kNoExperiments))
566 return;
567
568 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00569
570#if defined(OS_CHROMEOS)
571 // Some experiments were implemented via prefs on Chrome OS and we want to
572 // seamlessly migrate these prefs to about:flags for updated users.
573 MigrateChromeOSLabsPrefs(prefs, &enabled_experiments);
574#endif
575
[email protected]a314ee5a2010-10-26 21:23:28576 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07577
[email protected]8a6ff28d2010-12-02 16:35:19578 typedef std::map<std::string, std::string> NameToSwitchMap;
579 NameToSwitchMap name_to_switch_map;
580 for (size_t i = 0; i < num_experiments; ++i) {
581 const Experiment& e = experiments[i];
582 if (e.type == Experiment::SINGLE_VALUE) {
583 name_to_switch_map[e.internal_name] = e.command_line;
584 } else {
585 for (int j = 0; j < e.num_choices; ++j)
586 name_to_switch_map[NameForChoice(e, j)] = e.choices[j].command_line;
587 }
588 }
[email protected]e2ddbc92010-10-15 20:02:07589
590 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
591 flags_switches_.insert(switches::kFlagSwitchesBegin);
592 for (std::set<std::string>::iterator it = enabled_experiments.begin();
593 it != enabled_experiments.end();
594 ++it) {
595 const std::string& experiment_name = *it;
[email protected]8a6ff28d2010-12-02 16:35:19596 NameToSwitchMap::const_iterator name_to_switch_it =
597 name_to_switch_map.find(experiment_name);
598 if (name_to_switch_it == name_to_switch_map.end()) {
599 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07600 continue;
[email protected]8a6ff28d2010-12-02 16:35:19601 }
[email protected]e2ddbc92010-10-15 20:02:07602
[email protected]8a6ff28d2010-12-02 16:35:19603 command_line->AppendSwitch(name_to_switch_it->second);
604 flags_switches_.insert(name_to_switch_it->second);
[email protected]e2ddbc92010-10-15 20:02:07605 }
606 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
607 flags_switches_.insert(switches::kFlagSwitchesEnd);
608}
609
610bool FlagsState::IsRestartNeededToCommitChanges() {
611 return needs_restart_;
612}
613
614void FlagsState::SetExperimentEnabled(
615 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05616 needs_restart_ = true;
617
[email protected]8a6ff28d2010-12-02 16:35:19618 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
619 if (at_index != std::string::npos) {
620 DCHECK(enable);
621 // We're being asked to enable a multi-choice experiment. Disable the
622 // currently selected choice.
623 DCHECK_NE(at_index, 0u);
624 SetExperimentEnabled(prefs, internal_name.substr(0, at_index), false);
625
626 // And enable the new choice.
627 std::set<std::string> enabled_experiments;
628 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
629 enabled_experiments.insert(internal_name);
630 SetEnabledFlags(prefs, enabled_experiments);
631 return;
632 }
633
[email protected]ad2a3ded2010-08-27 13:19:05634 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24635 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05636
[email protected]8a6ff28d2010-12-02 16:35:19637 const Experiment* e = NULL;
638 for (size_t i = 0; i < num_experiments; ++i) {
639 if (experiments[i].internal_name == internal_name) {
640 e = experiments + i;
641 break;
642 }
643 }
644 DCHECK(e);
645
646 if (e->type == Experiment::SINGLE_VALUE) {
647 if (enable)
648 enabled_experiments.insert(internal_name);
649 else
650 enabled_experiments.erase(internal_name);
651 } else {
652 if (enable) {
653 // Enable the first choice.
654 enabled_experiments.insert(NameForChoice(*e, 0));
655 } else {
656 // Find the currently enabled choice and disable it.
657 for (int i = 0; i < e->num_choices; ++i) {
658 std::string choice_name = NameForChoice(*e, i);
659 if (enabled_experiments.find(choice_name) !=
660 enabled_experiments.end()) {
661 enabled_experiments.erase(choice_name);
662 // Continue on just in case there's a bug and more than one
663 // experiment for this choice was enabled.
664 }
665 }
666 }
667 }
[email protected]ad2a3ded2010-08-27 13:19:05668
[email protected]1a47d7e2010-10-15 00:37:24669 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05670}
671
[email protected]e2ddbc92010-10-15 20:02:07672void FlagsState::RemoveFlagsSwitches(
673 std::map<std::string, CommandLine::StringType>* switch_list) {
674 for (std::set<std::string>::const_iterator it = flags_switches_.begin();
675 it != flags_switches_.end();
676 ++it) {
677 switch_list->erase(*it);
678 }
679}
680
681void FlagsState::reset() {
682 needs_restart_ = false;
683 flags_switches_.clear();
684}
685
686} // namespace
687
688namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19689
690// WARNING: '@' is also used in the html file. If you update this constant you
691// also need to update the html file.
692const char kMultiSeparator[] = "@";
693
[email protected]e2ddbc92010-10-15 20:02:07694void ClearState() {
695 FlagsState::instance()->reset();
696}
[email protected]a314ee5a2010-10-26 21:23:28697
698void SetExperiments(const Experiment* e, size_t count) {
699 if (!e) {
700 experiments = kExperiments;
701 num_experiments = arraysize(kExperiments);
702 } else {
703 experiments = e;
704 num_experiments = count;
705 }
706}
707
[email protected]8a6ff28d2010-12-02 16:35:19708const Experiment* GetExperiments(size_t* count) {
709 *count = num_experiments;
710 return experiments;
711}
712
[email protected]e2ddbc92010-10-15 20:02:07713} // namespace testing
714
[email protected]1a47d7e2010-10-15 00:37:24715} // namespace about_flags