blob: 312f84430a4dfb2cb13b1435164f2cd2bff6d55a [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]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]1bc78422011-03-31 08:41:3818#include "chrome/browser/prefs/scoped_user_pref_update.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"
[email protected]c051a1b2011-01-21 23:30:1722#include "ui/base/l10n/l10n_util.h"
[email protected]ad2a3ded2010-08-27 13:19:0523
[email protected]1a47d7e2010-10-15 00:37:2424namespace about_flags {
[email protected]ad2a3ded2010-08-27 13:19:0525
[email protected]8a6ff28d2010-12-02 16:35:1926// Macros to simplify specifying the type.
[email protected]a82744532011-02-11 16:15:5327#define SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, switch_value) \
28 Experiment::SINGLE_VALUE, command_line_switch, switch_value, NULL, 0
29#define SINGLE_VALUE_TYPE(command_line_switch) \
30 SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, "")
31#define MULTI_VALUE_TYPE(choices) \
[email protected]0e6f56d2011-02-12 23:45:1532 Experiment::MULTI_VALUE, "", "", choices, arraysize(choices)
[email protected]8a6ff28d2010-12-02 16:35:1933
[email protected]e2ddbc92010-10-15 20:02:0734namespace {
35
[email protected]a314ee5a2010-10-26 21:23:2836const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0537
[email protected]ba8164242010-11-16 21:31:0038// Names for former Chrome OS Labs experiments, shared with prefs migration
39// code.
40const char kMediaPlayerExperimentName[] = "media-player";
41const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
42const char kVerticalTabsExperimentName[] = "vertical-tabs";
43
[email protected]4bc5050c2010-11-18 17:55:5444// RECORDING USER METRICS FOR FLAGS:
45// -----------------------------------------------------------------------------
46// The first line of the experiment is the internal name. If you'd like to
47// gather statistics about the usage of your flag, you should append a marker
48// comment to the end of the feature name, like so:
49// "my-special-feature", // FLAGS:RECORD_UMA
50//
51// After doing that, run //chrome/tools/extract_actions.py (see instructions at
52// the top of that file for details) to update the chromeactions.txt file, which
53// will enable UMA to record your feature flag.
54//
55// After your feature has shipped under a flag, you can locate the metrics
56// under the action name AboutFlags_internal-action-name. Actions are recorded
57// once per startup, so you should divide this number by AboutFlags_StartupTick
58// to get a sense of usage. Note that this will not be the same as number of
59// users with a given feature enabled because users can quit and relaunch
60// the application multiple times over a given time interval.
61// TODO(rsesek): See if there's a way to count per-user, rather than
62// per-startup.
63
[email protected]8a6ff28d2010-12-02 16:35:1964// To add a new experiment add to the end of kExperiments. There are two
65// distinct types of experiments:
66// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
67// macro for this type supplying the command line to the macro.
[email protected]28e35af2011-02-09 12:56:2268// . MULTI_VALUE: a list of choices, the first of which should correspond to a
69// deactivated state for this lab (i.e. no command line option). To specify
70// this type of experiment use the macro MULTI_VALUE_TYPE supplying it the
71// array of choices.
[email protected]8a6ff28d2010-12-02 16:35:1972// See the documentation of Experiment for details on the fields.
73//
74// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0575const Experiment kExperiments[] = {
76 {
[email protected]aac169d2011-03-18 19:53:0377 "expose-for-tabs", // FLAGS:RECORD_UMA
78 IDS_FLAGS_TABPOSE_NAME,
79 IDS_FLAGS_TABPOSE_DESCRIPTION,
80 kOsMac,
81#if defined(OS_MACOSX)
82 // The switch exists only on OS X.
83 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
84#else
85 SINGLE_VALUE_TYPE("")
86#endif
87 },
88 {
[email protected]ba8164242010-11-16 21:31:0089 kMediaPlayerExperimentName,
90 IDS_FLAGS_MEDIA_PLAYER_NAME,
91 IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION,
92 kOsCrOS,
93#if defined(OS_CHROMEOS)
94 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:1995 SINGLE_VALUE_TYPE(switches::kEnableMediaPlayer)
[email protected]ba8164242010-11-16 21:31:0096#else
[email protected]8a6ff28d2010-12-02 16:35:1997 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:0098#endif
99 },
100 {
101 kAdvancedFileSystemExperimentName,
102 IDS_FLAGS_ADVANCED_FS_NAME,
103 IDS_FLAGS_ADVANCED_FS_DESCRIPTION,
104 kOsCrOS,
105#if defined(OS_CHROMEOS)
106 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19107 SINGLE_VALUE_TYPE(switches::kEnableAdvancedFileSystem)
[email protected]ba8164242010-11-16 21:31:00108#else
[email protected]8a6ff28d2010-12-02 16:35:19109 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00110#endif
111 },
112 {
[email protected]4bc5050c2010-11-18 17:55:54113 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12114 IDS_FLAGS_SIDE_TABS_NAME,
115 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:00116 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19117 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:05118 },
119 {
[email protected]4bc5050c2010-11-18 17:55:54120 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12121 IDS_FLAGS_REMOTING_NAME,
[email protected]d99599862011-01-20 22:43:59122 IDS_FLAGS_REMOTING_DESCRIPTION,
123 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19124 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56125 },
[email protected]a6f03652010-09-28 15:59:07126 {
[email protected]4bc5050c2010-11-18 17:55:54127 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05128 IDS_FLAGS_CONFLICTS_CHECK_NAME,
129 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
130 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19131 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05132 },
133 {
[email protected]4bc5050c2010-11-18 17:55:54134 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12135 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
136 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19137#if defined(GOOGLE_CHROME_BUILD)
[email protected]9f8872b32011-03-04 19:44:45138 // For a Chrome build, we know we have a PDF plug-in on Windows, so it's
[email protected]be51dd252011-03-05 00:23:44139 // fully enabled. Linux still need some final polish.
140 kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19141#else
[email protected]be51dd252011-03-05 00:23:44142 // Otherwise, where we know Windows could be working if a viable PDF
143 // plug-in could be supplied, we'll keep the lab enabled. Mac always has
144 // PDF rasterization available, so no flag needed there.
145 kOsWin | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19146#endif
[email protected]8a6ff28d2010-12-02 16:35:19147 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42148 },
[email protected]580939a2010-10-12 18:54:37149 {
[email protected]bb461532010-11-26 21:50:23150 "crxless-web-apps",
151 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
152 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
153 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19154 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23155 },
156 {
[email protected]5963b772011-02-09 22:55:38157 "composited-layer-borders",
158 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
159 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
160 kOsAll,
161 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
162 },
163 {
[email protected]a8f1eaa2011-03-07 19:00:58164 "show-fps-counter",
165 IDS_FLAGS_SHOW_FPS_COUNTER,
166 IDS_FLAGS_SHOW_FPS_COUNTER_DESCRIPTION,
167 kOsAll,
168 SINGLE_VALUE_TYPE(switches::kShowFPSCounter)
169 },
170 {
[email protected]4bc5050c2010-11-18 17:55:54171 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12172 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
173 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14174 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19175 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05176 },
[email protected]11c4c812010-10-22 19:50:12177 // FIXME(scheib): Add Flags entry for WebGL,
178 // or pull it and the strings in generated_resources.grd by Dec 2010
179 // {
[email protected]4bc5050c2010-11-18 17:55:54180 // "webgl",
[email protected]11c4c812010-10-22 19:50:12181 // IDS_FLAGS_WEBGL_NAME,
182 // IDS_FLAGS_WEBGL_DESCRIPTION,
183 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19184 // SINGLE_VALUE_TYPE(switches::kDisableExperimentalWebGL)
[email protected]11c4c812010-10-22 19:50:12185 // }
[email protected]8d260e52010-10-13 01:03:05186 {
[email protected]4bc5050c2010-11-18 17:55:54187 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12188 IDS_FLAGS_PRINT_PREVIEW_NAME,
189 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]411c37f2011-03-25 22:55:34190 kOsMac | kOsWin | kOsLinux, // This switch is not available in CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19191 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53192 },
193 {
[email protected]4bc5050c2010-11-18 17:55:54194 "enable-nacl", // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40195 IDS_FLAGS_ENABLE_NACL_NAME,
196 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
197 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19198 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40199 },
200 {
[email protected]4bc5050c2010-11-18 17:55:54201 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53202 IDS_FLAGS_DNS_SERVER_NAME,
203 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
204 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19205 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53206 },
[email protected]177aceb2010-11-03 16:17:41207 {
[email protected]4bc5050c2010-11-18 17:55:54208 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32209 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
210 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
211 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19212 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32213 },
[email protected]3627b06d2010-11-12 16:36:16214 {
[email protected]4bc5050c2010-11-18 17:55:54215 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16216 IDS_FLAGS_CLICK_TO_PLAY_NAME,
217 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
218 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19219 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16220 },
[email protected]80bd24e2010-11-30 09:34:38221 {
222 "disable-hyperlink-auditing",
223 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
224 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
225 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19226 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51227 },
228 {
229 "experimental-location-features", // FLAGS:RECORD_UMA
230 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
231 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
232 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19233 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
234 },
235 {
[email protected]4c6452d2011-01-12 08:47:57236 "block-reading-third-party-cookies",
237 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
238 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
239 kOsAll,
240 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
241 },
[email protected]04227962011-01-20 02:03:09242 {
243 "disable-interactive-form-validation",
244 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
245 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
246 kOsAll,
247 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
248 },
[email protected]8df51192011-01-22 20:05:03249 {
250 "webaudio",
251 IDS_FLAGS_WEBAUDIO_NAME,
252 IDS_FLAGS_WEBAUDIO_DESCRIPTION,
253 kOsMac, // TODO(crogers): add windows and linux when FFT is ready.
254 SINGLE_VALUE_TYPE(switches::kEnableWebAudio)
255 },
[email protected]3828d6f2011-02-24 18:32:21256 {
257 "enable-history-quick-provider",
258 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER,
259 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER_DESCRIPTION,
260 kOsAll,
261 SINGLE_VALUE_TYPE(switches::kEnableHistoryQuickProvider)
262 },
[email protected]6c54e7e42011-03-02 20:52:34263 {
264 "p2papi",
265 IDS_FLAGS_P2P_API_NAME,
266 IDS_FLAGS_P2P_API_DESCRIPTION,
267 kOsAll,
268 SINGLE_VALUE_TYPE(switches::kEnableP2PApi)
269 },
[email protected]07d490bc2011-03-07 17:05:26270 {
271 "focus-existing-tab-on-open", // FLAGS:RECORD_UMA
272 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_NAME,
273 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_DESCRIPTION,
274 kOsAll,
275 SINGLE_VALUE_TYPE(switches::kFocusExistingTabOnOpen)
276 },
[email protected]d5dcfb32011-03-19 00:49:24277 {
278 "new-tab-page-4",
279 IDS_FLAGS_NEW_TAB_PAGE_4_NAME,
280 IDS_FLAGS_NEW_TAB_PAGE_4_DESCRIPTION,
281 kOsAll,
282 SINGLE_VALUE_TYPE(switches::kNewTabPage4)
283 },
[email protected]5aea1862011-03-23 23:55:39284 {
285 "tab-groups-context-menu",
286 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_NAME,
287 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_DESCRIPTION,
288 kOsWin,
289 SINGLE_VALUE_TYPE(switches::kEnableTabGroupsContextMenu)
290 },
[email protected]e9bf9d92011-03-31 20:57:15291 {
292 "ppapi-flash-in-process",
293 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_NAME,
294 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_DESCRIPTION,
[email protected]f2d3ce0d2011-04-01 18:19:30295 kOsAll,
[email protected]e9bf9d92011-03-31 20:57:15296 SINGLE_VALUE_TYPE(switches::kPpapiFlashInProcess)
297 },
[email protected]ad2a3ded2010-08-27 13:19:05298};
299
[email protected]a314ee5a2010-10-26 21:23:28300const Experiment* experiments = kExperiments;
301size_t num_experiments = arraysize(kExperiments);
302
[email protected]e2ddbc92010-10-15 20:02:07303// Stores and encapsulates the little state that about:flags has.
304class FlagsState {
305 public:
306 FlagsState() : needs_restart_(false) {}
307 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
308 bool IsRestartNeededToCommitChanges();
309 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28310 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07311 void RemoveFlagsSwitches(
312 std::map<std::string, CommandLine::StringType>* switch_list);
313 void reset();
314
315 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55316 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07317 return Singleton<FlagsState>::get();
318 }
319
320 private:
321 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53322 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07323
324 DISALLOW_COPY_AND_ASSIGN(FlagsState);
325};
326
[email protected]c7b7800a2010-10-07 18:51:35327// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05328// in a set.
[email protected]1a47d7e2010-10-15 00:37:24329void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05330 const ListValue* enabled_experiments = prefs->GetList(
331 prefs::kEnabledLabsExperiments);
332 if (!enabled_experiments)
333 return;
334
335 for (ListValue::const_iterator it = enabled_experiments->begin();
336 it != enabled_experiments->end();
337 ++it) {
338 std::string experiment_name;
339 if (!(*it)->GetAsString(&experiment_name)) {
340 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
341 continue;
342 }
343 result->insert(experiment_name);
344 }
345}
346
347// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24348void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05349 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
[email protected]1bc78422011-03-31 08:41:38350 ListPrefUpdate update(prefs, prefs::kEnabledLabsExperiments);
351 ListValue* experiments_list = update.Get();
[email protected]ad2a3ded2010-08-27 13:19:05352
353 experiments_list->Clear();
354 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
355 it != enabled_experiments.end();
356 ++it) {
357 experiments_list->Append(new StringValue(*it));
358 }
359}
360
[email protected]8a6ff28d2010-12-02 16:35:19361// Returns the name used in prefs for the choice at the specified index.
362std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34363 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
364 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19365 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
366 base::IntToString(index);
367}
368
369// Adds the internal names for the specified experiment to |names|.
370void AddInternalName(const Experiment& e, std::set<std::string>* names) {
371 if (e.type == Experiment::SINGLE_VALUE) {
372 names->insert(e.internal_name);
373 } else {
[email protected]2ce9c89752011-02-25 18:24:34374 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19375 for (int i = 0; i < e.num_choices; ++i)
376 names->insert(NameForChoice(e, i));
377 }
378}
379
[email protected]28e35af2011-02-09 12:56:22380// Confirms that an experiment is valid, used in a DCHECK in
381// SanitizeList below.
382bool ValidateExperiment(const Experiment& e) {
383 switch (e.type) {
384 case Experiment::SINGLE_VALUE:
385 DCHECK_EQ(0, e.num_choices);
386 DCHECK(!e.choices);
387 break;
388 case Experiment::MULTI_VALUE:
389 DCHECK_GT(e.num_choices, 0);
390 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53391 DCHECK(e.choices[0].command_line_switch);
392 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22393 break;
394 default:
395 NOTREACHED();
396 }
397 return true;
398}
399
[email protected]ad2a3ded2010-08-27 13:19:05400// Removes all experiments from prefs::kEnabledLabsExperiments that are
401// unknown, to prevent this list to become very long as experiments are added
402// and removed.
403void SanitizeList(PrefService* prefs) {
404 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22405 for (size_t i = 0; i < num_experiments; ++i) {
406 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19407 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22408 }
[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.
[email protected]8a6ff28d2010-12-02 16:35:19454Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22455 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34456 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19457 ListValue* result = new ListValue;
458 for (int i = 0; i < experiment.num_choices; ++i) {
459 const Experiment::Choice& choice = experiment.choices[i];
460 DictionaryValue* value = new DictionaryValue;
461 std::string name = NameForChoice(experiment, i);
462 value->SetString("description",
463 l10n_util::GetStringUTF16(choice.description_id));
464 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22465 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19466 result->Append(value);
467 }
468 return result;
469}
470
[email protected]e2ddbc92010-10-15 20:02:07471} // namespace
472
[email protected]1a47d7e2010-10-15 00:37:24473void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55474 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05475}
476
[email protected]1a47d7e2010-10-15 00:37:24477ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05478 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24479 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05480
481 int current_platform = GetCurrentPlatform();
482
483 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28484 for (size_t i = 0; i < num_experiments; ++i) {
485 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05486 if (!(experiment.supported_platforms & current_platform))
487 continue;
488
489 DictionaryValue* data = new DictionaryValue();
490 data->SetString("internal_name", experiment.internal_name);
491 data->SetString("name",
492 l10n_util::GetStringUTF16(experiment.visible_name_id));
493 data->SetString("description",
494 l10n_util::GetStringUTF16(
495 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05496
[email protected]28e35af2011-02-09 12:56:22497 switch (experiment.type) {
498 case Experiment::SINGLE_VALUE:
499 data->SetBoolean(
500 "enabled",
501 enabled_experiments.count(experiment.internal_name) > 0);
502 break;
503 case Experiment::MULTI_VALUE:
504 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
505 break;
506 default:
507 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19508 }
509
[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]8e8bb6d2010-12-13 08:18:55516 return FlagsState::GetInstance()->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]8e8bb6d2010-12-13 08:18:55521 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07522}
523
524void RemoveFlagsSwitches(
525 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55526 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07527}
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"));
[email protected]970b2fd2011-01-22 18:06:45556 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54557}
558
[email protected]e2ddbc92010-10-15 20:02:07559//////////////////////////////////////////////////////////////////////////////
560// FlagsState implementation.
561
562namespace {
563
564void FlagsState::ConvertFlagsToSwitches(
565 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07566 if (command_line->HasSwitch(switches::kNoExperiments))
567 return;
568
569 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00570
[email protected]a314ee5a2010-10-26 21:23:28571 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07572
[email protected]a82744532011-02-11 16:15:53573 typedef std::map<std::string, std::pair<std::string, std::string> >
574 NameToSwitchAndValueMap;
575 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19576 for (size_t i = 0; i < num_experiments; ++i) {
577 const Experiment& e = experiments[i];
578 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53579 name_to_switch_map[e.internal_name] =
580 std::pair<std::string, std::string>(e.command_line_switch,
581 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19582 } else {
583 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53584 name_to_switch_map[NameForChoice(e, j)] =
585 std::pair<std::string, std::string>(
586 e.choices[j].command_line_switch,
587 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19588 }
589 }
[email protected]e2ddbc92010-10-15 20:02:07590
591 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53592 flags_switches_.insert(
593 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
594 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07595 for (std::set<std::string>::iterator it = enabled_experiments.begin();
596 it != enabled_experiments.end();
597 ++it) {
598 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53599 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19600 name_to_switch_map.find(experiment_name);
601 if (name_to_switch_it == name_to_switch_map.end()) {
602 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07603 continue;
[email protected]8a6ff28d2010-12-02 16:35:19604 }
[email protected]e2ddbc92010-10-15 20:02:07605
[email protected]a82744532011-02-11 16:15:53606 const std::pair<std::string, std::string>&
607 switch_and_value_pair = name_to_switch_it->second;
608
609 command_line->AppendSwitchASCII(switch_and_value_pair.first,
610 switch_and_value_pair.second);
611 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07612 }
613 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53614 flags_switches_.insert(
615 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
616 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07617}
618
619bool FlagsState::IsRestartNeededToCommitChanges() {
620 return needs_restart_;
621}
622
623void FlagsState::SetExperimentEnabled(
624 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05625 needs_restart_ = true;
626
[email protected]8a6ff28d2010-12-02 16:35:19627 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
628 if (at_index != std::string::npos) {
629 DCHECK(enable);
630 // We're being asked to enable a multi-choice experiment. Disable the
631 // currently selected choice.
632 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22633 const std::string experiment_name = internal_name.substr(0, at_index);
634 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19635
[email protected]28e35af2011-02-09 12:56:22636 // And enable the new choice, if it is not the default first choice.
637 if (internal_name != experiment_name + "@0") {
638 std::set<std::string> enabled_experiments;
639 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
640 enabled_experiments.insert(internal_name);
641 SetEnabledFlags(prefs, enabled_experiments);
642 }
[email protected]8a6ff28d2010-12-02 16:35:19643 return;
644 }
645
[email protected]ad2a3ded2010-08-27 13:19:05646 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24647 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05648
[email protected]8a6ff28d2010-12-02 16:35:19649 const Experiment* e = NULL;
650 for (size_t i = 0; i < num_experiments; ++i) {
651 if (experiments[i].internal_name == internal_name) {
652 e = experiments + i;
653 break;
654 }
655 }
656 DCHECK(e);
657
658 if (e->type == Experiment::SINGLE_VALUE) {
659 if (enable)
660 enabled_experiments.insert(internal_name);
661 else
662 enabled_experiments.erase(internal_name);
663 } else {
664 if (enable) {
665 // Enable the first choice.
666 enabled_experiments.insert(NameForChoice(*e, 0));
667 } else {
668 // Find the currently enabled choice and disable it.
669 for (int i = 0; i < e->num_choices; ++i) {
670 std::string choice_name = NameForChoice(*e, i);
671 if (enabled_experiments.find(choice_name) !=
672 enabled_experiments.end()) {
673 enabled_experiments.erase(choice_name);
674 // Continue on just in case there's a bug and more than one
675 // experiment for this choice was enabled.
676 }
677 }
678 }
679 }
[email protected]ad2a3ded2010-08-27 13:19:05680
[email protected]1a47d7e2010-10-15 00:37:24681 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05682}
683
[email protected]e2ddbc92010-10-15 20:02:07684void FlagsState::RemoveFlagsSwitches(
685 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53686 for (std::map<std::string, std::string>::const_iterator
687 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
688 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07689 }
690}
691
692void FlagsState::reset() {
693 needs_restart_ = false;
694 flags_switches_.clear();
695}
696
697} // namespace
698
699namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19700
701// WARNING: '@' is also used in the html file. If you update this constant you
702// also need to update the html file.
703const char kMultiSeparator[] = "@";
704
[email protected]e2ddbc92010-10-15 20:02:07705void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55706 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07707}
[email protected]a314ee5a2010-10-26 21:23:28708
709void SetExperiments(const Experiment* e, size_t count) {
710 if (!e) {
711 experiments = kExperiments;
712 num_experiments = arraysize(kExperiments);
713 } else {
714 experiments = e;
715 num_experiments = count;
716 }
717}
718
[email protected]8a6ff28d2010-12-02 16:35:19719const Experiment* GetExperiments(size_t* count) {
720 *count = num_experiments;
721 return experiments;
722}
723
[email protected]e2ddbc92010-10-15 20:02:07724} // namespace testing
725
[email protected]1a47d7e2010-10-15 00:37:24726} // namespace about_flags