blob: c48dbe4d9ddcad8bb7a1bbbf51df8e57b4dcce6e [file] [log] [blame]
[email protected]ad2a3ded2010-08-27 13:19:051// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1a47d7e2010-10-15 00:37:245#include "chrome/browser/about_flags.h"
[email protected]ad2a3ded2010-08-27 13:19:056
7#include <algorithm>
8#include <iterator>
9#include <map>
10#include <set>
11
[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.
26#define SINGLE_VALUE_TYPE(command_line) Experiment::SINGLE_VALUE, \
27 command_line, NULL, 0
28#define MULTI_VALUE_TYPE(choices) Experiment::MULTI_VALUE, "", choices, \
29 arraysize(choices)
30
[email protected]e2ddbc92010-10-15 20:02:0731namespace {
32
[email protected]a314ee5a2010-10-26 21:23:2833const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0534
[email protected]ba8164242010-11-16 21:31:0035// Names for former Chrome OS Labs experiments, shared with prefs migration
36// code.
37const char kMediaPlayerExperimentName[] = "media-player";
38const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
39const char kVerticalTabsExperimentName[] = "vertical-tabs";
40
[email protected]8a6ff28d2010-12-02 16:35:1941// If adding a new choice, add it to the end of the list.
42const Experiment::Choice kInstantChoices[] = {
43 { IDS_FLAGS_INSTANT_TYPE_VERBATIM, switches::kEnableVerbatimInstant },
44 { IDS_FLAGS_INSTANT_TYPE_PREDICTIVE, switches::kEnablePredictiveInstant },
45 { IDS_FLAGS_INSTANT_TYPE_PREDICTIVE_NO_AUTO_COMPLETE,
46 switches::kEnablePredictiveNoAutoCompleteInstant
47 },
48};
49
[email protected]4bc5050c2010-11-18 17:55:5450// RECORDING USER METRICS FOR FLAGS:
51// -----------------------------------------------------------------------------
52// The first line of the experiment is the internal name. If you'd like to
53// gather statistics about the usage of your flag, you should append a marker
54// comment to the end of the feature name, like so:
55// "my-special-feature", // FLAGS:RECORD_UMA
56//
57// After doing that, run //chrome/tools/extract_actions.py (see instructions at
58// the top of that file for details) to update the chromeactions.txt file, which
59// will enable UMA to record your feature flag.
60//
61// After your feature has shipped under a flag, you can locate the metrics
62// under the action name AboutFlags_internal-action-name. Actions are recorded
63// once per startup, so you should divide this number by AboutFlags_StartupTick
64// to get a sense of usage. Note that this will not be the same as number of
65// users with a given feature enabled because users can quit and relaunch
66// the application multiple times over a given time interval.
67// TODO(rsesek): See if there's a way to count per-user, rather than
68// per-startup.
69
[email protected]8a6ff28d2010-12-02 16:35:1970// To add a new experiment add to the end of kExperiments. There are two
71// distinct types of experiments:
72// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
73// macro for this type supplying the command line to the macro.
74// . MULTI_VALUE: if enabled the command line of the selected choice is enabled.
75// To specify this type of experiment use the macro MULTI_VALUE_TYPE supplying
76// it the array of choices.
77// See the documentation of Experiment for details on the fields.
78//
79// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0580const Experiment kExperiments[] = {
81 {
[email protected]4bc5050c2010-11-18 17:55:5482 "expose-for-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:1283 IDS_FLAGS_TABPOSE_NAME,
84 IDS_FLAGS_TABPOSE_DESCRIPTION,
[email protected]ad2a3ded2010-08-27 13:19:0585 kOsMac,
86#if defined(OS_MACOSX)
87 // The switch exists only on OS X.
[email protected]8a6ff28d2010-12-02 16:35:1988 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
[email protected]ad2a3ded2010-08-27 13:19:0589#else
[email protected]8a6ff28d2010-12-02 16:35:1990 SINGLE_VALUE_TYPE("")
[email protected]ad2a3ded2010-08-27 13:19:0591#endif
92 },
93 {
[email protected]ba8164242010-11-16 21:31:0094 kMediaPlayerExperimentName,
95 IDS_FLAGS_MEDIA_PLAYER_NAME,
96 IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION,
97 kOsCrOS,
98#if defined(OS_CHROMEOS)
99 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19100 SINGLE_VALUE_TYPE(switches::kEnableMediaPlayer)
[email protected]ba8164242010-11-16 21:31:00101#else
[email protected]8a6ff28d2010-12-02 16:35:19102 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00103#endif
104 },
105 {
106 kAdvancedFileSystemExperimentName,
107 IDS_FLAGS_ADVANCED_FS_NAME,
108 IDS_FLAGS_ADVANCED_FS_DESCRIPTION,
109 kOsCrOS,
110#if defined(OS_CHROMEOS)
111 // The switch exists only on Chrome OS.
[email protected]8a6ff28d2010-12-02 16:35:19112 SINGLE_VALUE_TYPE(switches::kEnableAdvancedFileSystem)
[email protected]ba8164242010-11-16 21:31:00113#else
[email protected]8a6ff28d2010-12-02 16:35:19114 SINGLE_VALUE_TYPE("")
[email protected]ba8164242010-11-16 21:31:00115#endif
116 },
117 {
[email protected]4bc5050c2010-11-18 17:55:54118 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12119 IDS_FLAGS_SIDE_TABS_NAME,
120 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:00121 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19122 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:05123 },
124 {
[email protected]4bc5050c2010-11-18 17:55:54125 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12126 IDS_FLAGS_REMOTING_NAME,
[email protected]d99599862011-01-20 22:43:59127 IDS_FLAGS_REMOTING_DESCRIPTION,
128 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19129 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56130 },
[email protected]a6f03652010-09-28 15:59:07131 {
[email protected]4bc5050c2010-11-18 17:55:54132 "xss-auditor", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12133 IDS_FLAGS_XSS_AUDITOR_NAME,
134 IDS_FLAGS_XSS_AUDITOR_DESCRIPTION,
[email protected]b3ce30ea2010-10-01 09:33:53135 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19136 SINGLE_VALUE_TYPE(switches::kEnableXSSAuditor)
[email protected]b3ce30ea2010-10-01 09:33:53137 },
[email protected]aea2ff42010-10-04 18:04:19138 {
[email protected]4bc5050c2010-11-18 17:55:54139 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05140 IDS_FLAGS_CONFLICTS_CHECK_NAME,
141 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
142 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19143 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05144 },
145 {
[email protected]4bc5050c2010-11-18 17:55:54146 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12147 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
148 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19149#if defined(GOOGLE_CHROME_BUILD)
150 // For a Chrome build, we know we have a PDF plug-in, and so we'll
151 // enable by platform as we get things working.
152 0,
153#else
154 // Otherwise, where we know it could be working if a viable PDF
155 // plug-in could be supplied, we'll keep the lab enabled.
[email protected]7bee0b22010-10-05 17:00:47156 kOsWin,
[email protected]fa6d2a2f2010-11-30 21:47:19157#endif
[email protected]8a6ff28d2010-12-02 16:35:19158 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42159 },
[email protected]580939a2010-10-12 18:54:37160 {
[email protected]bb461532010-11-26 21:50:23161 "crxless-web-apps",
162 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
163 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
164 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19165 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23166 },
167 {
[email protected]96ccc7e2010-12-02 20:46:04168 "gpu-compositing",
169 IDS_FLAGS_ACCELERATED_COMPOSITING_NAME,
170 IDS_FLAGS_ACCELERATED_COMPOSITING_DESCRIPTION,
171 kOsAll,
172 SINGLE_VALUE_TYPE(switches::kEnableAcceleratedLayers)
173 },
[email protected]8b6588a2010-10-12 02:39:42174 {
[email protected]4bc5050c2010-11-18 17:55:54175 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12176 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
177 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14178 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19179 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05180 },
[email protected]11c4c812010-10-22 19:50:12181 // FIXME(scheib): Add Flags entry for WebGL,
182 // or pull it and the strings in generated_resources.grd by Dec 2010
183 // {
[email protected]4bc5050c2010-11-18 17:55:54184 // "webgl",
[email protected]11c4c812010-10-22 19:50:12185 // IDS_FLAGS_WEBGL_NAME,
186 // IDS_FLAGS_WEBGL_DESCRIPTION,
187 // kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19188 // SINGLE_VALUE_TYPE(switches::kDisableExperimentalWebGL)
[email protected]11c4c812010-10-22 19:50:12189 // }
[email protected]8d260e52010-10-13 01:03:05190 {
[email protected]4bc5050c2010-11-18 17:55:54191 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12192 IDS_FLAGS_PRINT_PREVIEW_NAME,
193 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]8d260e52010-10-13 01:03:05194 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19195 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53196 },
197 {
[email protected]4bc5050c2010-11-18 17:55:54198 "enable-nacl", // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40199 IDS_FLAGS_ENABLE_NACL_NAME,
200 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
201 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19202 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40203 },
204 {
[email protected]4bc5050c2010-11-18 17:55:54205 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53206 IDS_FLAGS_DNS_SERVER_NAME,
207 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
208 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19209 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53210 },
[email protected]177aceb2010-11-03 16:17:41211 {
[email protected]4bc5050c2010-11-18 17:55:54212 "page-prerender", // FLAGS:RECORD_UMA
[email protected]83d00d92010-11-04 19:20:21213 IDS_FLAGS_PAGE_PRERENDER_NAME,
214 IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION,
215 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19216 SINGLE_VALUE_TYPE(switches::kEnablePagePrerender)
[email protected]83d00d92010-11-04 19:20:21217 },
218 {
[email protected]4bc5050c2010-11-18 17:55:54219 "confirm-to-quit", // FLAGS:RECORD_UMA
[email protected]177aceb2010-11-03 16:17:41220 IDS_FLAGS_CONFIRM_TO_QUIT_NAME,
221 IDS_FLAGS_CONFIRM_TO_QUIT_DESCRIPTION,
222 kOsMac,
[email protected]8a6ff28d2010-12-02 16:35:19223 SINGLE_VALUE_TYPE(switches::kEnableConfirmToQuit)
[email protected]177aceb2010-11-03 16:17:41224 },
[email protected]9a40ebef2010-11-10 17:49:13225 {
[email protected]4bc5050c2010-11-18 17:55:54226 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32227 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
228 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
229 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19230 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32231 },
[email protected]3627b06d2010-11-12 16:36:16232 {
[email protected]4bc5050c2010-11-18 17:55:54233 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16234 IDS_FLAGS_CLICK_TO_PLAY_NAME,
235 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
236 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19237 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16238 },
[email protected]80bd24e2010-11-30 09:34:38239 {
240 "disable-hyperlink-auditing",
241 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
242 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
243 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19244 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51245 },
246 {
247 "experimental-location-features", // FLAGS:RECORD_UMA
248 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
249 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
250 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19251 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
252 },
253 {
254 "instant-type", // FLAGS:RECORD_UMA
255 IDS_FLAGS_INSTANT_TYPE_NAME,
256 IDS_FLAGS_INSTANT_TYPE_DESCRIPTION,
257 kOsWin,
258 MULTI_VALUE_TYPE(kInstantChoices)
259 },
[email protected]9e789742011-01-10 23:27:32260 {
261 "instant-autocomplete-immediately", // FLAGS:RECORD_UMA
262 IDS_FLAGS_INSTANT_AUTOCOMPLETE_IMMEDIATELY_NAME,
263 IDS_FLAGS_INSTANT_AUTOCOMPLETE_IMMEDIATELY_DESCRIPTION,
264 kOsWin | kOsLinux,
265 SINGLE_VALUE_TYPE(switches::kInstantAutocompleteImmediately)
266 },
[email protected]4c6452d2011-01-12 08:47:57267 {
268 "block-reading-third-party-cookies",
269 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
270 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
271 kOsAll,
272 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
273 },
[email protected]04227962011-01-20 02:03:09274 {
275 "disable-interactive-form-validation",
276 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
277 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
278 kOsAll,
279 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
280 },
[email protected]ad2a3ded2010-08-27 13:19:05281};
282
[email protected]a314ee5a2010-10-26 21:23:28283const Experiment* experiments = kExperiments;
284size_t num_experiments = arraysize(kExperiments);
285
[email protected]e2ddbc92010-10-15 20:02:07286// Stores and encapsulates the little state that about:flags has.
287class FlagsState {
288 public:
289 FlagsState() : needs_restart_(false) {}
290 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
291 bool IsRestartNeededToCommitChanges();
292 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28293 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07294 void RemoveFlagsSwitches(
295 std::map<std::string, CommandLine::StringType>* switch_list);
296 void reset();
297
298 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55299 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07300 return Singleton<FlagsState>::get();
301 }
302
303 private:
304 bool needs_restart_;
305 std::set<std::string> flags_switches_;
306
307 DISALLOW_COPY_AND_ASSIGN(FlagsState);
308};
309
[email protected]ba8164242010-11-16 21:31:00310#if defined(OS_CHROMEOS)
311// Migrates Chrome OS Labs settings to experiments adding flags to enabled
312// experiment list if the corresponding pref is on.
313void MigrateChromeOSLabsPrefs(PrefService* prefs,
314 std::set<std::string>* result) {
315 DCHECK(prefs);
316 DCHECK(result);
317 if (prefs->GetBoolean(prefs::kLabsMediaplayerEnabled))
318 result->insert(kMediaPlayerExperimentName);
319 if (prefs->GetBoolean(prefs::kLabsAdvancedFilesystemEnabled))
320 result->insert(kAdvancedFileSystemExperimentName);
321 if (prefs->GetBoolean(prefs::kUseVerticalTabs))
322 result->insert(kVerticalTabsExperimentName);
323 prefs->SetBoolean(prefs::kLabsMediaplayerEnabled, false);
324 prefs->SetBoolean(prefs::kLabsAdvancedFilesystemEnabled, false);
325 prefs->SetBoolean(prefs::kUseVerticalTabs, false);
326}
327#endif
328
[email protected]c7b7800a2010-10-07 18:51:35329// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05330// in a set.
[email protected]1a47d7e2010-10-15 00:37:24331void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05332 const ListValue* enabled_experiments = prefs->GetList(
333 prefs::kEnabledLabsExperiments);
334 if (!enabled_experiments)
335 return;
336
337 for (ListValue::const_iterator it = enabled_experiments->begin();
338 it != enabled_experiments->end();
339 ++it) {
340 std::string experiment_name;
341 if (!(*it)->GetAsString(&experiment_name)) {
342 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
343 continue;
344 }
345 result->insert(experiment_name);
346 }
347}
348
349// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24350void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05351 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
352 ListValue* experiments_list = prefs->GetMutableList(
353 prefs::kEnabledLabsExperiments);
354 if (!experiments_list)
355 return;
356
357 experiments_list->Clear();
358 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
359 it != enabled_experiments.end();
360 ++it) {
361 experiments_list->Append(new StringValue(*it));
362 }
363}
364
[email protected]8a6ff28d2010-12-02 16:35:19365// Returns the name used in prefs for the choice at the specified index.
366std::string NameForChoice(const Experiment& e, int index) {
367 DCHECK(e.type == Experiment::MULTI_VALUE);
368 DCHECK(index < e.num_choices);
369 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
370 base::IntToString(index);
371}
372
373// Adds the internal names for the specified experiment to |names|.
374void AddInternalName(const Experiment& e, std::set<std::string>* names) {
375 if (e.type == Experiment::SINGLE_VALUE) {
376 names->insert(e.internal_name);
377 } else {
378 DCHECK(e.type == Experiment::MULTI_VALUE);
379 for (int i = 0; i < e.num_choices; ++i)
380 names->insert(NameForChoice(e, i));
381 }
382}
383
[email protected]ad2a3ded2010-08-27 13:19:05384// Removes all experiments from prefs::kEnabledLabsExperiments that are
385// unknown, to prevent this list to become very long as experiments are added
386// and removed.
387void SanitizeList(PrefService* prefs) {
388 std::set<std::string> known_experiments;
[email protected]a314ee5a2010-10-26 21:23:28389 for (size_t i = 0; i < num_experiments; ++i)
[email protected]8a6ff28d2010-12-02 16:35:19390 AddInternalName(experiments[i], &known_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05391
392 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24393 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05394
395 std::set<std::string> new_enabled_experiments;
396 std::set_intersection(
397 known_experiments.begin(), known_experiments.end(),
398 enabled_experiments.begin(), enabled_experiments.end(),
399 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
400
[email protected]1a47d7e2010-10-15 00:37:24401 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05402}
403
[email protected]1a47d7e2010-10-15 00:37:24404void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05405 PrefService* prefs, std::set<std::string>* result) {
406 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24407 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05408}
409
[email protected]a314ee5a2010-10-26 21:23:28410// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
411// enabled on the current platform.
412void GetSanitizedEnabledFlagsForCurrentPlatform(
413 PrefService* prefs, std::set<std::string>* result) {
414 GetSanitizedEnabledFlags(prefs, result);
415
416 // Filter out any experiments that aren't enabled on the current platform. We
417 // don't remove these from prefs else syncing to a platform with a different
418 // set of experiments would be lossy.
419 std::set<std::string> platform_experiments;
420 int current_platform = GetCurrentPlatform();
421 for (size_t i = 0; i < num_experiments; ++i) {
422 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19423 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28424 }
425
426 std::set<std::string> new_enabled_experiments;
427 std::set_intersection(
428 platform_experiments.begin(), platform_experiments.end(),
429 result->begin(), result->end(),
430 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
431
432 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05433}
434
[email protected]8a6ff28d2010-12-02 16:35:19435// Returns the Value representing the choice data in the specified experiment.
436// If one of the choices is enabled |is_one_selected| is set to true.
437Value* CreateChoiceData(const Experiment& experiment,
438 const std::set<std::string>& enabled_experiments,
439 bool* is_one_selected) {
440 DCHECK(experiment.type == Experiment::MULTI_VALUE);
441 ListValue* result = new ListValue;
442 for (int i = 0; i < experiment.num_choices; ++i) {
443 const Experiment::Choice& choice = experiment.choices[i];
444 DictionaryValue* value = new DictionaryValue;
445 std::string name = NameForChoice(experiment, i);
446 value->SetString("description",
447 l10n_util::GetStringUTF16(choice.description_id));
448 value->SetString("internal_name", name);
449 bool is_selected = enabled_experiments.count(name) > 0;
450 if (is_selected)
451 *is_one_selected = true;
452 value->SetBoolean("selected", is_selected);
453 result->Append(value);
454 }
455 return result;
456}
457
[email protected]e2ddbc92010-10-15 20:02:07458} // namespace
459
[email protected]1a47d7e2010-10-15 00:37:24460void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55461 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05462}
463
[email protected]1a47d7e2010-10-15 00:37:24464ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05465 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24466 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05467
468 int current_platform = GetCurrentPlatform();
469
470 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28471 for (size_t i = 0; i < num_experiments; ++i) {
472 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05473 if (!(experiment.supported_platforms & current_platform))
474 continue;
475
476 DictionaryValue* data = new DictionaryValue();
477 data->SetString("internal_name", experiment.internal_name);
478 data->SetString("name",
479 l10n_util::GetStringUTF16(experiment.visible_name_id));
480 data->SetString("description",
481 l10n_util::GetStringUTF16(
482 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05483
[email protected]8a6ff28d2010-12-02 16:35:19484 bool enabled = enabled_experiments.count(experiment.internal_name) > 0;
485
486 if (experiment.type == Experiment::MULTI_VALUE) {
487 data->Set("choices", CreateChoiceData(experiment, enabled_experiments,
488 &enabled));
489 }
490
491 data->SetBoolean("enabled", enabled);
[email protected]ad2a3ded2010-08-27 13:19:05492 experiments_data->Append(data);
493 }
494 return experiments_data;
495}
496
[email protected]ad2a3ded2010-08-27 13:19:05497bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55498 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05499}
500
501void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35502 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55503 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07504}
505
506void RemoveFlagsSwitches(
507 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55508 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07509}
510
[email protected]a314ee5a2010-10-26 21:23:28511int GetCurrentPlatform() {
512#if defined(OS_MACOSX)
513 return kOsMac;
514#elif defined(OS_WIN)
515 return kOsWin;
516#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
517 return kOsCrOS;
518#elif defined(OS_LINUX)
519 return kOsLinux;
520#else
521#error Unknown platform
522#endif
523}
524
[email protected]4bc5050c2010-11-18 17:55:54525void RecordUMAStatistics(const PrefService* prefs) {
526 std::set<std::string> flags;
527 GetEnabledFlags(prefs, &flags);
528 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
529 ++it) {
530 std::string action("AboutFlags_");
531 action += *it;
532 UserMetrics::RecordComputedAction(action);
533 }
534 // Since flag metrics are recorded every startup, add a tick so that the
535 // stats can be made meaningful.
536 if (flags.size())
537 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
538}
539
[email protected]e2ddbc92010-10-15 20:02:07540//////////////////////////////////////////////////////////////////////////////
541// FlagsState implementation.
542
543namespace {
544
545void FlagsState::ConvertFlagsToSwitches(
546 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07547 if (command_line->HasSwitch(switches::kNoExperiments))
548 return;
549
550 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00551
552#if defined(OS_CHROMEOS)
553 // Some experiments were implemented via prefs on Chrome OS and we want to
554 // seamlessly migrate these prefs to about:flags for updated users.
555 MigrateChromeOSLabsPrefs(prefs, &enabled_experiments);
556#endif
557
[email protected]a314ee5a2010-10-26 21:23:28558 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07559
[email protected]8a6ff28d2010-12-02 16:35:19560 typedef std::map<std::string, std::string> NameToSwitchMap;
561 NameToSwitchMap name_to_switch_map;
562 for (size_t i = 0; i < num_experiments; ++i) {
563 const Experiment& e = experiments[i];
564 if (e.type == Experiment::SINGLE_VALUE) {
565 name_to_switch_map[e.internal_name] = e.command_line;
566 } else {
567 for (int j = 0; j < e.num_choices; ++j)
568 name_to_switch_map[NameForChoice(e, j)] = e.choices[j].command_line;
569 }
570 }
[email protected]e2ddbc92010-10-15 20:02:07571
572 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
573 flags_switches_.insert(switches::kFlagSwitchesBegin);
574 for (std::set<std::string>::iterator it = enabled_experiments.begin();
575 it != enabled_experiments.end();
576 ++it) {
577 const std::string& experiment_name = *it;
[email protected]8a6ff28d2010-12-02 16:35:19578 NameToSwitchMap::const_iterator name_to_switch_it =
579 name_to_switch_map.find(experiment_name);
580 if (name_to_switch_it == name_to_switch_map.end()) {
581 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07582 continue;
[email protected]8a6ff28d2010-12-02 16:35:19583 }
[email protected]e2ddbc92010-10-15 20:02:07584
[email protected]8a6ff28d2010-12-02 16:35:19585 command_line->AppendSwitch(name_to_switch_it->second);
586 flags_switches_.insert(name_to_switch_it->second);
[email protected]e2ddbc92010-10-15 20:02:07587 }
588 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
589 flags_switches_.insert(switches::kFlagSwitchesEnd);
590}
591
592bool FlagsState::IsRestartNeededToCommitChanges() {
593 return needs_restart_;
594}
595
596void FlagsState::SetExperimentEnabled(
597 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05598 needs_restart_ = true;
599
[email protected]8a6ff28d2010-12-02 16:35:19600 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
601 if (at_index != std::string::npos) {
602 DCHECK(enable);
603 // We're being asked to enable a multi-choice experiment. Disable the
604 // currently selected choice.
605 DCHECK_NE(at_index, 0u);
606 SetExperimentEnabled(prefs, internal_name.substr(0, at_index), false);
607
608 // And enable the new choice.
609 std::set<std::string> enabled_experiments;
610 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
611 enabled_experiments.insert(internal_name);
612 SetEnabledFlags(prefs, enabled_experiments);
613 return;
614 }
615
[email protected]ad2a3ded2010-08-27 13:19:05616 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24617 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05618
[email protected]8a6ff28d2010-12-02 16:35:19619 const Experiment* e = NULL;
620 for (size_t i = 0; i < num_experiments; ++i) {
621 if (experiments[i].internal_name == internal_name) {
622 e = experiments + i;
623 break;
624 }
625 }
626 DCHECK(e);
627
628 if (e->type == Experiment::SINGLE_VALUE) {
629 if (enable)
630 enabled_experiments.insert(internal_name);
631 else
632 enabled_experiments.erase(internal_name);
633 } else {
634 if (enable) {
635 // Enable the first choice.
636 enabled_experiments.insert(NameForChoice(*e, 0));
637 } else {
638 // Find the currently enabled choice and disable it.
639 for (int i = 0; i < e->num_choices; ++i) {
640 std::string choice_name = NameForChoice(*e, i);
641 if (enabled_experiments.find(choice_name) !=
642 enabled_experiments.end()) {
643 enabled_experiments.erase(choice_name);
644 // Continue on just in case there's a bug and more than one
645 // experiment for this choice was enabled.
646 }
647 }
648 }
649 }
[email protected]ad2a3ded2010-08-27 13:19:05650
[email protected]1a47d7e2010-10-15 00:37:24651 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05652}
653
[email protected]e2ddbc92010-10-15 20:02:07654void FlagsState::RemoveFlagsSwitches(
655 std::map<std::string, CommandLine::StringType>* switch_list) {
656 for (std::set<std::string>::const_iterator it = flags_switches_.begin();
657 it != flags_switches_.end();
658 ++it) {
659 switch_list->erase(*it);
660 }
661}
662
663void FlagsState::reset() {
664 needs_restart_ = false;
665 flags_switches_.clear();
666}
667
668} // namespace
669
670namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19671
672// WARNING: '@' is also used in the html file. If you update this constant you
673// also need to update the html file.
674const char kMultiSeparator[] = "@";
675
[email protected]e2ddbc92010-10-15 20:02:07676void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55677 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07678}
[email protected]a314ee5a2010-10-26 21:23:28679
680void SetExperiments(const Experiment* e, size_t count) {
681 if (!e) {
682 experiments = kExperiments;
683 num_experiments = arraysize(kExperiments);
684 } else {
685 experiments = e;
686 num_experiments = count;
687 }
688}
689
[email protected]8a6ff28d2010-12-02 16:35:19690const Experiment* GetExperiments(size_t* count) {
691 *count = num_experiments;
692 return experiments;
693}
694
[email protected]e2ddbc92010-10-15 20:02:07695} // namespace testing
696
[email protected]1a47d7e2010-10-15 00:37:24697} // namespace about_flags