blob: ba3f3b4d48393f82f928ca0d4eb105dabb539cbe [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]e2ddbc92010-10-15 20:02:0713#include "base/singleton.h"
[email protected]8a6ff28d2010-12-02 16:35:1914#include "base/string_number_conversions.h"
[email protected]ad2a3ded2010-08-27 13:19:0515#include "base/values.h"
[email protected]4bc5050c2010-11-18 17:55:5416#include "chrome/browser/metrics/user_metrics.h"
[email protected]ad2a3ded2010-08-27 13:19:0517#include "chrome/browser/prefs/pref_service.h"
[email protected]ad2a3ded2010-08-27 13:19:0518#include "chrome/common/chrome_switches.h"
19#include "chrome/common/pref_names.h"
20#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1721#include "ui/base/l10n/l10n_util.h"
[email protected]ad2a3ded2010-08-27 13:19:0522
[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.
[email protected]a82744532011-02-11 16:15:5326#define SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, switch_value) \
27 Experiment::SINGLE_VALUE, command_line_switch, switch_value, NULL, 0
28#define SINGLE_VALUE_TYPE(command_line_switch) \
29 SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, "")
30#define MULTI_VALUE_TYPE(choices) \
[email protected]0e6f56d2011-02-12 23:45:1531 Experiment::MULTI_VALUE, "", "", choices, arraysize(choices)
[email protected]8a6ff28d2010-12-02 16:35:1932
[email protected]e2ddbc92010-10-15 20:02:0733namespace {
34
[email protected]a314ee5a2010-10-26 21:23:2835const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0536
[email protected]ba8164242010-11-16 21:31:0037// Names for former Chrome OS Labs experiments, shared with prefs migration
38// code.
39const char kMediaPlayerExperimentName[] = "media-player";
40const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
41const char kVerticalTabsExperimentName[] = "vertical-tabs";
42
[email protected]0e6f56d2011-02-12 23:45:1543const Experiment::Choice kPagePrerenderChoices[] = {
44 { IDS_FLAGS_PAGE_PRERENDER_AUTOMATIC, "", "" },
45 { IDS_FLAGS_PAGE_PRERENDER_ENABLED,
46 switches::kPrerender, switches::kPrerenderSwitchValueEnabled },
47 { IDS_FLAGS_PAGE_PRERENDER_DISABLED,
48 switches::kPrerender, switches::kPrerenderSwitchValueDisabled },
49};
50
[email protected]4bc5050c2010-11-18 17:55:5451// RECORDING USER METRICS FOR FLAGS:
52// -----------------------------------------------------------------------------
53// The first line of the experiment is the internal name. If you'd like to
54// gather statistics about the usage of your flag, you should append a marker
55// comment to the end of the feature name, like so:
56// "my-special-feature", // FLAGS:RECORD_UMA
57//
58// After doing that, run //chrome/tools/extract_actions.py (see instructions at
59// the top of that file for details) to update the chromeactions.txt file, which
60// will enable UMA to record your feature flag.
61//
62// After your feature has shipped under a flag, you can locate the metrics
63// under the action name AboutFlags_internal-action-name. Actions are recorded
64// once per startup, so you should divide this number by AboutFlags_StartupTick
65// to get a sense of usage. Note that this will not be the same as number of
66// users with a given feature enabled because users can quit and relaunch
67// the application multiple times over a given time interval.
68// TODO(rsesek): See if there's a way to count per-user, rather than
69// per-startup.
70
[email protected]8a6ff28d2010-12-02 16:35:1971// To add a new experiment add to the end of kExperiments. There are two
72// distinct types of experiments:
73// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
74// macro for this type supplying the command line to the macro.
[email protected]28e35af2011-02-09 12:56:2275// . MULTI_VALUE: a list of choices, the first of which should correspond to a
76// deactivated state for this lab (i.e. no command line option). To specify
77// this type of experiment use the macro MULTI_VALUE_TYPE supplying it the
78// array of choices.
[email protected]8a6ff28d2010-12-02 16:35:1979// See the documentation of Experiment for details on the fields.
80//
81// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0582const Experiment kExperiments[] = {
83 {
[email protected]ba8164242010-11-16 21:31:0084 kMediaPlayerExperimentName,
85 IDS_FLAGS_MEDIA_PLAYER_NAME,
86 IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION,
87 kOsCrOS,
88#if defined(OS_CHROMEOS)
89 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:1990 SINGLE_VALUE_TYPE(switches::kEnableMediaPlayer)
[email protected]ba8164242010-11-16 21:31:0091#else
[email protected]8a6ff28d2010-12-02 16:35:1992 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:0093#endif
94 },
95 {
96 kAdvancedFileSystemExperimentName,
97 IDS_FLAGS_ADVANCED_FS_NAME,
98 IDS_FLAGS_ADVANCED_FS_DESCRIPTION,
99 kOsCrOS,
100#if defined(OS_CHROMEOS)
101 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19102 SINGLE_VALUE_TYPE(switches::kEnableAdvancedFileSystem)
[email protected]ba8164242010-11-16 21:31:00103#else
[email protected]8a6ff28d2010-12-02 16:35:19104 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00105#endif
106 },
107 {
[email protected]4bc5050c2010-11-18 17:55:54108 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12109 IDS_FLAGS_SIDE_TABS_NAME,
110 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:00111 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19112 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:05113 },
114 {
[email protected]4bc5050c2010-11-18 17:55:54115 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12116 IDS_FLAGS_REMOTING_NAME,
[email protected]d99599862011-01-20 22:43:59117 IDS_FLAGS_REMOTING_DESCRIPTION,
118 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19119 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56120 },
[email protected]a6f03652010-09-28 15:59:07121 {
[email protected]4bc5050c2010-11-18 17:55:54122 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05123 IDS_FLAGS_CONFLICTS_CHECK_NAME,
124 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
125 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19126 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05127 },
128 {
[email protected]4bc5050c2010-11-18 17:55:54129 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12130 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
131 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19132#if defined(GOOGLE_CHROME_BUILD)
[email protected]9f8872b32011-03-04 19:44:45133 // For a Chrome build, we know we have a PDF plug-in on Windows, so it's
134 // fully enabled. Mac and Linux still need some final polish.
135 kOsMac | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19136#else
137 // Otherwise, where we know it could be working if a viable PDF
138 // plug-in could be supplied, we'll keep the lab enabled.
[email protected]9f8872b32011-03-04 19:44:45139 kOsWin | kOsMac | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19140#endif
[email protected]8a6ff28d2010-12-02 16:35:19141 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42142 },
[email protected]580939a2010-10-12 18:54:37143 {
[email protected]bb461532010-11-26 21:50:23144 "crxless-web-apps",
145 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
146 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
147 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19148 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23149 },
150 {
[email protected]5963b772011-02-09 22:55:38151 "composited-layer-borders",
152 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
153 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
154 kOsAll,
155 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
156 },
157 {
[email protected]4bc5050c2010-11-18 17:55:54158 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12159 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
160 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14161 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19162 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05163 },
[email protected]11c4c812010-10-22 19:50:12164 // FIXME(scheib): Add Flags entry for WebGL,
165 // or pull it and the strings in generated_resources.grd by Dec 2010
166 // {
[email protected]4bc5050c2010-11-18 17:55:54167 // "webgl",
[email protected]11c4c812010-10-22 19:50:12168 // IDS_FLAGS_WEBGL_NAME,
169 // IDS_FLAGS_WEBGL_DESCRIPTION,
170 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19171 // SINGLE_VALUE_TYPE(switches::kDisableExperimentalWebGL)
[email protected]11c4c812010-10-22 19:50:12172 // }
[email protected]8d260e52010-10-13 01:03:05173 {
[email protected]4bc5050c2010-11-18 17:55:54174 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12175 IDS_FLAGS_PRINT_PREVIEW_NAME,
176 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]8d260e52010-10-13 01:03:05177 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19178 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53179 },
180 {
[email protected]4bc5050c2010-11-18 17:55:54181 "enable-nacl", // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40182 IDS_FLAGS_ENABLE_NACL_NAME,
183 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
184 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19185 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40186 },
187 {
[email protected]4bc5050c2010-11-18 17:55:54188 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53189 IDS_FLAGS_DNS_SERVER_NAME,
190 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
191 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19192 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53193 },
[email protected]177aceb2010-11-03 16:17:41194 {
[email protected]4bc5050c2010-11-18 17:55:54195 "page-prerender", // FLAGS:RECORD_UMA
[email protected]83d00d92010-11-04 19:20:21196 IDS_FLAGS_PAGE_PRERENDER_NAME,
197 IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION,
198 kOsAll,
[email protected]0e6f56d2011-02-12 23:45:15199 MULTI_VALUE_TYPE(kPagePrerenderChoices)
[email protected]83d00d92010-11-04 19:20:21200 },
201 {
[email protected]4bc5050c2010-11-18 17:55:54202 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32203 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
204 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
205 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19206 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32207 },
[email protected]3627b06d2010-11-12 16:36:16208 {
[email protected]4bc5050c2010-11-18 17:55:54209 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16210 IDS_FLAGS_CLICK_TO_PLAY_NAME,
211 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
212 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19213 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16214 },
[email protected]80bd24e2010-11-30 09:34:38215 {
216 "disable-hyperlink-auditing",
217 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
218 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
219 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19220 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51221 },
222 {
223 "experimental-location-features", // FLAGS:RECORD_UMA
224 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
225 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
226 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19227 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
228 },
229 {
[email protected]4c6452d2011-01-12 08:47:57230 "block-reading-third-party-cookies",
231 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
232 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
233 kOsAll,
234 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
235 },
[email protected]04227962011-01-20 02:03:09236 {
237 "disable-interactive-form-validation",
238 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
239 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
240 kOsAll,
241 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
242 },
[email protected]8df51192011-01-22 20:05:03243 {
244 "webaudio",
245 IDS_FLAGS_WEBAUDIO_NAME,
246 IDS_FLAGS_WEBAUDIO_DESCRIPTION,
247 kOsMac, // TODO(crogers): add windows and linux when FFT is ready.
248 SINGLE_VALUE_TYPE(switches::kEnableWebAudio)
249 },
[email protected]3828d6f2011-02-24 18:32:21250 {
251 "enable-history-quick-provider",
252 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER,
253 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER_DESCRIPTION,
254 kOsAll,
255 SINGLE_VALUE_TYPE(switches::kEnableHistoryQuickProvider)
256 },
[email protected]6c54e7e42011-03-02 20:52:34257 {
258 "p2papi",
259 IDS_FLAGS_P2P_API_NAME,
260 IDS_FLAGS_P2P_API_DESCRIPTION,
261 kOsAll,
262 SINGLE_VALUE_TYPE(switches::kEnableP2PApi)
263 },
[email protected]ad2a3ded2010-08-27 13:19:05264};
265
[email protected]a314ee5a2010-10-26 21:23:28266const Experiment* experiments = kExperiments;
267size_t num_experiments = arraysize(kExperiments);
268
[email protected]e2ddbc92010-10-15 20:02:07269// Stores and encapsulates the little state that about:flags has.
270class FlagsState {
271 public:
272 FlagsState() : needs_restart_(false) {}
273 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
274 bool IsRestartNeededToCommitChanges();
275 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28276 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07277 void RemoveFlagsSwitches(
278 std::map<std::string, CommandLine::StringType>* switch_list);
279 void reset();
280
281 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55282 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07283 return Singleton<FlagsState>::get();
284 }
285
286 private:
287 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53288 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07289
290 DISALLOW_COPY_AND_ASSIGN(FlagsState);
291};
292
[email protected]c7b7800a2010-10-07 18:51:35293// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05294// in a set.
[email protected]1a47d7e2010-10-15 00:37:24295void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05296 const ListValue* enabled_experiments = prefs->GetList(
297 prefs::kEnabledLabsExperiments);
298 if (!enabled_experiments)
299 return;
300
301 for (ListValue::const_iterator it = enabled_experiments->begin();
302 it != enabled_experiments->end();
303 ++it) {
304 std::string experiment_name;
305 if (!(*it)->GetAsString(&experiment_name)) {
306 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
307 continue;
308 }
309 result->insert(experiment_name);
310 }
311}
312
313// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24314void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05315 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
316 ListValue* experiments_list = prefs->GetMutableList(
317 prefs::kEnabledLabsExperiments);
318 if (!experiments_list)
319 return;
320
321 experiments_list->Clear();
322 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
323 it != enabled_experiments.end();
324 ++it) {
325 experiments_list->Append(new StringValue(*it));
326 }
327}
328
[email protected]8a6ff28d2010-12-02 16:35:19329// Returns the name used in prefs for the choice at the specified index.
330std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34331 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
332 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19333 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
334 base::IntToString(index);
335}
336
337// Adds the internal names for the specified experiment to |names|.
338void AddInternalName(const Experiment& e, std::set<std::string>* names) {
339 if (e.type == Experiment::SINGLE_VALUE) {
340 names->insert(e.internal_name);
341 } else {
[email protected]2ce9c89752011-02-25 18:24:34342 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19343 for (int i = 0; i < e.num_choices; ++i)
344 names->insert(NameForChoice(e, i));
345 }
346}
347
[email protected]28e35af2011-02-09 12:56:22348// Confirms that an experiment is valid, used in a DCHECK in
349// SanitizeList below.
350bool ValidateExperiment(const Experiment& e) {
351 switch (e.type) {
352 case Experiment::SINGLE_VALUE:
353 DCHECK_EQ(0, e.num_choices);
354 DCHECK(!e.choices);
355 break;
356 case Experiment::MULTI_VALUE:
357 DCHECK_GT(e.num_choices, 0);
358 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53359 DCHECK(e.choices[0].command_line_switch);
360 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22361 break;
362 default:
363 NOTREACHED();
364 }
365 return true;
366}
367
[email protected]ad2a3ded2010-08-27 13:19:05368// Removes all experiments from prefs::kEnabledLabsExperiments that are
369// unknown, to prevent this list to become very long as experiments are added
370// and removed.
371void SanitizeList(PrefService* prefs) {
372 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22373 for (size_t i = 0; i < num_experiments; ++i) {
374 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19375 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22376 }
[email protected]ad2a3ded2010-08-27 13:19:05377
378 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24379 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05380
381 std::set<std::string> new_enabled_experiments;
382 std::set_intersection(
383 known_experiments.begin(), known_experiments.end(),
384 enabled_experiments.begin(), enabled_experiments.end(),
385 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
386
[email protected]1a47d7e2010-10-15 00:37:24387 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05388}
389
[email protected]1a47d7e2010-10-15 00:37:24390void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05391 PrefService* prefs, std::set<std::string>* result) {
392 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24393 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05394}
395
[email protected]a314ee5a2010-10-26 21:23:28396// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
397// enabled on the current platform.
398void GetSanitizedEnabledFlagsForCurrentPlatform(
399 PrefService* prefs, std::set<std::string>* result) {
400 GetSanitizedEnabledFlags(prefs, result);
401
402 // Filter out any experiments that aren't enabled on the current platform. We
403 // don't remove these from prefs else syncing to a platform with a different
404 // set of experiments would be lossy.
405 std::set<std::string> platform_experiments;
406 int current_platform = GetCurrentPlatform();
407 for (size_t i = 0; i < num_experiments; ++i) {
408 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19409 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28410 }
411
412 std::set<std::string> new_enabled_experiments;
413 std::set_intersection(
414 platform_experiments.begin(), platform_experiments.end(),
415 result->begin(), result->end(),
416 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
417
418 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05419}
420
[email protected]8a6ff28d2010-12-02 16:35:19421// Returns the Value representing the choice data in the specified experiment.
[email protected]8a6ff28d2010-12-02 16:35:19422Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22423 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34424 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19425 ListValue* result = new ListValue;
426 for (int i = 0; i < experiment.num_choices; ++i) {
427 const Experiment::Choice& choice = experiment.choices[i];
428 DictionaryValue* value = new DictionaryValue;
429 std::string name = NameForChoice(experiment, i);
430 value->SetString("description",
431 l10n_util::GetStringUTF16(choice.description_id));
432 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22433 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19434 result->Append(value);
435 }
436 return result;
437}
438
[email protected]e2ddbc92010-10-15 20:02:07439} // namespace
440
[email protected]1a47d7e2010-10-15 00:37:24441void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55442 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05443}
444
[email protected]1a47d7e2010-10-15 00:37:24445ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05446 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24447 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05448
449 int current_platform = GetCurrentPlatform();
450
451 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28452 for (size_t i = 0; i < num_experiments; ++i) {
453 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05454 if (!(experiment.supported_platforms & current_platform))
455 continue;
456
457 DictionaryValue* data = new DictionaryValue();
458 data->SetString("internal_name", experiment.internal_name);
459 data->SetString("name",
460 l10n_util::GetStringUTF16(experiment.visible_name_id));
461 data->SetString("description",
462 l10n_util::GetStringUTF16(
463 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05464
[email protected]28e35af2011-02-09 12:56:22465 switch (experiment.type) {
466 case Experiment::SINGLE_VALUE:
467 data->SetBoolean(
468 "enabled",
469 enabled_experiments.count(experiment.internal_name) > 0);
470 break;
471 case Experiment::MULTI_VALUE:
472 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
473 break;
474 default:
475 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19476 }
477
[email protected]ad2a3ded2010-08-27 13:19:05478 experiments_data->Append(data);
479 }
480 return experiments_data;
481}
482
[email protected]ad2a3ded2010-08-27 13:19:05483bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55484 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05485}
486
487void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35488 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55489 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07490}
491
492void RemoveFlagsSwitches(
493 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55494 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07495}
496
[email protected]a314ee5a2010-10-26 21:23:28497int GetCurrentPlatform() {
498#if defined(OS_MACOSX)
499 return kOsMac;
500#elif defined(OS_WIN)
501 return kOsWin;
502#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
503 return kOsCrOS;
504#elif defined(OS_LINUX)
505 return kOsLinux;
506#else
507#error Unknown platform
508#endif
509}
510
[email protected]4bc5050c2010-11-18 17:55:54511void RecordUMAStatistics(const PrefService* prefs) {
512 std::set<std::string> flags;
513 GetEnabledFlags(prefs, &flags);
514 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
515 ++it) {
516 std::string action("AboutFlags_");
517 action += *it;
518 UserMetrics::RecordComputedAction(action);
519 }
520 // Since flag metrics are recorded every startup, add a tick so that the
521 // stats can be made meaningful.
522 if (flags.size())
523 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
[email protected]970b2fd2011-01-22 18:06:45524 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54525}
526
[email protected]e2ddbc92010-10-15 20:02:07527//////////////////////////////////////////////////////////////////////////////
528// FlagsState implementation.
529
530namespace {
531
532void FlagsState::ConvertFlagsToSwitches(
533 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07534 if (command_line->HasSwitch(switches::kNoExperiments))
535 return;
536
537 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00538
[email protected]a314ee5a2010-10-26 21:23:28539 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07540
[email protected]a82744532011-02-11 16:15:53541 typedef std::map<std::string, std::pair<std::string, std::string> >
542 NameToSwitchAndValueMap;
543 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19544 for (size_t i = 0; i < num_experiments; ++i) {
545 const Experiment& e = experiments[i];
546 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53547 name_to_switch_map[e.internal_name] =
548 std::pair<std::string, std::string>(e.command_line_switch,
549 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19550 } else {
551 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53552 name_to_switch_map[NameForChoice(e, j)] =
553 std::pair<std::string, std::string>(
554 e.choices[j].command_line_switch,
555 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19556 }
557 }
[email protected]e2ddbc92010-10-15 20:02:07558
559 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53560 flags_switches_.insert(
561 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
562 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07563 for (std::set<std::string>::iterator it = enabled_experiments.begin();
564 it != enabled_experiments.end();
565 ++it) {
566 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53567 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19568 name_to_switch_map.find(experiment_name);
569 if (name_to_switch_it == name_to_switch_map.end()) {
570 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07571 continue;
[email protected]8a6ff28d2010-12-02 16:35:19572 }
[email protected]e2ddbc92010-10-15 20:02:07573
[email protected]a82744532011-02-11 16:15:53574 const std::pair<std::string, std::string>&
575 switch_and_value_pair = name_to_switch_it->second;
576
577 command_line->AppendSwitchASCII(switch_and_value_pair.first,
578 switch_and_value_pair.second);
579 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07580 }
581 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53582 flags_switches_.insert(
583 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
584 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07585}
586
587bool FlagsState::IsRestartNeededToCommitChanges() {
588 return needs_restart_;
589}
590
591void FlagsState::SetExperimentEnabled(
592 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05593 needs_restart_ = true;
594
[email protected]8a6ff28d2010-12-02 16:35:19595 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
596 if (at_index != std::string::npos) {
597 DCHECK(enable);
598 // We're being asked to enable a multi-choice experiment. Disable the
599 // currently selected choice.
600 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22601 const std::string experiment_name = internal_name.substr(0, at_index);
602 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19603
[email protected]28e35af2011-02-09 12:56:22604 // And enable the new choice, if it is not the default first choice.
605 if (internal_name != experiment_name + "@0") {
606 std::set<std::string> enabled_experiments;
607 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
608 enabled_experiments.insert(internal_name);
609 SetEnabledFlags(prefs, enabled_experiments);
610 }
[email protected]8a6ff28d2010-12-02 16:35:19611 return;
612 }
613
[email protected]ad2a3ded2010-08-27 13:19:05614 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24615 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05616
[email protected]8a6ff28d2010-12-02 16:35:19617 const Experiment* e = NULL;
618 for (size_t i = 0; i < num_experiments; ++i) {
619 if (experiments[i].internal_name == internal_name) {
620 e = experiments + i;
621 break;
622 }
623 }
624 DCHECK(e);
625
626 if (e->type == Experiment::SINGLE_VALUE) {
627 if (enable)
628 enabled_experiments.insert(internal_name);
629 else
630 enabled_experiments.erase(internal_name);
631 } else {
632 if (enable) {
633 // Enable the first choice.
634 enabled_experiments.insert(NameForChoice(*e, 0));
635 } else {
636 // Find the currently enabled choice and disable it.
637 for (int i = 0; i < e->num_choices; ++i) {
638 std::string choice_name = NameForChoice(*e, i);
639 if (enabled_experiments.find(choice_name) !=
640 enabled_experiments.end()) {
641 enabled_experiments.erase(choice_name);
642 // Continue on just in case there's a bug and more than one
643 // experiment for this choice was enabled.
644 }
645 }
646 }
647 }
[email protected]ad2a3ded2010-08-27 13:19:05648
[email protected]1a47d7e2010-10-15 00:37:24649 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05650}
651
[email protected]e2ddbc92010-10-15 20:02:07652void FlagsState::RemoveFlagsSwitches(
653 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53654 for (std::map<std::string, std::string>::const_iterator
655 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
656 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07657 }
658}
659
660void FlagsState::reset() {
661 needs_restart_ = false;
662 flags_switches_.clear();
663}
664
665} // namespace
666
667namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19668
669// WARNING: '@' is also used in the html file. If you update this constant you
670// also need to update the html file.
671const char kMultiSeparator[] = "@";
672
[email protected]e2ddbc92010-10-15 20:02:07673void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55674 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07675}
[email protected]a314ee5a2010-10-26 21:23:28676
677void SetExperiments(const Experiment* e, size_t count) {
678 if (!e) {
679 experiments = kExperiments;
680 num_experiments = arraysize(kExperiments);
681 } else {
682 experiments = e;
683 num_experiments = count;
684 }
685}
686
[email protected]8a6ff28d2010-12-02 16:35:19687const Experiment* GetExperiments(size_t* count) {
688 *count = num_experiments;
689 return experiments;
690}
691
[email protected]e2ddbc92010-10-15 20:02:07692} // namespace testing
693
[email protected]1a47d7e2010-10-15 00:37:24694} // namespace about_flags