summaryrefslogtreecommitdiffstats
path: root/chrome/browser/about_flags.cc
blob: e5995634a41427d5a8650c166db202646ba9f1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/about_flags.h"

#include <algorithm>
#include <iterator>
#include <map>
#include <set>

#include "app/l10n_util.h"
#include "base/command_line.h"
#include "base/singleton.h"
#include "base/values.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "grit/generated_resources.h"

namespace about_flags {

namespace {

const unsigned kOsAll = kOsMac | kOsWin | kOsLinux | kOsCrOS;

// Names for former Chrome OS Labs experiments, shared with prefs migration
// code.
const char kMediaPlayerExperimentName[] = "media-player";
const char kAdvancedFileSystemExperimentName[] = "advanced-file-system";
const char kVerticalTabsExperimentName[] = "vertical-tabs";

// RECORDING USER METRICS FOR FLAGS:
// -----------------------------------------------------------------------------
// The first line of the experiment is the internal name. If you'd like to
// gather statistics about the usage of your flag, you should append a marker
// comment to the end of the feature name, like so:
//   "my-special-feature",  // FLAGS:RECORD_UMA
//
// After doing that, run //chrome/tools/extract_actions.py (see instructions at
// the top of that file for details) to update the chromeactions.txt file, which
// will enable UMA to record your feature flag.
//
// After your feature has shipped under a flag, you can locate the metrics
// under the action name AboutFlags_internal-action-name. Actions are recorded
// once per startup, so you should divide this number by AboutFlags_StartupTick
// to get a sense of usage. Note that this will not be the same as number of
// users with a given feature enabled because users can quit and relaunch
// the application multiple times over a given time interval.
// TODO(rsesek): See if there's a way to count per-user, rather than
// per-startup.

const Experiment kExperiments[] = {
  {
    "expose-for-tabs",  // FLAGS:RECORD_UMA
    IDS_FLAGS_TABPOSE_NAME,
    IDS_FLAGS_TABPOSE_DESCRIPTION,
    kOsMac,
#if defined(OS_MACOSX)
    // The switch exists only on OS X.
    switches::kEnableExposeForTabs
#else
    ""
#endif
  },
  {
    kMediaPlayerExperimentName,
    IDS_FLAGS_MEDIA_PLAYER_NAME,
    IDS_FLAGS_MEDIA_PLAYER_DESCRIPTION,
    kOsCrOS,
#if defined(OS_CHROMEOS)
    // The switch exists only on Chrome OS.
    switches::kEnableMediaPlayer
#else
    ""
#endif
  },
  {
    kAdvancedFileSystemExperimentName,
    IDS_FLAGS_ADVANCED_FS_NAME,
    IDS_FLAGS_ADVANCED_FS_DESCRIPTION,
    kOsCrOS,
#if defined(OS_CHROMEOS)
    // The switch exists only on Chrome OS.
    switches::kEnableAdvancedFileSystem
#else
    ""
#endif
  },
  {
    "vertical-tabs",  // FLAGS:RECORD_UMA
    IDS_FLAGS_SIDE_TABS_NAME,
    IDS_FLAGS_SIDE_TABS_DESCRIPTION,
    kOsWin | kOsCrOS,
    switches::kEnableVerticalTabs
  },
  {
    "tabbed-options",  // FLAGS:RECORD_UMA
    IDS_FLAGS_TABBED_OPTIONS_NAME,
    IDS_FLAGS_TABBED_OPTIONS_DESCRIPTION,
    kOsWin | kOsLinux | kOsMac,  // Enabled by default on CrOS.
    switches::kEnableTabbedOptions
  },
  {
    "remoting",  // FLAGS:RECORD_UMA
    IDS_FLAGS_REMOTING_NAME,
#if defined(OS_WIN)
    // Windows only supports host functionality at the moment.
    IDS_FLAGS_REMOTING_HOST_DESCRIPTION,
#elif defined(OS_LINUX)  // Also true for CrOS.
    // Linux only supports client functionality at the moment.
    IDS_FLAGS_REMOTING_CLIENT_DESCRIPTION,
#else
    // On other platforms, this lab isn't available at all.
    0,
#endif
    kOsWin | kOsLinux | kOsCrOS,
    switches::kEnableRemoting
  },
  {
    "disable-outdated-plugins",  // FLAGS:RECORD_UMA
    IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_NAME,
    IDS_FLAGS_DISABLE_OUTDATED_PLUGINS_DESCRIPTION,
    kOsAll,
    switches::kDisableOutdatedPlugins
  },
  {
    "xss-auditor",  // FLAGS:RECORD_UMA
    IDS_FLAGS_XSS_AUDITOR_NAME,
    IDS_FLAGS_XSS_AUDITOR_DESCRIPTION,
    kOsAll,
    switches::kEnableXSSAuditor
  },
  {
    "background-webapps",  // FLAGS:RECORD_UMA
    IDS_FLAGS_BACKGROUND_WEBAPPS_NAME,
    IDS_FLAGS_BACKGROUND_WEBAPPS_DESCRIPTION,
    kOsMac | kOsLinux | kOsCrOS,  // Enabled by default on windows
    switches::kEnableBackgroundMode
  },
  {
    "conflicting-modules-check",  // FLAGS:RECORD_UMA
    IDS_FLAGS_CONFLICTS_CHECK_NAME,
    IDS_FLAGS_CONFLICTS_CHECK_DESCRIPTION,
    kOsWin,
    switches::kConflictingModulesCheck
  },
  {
    "cloud-print-proxy",  // FLAGS:RECORD_UMA
    IDS_FLAGS_CLOUD_PRINT_PROXY_NAME,
    IDS_FLAGS_CLOUD_PRINT_PROXY_DESCRIPTION,
    kOsWin,
    switches::kEnableCloudPrintProxy
  },
  {
    "crxless-web-apps",
    IDS_FLAGS_CRXLESS_WEB_APPS_NAME,
    IDS_FLAGS_CRXLESS_WEB_APPS_DESCRIPTION,
    kOsAll,
    switches::kEnableCrxlessWebApps
  },
  {
    "match-preview",  // FLAGS:RECORD_UMA
    IDS_FLAGS_PREDICTIVE_INSTANT_NAME,
    IDS_FLAGS_PREDICTIVE_INSTANT_DESCRIPTION,
    kOsMac,
    switches::kEnablePredictiveInstant
  },
  // FIXME(scheib): Add Flags entry for accelerated Compositing,
  // or pull it and the strings in generated_resources.grd by Dec 2010
  // {
  //   "gpu-compositing",
  //   IDS_FLAGS_ACCELERATED_COMPOSITING_NAME,
  //   IDS_FLAGS_ACCELERATED_COMPOSITING_DESCRIPTION,
  //   kOsAll,
  //   switches::kDisableAcceleratedCompositing
  // },
  {
    "gpu-canvas-2d",  // FLAGS:RECORD_UMA
    IDS_FLAGS_ACCELERATED_CANVAS_2D_NAME,
    IDS_FLAGS_ACCELERATED_CANVAS_2D_DESCRIPTION,
    kOsWin | kOsLinux | kOsCrOS,
    switches::kEnableAccelerated2dCanvas
  },
  // FIXME(scheib): Add Flags entry for WebGL,
  // or pull it and the strings in generated_resources.grd by Dec 2010
  // {
  //   "webgl",
  //   IDS_FLAGS_WEBGL_NAME,
  //   IDS_FLAGS_WEBGL_DESCRIPTION,
  //   kOsAll,
  //   switches::kDisableExperimentalWebGL
  // }
  {
    "print-preview",  // FLAGS:RECORD_UMA
    IDS_FLAGS_PRINT_PREVIEW_NAME,
    IDS_FLAGS_PRINT_PREVIEW_DESCRIPTION,
    kOsAll,
    switches::kEnablePrintPreview
  },
  {
    "enable-nacl",  // FLAGS:RECORD_UMA
    IDS_FLAGS_ENABLE_NACL_NAME,
    IDS_FLAGS_ENABLE_NACL_DESCRIPTION,
    kOsAll,
    switches::kEnableNaCl
  },
  {
    "dns-server",  // FLAGS:RECORD_UMA
    IDS_FLAGS_DNS_SERVER_NAME,
    IDS_FLAGS_DNS_SERVER_DESCRIPTION,
    kOsLinux,
    switches::kDnsServer
  },
  {
    "page-prerender",  // FLAGS:RECORD_UMA
    IDS_FLAGS_PAGE_PRERENDER_NAME,
    IDS_FLAGS_PAGE_PRERENDER_DESCRIPTION,
    kOsAll,
    switches::kEnablePagePrerender
  },
  {
    "confirm-to-quit",  // FLAGS:RECORD_UMA
    IDS_FLAGS_CONFIRM_TO_QUIT_NAME,
    IDS_FLAGS_CONFIRM_TO_QUIT_DESCRIPTION,
    kOsMac,
    switches::kEnableConfirmToQuit
  },
  {
    "extension-apis",  // FLAGS:RECORD_UMA
    IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_NAME,
    IDS_FLAGS_EXPERIMENTAL_EXTENSION_APIS_DESCRIPTION,
    kOsAll,
    switches::kEnableExperimentalExtensionApis
  },
  {
    "click-to-play",  // FLAGS:RECORD_UMA
    IDS_FLAGS_CLICK_TO_PLAY_NAME,
    IDS_FLAGS_CLICK_TO_PLAY_DESCRIPTION,
    kOsAll,
    switches::kEnableClickToPlay
  },
  {
    "disable-hyperlink-auditing",
    IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_NAME,
    IDS_FLAGS_DISABLE_HYPERLINK_AUDITING_DESCRIPTION,
    kOsAll,
    switches::kNoPings
  }
};

const Experiment* experiments = kExperiments;
size_t num_experiments = arraysize(kExperiments);

// Stores and encapsulates the little state that about:flags has.
class FlagsState {
 public:
  FlagsState() : needs_restart_(false) {}
  void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line);
  bool IsRestartNeededToCommitChanges();
  void SetExperimentEnabled(
      PrefService* prefs, const std::string& internal_name, bool enable);
  void RemoveFlagsSwitches(
      std::map<std::string, CommandLine::StringType>* switch_list);
  void reset();

