droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 1 | // 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 | |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 10 | #include <dispatch/dispatch.h> |
sdefresne | 60feea1 | 2015-06-04 08:39:51 | [diff] [blame] | 11 | #import <Foundation/Foundation.h> |
| 12 | |
lpromero | 6853727 | 2015-04-29 11:16:01 | [diff] [blame] | 13 | #include <string> |
| 14 | |
droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 15 | #include "base/command_line.h" |
vabr | 5b82ac3 | 2015-11-27 13:03:43 | [diff] [blame] | 16 | #include "base/metrics/field_trial.h" |
sdefresne | b53a987 | 2015-05-18 20:01:49 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
vabr | 5b82ac3 | 2015-11-27 13:03:43 | [diff] [blame] | 18 | #include "components/autofill/core/common/autofill_switches.h" |
sdefresne | 0f2ef35 | 2015-07-27 19:18:00 | [diff] [blame] | 19 | #include "components/enhanced_bookmarks/enhanced_bookmark_features.h" |
lpromero | 6853727 | 2015-04-29 11:16:01 | [diff] [blame] | 20 | #include "components/variations/variations_associated_data.h" |
droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 21 | #include "ios/chrome/browser/chrome_switches.h" |
bzanotti | a5f249ed | 2015-06-24 10:02:59 | [diff] [blame] | 22 | #include "ios/web/public/web_view_creation_util.h" |
droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 23 | |
sdefresne | 8b192a7 | 2015-05-13 13:25:19 | [diff] [blame] | 24 | namespace { |
| 25 | NSString* const kEnableAlertOnBackgroundUpload = |
| 26 | @"EnableAlertsOnBackgroundUpload"; |
ioanap | 744ba198 | 2015-08-11 13:25:04 | [diff] [blame] | 27 | NSString* const kEnableViewCopyPasswords = @"EnableViewCopyPasswords"; |
vabr | 5b82ac3 | 2015-11-27 13:03:43 | [diff] [blame] | 28 | NSString* const kHeuristicsForPasswordGeneration = |
| 29 | @"HeuristicsForPasswordGeneration"; |
stuartmorgan | e0dc7115 | 2015-10-28 18:24:10 | [diff] [blame] | 30 | |
| 31 | enum 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 | }; |
| 38 | WKWebViewEligibility g_wkwebview_trial_eligibility = |
| 39 | WKWebViewEligibility::UNSET; |
sdefresne | 8b192a7 | 2015-05-13 13:25:19 | [diff] [blame] | 40 | } // namespace |
| 41 | |
droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 42 | namespace experimental_flags { |
| 43 | |
sdefresne | 8b192a7 | 2015-05-13 13:25:19 | [diff] [blame] | 44 | bool IsAlertOnBackgroundUploadEnabled() { |
| 45 | return [[NSUserDefaults standardUserDefaults] |
| 46 | boolForKey:kEnableAlertOnBackgroundUpload]; |
| 47 | } |
| 48 | |
sdefresne | 60feea1 | 2015-06-04 08:39:51 | [diff] [blame] | 49 | bool IsBookmarkCollectionEnabled() { |
sdefresne | 0f2ef35 | 2015-07-27 19:18:00 | [diff] [blame] | 50 | return enhanced_bookmarks::IsEnhancedBookmarksEnabled(); |
sdefresne | 60feea1 | 2015-06-04 08:39:51 | [diff] [blame] | 51 | } |
| 52 | |
stuartmorgan | e0dc7115 | 2015-10-28 18:24:10 | [diff] [blame] | 53 | void 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 | |
jbbegue | cba2cb0b | 2015-11-06 10:35:24 | [diff] [blame] | 62 | bool 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 | |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 78 | // 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. |
| 83 | bool 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 | |
sdefresne | b53a987 | 2015-05-18 20:01:49 | [diff] [blame] | 126 | bool IsWKWebViewEnabled() { |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 127 | if (!CanCheckWKWebViewExperiment()) { |
| 128 | return false; |
stuartmorgan | e0dc7115 | 2015-10-28 18:24:10 | [diff] [blame] | 129 | } |
| 130 | |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 131 | // Check if the experimental flag is turned on. |
stuartmorgan | e0dc7115 | 2015-10-28 18:24:10 | [diff] [blame] | 132 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 133 | if (command_line->HasSwitch(switches::kEnableIOSWKWebView)) |
| 134 | return true; |
| 135 | else if (command_line->HasSwitch(switches::kDisableIOSWKWebView)) |
stuartmorgan | e0dc7115 | 2015-10-28 18:24:10 | [diff] [blame] | 136 | return false; |
| 137 | |
| 138 | // Now that it's been established that user is a candidate, set up the trial |
| 139 | // by checking the group. |
sdefresne | b53a987 | 2015-05-18 20:01:49 | [diff] [blame] | 140 | std::string group_name = |
| 141 | base::FieldTrialList::FindFullName("IOSUseWKWebView"); |
| 142 | |
sdefresne | b53a987 | 2015-05-18 20:01:49 | [diff] [blame] | 143 | // Check if the finch experiment is turned on. |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 144 | return base::StartsWith(group_name, "Enabled", |
| 145 | base::CompareCase::INSENSITIVE_ASCII); |
sdefresne | b53a987 | 2015-05-18 20:01:49 | [diff] [blame] | 146 | } |
| 147 | |
rohitrao | 4b79d93 | 2015-12-02 19:37:38 | [diff] [blame^] | 148 | std::string GetWKWebViewSearchParams() { |
| 149 | if (!CanCheckWKWebViewExperiment()) { |
| 150 | return std::string(); |
| 151 | } |
| 152 | |
| 153 | return variations::GetVariationParamValue("IOSUseWKWebView", "esrch"); |
| 154 | } |
| 155 | |
lpromero | 9abb76b | 2015-09-03 15:10:02 | [diff] [blame] | 156 | bool AreKeyboardCommandsEnabled() { |
| 157 | return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 158 | switches::kDisableKeyboardCommands); |
| 159 | } |
| 160 | |
ioanap | 744ba198 | 2015-08-11 13:25:04 | [diff] [blame] | 161 | bool IsViewCopyPasswordsEnabled() { |
| 162 | NSString* viewCopyPasswordFlag = [[NSUserDefaults standardUserDefaults] |
| 163 | objectForKey:kEnableViewCopyPasswords]; |
| 164 | if ([viewCopyPasswordFlag isEqualToString:@"Enabled"]) |
| 165 | return true; |
| 166 | return false; |
| 167 | } |
| 168 | |
vabr | 5b82ac3 | 2015-11-27 13:03:43 | [diff] [blame] | 169 | bool 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 | |
| 182 | bool 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 | |
droger | f83b9126 | 2015-03-05 19:45:19 | [diff] [blame] | 192 | } // namespace experimental_flags |