blob: 4d59c63573fa2578d2caa105c6c7ca16a50127c1 [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
[email protected]be51dd252011-03-05 00:23:44134 // fully enabled. Linux still need some final polish.
135 kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19136#else
[email protected]be51dd252011-03-05 00:23:44137 // Otherwise, where we know Windows could be working if a viable PDF
138 // plug-in could be supplied, we'll keep the lab enabled. Mac always has
139 // PDF rasterization available, so no flag needed there.
140 kOsWin | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19141#endif
[email protected]8a6ff28d2010-12-02 16:35:19142 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42143 },
[email protected]580939a2010-10-12 18:54:37144 {
[email protected]bb461532010-11-26 21:50:23145 "crxless-web-apps",
146 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
147 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
148 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19149 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23150 },
151 {
[email protected]5963b772011-02-09 22:55:38152 "composited-layer-borders",
153 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
154 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
155 kOsAll,
156 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
157 },
158 {
[email protected]4bc5050c2010-11-18 17:55:54159 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12160 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
161 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14162 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19163 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05164 },
[email protected]11c4c812010-10-22 19:50:12165 // FIXME(scheib): Add Flags entry for WebGL,
166 // or pull it and the strings in generated_resources.grd by Dec 2010
167 // {
[email protected]4bc5050c2010-11-18 17:55:54168 // "webgl",
[email protected]11c4c812010-10-22 19:50:12169 // IDS_FLAGS_WEBGL_NAME,
170 // IDS_FLAGS_WEBGL_DESCRIPTION,
171 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19172 // SINGLE_VALUE_TYPE(switches::kDisableExperimentalWebGL)
[email protected]11c4c812010-10-22 19:50:12173 // }
[email protected]8d260e52010-10-13 01:03:05174 {
[email protected]4bc5050c2010-11-18 17:55:54175 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12176 IDS_FLAGS_PRINT_PREVIEW_NAME,
177 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]8d260e52010-10-13 01:03:05178 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19179 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53180 },
181 {
[email protected]4bc5050c2010-11-18 17:55:54182 "enable-nacl", // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40183 IDS_FLAGS_ENABLE_NACL_NAME,
184 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
185 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19186 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40187 },
188 {
[email protected]4bc5050c2010-11-18 17:55:54189 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53190 IDS_FLAGS_DNS_SERVER_NAME,
191 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
192 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19193 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53194 },
[email protected]177aceb2010-11-03 16:17:41195 {
[email protected]4bc5050c2010-11-18 17:55:54196 "page-prerender", // FLAGS:RECORD_UMA
[email protected]83d00d92010-11-04 19:20:21197 IDS_FLAGS_PAGE_PRERENDER_NAME,
198 IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION,
199 kOsAll,
[email protected]0e6f56d2011-02-12 23:45:15200 MULTI_VALUE_TYPE(kPagePrerenderChoices)
[email protected]83d00d92010-11-04 19:20:21201 },
202 {
[email protected]4bc5050c2010-11-18 17:55:54203 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32204 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
205 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
206 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19207 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32208 },
[email protected]3627b06d2010-11-12 16:36:16209 {
[email protected]4bc5050c2010-11-18 17:55:54210 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16211 IDS_FLAGS_CLICK_TO_PLAY_NAME,
212 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
213 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19214 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16215 },
[email protected]80bd24e2010-11-30 09:34:38216 {
217 "disable-hyperlink-auditing",
218 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
219 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
220 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19221 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51222 },
223 {
224 "experimental-location-features", // FLAGS:RECORD_UMA
225 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
226 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
227 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19228 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
229 },
230 {
[email protected]4c6452d2011-01-12 08:47:57231 "block-reading-third-party-cookies",
232 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
233 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
234 kOsAll,
235 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
236 },
[email protected]04227962011-01-20 02:03:09237 {
238 "disable-interactive-form-validation",
239 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
240 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
241 kOsAll,
242 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
243 },
[email protected]8df51192011-01-22 20:05:03244 {
245 "webaudio",
246 IDS_FLAGS_WEBAUDIO_NAME,
247 IDS_FLAGS_WEBAUDIO_DESCRIPTION,
248 kOsMac, // TODO(crogers): add windows and linux when FFT is ready.
249 SINGLE_VALUE_TYPE(switches::kEnableWebAudio)
250 },
[email protected]3828d6f2011-02-24 18:32:21251 {
252 "enable-history-quick-provider",
253 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER,
254 IDS_FLAGS_ENABLE_HISTORY_QUICK_PROVIDER_DESCRIPTION,
255 kOsAll,
256 SINGLE_VALUE_TYPE(switches::kEnableHistoryQuickProvider)
257 },
[email protected]6c54e7e42011-03-02 20:52:34258 {
259 "p2papi",
260 IDS_FLAGS_P2P_API_NAME,
261 IDS_FLAGS_P2P_API_DESCRIPTION,
262 kOsAll,
263 SINGLE_VALUE_TYPE(switches::kEnableP2PApi)
264 },
[email protected]07d490bc2011-03-07 17:05:26265 {
266 "focus-existing-tab-on-open", // FLAGS:RECORD_UMA
267 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_NAME,
268 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_DESCRIPTION,
269 kOsAll,
270 SINGLE_VALUE_TYPE(switches::kFocusExistingTabOnOpen)
271 },
[email protected]ad2a3ded2010-08-27 13:19:05272};
273
[email protected]a314ee5a2010-10-26 21:23:28274const Experiment* experiments = kExperiments;
275size_t num_experiments = arraysize(kExperiments);
276
[email protected]e2ddbc92010-10-15 20:02:07277// Stores and encapsulates the little state that about:flags has.
278class FlagsState {
279 public:
280 FlagsState() : needs_restart_(false) {}
281 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
282 bool IsRestartNeededToCommitChanges();
283 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28284 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07285 void RemoveFlagsSwitches(
286 std::map<std::string, CommandLine::StringType>* switch_list);
287 void reset();
288
289 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55290 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07291 return Singleton<FlagsState>::get();
292 }
293
294 private:
295 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53296 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07297
298 DISALLOW_COPY_AND_ASSIGN(FlagsState);
299};
300
[email protected]c7b7800a2010-10-07 18:51:35301// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05302// in a set.
[email protected]1a47d7e2010-10-15 00:37:24303void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05304 const ListValue* enabled_experiments = prefs->GetList(
305 prefs::kEnabledLabsExperiments);
306 if (!enabled_experiments)
307 return;
308
309 for (ListValue::const_iterator it = enabled_experiments->begin();
310 it != enabled_experiments->end();
311 ++it) {
312 std::string experiment_name;
313 if (!(*it)->GetAsString(&experiment_name)) {
314 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
315 continue;
316 }
317 result->insert(experiment_name);
318 }
319}
320
321// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24322void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05323 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
324 ListValue* experiments_list = prefs->GetMutableList(
325 prefs::kEnabledLabsExperiments);
326 if (!experiments_list)
327 return;
328
329 experiments_list->Clear();
330 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
331 it != enabled_experiments.end();
332 ++it) {
333 experiments_list->Append(new StringValue(*it));
334 }
335}
336
[email protected]8a6ff28d2010-12-02 16:35:19337// Returns the name used in prefs for the choice at the specified index.
338std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34339 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
340 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19341 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
342 base::IntToString(index);
343}
344
345// Adds the internal names for the specified experiment to |names|.
346void AddInternalName(const Experiment& e, std::set<std::string>* names) {
347 if (e.type == Experiment::SINGLE_VALUE) {
348 names->insert(e.internal_name);
349 } else {
[email protected]2ce9c89752011-02-25 18:24:34350 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19351 for (int i = 0; i < e.num_choices; ++i)
352 names->insert(NameForChoice(e, i));
353 }
354}
355
[email protected]28e35af2011-02-09 12:56:22356// Confirms that an experiment is valid, used in a DCHECK in
357// SanitizeList below.
358bool ValidateExperiment(const Experiment& e) {
359 switch (e.type) {
360 case Experiment::SINGLE_VALUE:
361 DCHECK_EQ(0, e.num_choices);
362 DCHECK(!e.choices);
363 break;
364 case Experiment::MULTI_VALUE:
365 DCHECK_GT(e.num_choices, 0);
366 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53367 DCHECK(e.choices[0].command_line_switch);
368 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22369 break;
370 default:
371 NOTREACHED();
372 }
373 return true;
374}
375
[email protected]ad2a3ded2010-08-27 13:19:05376// Removes all experiments from prefs::kEnabledLabsExperiments that are
377// unknown, to prevent this list to become very long as experiments are added
378// and removed.
379void SanitizeList(PrefService* prefs) {
380 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22381 for (size_t i = 0; i < num_experiments; ++i) {
382 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19383 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22384 }
[email protected]ad2a3ded2010-08-27 13:19:05385
386 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24387 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05388
389 std::set<std::string> new_enabled_experiments;
390 std::set_intersection(
391 known_experiments.begin(), known_experiments.end(),
392 enabled_experiments.begin(), enabled_experiments.end(),
393 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
394
[email protected]1a47d7e2010-10-15 00:37:24395 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05396}
397
[email protected]1a47d7e2010-10-15 00:37:24398void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05399 PrefService* prefs, std::set<std::string>* result) {
400 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24401 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05402}
403
[email protected]a314ee5a2010-10-26 21:23:28404// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
405// enabled on the current platform.
406void GetSanitizedEnabledFlagsForCurrentPlatform(
407 PrefService* prefs, std::set<std::string>* result) {
408 GetSanitizedEnabledFlags(prefs, result);
409
410 // Filter out any experiments that aren't enabled on the current platform. We
411 // don't remove these from prefs else syncing to a platform with a different
412 // set of experiments would be lossy.
413 std::set<std::string> platform_experiments;
414 int current_platform = GetCurrentPlatform();
415 for (size_t i = 0; i < num_experiments; ++i) {
416 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19417 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28418 }
419
420 std::set<std::string> new_enabled_experiments;
421 std::set_intersection(
422 platform_experiments.begin(), platform_experiments.end(),
423 result->begin(), result->end(),
424 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
425
426 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05427}
428
[email protected]8a6ff28d2010-12-02 16:35:19429// Returns the Value representing the choice data in the specified experiment.
[email protected]8a6ff28d2010-12-02 16:35:19430Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22431 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34432 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19433 ListValue* result = new ListValue;
434 for (int i = 0; i < experiment.num_choices; ++i) {
435 const Experiment::Choice& choice = experiment.choices[i];
436 DictionaryValue* value = new DictionaryValue;
437 std::string name = NameForChoice(experiment, i);
438 value->SetString("description",
439 l10n_util::GetStringUTF16(choice.description_id));
440 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22441 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19442 result->Append(value);
443 }
444 return result;
445}
446
[email protected]e2ddbc92010-10-15 20:02:07447} // namespace
448
[email protected]1a47d7e2010-10-15 00:37:24449void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55450 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05451}
452
[email protected]1a47d7e2010-10-15 00:37:24453ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05454 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24455 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05456
457 int current_platform = GetCurrentPlatform();
458
459 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28460 for (size_t i = 0; i < num_experiments; ++i) {
461 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05462 if (!(experiment.supported_platforms & current_platform))
463 continue;
464
465 DictionaryValue* data = new DictionaryValue();
466 data->SetString("internal_name", experiment.internal_name);
467 data->SetString("name",
468 l10n_util::GetStringUTF16(experiment.visible_name_id));
469 data->SetString("description",
470 l10n_util::GetStringUTF16(
471 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05472
[email protected]28e35af2011-02-09 12:56:22473 switch (experiment.type) {
474 case Experiment::SINGLE_VALUE:
475 data->SetBoolean(
476 "enabled",
477 enabled_experiments.count(experiment.internal_name) > 0);
478 break;
479 case Experiment::MULTI_VALUE:
480 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
481 break;
482 default:
483 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19484 }
485
[email protected]ad2a3ded2010-08-27 13:19:05486 experiments_data->Append(data);
487 }
488 return experiments_data;
489}
490
[email protected]ad2a3ded2010-08-27 13:19:05491bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55492 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05493}
494
495void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35496 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55497 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07498}
499
500void RemoveFlagsSwitches(
501 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55502 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07503}
504
[email protected]a314ee5a2010-10-26 21:23:28505int GetCurrentPlatform() {
506#if defined(OS_MACOSX)
507 return kOsMac;
508#elif defined(OS_WIN)
509 return kOsWin;
510#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
511 return kOsCrOS;
512#elif defined(OS_LINUX)
513 return kOsLinux;
514#else
515#error Unknown platform
516#endif
517}
518
[email protected]4bc5050c2010-11-18 17:55:54519void RecordUMAStatistics(const PrefService* prefs) {
520 std::set<std::string> flags;
521 GetEnabledFlags(prefs, &flags);
522 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
523 ++it) {
524 std::string action("AboutFlags_");
525 action += *it;
526 UserMetrics::RecordComputedAction(action);
527 }
528 // Since flag metrics are recorded every startup, add a tick so that the
529 // stats can be made meaningful.
530 if (flags.size())
531 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
[email protected]970b2fd2011-01-22 18:06:45532 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54533}
534
[email protected]e2ddbc92010-10-15 20:02:07535//////////////////////////////////////////////////////////////////////////////
536// FlagsState implementation.
537
538namespace {
539
540void FlagsState::ConvertFlagsToSwitches(
541 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07542 if (command_line->HasSwitch(switches::kNoExperiments))
543 return;
544
545 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00546
[email protected]a314ee5a2010-10-26 21:23:28547 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07548
[email protected]a82744532011-02-11 16:15:53549 typedef std::map<std::string, std::pair<std::string, std::string> >
550 NameToSwitchAndValueMap;
551 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19552 for (size_t i = 0; i < num_experiments; ++i) {
553 const Experiment& e = experiments[i];
554 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53555 name_to_switch_map[e.internal_name] =
556 std::pair<std::string, std::string>(e.command_line_switch,
557 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19558 } else {
559 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53560 name_to_switch_map[NameForChoice(e, j)] =
561 std::pair<std::string, std::string>(
562 e.choices[j].command_line_switch,
563 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19564 }
565 }
[email protected]e2ddbc92010-10-15 20:02:07566
567 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53568 flags_switches_.insert(
569 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
570 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07571 for (std::set<std::string>::iterator it = enabled_experiments.begin();
572 it != enabled_experiments.end();
573 ++it) {
574 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53575 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19576 name_to_switch_map.find(experiment_name);
577 if (name_to_switch_it == name_to_switch_map.end()) {
578 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07579 continue;
[email protected]8a6ff28d2010-12-02 16:35:19580 }
[email protected]e2ddbc92010-10-15 20:02:07581
[email protected]a82744532011-02-11 16:15:53582 const std::pair<std::string, std::string>&
583 switch_and_value_pair = name_to_switch_it->second;
584
585 command_line->AppendSwitchASCII(switch_and_value_pair.first,
586 switch_and_value_pair.second);
587 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07588 }
589 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53590 flags_switches_.insert(
591 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
592 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07593}
594
595bool FlagsState::IsRestartNeededToCommitChanges() {
596 return needs_restart_;
597}
598
599void FlagsState::SetExperimentEnabled(
600 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05601 needs_restart_ = true;
602
[email protected]8a6ff28d2010-12-02 16:35:19603 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
604 if (at_index != std::string::npos) {
605 DCHECK(enable);
606 // We're being asked to enable a multi-choice experiment. Disable the
607 // currently selected choice.
608 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22609 const std::string experiment_name = internal_name.substr(0, at_index);
610 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19611
[email protected]28e35af2011-02-09 12:56:22612 // And enable the new choice, if it is not the default first choice.
613 if (internal_name != experiment_name + "@0") {
614 std::set<std::string> enabled_experiments;
615 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
616 enabled_experiments.insert(internal_name);
617 SetEnabledFlags(prefs, enabled_experiments);
618 }
[email protected]8a6ff28d2010-12-02 16:35:19619 return;
620 }
621
[email protected]ad2a3ded2010-08-27 13:19:05622 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24623 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05624
[email protected]8a6ff28d2010-12-02 16:35:19625 const Experiment* e = NULL;
626 for (size_t i = 0; i < num_experiments; ++i) {
627 if (experiments[i].internal_name == internal_name) {
628 e = experiments + i;
629 break;
630 }
631 }
632 DCHECK(e);
633
634 if (e->type == Experiment::SINGLE_VALUE) {
635 if (enable)
636 enabled_experiments.insert(internal_name);
637 else
638 enabled_experiments.erase(internal_name);
639 } else {
640 if (enable) {
641 // Enable the first choice.
642 enabled_experiments.insert(NameForChoice(*e, 0));
643 } else {
644 // Find the currently enabled choice and disable it.
645 for (int i = 0; i < e->num_choices; ++i) {
646 std::string choice_name = NameForChoice(*e, i);
647 if (enabled_experiments.find(choice_name) !=
648 enabled_experiments.end()) {
649 enabled_experiments.erase(choice_name);
650 // Continue on just in case there's a bug and more than one
651 // experiment for this choice was enabled.
652 }
653 }
654 }
655 }
[email protected]ad2a3ded2010-08-27 13:19:05656
[email protected]1a47d7e2010-10-15 00:37:24657 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05658}
659
[email protected]e2ddbc92010-10-15 20:02:07660void FlagsState::RemoveFlagsSwitches(
661 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53662 for (std::map<std::string, std::string>::const_iterator
663 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
664 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07665 }
666}
667
668void FlagsState::reset() {
669 needs_restart_ = false;
670 flags_switches_.clear();
671}
672
673} // namespace
674
675namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19676
677// WARNING: '@' is also used in the html file. If you update this constant you
678// also need to update the html file.
679const char kMultiSeparator[] = "@";
680
[email protected]e2ddbc92010-10-15 20:02:07681void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55682 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07683}
[email protected]a314ee5a2010-10-26 21:23:28684
685void SetExperiments(const Experiment* e, size_t count) {
686 if (!e) {
687 experiments = kExperiments;
688 num_experiments = arraysize(kExperiments);
689 } else {
690 experiments = e;
691 num_experiments = count;
692 }
693}
694
[email protected]8a6ff28d2010-12-02 16:35:19695const Experiment* GetExperiments(size_t* count) {
696 *count = num_experiments;
697 return experiments;
698}
699
[email protected]e2ddbc92010-10-15 20:02:07700} // namespace testing
701
[email protected]1a47d7e2010-10-15 00:37:24702} // namespace about_flags