  // Returns the singleton instance of this class
  static FlagsState* instance() {
    return Singleton<FlagsState>::get();
  }

 private:
  bool needs_restart_;
  std::set<std::string> flags_switches_;

  DISALLOW_COPY_AND_ASSIGN(FlagsState);
};

#if defined(OS_CHROMEOS)
// Migrates Chrome OS Labs settings to experiments adding flags to enabled
// experiment list if the corresponding pref is on.
void MigrateChromeOSLabsPrefs(PrefService* prefs,
                              std::set<std::string>* result) {
  DCHECK(prefs);
  DCHECK(result);
  if (prefs->GetBoolean(prefs::kLabsMediaplayerEnabled))
    result->insert(kMediaPlayerExperimentName);
  if (prefs->GetBoolean(prefs::kLabsAdvancedFilesystemEnabled))
    result->insert(kAdvancedFileSystemExperimentName);
  if (prefs->GetBoolean(prefs::kUseVerticalTabs))
    result->insert(kVerticalTabsExperimentName);
  prefs->SetBoolean(prefs::kLabsMediaplayerEnabled, false);
  prefs->SetBoolean(prefs::kLabsAdvancedFilesystemEnabled, false);
  prefs->SetBoolean(prefs::kUseVerticalTabs, false);
}
#endif

// Extracts the list of enabled lab experiments from preferences and stores them
// in a set.
void GetEnabledFlags(const PrefService* prefs, std::set<std::string>* result) {
  const ListValue* enabled_experiments = prefs->GetList(
      prefs::kEnabledLabsExperiments);
  if (!enabled_experiments)
    return;

  for (ListValue::const_iterator it = enabled_experiments->begin();
       it != enabled_experiments->end();
       ++it) {
    std::string experiment_name;
    if (!(*it)->GetAsString(&experiment_name)) {
      LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
      continue;
    }
    result->insert(experiment_name);
  }
}

// Takes a set of enabled lab experiments
void SetEnabledFlags(
    PrefService* prefs, const std::set<std::string>& enabled_experiments) {
  ListValue* experiments_list = prefs->GetMutableList(
      prefs::kEnabledLabsExperiments);
  if (!experiments_list)
    return;

  experiments_list->Clear();
  for (std::set<std::string>::const_iterator it = enabled_experiments.begin();
       it != enabled_experiments.end();
       ++it) {
    experiments_list->Append(new StringValue(*it));
  }
}

// Removes all experiments from prefs::kEnabledLabsExperiments that are
// unknown, to prevent this list to become very long as experiments are added
// and removed.
void SanitizeList(PrefService* prefs) {
  std::set<std::string> known_experiments;
  for (size_t i = 0; i < num_experiments; ++i)
    known_experiments.insert(experiments[i].internal_name);

  std::set<std::string> enabled_experiments;
  GetEnabledFlags(prefs, &enabled_experiments);

  std::set<std::string> new_enabled_experiments;
  std::set_intersection(
      known_experiments.begin(), known_experiments.end(),
      enabled_experiments.begin(), enabled_experiments.end(),
      std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));

