blob: c6ceafcb864f9ccbf7e3c8595aa3254bf132b9cf [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]d208f4d82011-05-23 21:52:0315#include "base/utf_string_conversions.h"
[email protected]ad2a3ded2010-08-27 13:19:0516#include "base/values.h"
[email protected]d208f4d82011-05-23 21:52:0317#include "chrome/browser/plugin_updater.h"
[email protected]ad2a3ded2010-08-27 13:19:0518#include "chrome/browser/prefs/pref_service.h"
[email protected]1bc78422011-03-31 08:41:3819#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]d208f4d82011-05-23 21:52:0320#include "chrome/common/chrome_content_client.h"
[email protected]ad2a3ded2010-08-27 13:19:0521#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
[email protected]afd1e522011-04-27 23:29:5923#include "content/browser/user_metrics.h"
[email protected]ad2a3ded2010-08-27 13:19:0524#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1725#include "ui/base/l10n/l10n_util.h"
[email protected]8ff7f342011-05-25 01:49:4726#include "ui/gfx/gl/gl_switches.h"
[email protected]ad2a3ded2010-08-27 13:19:0527
[email protected]1a47d7e2010-10-15 00:37:2428namespace about_flags {
[email protected]ad2a3ded2010-08-27 13:19:0529
[email protected]8a6ff28d2010-12-02 16:35:1930// Macros to simplify specifying the type.
[email protected]a82744532011-02-11 16:15:5331#define SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, switch_value) \
32 Experiment::SINGLE_VALUE, command_line_switch, switch_value, NULL, 0
33#define SINGLE_VALUE_TYPE(command_line_switch) \
34 SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, "")
35#define MULTI_VALUE_TYPE(choices) \
[email protected]0e6f56d2011-02-12 23:45:1536 Experiment::MULTI_VALUE, "", "", choices, arraysize(choices)
[email protected]8a6ff28d2010-12-02 16:35:1937
[email protected]e2ddbc92010-10-15 20:02:0738namespace {
39
[email protected]a314ee5a2010-10-26 21:23:2840const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0541
[email protected]ba8164242010-11-16 21:31:0042// Names for former Chrome OS Labs experiments, shared with prefs migration
43// code.
44const char kMediaPlayerExperimentName[] = "media-player";
45const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
46const char kVerticalTabsExperimentName[] = "vertical-tabs";
47
[email protected]4bc5050c2010-11-18 17:55:5448// RECORDING USER METRICS FOR FLAGS:
49// -----------------------------------------------------------------------------
50// The first line of the experiment is the internal name. If you'd like to
51// gather statistics about the usage of your flag, you should append a marker
52// comment to the end of the feature name, like so:
53// "my-special-feature", // FLAGS:RECORD_UMA
54//
55// After doing that, run //chrome/tools/extract_actions.py (see instructions at
56// the top of that file for details) to update the chromeactions.txt file, which
57// will enable UMA to record your feature flag.
58//
59// After your feature has shipped under a flag, you can locate the metrics
60// under the action name AboutFlags_internal-action-name. Actions are recorded
61// once per startup, so you should divide this number by AboutFlags_StartupTick
62// to get a sense of usage. Note that this will not be the same as number of
63// users with a given feature enabled because users can quit and relaunch
64// the application multiple times over a given time interval.
65// TODO(rsesek): See if there's a way to count per-user, rather than
66// per-startup.
67
[email protected]8a6ff28d2010-12-02 16:35:1968// To add a new experiment add to the end of kExperiments. There are two
69// distinct types of experiments:
70// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
71// macro for this type supplying the command line to the macro.
[email protected]28e35af2011-02-09 12:56:2272// . MULTI_VALUE: a list of choices, the first of which should correspond to a
73// deactivated state for this lab (i.e. no command line option). To specify
74// this type of experiment use the macro MULTI_VALUE_TYPE supplying it the
75// array of choices.
[email protected]8a6ff28d2010-12-02 16:35:1976// See the documentation of Experiment for details on the fields.
77//
78// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0579const Experiment kExperiments[] = {
80 {
[email protected]aac169d2011-03-18 19:53:0381 "expose-for-tabs", // FLAGS:RECORD_UMA
82 IDS_FLAGS_TABPOSE_NAME,
83 IDS_FLAGS_TABPOSE_DESCRIPTION,
84 kOsMac,
85#if defined(OS_MACOSX)
86 // The switch exists only on OS X.
87 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
88#else
89 SINGLE_VALUE_TYPE("")
90#endif
91 },
92 {
[email protected]4bc5050c2010-11-18 17:55:5493 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:1294 IDS_FLAGS_SIDE_TABS_NAME,
95 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:0096 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:1997 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:0598 },
99 {
[email protected]4bc5050c2010-11-18 17:55:54100 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12101 IDS_FLAGS_REMOTING_NAME,
[email protected]d99599862011-01-20 22:43:59102 IDS_FLAGS_REMOTING_DESCRIPTION,
103 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19104 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56105 },
[email protected]a6f03652010-09-28 15:59:07106 {
[email protected]4bc5050c2010-11-18 17:55:54107 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05108 IDS_FLAGS_CONFLICTS_CHECK_NAME,
109 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
110 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19111 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05112 },
113 {
[email protected]4bc5050c2010-11-18 17:55:54114 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12115 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
116 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19117#if defined(GOOGLE_CHROME_BUILD)
[email protected]9f8872b32011-03-04 19:44:45118 // For a Chrome build, we know we have a PDF plug-in on Windows, so it's
[email protected]be51dd252011-03-05 00:23:44119 // fully enabled. Linux still need some final polish.
120 kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19121#else
[email protected]be51dd252011-03-05 00:23:44122 // Otherwise, where we know Windows could be working if a viable PDF
123 // plug-in could be supplied, we'll keep the lab enabled. Mac always has
124 // PDF rasterization available, so no flag needed there.
125 kOsWin | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19126#endif
[email protected]8a6ff28d2010-12-02 16:35:19127 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42128 },
[email protected]580939a2010-10-12 18:54:37129 {
[email protected]bb461532010-11-26 21:50:23130 "crxless-web-apps",
131 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
132 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
133 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19134 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23135 },
136 {
[email protected]96c6f4c2011-05-18 19:36:22137 "ignore-gpu-blacklist",
138 IDS_FLAGS_IGNORE_GPU_BLACKLIST_NAME,
139 IDS_FLAGS_IGNORE_GPU_BLACKLIST_DESCRIPTION,
140 kOsAll,
141 SINGLE_VALUE_TYPE(switches::kIgnoreGpuBlacklist)
142 },
143 {
144 "force-compositing-mode",
145 IDS_FLAGS_FORCE_COMPOSITING_MODE_NAME,
146 IDS_FLAGS_FORCE_COMPOSITING_MODE_DESCRIPTION,
147 kOsAll,
148 SINGLE_VALUE_TYPE(switches::kForceCompositingMode)
149 },
150 {
[email protected]5963b772011-02-09 22:55:38151 "composited-layer-borders",
152 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
153 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
154 kOsAll,
155 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
156 },
157 {
[email protected]a8f1eaa2011-03-07 19:00:58158 "show-fps-counter",
159 IDS_FLAGS_SHOW_FPS_COUNTER,
160 IDS_FLAGS_SHOW_FPS_COUNTER_DESCRIPTION,
161 kOsAll,
162 SINGLE_VALUE_TYPE(switches::kShowFPSCounter)
163 },
[email protected]8ff7f342011-05-25 01:49:47164 {
165 "disable-gpu-vsync",
166 IDS_FLAGS_DISABLE_GPU_VSYNC_NAME,
167 IDS_FLAGS_DISABLE_GPU_VSYNC_DESCRIPTION,
168 kOsAll,
169 SINGLE_VALUE_TYPE(switches::kDisableGpuVsync)
170 },
[email protected]64b33432011-06-01 05:17:59171#if !defined(GOOGLE_CHROME_BUILD) || defined(OS_MACOSX)
[email protected]38e46812011-05-09 20:49:22172 // Only expose this for Chromium builds where users may not have the PDF
[email protected]64b33432011-06-01 05:17:59173 // plugin. Do not give Google Chrome users the option to disable it here,
174 // except on Mac, where it is disabled.
[email protected]8d260e52010-10-13 01:03:05175 {
[email protected]4bc5050c2010-11-18 17:55:54176 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12177 IDS_FLAGS_PRINT_PREVIEW_NAME,
178 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]38e46812011-05-09 20:49:22179 kOsMac | kOsWin | kOsLinux, // This switch is not available in CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19180 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53181 },
[email protected]38e46812011-05-09 20:49:22182#endif
[email protected]d208f4d82011-05-23 21:52:03183 // TODO(dspringer): When NaCl is on by default, remove this flag entry.
[email protected]2fe15fcb2010-10-21 20:39:53184 {
[email protected]d208f4d82011-05-23 21:52:03185 switches::kEnableNaCl, // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40186 IDS_FLAGS_ENABLE_NACL_NAME,
187 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
188 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19189 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40190 },
191 {
[email protected]4bc5050c2010-11-18 17:55:54192 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53193 IDS_FLAGS_DNS_SERVER_NAME,
194 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
195 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19196 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53197 },
[email protected]177aceb2010-11-03 16:17:41198 {
[email protected]4bc5050c2010-11-18 17:55:54199 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32200 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
201 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
202 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19203 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32204 },
[email protected]3627b06d2010-11-12 16:36:16205 {
[email protected]4bc5050c2010-11-18 17:55:54206 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16207 IDS_FLAGS_CLICK_TO_PLAY_NAME,
208 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
209 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19210 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16211 },
[email protected]80bd24e2010-11-30 09:34:38212 {
213 "disable-hyperlink-auditing",
214 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
215 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
216 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19217 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51218 },
219 {
220 "experimental-location-features", // FLAGS:RECORD_UMA
221 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
222 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
223 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19224 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
225 },
226 {
[email protected]4c6452d2011-01-12 08:47:57227 "block-reading-third-party-cookies",
228 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
229 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
230 kOsAll,
231 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
232 },
[email protected]04227962011-01-20 02:03:09233 {
234 "disable-interactive-form-validation",
235 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
236 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
237 kOsAll,
238 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
239 },
[email protected]8df51192011-01-22 20:05:03240 {
241 "webaudio",
242 IDS_FLAGS_WEBAUDIO_NAME,
243 IDS_FLAGS_WEBAUDIO_DESCRIPTION,
[email protected]54afa742011-04-29 21:21:34244// This switch is currently not available in CrOS.
245// TODO(crogers): FFmpeg Windows DLLs need to be rebuilt for chromium.
246#if defined(GOOGLE_CHROME_BUILD)
247 kOsMac | kOsWin | kOsLinux,
248#else
249 kOsMac | kOsLinux,
250#endif
[email protected]8df51192011-01-22 20:05:03251 SINGLE_VALUE_TYPE(switches::kEnableWebAudio)
252 },
[email protected]3828d6f2011-02-24 18:32:21253 {
[email protected]6c54e7e42011-03-02 20:52:34254 "p2papi",
255 IDS_FLAGS_P2P_API_NAME,
256 IDS_FLAGS_P2P_API_DESCRIPTION,
257 kOsAll,
[email protected]a026daa2011-04-20 15:49:51258#if defined(ENABLE_P2P_APIS)
[email protected]6c54e7e42011-03-02 20:52:34259 SINGLE_VALUE_TYPE(switches::kEnableP2PApi)
[email protected]a026daa2011-04-20 15:49:51260#else
261 SINGLE_VALUE_TYPE("")
262#endif
[email protected]6c54e7e42011-03-02 20:52:34263 },
[email protected]07d490bc2011-03-07 17:05:26264 {
265 "focus-existing-tab-on-open", // FLAGS:RECORD_UMA
266 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_NAME,
267 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_DESCRIPTION,
268 kOsAll,
269 SINGLE_VALUE_TYPE(switches::kFocusExistingTabOnOpen)
270 },
[email protected]d5dcfb32011-03-19 00:49:24271 {
[email protected]ac86c982011-05-11 13:26:05272 "compact-navigation",
273 IDS_FLAGS_ENABLE_COMPACT_NAVIGATION,
274 IDS_FLAGS_ENABLE_COMPACT_NAVIGATION_DESCRIPTION,
275 kOsWin, // TODO(stevet): Add other platforms when ready.
276 SINGLE_VALUE_TYPE(switches::kEnableCompactNavigation)
277 },
278 {
[email protected]d5dcfb32011-03-19 00:49:24279 "new-tab-page-4",
280 IDS_FLAGS_NEW_TAB_PAGE_4_NAME,
281 IDS_FLAGS_NEW_TAB_PAGE_4_DESCRIPTION,
282 kOsAll,
283 SINGLE_VALUE_TYPE(switches::kNewTabPage4)
284 },
[email protected]5aea1862011-03-23 23:55:39285 {
286 "tab-groups-context-menu",
287 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_NAME,
288 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_DESCRIPTION,
289 kOsWin,
290 SINGLE_VALUE_TYPE(switches::kEnableTabGroupsContextMenu)
291 },
[email protected]e9bf9d92011-03-31 20:57:15292 {
293 "ppapi-flash-in-process",
294 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_NAME,
295 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_DESCRIPTION,
[email protected]f2d3ce0d2011-04-01 18:19:30296 kOsAll,
[email protected]e9bf9d92011-03-31 20:57:15297 SINGLE_VALUE_TYPE(switches::kPpapiFlashInProcess)
298 },
[email protected]ce5737142011-04-14 23:35:09299 {
[email protected]20b5df962011-04-18 22:07:43300 "multi-profiles",
301 IDS_FLAGS_MULTI_PROFILES_NAME,
302 IDS_FLAGS_MULTI_PROFILES_DESCRIPTION,
303 kOsAll,
304 SINGLE_VALUE_TYPE(switches::kMultiProfiles)
305 },
[email protected]cc057b02011-04-21 17:19:04306 {
[email protected]08338b862011-05-05 20:22:23307 "restrict-instant-to-search",
308 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_NAME,
309 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_DESCRIPTION,
310 kOsAll,
311 SINGLE_VALUE_TYPE(switches::kRestrictInstantToSearch)
312 },
[email protected]b652fc82011-05-23 07:53:05313 {
314 "indexeddb-use-leveldb", // FLAGS:RECORD_UMA
315 IDS_FLAGS_INDEXEDDB_USE_LEVELDB_NAME,
316 IDS_FLAGS_INDEXEDDB_USE_LEVELDB_DESCRIPTION,
317 kOsAll,
318 SINGLE_VALUE_TYPE(switches::kLevelDBIndexedDatabase)
319 },
[email protected]f2557bd2011-06-01 02:33:07320 {
321 "preload-instant-search",
322 IDS_FLAGS_PRELOAD_INSTANT_SEARCH_NAME,
323 IDS_FLAGS_PRELOAD_INSTANT_SEARCH_DESCRIPTION,
324 kOsAll,
325 SINGLE_VALUE_TYPE(switches::kPreloadInstantSearch)
326 },
[email protected]354e33b2011-06-15 00:29:10327 {
328 "static-ip-config",
329 IDS_FLAGS_STATIC_IP_CONFIG_NAME,
330 IDS_FLAGS_STATIC_IP_CONFIG_DESCRIPTION,
331 kOsCrOS,
332#if defined(OS_CHROMEOS)
333 // This switch exists only on Chrome OS.
334 SINGLE_VALUE_TYPE(switches::kEnableStaticIPConfig)
335#else
336 SINGLE_VALUE_TYPE("")
337#endif
338 },
[email protected]ad2a3ded2010-08-27 13:19:05339};
340
[email protected]a314ee5a2010-10-26 21:23:28341const Experiment* experiments = kExperiments;
342size_t num_experiments = arraysize(kExperiments);
343
[email protected]e2ddbc92010-10-15 20:02:07344// Stores and encapsulates the little state that about:flags has.
345class FlagsState {
346 public:
347 FlagsState() : needs_restart_(false) {}
348 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
349 bool IsRestartNeededToCommitChanges();
350 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28351 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07352 void RemoveFlagsSwitches(
353 std::map<std::string, CommandLine::StringType>* switch_list);
354 void reset();
355
356 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55357 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07358 return Singleton<FlagsState>::get();
359 }
360
361 private:
362 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53363 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07364
365 DISALLOW_COPY_AND_ASSIGN(FlagsState);
366};
367
[email protected]c7b7800a2010-10-07 18:51:35368// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05369// in a set.
[email protected]1a47d7e2010-10-15 00:37:24370void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05371 const ListValue* enabled_experiments = prefs->GetList(
372 prefs::kEnabledLabsExperiments);
373 if (!enabled_experiments)
374 return;
375
376 for (ListValue::const_iterator it = enabled_experiments->begin();
377 it != enabled_experiments->end();
378 ++it) {
379 std::string experiment_name;
380 if (!(*it)->GetAsString(&experiment_name)) {
381 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
382 continue;
383 }
384 result->insert(experiment_name);
385 }
386}
387
388// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24389void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05390 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
[email protected]1bc78422011-03-31 08:41:38391 ListPrefUpdate update(prefs, prefs::kEnabledLabsExperiments);
392 ListValue* experiments_list = update.Get();
[email protected]ad2a3ded2010-08-27 13:19:05393
394 experiments_list->Clear();
395 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
396 it != enabled_experiments.end();
397 ++it) {
398 experiments_list->Append(new StringValue(*it));
399 }
400}
401
[email protected]8a6ff28d2010-12-02 16:35:19402// Returns the name used in prefs for the choice at the specified index.
403std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34404 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
405 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19406 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
407 base::IntToString(index);
408}
409
410// Adds the internal names for the specified experiment to |names|.
411void AddInternalName(const Experiment& e, std::set<std::string>* names) {
412 if (e.type == Experiment::SINGLE_VALUE) {
413 names->insert(e.internal_name);
414 } else {
[email protected]2ce9c89752011-02-25 18:24:34415 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19416 for (int i = 0; i < e.num_choices; ++i)
417 names->insert(NameForChoice(e, i));
418 }
419}
420
[email protected]28e35af2011-02-09 12:56:22421// Confirms that an experiment is valid, used in a DCHECK in
422// SanitizeList below.
423bool ValidateExperiment(const Experiment& e) {
424 switch (e.type) {
425 case Experiment::SINGLE_VALUE:
426 DCHECK_EQ(0, e.num_choices);
427 DCHECK(!e.choices);
428 break;
429 case Experiment::MULTI_VALUE:
430 DCHECK_GT(e.num_choices, 0);
431 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53432 DCHECK(e.choices[0].command_line_switch);
433 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22434 break;
435 default:
436 NOTREACHED();
437 }
438 return true;
439}
440
[email protected]ad2a3ded2010-08-27 13:19:05441// Removes all experiments from prefs::kEnabledLabsExperiments that are
442// unknown, to prevent this list to become very long as experiments are added
443// and removed.
444void SanitizeList(PrefService* prefs) {
445 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22446 for (size_t i = 0; i < num_experiments; ++i) {
447 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19448 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22449 }
[email protected]ad2a3ded2010-08-27 13:19:05450
451 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24452 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05453
454 std::set<std::string> new_enabled_experiments;
455 std::set_intersection(
456 known_experiments.begin(), known_experiments.end(),
457 enabled_experiments.begin(), enabled_experiments.end(),
458 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
459
[email protected]1a47d7e2010-10-15 00:37:24460 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05461}
462
[email protected]1a47d7e2010-10-15 00:37:24463void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05464 PrefService* prefs, std::set<std::string>* result) {
465 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24466 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05467}
468
[email protected]a314ee5a2010-10-26 21:23:28469// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
470// enabled on the current platform.
471void GetSanitizedEnabledFlagsForCurrentPlatform(
472 PrefService* prefs, std::set<std::string>* result) {
473 GetSanitizedEnabledFlags(prefs, result);
474
475 // Filter out any experiments that aren't enabled on the current platform. We
476 // don't remove these from prefs else syncing to a platform with a different
477 // set of experiments would be lossy.
478 std::set<std::string> platform_experiments;
479 int current_platform = GetCurrentPlatform();
480 for (size_t i = 0; i < num_experiments; ++i) {
481 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19482 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28483 }
484
485 std::set<std::string> new_enabled_experiments;
486 std::set_intersection(
487 platform_experiments.begin(), platform_experiments.end(),
488 result->begin(), result->end(),
489 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
490
491 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05492}
493
[email protected]8a6ff28d2010-12-02 16:35:19494// Returns the Value representing the choice data in the specified experiment.
[email protected]8a6ff28d2010-12-02 16:35:19495Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22496 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34497 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19498 ListValue* result = new ListValue;
499 for (int i = 0; i < experiment.num_choices; ++i) {
500 const Experiment::Choice& choice = experiment.choices[i];
501 DictionaryValue* value = new DictionaryValue;
502 std::string name = NameForChoice(experiment, i);
503 value->SetString("description",
504 l10n_util::GetStringUTF16(choice.description_id));
505 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22506 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19507 result->Append(value);
508 }
509 return result;
510}
511
[email protected]e2ddbc92010-10-15 20:02:07512} // namespace
513
[email protected]1a47d7e2010-10-15 00:37:24514void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55515 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05516}
517
[email protected]1a47d7e2010-10-15 00:37:24518ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05519 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24520 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05521
522 int current_platform = GetCurrentPlatform();
523
524 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28525 for (size_t i = 0; i < num_experiments; ++i) {
526 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05527 if (!(experiment.supported_platforms & current_platform))
528 continue;
529
530 DictionaryValue* data = new DictionaryValue();
531 data->SetString("internal_name", experiment.internal_name);
532 data->SetString("name",
533 l10n_util::GetStringUTF16(experiment.visible_name_id));
534 data->SetString("description",
535 l10n_util::GetStringUTF16(
536 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05537
[email protected]28e35af2011-02-09 12:56:22538 switch (experiment.type) {
539 case Experiment::SINGLE_VALUE:
540 data->SetBoolean(
541 "enabled",
542 enabled_experiments.count(experiment.internal_name) > 0);
543 break;
544 case Experiment::MULTI_VALUE:
545 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
546 break;
547 default:
548 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19549 }
550
[email protected]ad2a3ded2010-08-27 13:19:05551 experiments_data->Append(data);
552 }
553 return experiments_data;
554}
555
[email protected]ad2a3ded2010-08-27 13:19:05556bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55557 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05558}
559
560void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35561 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55562 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07563}
564
565void RemoveFlagsSwitches(
566 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55567 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07568}
569
[email protected]a314ee5a2010-10-26 21:23:28570int GetCurrentPlatform() {
571#if defined(OS_MACOSX)
572 return kOsMac;
573#elif defined(OS_WIN)
574 return kOsWin;
575#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
576 return kOsCrOS;
577#elif defined(OS_LINUX)
578 return kOsLinux;
579#else
580#error Unknown platform
581#endif
582}
583
[email protected]4bc5050c2010-11-18 17:55:54584void RecordUMAStatistics(const PrefService* prefs) {
585 std::set<std::string> flags;
586 GetEnabledFlags(prefs, &flags);
587 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
588 ++it) {
589 std::string action("AboutFlags_");
590 action += *it;
591 UserMetrics::RecordComputedAction(action);
592 }
593 // Since flag metrics are recorded every startup, add a tick so that the
594 // stats can be made meaningful.
595 if (flags.size())
596 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
[email protected]970b2fd2011-01-22 18:06:45597 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54598}
599
[email protected]e2ddbc92010-10-15 20:02:07600//////////////////////////////////////////////////////////////////////////////
601// FlagsState implementation.
602
603namespace {
604
605void FlagsState::ConvertFlagsToSwitches(
606 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07607 if (command_line->HasSwitch(switches::kNoExperiments))
608 return;
609
610 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00611
[email protected]a314ee5a2010-10-26 21:23:28612 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07613
[email protected]a82744532011-02-11 16:15:53614 typedef std::map<std::string, std::pair<std::string, std::string> >
615 NameToSwitchAndValueMap;
616 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19617 for (size_t i = 0; i < num_experiments; ++i) {
618 const Experiment& e = experiments[i];
619 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53620 name_to_switch_map[e.internal_name] =
621 std::pair<std::string, std::string>(e.command_line_switch,
622 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19623 } else {
624 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53625 name_to_switch_map[NameForChoice(e, j)] =
626 std::pair<std::string, std::string>(
627 e.choices[j].command_line_switch,
628 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19629 }
630 }
[email protected]e2ddbc92010-10-15 20:02:07631
632 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53633 flags_switches_.insert(
634 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
635 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07636 for (std::set<std::string>::iterator it = enabled_experiments.begin();
637 it != enabled_experiments.end();
638 ++it) {
639 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53640 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19641 name_to_switch_map.find(experiment_name);
642 if (name_to_switch_it == name_to_switch_map.end()) {
643 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07644 continue;
[email protected]8a6ff28d2010-12-02 16:35:19645 }
[email protected]e2ddbc92010-10-15 20:02:07646
[email protected]a82744532011-02-11 16:15:53647 const std::pair<std::string, std::string>&
648 switch_and_value_pair = name_to_switch_it->second;
649
650 command_line->AppendSwitchASCII(switch_and_value_pair.first,
651 switch_and_value_pair.second);
652 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07653 }
654 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53655 flags_switches_.insert(
656 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
657 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07658}
659
660bool FlagsState::IsRestartNeededToCommitChanges() {
661 return needs_restart_;
662}
663
664void FlagsState::SetExperimentEnabled(
665 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05666 needs_restart_ = true;
667
[email protected]8a6ff28d2010-12-02 16:35:19668 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
669 if (at_index != std::string::npos) {
670 DCHECK(enable);
671 // We're being asked to enable a multi-choice experiment. Disable the
672 // currently selected choice.
673 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22674 const std::string experiment_name = internal_name.substr(0, at_index);
675 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19676
[email protected]28e35af2011-02-09 12:56:22677 // And enable the new choice, if it is not the default first choice.
678 if (internal_name != experiment_name + "@0") {
679 std::set<std::string> enabled_experiments;
680 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
681 enabled_experiments.insert(internal_name);
682 SetEnabledFlags(prefs, enabled_experiments);
683 }
[email protected]8a6ff28d2010-12-02 16:35:19684 return;
685 }
686
[email protected]ad2a3ded2010-08-27 13:19:05687 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24688 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05689
[email protected]8a6ff28d2010-12-02 16:35:19690 const Experiment* e = NULL;
691 for (size_t i = 0; i < num_experiments; ++i) {
692 if (experiments[i].internal_name == internal_name) {
693 e = experiments + i;
694 break;
695 }
696 }
697 DCHECK(e);
698
699 if (e->type == Experiment::SINGLE_VALUE) {
[email protected]d208f4d82011-05-23 21:52:03700 if (enable) {
[email protected]8a6ff28d2010-12-02 16:35:19701 enabled_experiments.insert(internal_name);
[email protected]d208f4d82011-05-23 21:52:03702 // If enabling NaCl, make sure the plugin is also enabled. See bug
703 // http://code.google.com/p/chromium/issues/detail?id=81010 for more
704 // information.
705 // TODO(dspringer): When NaCl is on by default, remove this code.
706 if (internal_name == switches::kEnableNaCl) {
707 PluginUpdater* plugin_updater = PluginUpdater::GetInstance();
708 string16 nacl_plugin_name =
709 ASCIIToUTF16(chrome::ChromeContentClient::kNaClPluginName);
710 plugin_updater->EnablePluginGroup(true, nacl_plugin_name);
711 }
712 } else {
[email protected]8a6ff28d2010-12-02 16:35:19713 enabled_experiments.erase(internal_name);
[email protected]d208f4d82011-05-23 21:52:03714 }
[email protected]8a6ff28d2010-12-02 16:35:19715 } else {
716 if (enable) {
717 // Enable the first choice.
718 enabled_experiments.insert(NameForChoice(*e, 0));
719 } else {
720 // Find the currently enabled choice and disable it.
721 for (int i = 0; i < e->num_choices; ++i) {
722 std::string choice_name = NameForChoice(*e, i);
723 if (enabled_experiments.find(choice_name) !=
724 enabled_experiments.end()) {
725 enabled_experiments.erase(choice_name);
726 // Continue on just in case there's a bug and more than one
727 // experiment for this choice was enabled.
728 }
729 }
730 }
731 }
[email protected]ad2a3ded2010-08-27 13:19:05732
[email protected]1a47d7e2010-10-15 00:37:24733 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05734}
735
[email protected]e2ddbc92010-10-15 20:02:07736void FlagsState::RemoveFlagsSwitches(
737 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53738 for (std::map<std::string, std::string>::const_iterator
739 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
740 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07741 }
742}
743
744void FlagsState::reset() {
745 needs_restart_ = false;
746 flags_switches_.clear();
747}
748
[email protected]38e46812011-05-09 20:49:22749} // namespace
[email protected]e2ddbc92010-10-15 20:02:07750
751namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19752
753// WARNING: '@' is also used in the html file. If you update this constant you
754// also need to update the html file.
755const char kMultiSeparator[] = "@";
756
[email protected]e2ddbc92010-10-15 20:02:07757void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55758 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07759}
[email protected]a314ee5a2010-10-26 21:23:28760
761void SetExperiments(const Experiment* e, size_t count) {
762 if (!e) {
763 experiments = kExperiments;
764 num_experiments = arraysize(kExperiments);
765 } else {
766 experiments = e;
767 num_experiments = count;
768 }
769}
770
[email protected]8a6ff28d2010-12-02 16:35:19771const Experiment* GetExperiments(size_t* count) {
772 *count = num_experiments;
773 return experiments;
774}
775
[email protected]e2ddbc92010-10-15 20:02:07776} // namespace testing
777
[email protected]1a47d7e2010-10-15 00:37:24778} // namespace about_flags