blob: 3b31adf9af4718ed6364534d3ef4bec277adba56 [file] [log] [blame]
drogerf83b91262015-03-05 19:45:191// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file can be empty. Its purpose is to contain the relatively short lived
6// definitions required for experimental flags.
7
8#include "ios/chrome/browser/experimental_flags.h"
9
rohitrao4b79d932015-12-02 19:37:3810#include <dispatch/dispatch.h>
sdefresne60feea12015-06-04 08:39:5111#import <Foundation/Foundation.h>
12
lpromero68537272015-04-29 11:16:0113#include <string>
14
drogerf83b91262015-03-05 19:45:1915#include "base/command_line.h"
vabr5b82ac32015-11-27 13:03:4316#include "base/metrics/field_trial.h"
sdefresneb53a9872015-05-18 20:01:4917#include "base/strings/string_util.h"
vabr5b82ac32015-11-27 13:03:4318#include "components/autofill/core/common/autofill_switches.h"
sdefresne0f2ef352015-07-27 19:18:0019#include "components/enhanced_bookmarks/enhanced_bookmark_features.h"
lpromero68537272015-04-29 11:16:0120#include "components/variations/variations_associated_data.h"
drogerf83b91262015-03-05 19:45:1921#include "ios/chrome/browser/chrome_switches.h"
bzanottia5f249ed2015-06-24 10:02:5922#include "ios/web/public/web_view_creation_util.h"
drogerf83b91262015-03-05 19:45:1923
sdefresne8b192a72015-05-13 13:25:1924namespace {
25NSString* const kEnableAlertOnBackgroundUpload =
26 @"EnableAlertsOnBackgroundUpload";
ioanap744ba1982015-08-11 13:25:0427NSString* const kEnableViewCopyPasswords = @"EnableViewCopyPasswords";
vabr5b82ac32015-11-27 13:03:4328NSString* const kHeuristicsForPasswordGeneration =
29 @"HeuristicsForPasswordGeneration";
stuartmorgane0dc71152015-10-28 18:24:1030
31enum class WKWebViewEligibility {
32 // UNSET indicates that no explicit call to set eligibility has been made,
33 // nor has a default value been assumed due to checking eligibility.
34 UNSET,
35 ELIGIBLE,
36 INELIGIBLE
37};
38WKWebViewEligibility g_wkwebview_trial_eligibility =
39 WKWebViewEligibility::UNSET;
sdefresne8b192a72015-05-13 13:25:1940} // namespace
41
drogerf83b91262015-03-05 19:45:1942namespace experimental_flags {
43
sdefresne8b192a72015-05-13 13:25:1944bool IsAlertOnBackgroundUploadEnabled() {
45 return [[NSUserDefaults standardUserDefaults]
46 boolForKey:kEnableAlertOnBackgroundUpload];
47}
48
sdefresne60feea12015-06-04 08:39:5149bool IsBookmarkCollectionEnabled() {
sdefresne0f2ef352015-07-27 19:18:0050 return enhanced_bookmarks::IsEnhancedBookmarksEnabled();
sdefresne60feea12015-06-04 08:39:5151}
52
stuartmorgane0dc71152015-10-28 18:24:1053void SetWKWebViewTrialEligibility(bool eligible) {
54 // It's critical that the enabled state be consistently reported throughout
55 // the life of the app, so ensure that this has not already been set.
56 DCHECK(g_wkwebview_trial_eligibility == WKWebViewEligibility::UNSET);
57
58 g_wkwebview_trial_eligibility = eligible ? WKWebViewEligibility::ELIGIBLE
59 : WKWebViewEligibility::INELIGIBLE;
60}
61
jbbeguecba2cb0b2015-11-06 10:35:2462bool IsLRUSnapshotCacheEnabled() {
63 // Check if the experimental flag is forced on or off.
64 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
65 if (command_line->HasSwitch(switches::kEnableLRUSnapshotCache)) {
66 return true;
67 } else if (command_line->HasSwitch(switches::kDisableLRUSnapshotCache)) {
68 return false;
69 }
70
71 // Check if the finch experiment is turned on.
72 std::string group_name =
73 base::FieldTrialList::FindFullName("IOSLRUSnapshotCache");
74 return base::StartsWith(group_name, "Enabled",
75 base::CompareCase::INSENSITIVE_ASCII);
76}
77
rohitrao4b79d932015-12-02 19:37:3878// Helper method that returns true if it is safe to check the finch group for
79// the IOSUseWKWebView experiment. Some users are ineligible to be in the
80// trial, so for those users, this method returns false. If this method returns
81// false, do not check for the current finch group, as doing so will incorrectly
82// mark the current user as being in the experiment.
83bool CanCheckWKWebViewExperiment() {
84 // True if this user is eligible for the WKWebView experiment and it is ok to
85 // check the experiment group.
86 static bool ok_to_check_finch = false;
87 static dispatch_once_t once;
88 dispatch_once(&once, ^{
89 // If g_wkwebview_trial_eligibility hasn't been set, default it to
90 // ineligible. This ensures future calls to try to set it will DCHECK.
91 if (g_wkwebview_trial_eligibility == WKWebViewEligibility::UNSET) {
92 g_wkwebview_trial_eligibility = WKWebViewEligibility::INELIGIBLE;
93 }
94
95 // If WKWebView isn't supported, don't activate the experiment at all. This
96 // avoids someone being slotted into the WKWebView bucket (and thus
97 // reporting as WKWebView), but actually running UIWebView.
98 if (!web::IsWKWebViewSupported()) {
99 ok_to_check_finch = false;
100 return;
101 }
102
103 // Check for a flag forcing a specific group. Even ineligible users can be
104 // opted into WKWebView if an override flag is set.
105 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
106 bool trial_overridden =
107 command_line->HasSwitch(switches::kEnableIOSWKWebView) ||
108 command_line->HasSwitch(switches::kDisableIOSWKWebView);
109
110 // If the user isn't eligible for the trial (i.e., their state is such that
111 // they should not be automatically selected for the group), and there's no
112 // explicit override, don't check the group (again, to avoid having them
113 // report as part of a group at all).
114 if (g_wkwebview_trial_eligibility == WKWebViewEligibility::INELIGIBLE &&
115 !trial_overridden) {
116 ok_to_check_finch = false;
117 return;
118 }
119
120 ok_to_check_finch = true;
121 });
122
123 return ok_to_check_finch;
124}
125
sdefresneb53a9872015-05-18 20:01:49126bool IsWKWebViewEnabled() {
rohitrao4b79d932015-12-02 19:37:38127 if (!CanCheckWKWebViewExperiment()) {
128 return false;
stuartmorgane0dc71152015-10-28 18:24:10129 }
130
rohitrao4b79d932015-12-02 19:37:38131 // Check if the experimental flag is turned on.
stuartmorgane0dc71152015-10-28 18:24:10132 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
rohitrao4b79d932015-12-02 19:37:38133 if (command_line->HasSwitch(switches::kEnableIOSWKWebView))
134 return true;
135 else if (command_line->HasSwitch(switches::kDisableIOSWKWebView))
stuartmorgane0dc71152015-10-28 18:24:10136 return false;
137
138 // Now that it's been established that user is a candidate, set up the trial
139 // by checking the group.
sdefresneb53a9872015-05-18 20:01:49140 std::string group_name =
141 base::FieldTrialList::FindFullName("IOSUseWKWebView");
142
sdefresneb53a9872015-05-18 20:01:49143 // Check if the finch experiment is turned on.
brettw95509312015-07-16 23:57:33144 return base::StartsWith(group_name, "Enabled",
145 base::CompareCase::INSENSITIVE_ASCII);
sdefresneb53a9872015-05-18 20:01:49146}
147
rohitrao4b79d932015-12-02 19:37:38148std::string GetWKWebViewSearchParams() {
149 if (!CanCheckWKWebViewExperiment()) {
150 return std::string();
151 }
152
153 return variations::GetVariationParamValue("IOSUseWKWebView", "esrch");
154}
155
lpromero9abb76b2015-09-03 15:10:02156bool AreKeyboardCommandsEnabled() {
157 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
158 switches::kDisableKeyboardCommands);
159}
160
ioanap744ba1982015-08-11 13:25:04161bool IsViewCopyPasswordsEnabled() {
162 NSString* viewCopyPasswordFlag = [[NSUserDefaults standardUserDefaults]
163 objectForKey:kEnableViewCopyPasswords];
164 if ([viewCopyPasswordFlag isEqualToString:@"Enabled"])
165 return true;
166 return false;
167}
168
vabr5b82ac32015-11-27 13:03:43169bool IsPasswordGenerationEnabled() {
170 // This call activates the field trial, if needed, so it must come before any
171 // early returns.
172 std::string group_name =
173 base::FieldTrialList::FindFullName("PasswordGeneration");
174 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
175 if (command_line->HasSwitch(switches::kEnableIOSPasswordGeneration))
176 return true;
177 if (command_line->HasSwitch(switches::kDisableIOSPasswordGeneration))
178 return false;
179 return group_name != "Disabled";
180}
181
182bool UseOnlyLocalHeuristicsForPasswordGeneration() {
183 if ([[NSUserDefaults standardUserDefaults]
184 boolForKey:kHeuristicsForPasswordGeneration]) {
185 return true;
186 }
187 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
188 return command_line->HasSwitch(
189 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration);
190}
191
drogerf83b91262015-03-05 19:45:19192} // namespace experimental_flags