  SetEnabledFlags(prefs, new_enabled_experiments);
}

void GetSanitizedEnabledFlags(
    PrefService* prefs, std::set<std::string>* result) {
  SanitizeList(prefs);
  GetEnabledFlags(prefs, result);
}

// Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
// enabled on the current platform.
void GetSanitizedEnabledFlagsForCurrentPlatform(
    PrefService* prefs, std::set<std::string>* result) {
  GetSanitizedEnabledFlags(prefs, result);

  // Filter out any experiments that aren't enabled on the current platform.  We
  // don't remove these from prefs else syncing to a platform with a different
  // set of experiments would be lossy.
  std::set<std::string> platform_experiments;
  int current_platform = GetCurrentPlatform();
  for (size_t i = 0; i < num_experiments; ++i) {
    if (experiments[i].supported_platforms & current_platform)
      platform_experiments.insert(experiments[i].internal_name);
  }

  std::set<std::string> new_enabled_experiments;
  std::set_intersection(
      platform_experiments.begin(), platform_experiments.end(),
      result->begin(), result->end(),
      std::inserter(new_enabled_experiments, new_enabled_experiments.begin()));

  result->swap(new_enabled_experiments);
}

}  // namespace

void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line) {
  FlagsState::instance()->ConvertFlagsToSwitches(prefs, command_line);
}

