blob: 9f7c14419d3fe3ef2dc8b24ae5b617f7a1374f3f [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]ad2a3ded2010-08-27 13:19:0526
[email protected]1a47d7e2010-10-15 00:37:2427namespace about_flags {
[email protected]ad2a3ded2010-08-27 13:19:0528
[email protected]8a6ff28d2010-12-02 16:35:1929// Macros to simplify specifying the type.
[email protected]a82744532011-02-11 16:15:5330#define SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, switch_value) \
31 Experiment::SINGLE_VALUE, command_line_switch, switch_value, NULL, 0
32#define SINGLE_VALUE_TYPE(command_line_switch) \
33 SINGLE_VALUE_TYPE_AND_VALUE(command_line_switch, "")
34#define MULTI_VALUE_TYPE(choices) \
[email protected]0e6f56d2011-02-12 23:45:1535 Experiment::MULTI_VALUE, "", "", choices, arraysize(choices)
[email protected]8a6ff28d2010-12-02 16:35:1936
[email protected]e2ddbc92010-10-15 20:02:0737namespace {
38
[email protected]a314ee5a2010-10-26 21:23:2839const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;
[email protected]ad2a3ded2010-08-27 13:19:0540
[email protected]ba8164242010-11-16 21:31:0041// Names for former Chrome OS Labs experiments, shared with prefs migration
42// code.
43const char kMediaPlayerExperimentName[] = "media-player";
44const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
45const char kVerticalTabsExperimentName[] = "vertical-tabs";
46
[email protected]4bc5050c2010-11-18 17:55:5447// RECORDING USER METRICS FOR FLAGS:
48// -----------------------------------------------------------------------------
49// The first line of the experiment is the internal name. If you'd like to
50// gather statistics about the usage of your flag, you should append a marker
51// comment to the end of the feature name, like so:
52// "my-special-feature", // FLAGS:RECORD_UMA
53//
54// After doing that, run //chrome/tools/extract_actions.py (see instructions at
55// the top of that file for details) to update the chromeactions.txt file, which
56// will enable UMA to record your feature flag.
57//
58// After your feature has shipped under a flag, you can locate the metrics
59// under the action name AboutFlags_internal-action-name. Actions are recorded
60// once per startup, so you should divide this number by AboutFlags_StartupTick
61// to get a sense of usage. Note that this will not be the same as number of
62// users with a given feature enabled because users can quit and relaunch
63// the application multiple times over a given time interval.
64// TODO(rsesek): See if there's a way to count per-user, rather than
65// per-startup.
66
[email protected]8a6ff28d2010-12-02 16:35:1967// To add a new experiment add to the end of kExperiments. There are two
68// distinct types of experiments:
69// . SINGLE_VALUE: experiment is either on or off. Use the SINGLE_VALUE_TYPE
70// macro for this type supplying the command line to the macro.
[email protected]28e35af2011-02-09 12:56:2271// . MULTI_VALUE: a list of choices, the first of which should correspond to a
72// deactivated state for this lab (i.e. no command line option). To specify
73// this type of experiment use the macro MULTI_VALUE_TYPE supplying it the
74// array of choices.
[email protected]8a6ff28d2010-12-02 16:35:1975// See the documentation of Experiment for details on the fields.
76//
77// When adding a new choice, add it to the end of the list.
[email protected]ad2a3ded2010-08-27 13:19:0578const Experiment kExperiments[] = {
79 {
[email protected]aac169d2011-03-18 19:53:0380 "expose-for-tabs", // FLAGS:RECORD_UMA
81 IDS_FLAGS_TABPOSE_NAME,
82 IDS_FLAGS_TABPOSE_DESCRIPTION,
83 kOsMac,
84#if defined(OS_MACOSX)
85 // The switch exists only on OS X.
86 SINGLE_VALUE_TYPE(switches::kEnableExposeForTabs)
87#else
88 SINGLE_VALUE_TYPE("")
89#endif
90 },
91 {
[email protected]4bc5050c2010-11-18 17:55:5492 "vertical-tabs", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:1293 IDS_FLAGS_SIDE_TABS_NAME,
94 IDS_FLAGS_SIDE_TABS_DESCRIPTION,
[email protected]ba8164242010-11-16 21:31:0095 kOsWin | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:1996 SINGLE_VALUE_TYPE(switches::kEnableVerticalTabs)
[email protected]654151872010-09-13 22:43:0597 },
98 {
[email protected]4bc5050c2010-11-18 17:55:5499 "remoting", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12100 IDS_FLAGS_REMOTING_NAME,
[email protected]d99599862011-01-20 22:43:59101 IDS_FLAGS_REMOTING_DESCRIPTION,
102 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19103 SINGLE_VALUE_TYPE(switches::kEnableRemoting)
[email protected]52fa2d52010-09-25 14:08:56104 },
[email protected]a6f03652010-09-28 15:59:07105 {
[email protected]4bc5050c2010-11-18 17:55:54106 "conflicting-modules-check", // FLAGS:RECORD_UMA
[email protected]c1bbaa82010-11-08 11:17:05107 IDS_FLAGS_CONFLICTS_CHECK_NAME,
108 IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
109 kOsWin,
[email protected]8a6ff28d2010-12-02 16:35:19110 SINGLE_VALUE_TYPE(switches::kConflictingModulesCheck)
[email protected]c1bbaa82010-11-08 11:17:05111 },
112 {
[email protected]4bc5050c2010-11-18 17:55:54113 "cloud-print-proxy", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12114 IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
115 IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
[email protected]fa6d2a2f2010-11-30 21:47:19116#if defined(GOOGLE_CHROME_BUILD)
[email protected]9f8872b32011-03-04 19:44:45117 // For a Chrome build, we know we have a PDF plug-in on Windows, so it's
[email protected]be51dd252011-03-05 00:23:44118 // fully enabled. Linux still need some final polish.
119 kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19120#else
[email protected]be51dd252011-03-05 00:23:44121 // Otherwise, where we know Windows could be working if a viable PDF
122 // plug-in could be supplied, we'll keep the lab enabled. Mac always has
123 // PDF rasterization available, so no flag needed there.
124 kOsWin | kOsLinux,
[email protected]fa6d2a2f2010-11-30 21:47:19125#endif
[email protected]8a6ff28d2010-12-02 16:35:19126 SINGLE_VALUE_TYPE(switches::kEnableCloudPrintProxy)
[email protected]8b6588a2010-10-12 02:39:42127 },
[email protected]580939a2010-10-12 18:54:37128 {
[email protected]bb461532010-11-26 21:50:23129 "crxless-web-apps",
130 IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
131 IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
132 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19133 SINGLE_VALUE_TYPE(switches::kEnableCrxlessWebApps)
[email protected]bb461532010-11-26 21:50:23134 },
135 {
[email protected]96c6f4c2011-05-18 19:36:22136 "ignore-gpu-blacklist",
137 IDS_FLAGS_IGNORE_GPU_BLACKLIST_NAME,
138 IDS_FLAGS_IGNORE_GPU_BLACKLIST_DESCRIPTION,
139 kOsAll,
140 SINGLE_VALUE_TYPE(switches::kIgnoreGpuBlacklist)
141 },
142 {
143 "force-compositing-mode",
144 IDS_FLAGS_FORCE_COMPOSITING_MODE_NAME,
145 IDS_FLAGS_FORCE_COMPOSITING_MODE_DESCRIPTION,
146 kOsAll,
147 SINGLE_VALUE_TYPE(switches::kForceCompositingMode)
148 },
149 {
[email protected]5963b772011-02-09 22:55:38150 "composited-layer-borders",
151 IDS_FLAGS_COMPOSITED_LAYER_BORDERS,
152 IDS_FLAGS_COMPOSITED_LAYER_BORDERS_DESCRIPTION,
153 kOsAll,
154 SINGLE_VALUE_TYPE(switches::kShowCompositedLayerBorders)
155 },
156 {
[email protected]a8f1eaa2011-03-07 19:00:58157 "show-fps-counter",
158 IDS_FLAGS_SHOW_FPS_COUNTER,
159 IDS_FLAGS_SHOW_FPS_COUNTER_DESCRIPTION,
160 kOsAll,
161 SINGLE_VALUE_TYPE(switches::kShowFPSCounter)
162 },
163 {
[email protected]4bc5050c2010-11-18 17:55:54164 "gpu-canvas-2d", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12165 IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
166 IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
[email protected]1af1dfe2010-10-19 23:49:14167 kOsWin | kOsLinux | kOsCrOS,
[email protected]8a6ff28d2010-12-02 16:35:19168 SINGLE_VALUE_TYPE(switches::kEnableAccelerated2dCanvas)
[email protected]8d260e52010-10-13 01:03:05169 },
[email protected]38e46812011-05-09 20:49:22170#if !defined(GOOGLE_CHROME_BUILD)
171 // Only expose this for Chromium builds where users may not have the PDF
172 // plugin. Do not give Google Chrome users the option to disable it here.
[email protected]8d260e52010-10-13 01:03:05173 {
[email protected]4bc5050c2010-11-18 17:55:54174 "print-preview", // FLAGS:RECORD_UMA
[email protected]9486c1f2010-10-14 19:52:12175 IDS_FLAGS_PRINT_PREVIEW_NAME,
176 IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
[email protected]38e46812011-05-09 20:49:22177 kOsMac | kOsWin | kOsLinux, // This switch is not available in CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19178 SINGLE_VALUE_TYPE(switches::kEnablePrintPreview)
[email protected]2fe15fcb2010-10-21 20:39:53179 },
[email protected]38e46812011-05-09 20:49:22180#endif
[email protected]d208f4d82011-05-23 21:52:03181 // TODO(dspringer): When NaCl is on by default, remove this flag entry.
[email protected]2fe15fcb2010-10-21 20:39:53182 {
[email protected]d208f4d82011-05-23 21:52:03183 switches::kEnableNaCl, // FLAGS:RECORD_UMA
[email protected]4ff87d202010-11-06 01:28:40184 IDS_FLAGS_ENABLE_NACL_NAME,
185 IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
186 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19187 SINGLE_VALUE_TYPE(switches::kEnableNaCl)
[email protected]4ff87d202010-11-06 01:28:40188 },
189 {
[email protected]4bc5050c2010-11-18 17:55:54190 "dns-server", // FLAGS:RECORD_UMA
[email protected]2fe15fcb2010-10-21 20:39:53191 IDS_FLAGS_DNS_SERVER_NAME,
192 IDS_FLAGS_DNS_SERVER_DESCRIPTION,
193 kOsLinux,
[email protected]8a6ff28d2010-12-02 16:35:19194 SINGLE_VALUE_TYPE(switches::kDnsServer)
[email protected]2fe15fcb2010-10-21 20:39:53195 },
[email protected]177aceb2010-11-03 16:17:41196 {
[email protected]4bc5050c2010-11-18 17:55:54197 "extension-apis", // FLAGS:RECORD_UMA
[email protected]11dd68cd52010-11-12 01:15:32198 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
199 IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
200 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19201 SINGLE_VALUE_TYPE(switches::kEnableExperimentalExtensionApis)
[email protected]11dd68cd52010-11-12 01:15:32202 },
[email protected]3627b06d2010-11-12 16:36:16203 {
[email protected]4bc5050c2010-11-18 17:55:54204 "click-to-play", // FLAGS:RECORD_UMA
[email protected]3627b06d2010-11-12 16:36:16205 IDS_FLAGS_CLICK_TO_PLAY_NAME,
206 IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
207 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19208 SINGLE_VALUE_TYPE(switches::kEnableClickToPlay)
[email protected]3627b06d2010-11-12 16:36:16209 },
[email protected]80bd24e2010-11-30 09:34:38210 {
211 "disable-hyperlink-auditing",
212 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
213 IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
214 kOsAll,
[email protected]8a6ff28d2010-12-02 16:35:19215 SINGLE_VALUE_TYPE(switches::kNoPings)
[email protected]feb28fef2010-12-01 10:52:51216 },
217 {
218 "experimental-location-features", // FLAGS:RECORD_UMA
219 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_NAME,
220 IDS_FLAGS_EXPERIMENTAL_LOCATION_FEATURES_DESCRIPTION,
221 kOsMac | kOsWin | kOsLinux, // Currently does nothing on CrOS.
[email protected]8a6ff28d2010-12-02 16:35:19222 SINGLE_VALUE_TYPE(switches::kExperimentalLocationFeatures)
223 },
224 {
[email protected]4c6452d2011-01-12 08:47:57225 "block-reading-third-party-cookies",
226 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_NAME,
227 IDS_FLAGS_BLOCK_ALL_THIRD_PARTY_COOKIES_DESCRIPTION,
228 kOsAll,
229 SINGLE_VALUE_TYPE(switches::kBlockReadingThirdPartyCookies)
230 },
[email protected]04227962011-01-20 02:03:09231 {
232 "disable-interactive-form-validation",
233 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_NAME,
234 IDS_FLAGS_DISABLE_INTERACTIVE_FORM_VALIDATION_DESCRIPTION,
235 kOsAll,
236 SINGLE_VALUE_TYPE(switches::kDisableInteractiveFormValidation)
237 },
[email protected]8df51192011-01-22 20:05:03238 {
239 "webaudio",
240 IDS_FLAGS_WEBAUDIO_NAME,
241 IDS_FLAGS_WEBAUDIO_DESCRIPTION,
[email protected]54afa742011-04-29 21:21:34242// This switch is currently not available in CrOS.
243// TODO(crogers): FFmpeg Windows DLLs need to be rebuilt for chromium.
244#if defined(GOOGLE_CHROME_BUILD)
245 kOsMac | kOsWin | kOsLinux,
246#else
247 kOsMac | kOsLinux,
248#endif
[email protected]8df51192011-01-22 20:05:03249 SINGLE_VALUE_TYPE(switches::kEnableWebAudio)
250 },
[email protected]3828d6f2011-02-24 18:32:21251 {
[email protected]6c54e7e42011-03-02 20:52:34252 "p2papi",
253 IDS_FLAGS_P2P_API_NAME,
254 IDS_FLAGS_P2P_API_DESCRIPTION,
255 kOsAll,
[email protected]a026daa2011-04-20 15:49:51256#if defined(ENABLE_P2P_APIS)
[email protected]6c54e7e42011-03-02 20:52:34257 SINGLE_VALUE_TYPE(switches::kEnableP2PApi)
[email protected]a026daa2011-04-20 15:49:51258#else
259 SINGLE_VALUE_TYPE("")
260#endif
[email protected]6c54e7e42011-03-02 20:52:34261 },
[email protected]07d490bc2011-03-07 17:05:26262 {
263 "focus-existing-tab-on-open", // FLAGS:RECORD_UMA
264 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_NAME,
265 IDS_FLAGS_FOCUS_EXISTING_TAB_ON_OPEN_DESCRIPTION,
266 kOsAll,
267 SINGLE_VALUE_TYPE(switches::kFocusExistingTabOnOpen)
268 },
[email protected]d5dcfb32011-03-19 00:49:24269 {
[email protected]ac86c982011-05-11 13:26:05270 "compact-navigation",
271 IDS_FLAGS_ENABLE_COMPACT_NAVIGATION,
272 IDS_FLAGS_ENABLE_COMPACT_NAVIGATION_DESCRIPTION,
273 kOsWin, // TODO(stevet): Add other platforms when ready.
274 SINGLE_VALUE_TYPE(switches::kEnableCompactNavigation)
275 },
276 {
[email protected]d5dcfb32011-03-19 00:49:24277 "new-tab-page-4",
278 IDS_FLAGS_NEW_TAB_PAGE_4_NAME,
279 IDS_FLAGS_NEW_TAB_PAGE_4_DESCRIPTION,
280 kOsAll,
281 SINGLE_VALUE_TYPE(switches::kNewTabPage4)
282 },
[email protected]5aea1862011-03-23 23:55:39283 {
284 "tab-groups-context-menu",
285 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_NAME,
286 IDS_FLAGS_TAB_GROUPS_CONTEXT_MENU_DESCRIPTION,
287 kOsWin,
288 SINGLE_VALUE_TYPE(switches::kEnableTabGroupsContextMenu)
289 },
[email protected]e9bf9d92011-03-31 20:57:15290 {
291 "ppapi-flash-in-process",
292 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_NAME,
293 IDS_FLAGS_PPAPI_FLASH_IN_PROCESS_DESCRIPTION,
[email protected]f2d3ce0d2011-04-01 18:19:30294 kOsAll,
[email protected]e9bf9d92011-03-31 20:57:15295 SINGLE_VALUE_TYPE(switches::kPpapiFlashInProcess)
296 },
[email protected]ce5737142011-04-14 23:35:09297 {
[email protected]c3647902011-04-15 06:40:33298 "enable-vpn",
299 IDS_FLAGS_ENABLE_VPN_NAME,
300 IDS_FLAGS_ENABLE_VPN_DESCRIPTION,
301 kOsCrOS,
302#if defined(OS_CHROMEOS)
303 // The switch exists only on Chrome OS.
304 SINGLE_VALUE_TYPE(switches::kEnableVPN)
305#else
306 SINGLE_VALUE_TYPE("")
307#endif
308 },
[email protected]20b5df962011-04-18 22:07:43309 {
310 "multi-profiles",
311 IDS_FLAGS_MULTI_PROFILES_NAME,
312 IDS_FLAGS_MULTI_PROFILES_DESCRIPTION,
313 kOsAll,
314 SINGLE_VALUE_TYPE(switches::kMultiProfiles)
315 },
[email protected]cc057b02011-04-21 17:19:04316 {
[email protected]08338b862011-05-05 20:22:23317 "restrict-instant-to-search",
318 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_NAME,
319 IDS_FLAGS_RESTRICT_INSTANT_TO_SEARCH_DESCRIPTION,
320 kOsAll,
321 SINGLE_VALUE_TYPE(switches::kRestrictInstantToSearch)
322 },
[email protected]b652fc82011-05-23 07:53:05323 {
324 "indexeddb-use-leveldb", // FLAGS:RECORD_UMA
325 IDS_FLAGS_INDEXEDDB_USE_LEVELDB_NAME,
326 IDS_FLAGS_INDEXEDDB_USE_LEVELDB_DESCRIPTION,
327 kOsAll,
328 SINGLE_VALUE_TYPE(switches::kLevelDBIndexedDatabase)
329 },
[email protected]ad2a3ded2010-08-27 13:19:05330};
331
[email protected]a314ee5a2010-10-26 21:23:28332const Experiment* experiments = kExperiments;
333size_t num_experiments = arraysize(kExperiments);
334
[email protected]e2ddbc92010-10-15 20:02:07335// Stores and encapsulates the little state that about:flags has.
336class FlagsState {
337 public:
338 FlagsState() : needs_restart_(false) {}
339 void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
340 bool IsRestartNeededToCommitChanges();
341 void SetExperimentEnabled(
[email protected]a314ee5a2010-10-26 21:23:28342 PrefService* prefs, const std::string& internal_name, bool enable);
[email protected]e2ddbc92010-10-15 20:02:07343 void RemoveFlagsSwitches(
344 std::map<std::string, CommandLine::StringType>* switch_list);
345 void reset();
346
347 // Returns the singleton instance of this class
[email protected]8e8bb6d2010-12-13 08:18:55348 static FlagsState* GetInstance() {
[email protected]e2ddbc92010-10-15 20:02:07349 return Singleton<FlagsState>::get();
350 }
351
352 private:
353 bool needs_restart_;
[email protected]a82744532011-02-11 16:15:53354 std::map<std::string, std::string> flags_switches_;
[email protected]e2ddbc92010-10-15 20:02:07355
356 DISALLOW_COPY_AND_ASSIGN(FlagsState);
357};
358
[email protected]c7b7800a2010-10-07 18:51:35359// Extracts the list of enabled lab experiments from preferences and stores them
[email protected]ad2a3ded2010-08-27 13:19:05360// in a set.
[email protected]1a47d7e2010-10-15 00:37:24361void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
[email protected]ad2a3ded2010-08-27 13:19:05362 const ListValue* enabled_experiments = prefs->GetList(
363 prefs::kEnabledLabsExperiments);
364 if (!enabled_experiments)
365 return;
366
367 for (ListValue::const_iterator it = enabled_experiments->begin();
368 it != enabled_experiments->end();
369 ++it) {
370 std::string experiment_name;
371 if (!(*it)->GetAsString(&experiment_name)) {
372 LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
373 continue;
374 }
375 result->insert(experiment_name);
376 }
377}
378
379// Takes a set of enabled lab experiments
[email protected]1a47d7e2010-10-15 00:37:24380void SetEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05381 PrefService* prefs, const std::set<std::string>& enabled_experiments) {
[email protected]1bc78422011-03-31 08:41:38382 ListPrefUpdate update(prefs, prefs::kEnabledLabsExperiments);
383 ListValue* experiments_list = update.Get();
[email protected]ad2a3ded2010-08-27 13:19:05384
385 experiments_list->Clear();
386 for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
387 it != enabled_experiments.end();
388 ++it) {
389 experiments_list->Append(new StringValue(*it));
390 }
391}
392
[email protected]8a6ff28d2010-12-02 16:35:19393// Returns the name used in prefs for the choice at the specified index.
394std::string NameForChoice(const Experiment& e, int index) {
[email protected]2ce9c89752011-02-25 18:24:34395 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
396 DCHECK_LT(index, e.num_choices);
[email protected]8a6ff28d2010-12-02 16:35:19397 return std::string(e.internal_name) + about_flags::testing::kMultiSeparator +
398 base::IntToString(index);
399}
400
401// Adds the internal names for the specified experiment to |names|.
402void AddInternalName(const Experiment& e, std::set<std::string>* names) {
403 if (e.type == Experiment::SINGLE_VALUE) {
404 names->insert(e.internal_name);
405 } else {
[email protected]2ce9c89752011-02-25 18:24:34406 DCHECK_EQ(Experiment::MULTI_VALUE, e.type);
[email protected]8a6ff28d2010-12-02 16:35:19407 for (int i = 0; i < e.num_choices; ++i)
408 names->insert(NameForChoice(e, i));
409 }
410}
411
[email protected]28e35af2011-02-09 12:56:22412// Confirms that an experiment is valid, used in a DCHECK in
413// SanitizeList below.
414bool ValidateExperiment(const Experiment& e) {
415 switch (e.type) {
416 case Experiment::SINGLE_VALUE:
417 DCHECK_EQ(0, e.num_choices);
418 DCHECK(!e.choices);
419 break;
420 case Experiment::MULTI_VALUE:
421 DCHECK_GT(e.num_choices, 0);
422 DCHECK(e.choices);
[email protected]a82744532011-02-11 16:15:53423 DCHECK(e.choices[0].command_line_switch);
424 DCHECK_EQ('\0', e.choices[0].command_line_switch[0]);
[email protected]28e35af2011-02-09 12:56:22425 break;
426 default:
427 NOTREACHED();
428 }
429 return true;
430}
431
[email protected]ad2a3ded2010-08-27 13:19:05432// Removes all experiments from prefs::kEnabledLabsExperiments that are
433// unknown, to prevent this list to become very long as experiments are added
434// and removed.
435void SanitizeList(PrefService* prefs) {
436 std::set<std::string> known_experiments;
[email protected]28e35af2011-02-09 12:56:22437 for (size_t i = 0; i < num_experiments; ++i) {
438 DCHECK(ValidateExperiment(experiments[i]));
[email protected]8a6ff28d2010-12-02 16:35:19439 AddInternalName(experiments[i], &known_experiments);
[email protected]28e35af2011-02-09 12:56:22440 }
[email protected]ad2a3ded2010-08-27 13:19:05441
442 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24443 GetEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05444
445 std::set<std::string> new_enabled_experiments;
446 std::set_intersection(
447 known_experiments.begin(), known_experiments.end(),
448 enabled_experiments.begin(), enabled_experiments.end(),
449 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
450
[email protected]1a47d7e2010-10-15 00:37:24451 SetEnabledFlags(prefs, new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05452}
453
[email protected]1a47d7e2010-10-15 00:37:24454void GetSanitizedEnabledFlags(
[email protected]ad2a3ded2010-08-27 13:19:05455 PrefService* prefs, std::set<std::string>* result) {
456 SanitizeList(prefs);
[email protected]1a47d7e2010-10-15 00:37:24457 GetEnabledFlags(prefs, result);
[email protected]ad2a3ded2010-08-27 13:19:05458}
459
[email protected]a314ee5a2010-10-26 21:23:28460// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
461// enabled on the current platform.
462void GetSanitizedEnabledFlagsForCurrentPlatform(
463 PrefService* prefs, std::set<std::string>* result) {
464 GetSanitizedEnabledFlags(prefs, result);
465
466 // Filter out any experiments that aren't enabled on the current platform. We
467 // don't remove these from prefs else syncing to a platform with a different
468 // set of experiments would be lossy.
469 std::set<std::string> platform_experiments;
470 int current_platform = GetCurrentPlatform();
471 for (size_t i = 0; i < num_experiments; ++i) {
472 if (experiments[i].supported_platforms & current_platform)
[email protected]8a6ff28d2010-12-02 16:35:19473 AddInternalName(experiments[i], &platform_experiments);
[email protected]a314ee5a2010-10-26 21:23:28474 }
475
476 std::set<std::string> new_enabled_experiments;
477 std::set_intersection(
478 platform_experiments.begin(), platform_experiments.end(),
479 result->begin(), result->end(),
480 std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));
481
482 result->swap(new_enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05483}
484
[email protected]8a6ff28d2010-12-02 16:35:19485// Returns the Value representing the choice data in the specified experiment.
[email protected]8a6ff28d2010-12-02 16:35:19486Value* CreateChoiceData(const Experiment& experiment,
[email protected]28e35af2011-02-09 12:56:22487 const std::set<std::string>& enabled_experiments) {
[email protected]2ce9c89752011-02-25 18:24:34488 DCHECK_EQ(Experiment::MULTI_VALUE, experiment.type);
[email protected]8a6ff28d2010-12-02 16:35:19489 ListValue* result = new ListValue;
490 for (int i = 0; i < experiment.num_choices; ++i) {
491 const Experiment::Choice& choice = experiment.choices[i];
492 DictionaryValue* value = new DictionaryValue;
493 std::string name = NameForChoice(experiment, i);
494 value->SetString("description",
495 l10n_util::GetStringUTF16(choice.description_id));
496 value->SetString("internal_name", name);
[email protected]28e35af2011-02-09 12:56:22497 value->SetBoolean("selected", enabled_experiments.count(name) > 0);
[email protected]8a6ff28d2010-12-02 16:35:19498 result->Append(value);
499 }
500 return result;
501}
502
[email protected]e2ddbc92010-10-15 20:02:07503} // namespace
504
[email protected]1a47d7e2010-10-15 00:37:24505void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
[email protected]8e8bb6d2010-12-13 08:18:55506 FlagsState::GetInstance()->ConvertFlagsToSwitches(prefs, command_line);
[email protected]ad2a3ded2010-08-27 13:19:05507}
508
[email protected]1a47d7e2010-10-15 00:37:24509ListValue* GetFlagsExperimentsData(PrefService* prefs) {
[email protected]ad2a3ded2010-08-27 13:19:05510 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24511 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05512
513 int current_platform = GetCurrentPlatform();
514
515 ListValue* experiments_data = new ListValue();
[email protected]a314ee5a2010-10-26 21:23:28516 for (size_t i = 0; i < num_experiments; ++i) {
517 const Experiment& experiment = experiments[i];
[email protected]ad2a3ded2010-08-27 13:19:05518 if (!(experiment.supported_platforms & current_platform))
519 continue;
520
521 DictionaryValue* data = new DictionaryValue();
522 data->SetString("internal_name", experiment.internal_name);
523 data->SetString("name",
524 l10n_util::GetStringUTF16(experiment.visible_name_id));
525 data->SetString("description",
526 l10n_util::GetStringUTF16(
527 experiment.visible_description_id));
[email protected]ad2a3ded2010-08-27 13:19:05528
[email protected]28e35af2011-02-09 12:56:22529 switch (experiment.type) {
530 case Experiment::SINGLE_VALUE:
531 data->SetBoolean(
532 "enabled",
533 enabled_experiments.count(experiment.internal_name) > 0);
534 break;
535 case Experiment::MULTI_VALUE:
536 data->Set("choices", CreateChoiceData(experiment, enabled_experiments));
537 break;
538 default:
539 NOTREACHED();
[email protected]8a6ff28d2010-12-02 16:35:19540 }
541
[email protected]ad2a3ded2010-08-27 13:19:05542 experiments_data->Append(data);
543 }
544 return experiments_data;
545}
546
[email protected]ad2a3ded2010-08-27 13:19:05547bool IsRestartNeededToCommitChanges() {
[email protected]8e8bb6d2010-12-13 08:18:55548 return FlagsState::GetInstance()->IsRestartNeededToCommitChanges();
[email protected]ad2a3ded2010-08-27 13:19:05549}
550
551void SetExperimentEnabled(
[email protected]c7b7800a2010-10-07 18:51:35552 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]8e8bb6d2010-12-13 08:18:55553 FlagsState::GetInstance()->SetExperimentEnabled(prefs, internal_name, enable);
[email protected]e2ddbc92010-10-15 20:02:07554}
555
556void RemoveFlagsSwitches(
557 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]8e8bb6d2010-12-13 08:18:55558 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list);
[email protected]e2ddbc92010-10-15 20:02:07559}
560
[email protected]a314ee5a2010-10-26 21:23:28561int GetCurrentPlatform() {
562#if defined(OS_MACOSX)
563 return kOsMac;
564#elif defined(OS_WIN)
565 return kOsWin;
566#elif defined(OS_CHROMEOS) // Needs to be before the OS_LINUX check.
567 return kOsCrOS;
568#elif defined(OS_LINUX)
569 return kOsLinux;
570#else
571#error Unknown platform
572#endif
573}
574
[email protected]4bc5050c2010-11-18 17:55:54575void RecordUMAStatistics(const PrefService* prefs) {
576 std::set<std::string> flags;
577 GetEnabledFlags(prefs, &flags);
578 for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
579 ++it) {
580 std::string action("AboutFlags_");
581 action += *it;
582 UserMetrics::RecordComputedAction(action);
583 }
584 // Since flag metrics are recorded every startup, add a tick so that the
585 // stats can be made meaningful.
586 if (flags.size())
587 UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
[email protected]970b2fd2011-01-22 18:06:45588 UserMetrics::RecordAction(UserMetricsAction("StartupTick"));
[email protected]4bc5050c2010-11-18 17:55:54589}
590
[email protected]e2ddbc92010-10-15 20:02:07591//////////////////////////////////////////////////////////////////////////////
592// FlagsState implementation.
593
594namespace {
595
596void FlagsState::ConvertFlagsToSwitches(
597 PrefService* prefs, CommandLine* command_line) {
[email protected]e2ddbc92010-10-15 20:02:07598 if (command_line->HasSwitch(switches::kNoExperiments))
599 return;
600
601 std::set<std::string> enabled_experiments;
[email protected]ba8164242010-11-16 21:31:00602
[email protected]a314ee5a2010-10-26 21:23:28603 GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);
[email protected]e2ddbc92010-10-15 20:02:07604
[email protected]a82744532011-02-11 16:15:53605 typedef std::map<std::string, std::pair<std::string, std::string> >
606 NameToSwitchAndValueMap;
607 NameToSwitchAndValueMap name_to_switch_map;
[email protected]8a6ff28d2010-12-02 16:35:19608 for (size_t i = 0; i < num_experiments; ++i) {
609 const Experiment& e = experiments[i];
610 if (e.type == Experiment::SINGLE_VALUE) {
[email protected]a82744532011-02-11 16:15:53611 name_to_switch_map[e.internal_name] =
612 std::pair<std::string, std::string>(e.command_line_switch,
613 e.command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19614 } else {
615 for (int j = 0; j < e.num_choices; ++j)
[email protected]a82744532011-02-11 16:15:53616 name_to_switch_map[NameForChoice(e, j)] =
617 std::pair<std::string, std::string>(
618 e.choices[j].command_line_switch,
619 e.choices[j].command_line_value);
[email protected]8a6ff28d2010-12-02 16:35:19620 }
621 }
[email protected]e2ddbc92010-10-15 20:02:07622
623 command_line->AppendSwitch(switches::kFlagSwitchesBegin);
[email protected]a82744532011-02-11 16:15:53624 flags_switches_.insert(
625 std::pair<std::string, std::string>(switches::kFlagSwitchesBegin,
626 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07627 for (std::set<std::string>::iterator it = enabled_experiments.begin();
628 it != enabled_experiments.end();
629 ++it) {
630 const std::string& experiment_name = *it;
[email protected]a82744532011-02-11 16:15:53631 NameToSwitchAndValueMap::const_iterator name_to_switch_it =
[email protected]8a6ff28d2010-12-02 16:35:19632 name_to_switch_map.find(experiment_name);
633 if (name_to_switch_it == name_to_switch_map.end()) {
634 NOTREACHED();
[email protected]e2ddbc92010-10-15 20:02:07635 continue;
[email protected]8a6ff28d2010-12-02 16:35:19636 }
[email protected]e2ddbc92010-10-15 20:02:07637
[email protected]a82744532011-02-11 16:15:53638 const std::pair<std::string, std::string>&
639 switch_and_value_pair = name_to_switch_it->second;
640
641 command_line->AppendSwitchASCII(switch_and_value_pair.first,
642 switch_and_value_pair.second);
643 flags_switches_[switch_and_value_pair.first] = switch_and_value_pair.second;
[email protected]e2ddbc92010-10-15 20:02:07644 }
645 command_line->AppendSwitch(switches::kFlagSwitchesEnd);
[email protected]a82744532011-02-11 16:15:53646 flags_switches_.insert(
647 std::pair<std::string, std::string>(switches::kFlagSwitchesEnd,
648 std::string()));
[email protected]e2ddbc92010-10-15 20:02:07649}
650
651bool FlagsState::IsRestartNeededToCommitChanges() {
652 return needs_restart_;
653}
654
655void FlagsState::SetExperimentEnabled(
656 PrefService* prefs, const std::string& internal_name, bool enable) {
[email protected]ad2a3ded2010-08-27 13:19:05657 needs_restart_ = true;
658
[email protected]8a6ff28d2010-12-02 16:35:19659 size_t at_index = internal_name.find(about_flags::testing::kMultiSeparator);
660 if (at_index != std::string::npos) {
661 DCHECK(enable);
662 // We're being asked to enable a multi-choice experiment. Disable the
663 // currently selected choice.
664 DCHECK_NE(at_index, 0u);
[email protected]28e35af2011-02-09 12:56:22665 const std::string experiment_name = internal_name.substr(0, at_index);
666 SetExperimentEnabled(prefs, experiment_name, false);
[email protected]8a6ff28d2010-12-02 16:35:19667
[email protected]28e35af2011-02-09 12:56:22668 // And enable the new choice, if it is not the default first choice.
669 if (internal_name != experiment_name + "@0") {
670 std::set<std::string> enabled_experiments;
671 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
672 enabled_experiments.insert(internal_name);
673 SetEnabledFlags(prefs, enabled_experiments);
674 }
[email protected]8a6ff28d2010-12-02 16:35:19675 return;
676 }
677
[email protected]ad2a3ded2010-08-27 13:19:05678 std::set<std::string> enabled_experiments;
[email protected]1a47d7e2010-10-15 00:37:24679 GetSanitizedEnabledFlags(prefs, &enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05680
[email protected]8a6ff28d2010-12-02 16:35:19681 const Experiment* e = NULL;
682 for (size_t i = 0; i < num_experiments; ++i) {
683 if (experiments[i].internal_name == internal_name) {
684 e = experiments + i;
685 break;
686 }
687 }
688 DCHECK(e);
689
690 if (e->type == Experiment::SINGLE_VALUE) {
[email protected]d208f4d82011-05-23 21:52:03691 if (enable) {
[email protected]8a6ff28d2010-12-02 16:35:19692 enabled_experiments.insert(internal_name);
[email protected]d208f4d82011-05-23 21:52:03693 // If enabling NaCl, make sure the plugin is also enabled. See bug
694 // http://code.google.com/p/chromium/issues/detail?id=81010 for more
695 // information.
696 // TODO(dspringer): When NaCl is on by default, remove this code.
697 if (internal_name == switches::kEnableNaCl) {
698 PluginUpdater* plugin_updater = PluginUpdater::GetInstance();
699 string16 nacl_plugin_name =
700 ASCIIToUTF16(chrome::ChromeContentClient::kNaClPluginName);
701 plugin_updater->EnablePluginGroup(true, nacl_plugin_name);
702 }
703 } else {
[email protected]8a6ff28d2010-12-02 16:35:19704 enabled_experiments.erase(internal_name);
[email protected]d208f4d82011-05-23 21:52:03705 }
[email protected]8a6ff28d2010-12-02 16:35:19706 } else {
707 if (enable) {
708 // Enable the first choice.
709 enabled_experiments.insert(NameForChoice(*e, 0));
710 } else {
711 // Find the currently enabled choice and disable it.
712 for (int i = 0; i < e->num_choices; ++i) {
713 std::string choice_name = NameForChoice(*e, i);
714 if (enabled_experiments.find(choice_name) !=
715 enabled_experiments.end()) {
716 enabled_experiments.erase(choice_name);
717 // Continue on just in case there's a bug and more than one
718 // experiment for this choice was enabled.
719 }
720 }
721 }
722 }
[email protected]ad2a3ded2010-08-27 13:19:05723
[email protected]1a47d7e2010-10-15 00:37:24724 SetEnabledFlags(prefs, enabled_experiments);
[email protected]ad2a3ded2010-08-27 13:19:05725}
726
[email protected]e2ddbc92010-10-15 20:02:07727void FlagsState::RemoveFlagsSwitches(
728 std::map<std::string, CommandLine::StringType>* switch_list) {
[email protected]a82744532011-02-11 16:15:53729 for (std::map<std::string, std::string>::const_iterator
730 it = flags_switches_.begin(); it != flags_switches_.end(); ++it) {
731 switch_list->erase(it->first);
[email protected]e2ddbc92010-10-15 20:02:07732 }
733}
734
735void FlagsState::reset() {
736 needs_restart_ = false;
737 flags_switches_.clear();
738}
739
[email protected]38e46812011-05-09 20:49:22740} // namespace
[email protected]e2ddbc92010-10-15 20:02:07741
742namespace testing {
[email protected]8a6ff28d2010-12-02 16:35:19743
744// WARNING: '@' is also used in the html file. If you update this constant you
745// also need to update the html file.
746const char kMultiSeparator[] = "@";
747
[email protected]e2ddbc92010-10-15 20:02:07748void ClearState() {
[email protected]8e8bb6d2010-12-13 08:18:55749 FlagsState::GetInstance()->reset();
[email protected]e2ddbc92010-10-15 20:02:07750}
[email protected]a314ee5a2010-10-26 21:23:28751
752void SetExperiments(const Experiment* e, size_t count) {
753 if (!e) {
754 experiments = kExperiments;
755 num_experiments = arraysize(kExperiments);
756 } else {
757 experiments = e;
758 num_experiments = count;
759 }
760}
761
[email protected]8a6ff28d2010-12-02 16:35:19762const Experiment* GetExperiments(size_t* count) {
763 *count = num_experiments;
764 return experiments;
765}
766
[email protected]e2ddbc92010-10-15 20:02:07767} // namespace testing
768
[email protected]1a47d7e2010-10-15 00:37:24769} // namespace about_flags