ListValue* GetFlagsExperimentsData(PrefService* prefs) {
  std::set<std::string> enabled_experiments;
  GetSanitizedEnabledFlags(prefs, &enabled_experiments);

  int current_platform = GetCurrentPlatform();

  ListValue* experiments_data = new ListValue();
  for (size_t i = 0; i < num_experiments; ++i) {
    const Experiment& experiment = experiments[i];
    if (!(experiment.supported_platforms & current_platform))
      continue;

    DictionaryValue* data = new DictionaryValue();
    data->SetString("internal_name", experiment.internal_name);
    data->SetString("name",
                    l10n_util::GetStringUTF16(experiment.visible_name_id));
    data->SetString("description",
                    l10n_util::GetStringUTF16(
                        experiment.visible_description_id));
    data->SetBoolean("enabled",
                      enabled_experiments.count(experiment.internal_name) > 0);

    experiments_data->Append(data);
  }
  return experiments_data;
}

bool IsRestartNeededToCommitChanges() {
  return FlagsState::instance()->IsRestartNeededToCommitChanges();
}

void SetExperimentEnabled(
    PrefService* prefs, const std::string& internal_name, bool enable) {
  FlagsState::instance()->SetExperimentEnabled(prefs, internal_name, enable);
}

void RemoveFlagsSwitches(
    std::map<std::string, CommandLine::StringType>* switch_list) {
  FlagsState::instance()->RemoveFlagsSwitches(switch_list);
}

int GetCurrentPlatform() {
#if defined(OS_MACOSX)
  return kOsMac;
#elif defined(OS_WIN)
  return kOsWin;
#elif defined(OS_CHROMEOS)  // Needs to be before the OS_LINUX check.
  return kOsCrOS;
#elif defined(OS_LINUX)
  return kOsLinux;
#else
#error Unknown platform
#endif
}

void RecordUMAStatistics(const PrefService* prefs) {
  std::set<std::string> flags;
  GetEnabledFlags(prefs, &flags);
  for (std::set<std::string>::iterator it = flags.begin(); it != flags.end();
       ++it) {
    std::string action("AboutFlags_");
    action += *it;
    UserMetrics::RecordComputedAction(action);
  }
  // Since flag metrics are recorded every startup, add a tick so that the
  // stats can be made meaningful.
  if (flags.size())
    UserMetrics::RecordAction(UserMetricsAction("AboutFlags_StartupTick"));
}

//////////////////////////////////////////////////////////////////////////////
// FlagsState implementation.

namespace {

void FlagsState::ConvertFlagsToSwitches(
    PrefService* prefs, CommandLine* command_line) {
  if (command_line->HasSwitch(switches::kNoExperiments))
    return;

  std::set<std::string> enabled_experiments;

#if defined(OS_CHROMEOS)
  // Some experiments were implemented via prefs on Chrome OS and we want to
  // seamlessly migrate these prefs to about:flags for updated users.
  MigrateChromeOSLabsPrefs(prefs, &enabled_experiments);
#endif

  GetSanitizedEnabledFlagsForCurrentPlatform(prefs, &enabled_experiments);

  std::map<std::string, const Experiment*> experiment_map;
  for (size_t i = 0; i < num_experiments; ++i)
    experiment_map[experiments[i].internal_name] = &experiments[i];

  command_line->AppendSwitch(switches::kFlagSwitchesBegin);
  flags_switches_.insert(switches::kFlagSwitchesBegin);
  for (std::set<std::string>::iterator it = enabled_experiments.begin();
       it != enabled_experiments.end();
       ++it) {
    const std::string& experiment_name = *it;
    std::map<std::string, const Experiment*>::iterator experiment =
        experiment_map.find(experiment_name);
    DCHECK(experiment != experiment_map.end());
    if (experiment == experiment_map.end())
      continue;

    command_line->AppendSwitch(experiment->second->command_line);
    flags_switches_.insert(experiment->second->command_line);
  }
  command_line->AppendSwitch(switches::kFlagSwitchesEnd);
  flags_switches_.insert(switches::kFlagSwitchesEnd);
}

bool FlagsState::IsRestartNeededToCommitChanges() {
  return needs_restart_;
}

void FlagsState::SetExperimentEnabled(
    PrefService* prefs, const std::string& internal_name, bool enable) {
  needs_restart_ = true;

  std::set<std::string> enabled_experiments;
  GetSanitizedEnabledFlags(prefs, &enabled_experiments);

  if (enable)
    enabled_experiments.insert(internal_name);
  else
    enabled_experiments.erase(internal_name);

  SetEnabledFlags(prefs, enabled_experiments);
}

void FlagsState::RemoveFlagsSwitches(
    std::map<std::string, CommandLine::StringType>* switch_list) {
  for (std::set<std::string>::const_iterator it = flags_switches_.begin();
       it != flags_switches_.end();
       ++it) {
    switch_list->erase(*it);
  }
}

void FlagsState::reset() {
  needs_restart_ = false;
  flags_switches_.clear();
}

} // namespace

namespace testing {
void ClearState() {
  FlagsState::instance()->reset();
}

void SetExperiments(const Experiment* e, size_t count) {
  if (!e) {
    experiments = kExperiments;
    num_experiments = arraysize(kExperiments);
  } else {
    experiments = e;
    num_experiments = count;
  }
}

}  // namespace testing

}  // namespace about_flags