blob: daf52adff257bcdc6a1656058bb2bf4706e03b24 [file] [log] [blame]
sdefresne14900ee2015-11-27 14:43:211// Copyright 2012 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// Implementation of about_flags for iOS that sets flags based on experimental
6// settings.
7
8#include "ios/chrome/browser/about_flags.h"
9
avi571943672015-12-22 02:12:4910#include <stddef.h>
11#include <stdint.h>
sdefresne14900ee2015-11-27 14:43:2112#import <UIKit/UIKit.h>
13
14#include "base/bind.h"
15#include "base/command_line.h"
16#include "base/logging.h"
avi571943672015-12-22 02:12:4917#include "base/macros.h"
sdefresne14900ee2015-11-27 14:43:2118#include "base/memory/singleton.h"
19#include "base/strings/stringprintf.h"
20#include "base/strings/sys_string_conversions.h"
21#include "base/sys_info.h"
robliao59eed1a2016-10-28 17:12:1622#include "base/task_scheduler/switches.h"
sdefresne14900ee2015-11-27 14:43:2123#include "components/dom_distiller/core/dom_distiller_switches.h"
Tommy Nyquistc1d6dea12017-07-26 20:37:2324#include "components/feature_engagement/public/feature_constants.h"
25#include "components/feature_engagement/public/feature_list.h"
sdefresne14900ee2015-11-27 14:43:2126#include "components/flags_ui/feature_entry.h"
27#include "components/flags_ui/feature_entry_macros.h"
28#include "components/flags_ui/flags_storage.h"
29#include "components/flags_ui/flags_ui_switches.h"
noyau4cfb1332016-10-25 17:05:4230#include "components/ntp_tiles/switches.h"
Justin Donnelly33d712e2017-08-23 21:32:5131#include "components/omnibox/browser/omnibox_field_trial.h"
mathp9b4c11d2017-07-06 20:24:1332#include "components/payments/core/features.h"
Marc Treib2752e8b2017-08-04 14:12:0933#include "components/search_provider_logos/features.h"
elawrence816f6790e2017-06-16 18:19:2834#include "components/security_state/core/switches.h"
jlebel2cf54d72017-03-29 13:51:4035#include "components/signin/core/common/signin_switches.h"
sdefresne36579782016-02-05 11:08:2536#include "components/strings/grit/components_strings.h"
Marti Wong87d31d72017-08-25 02:44:5237#include "ios/chrome/browser/bookmarks/bookmark_new_generation_features.h"
sdefresne14900ee2015-11-27 14:43:2138#include "ios/chrome/browser/chrome_switches.h"
vabr0215a8e2017-03-28 12:47:3439#include "ios/chrome/browser/ios_chrome_flag_descriptions.h"
Mike Doughertya5e5ad52017-08-05 00:11:5840#include "ios/chrome/browser/ssl/captive_portal_features.h"
sdefresne14900ee2015-11-27 14:43:2141#include "ios/chrome/grit/ios_strings.h"
msardafc76f662017-02-24 12:46:2842#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
sdefresne14900ee2015-11-27 14:43:2143#include "ios/web/public/user_agent.h"
44#include "ios/web/public/web_view_creation_util.h"
45
46#if !defined(OFFICIAL_BUILD)
47#include "components/variations/variations_switches.h"
vitaliii489217aa2017-01-30 14:50:2248#endif
stkhapuginc1be1792016-12-13 14:30:5349
50#if !defined(__has_feature) || !__has_feature(objc_arc)
51#error "This file requires ARC support."
52#endif
sdefresne14900ee2015-11-27 14:43:2153
elawrence816f6790e2017-06-16 18:19:2854using flags_ui::FeatureEntry;
55
sdefresne14900ee2015-11-27 14:43:2156namespace {
elawrence816f6790e2017-06-16 18:19:2857const FeatureEntry::Choice kMarkHttpAsChoices[] = {
58 {flags_ui::kGenericExperimentChoiceDefault, "", ""},
59 {flag_descriptions::kMarkHttpAsNonSecureAfterEditing,
60 security_state::switches::kMarkHttpAs,
61 security_state::switches::kMarkHttpAsNonSecureAfterEditing},
62 {flag_descriptions::kMarkHttpAsNonSecureWhileIncognito,
63 security_state::switches::kMarkHttpAs,
64 security_state::switches::kMarkHttpAsNonSecureWhileIncognito},
65 {flag_descriptions::kMarkHttpAsNonSecureWhileIncognitoOrEditing,
66 security_state::switches::kMarkHttpAs,
67 security_state::switches::kMarkHttpAsNonSecureWhileIncognitoOrEditing},
68 {flag_descriptions::kMarkHttpAsDangerous,
69 security_state::switches::kMarkHttpAs,
70 security_state::switches::kMarkHttpAsDangerous}};
71
Marc Treib1aa164b2017-08-08 15:37:4072const FeatureEntry::FeatureParam kUseDdljsonApiTest0[] = {
73 {search_provider_logos::features::kDdljsonOverrideUrlParam,
74 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios0.json"}};
Marc Treib2752e8b2017-08-04 14:12:0975const FeatureEntry::FeatureParam kUseDdljsonApiTest1[] = {
76 {search_provider_logos::features::kDdljsonOverrideUrlParam,
77 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios1.json"}};
78const FeatureEntry::FeatureParam kUseDdljsonApiTest2[] = {
79 {search_provider_logos::features::kDdljsonOverrideUrlParam,
80 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios2.json"}};
81const FeatureEntry::FeatureParam kUseDdljsonApiTest3[] = {
82 {search_provider_logos::features::kDdljsonOverrideUrlParam,
83 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios3.json"}};
84const FeatureEntry::FeatureParam kUseDdljsonApiTest4[] = {
85 {search_provider_logos::features::kDdljsonOverrideUrlParam,
86 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios4.json"}};
87
88const FeatureEntry::FeatureVariation kUseDdljsonApiVariations[] = {
Marc Treib1aa164b2017-08-08 15:37:4089 {"(force test doodle 0)", kUseDdljsonApiTest0,
90 arraysize(kUseDdljsonApiTest0), nullptr},
Marc Treib2752e8b2017-08-04 14:12:0991 {"(force test doodle 1)", kUseDdljsonApiTest1,
92 arraysize(kUseDdljsonApiTest1), nullptr},
93 {"(force test doodle 2)", kUseDdljsonApiTest2,
94 arraysize(kUseDdljsonApiTest2), nullptr},
95 {"(force test doodle 3)", kUseDdljsonApiTest3,
96 arraysize(kUseDdljsonApiTest3), nullptr},
97 {"(force test doodle 4)", kUseDdljsonApiTest4,
98 arraysize(kUseDdljsonApiTest4), nullptr}};
99
Cooper Knaak1e026562017-07-26 05:22:28100// To add a new entry, add to the end of kFeatureEntries. There are four
sdefresne14900ee2015-11-27 14:43:21101// distinct types of entries:
Cooper Knaak1e026562017-07-26 05:22:28102// . ENABLE_DISABLE_VALUE: entry is either enabled, disabled, or uses the
103// default value for this feature. Use the ENABLE_DISABLE_VALUE_TYPE
sdefresne14900ee2015-11-27 14:43:21104// macro for this type supplying the command line to the macro.
105// . MULTI_VALUE: a list of choices, the first of which should correspond to a
106// deactivated state for this lab (i.e. no command line option). To specify
107// this type of entry use the macro MULTI_VALUE_TYPE supplying it the
108// array of choices.
Cooper Knaak1e026562017-07-26 05:22:28109// . FEATURE_VALUE: entry is associated with a base::Feature instance. Entry is
110// either enabled, disabled, or uses the default value of the associated
111// base::Feature instance. To specify this type of entry use the macro
112// FEATURE_VALUE_TYPE supplying it the base::Feature instance.
113// . FEATURE_WITH_PARAM_VALUES: a list of choices associated with a
114// base::Feature instance. Choices corresponding to the default state, a
115// universally enabled state, and a universally disabled state are
116// automatically included. To specify this type of entry use the macro
117// FEATURE_WITH_PARAMS_VALUE_TYPE supplying it the base::Feature instance and
118// the array of choices.
119//
sdefresne14900ee2015-11-27 14:43:21120// See the documentation of FeatureEntry for details on the fields.
121//
122// When adding a new choice, add it to the end of the list.
123const flags_ui::FeatureEntry kFeatureEntries[] = {
vabr0215a8e2017-03-28 12:47:34124 {"contextual-search", flag_descriptions::kContextualSearch,
125 flag_descriptions::kContextualSearchDescription, flags_ui::kOsIos,
sdefresne14900ee2015-11-27 14:43:21126 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableContextualSearch,
127 switches::kDisableContextualSearch)},
vabr0215a8e2017-03-28 12:47:34128 {"ios-physical-web", flag_descriptions::kPhysicalWeb,
129 flag_descriptions::kPhysicalWebDescription, flags_ui::kOsIos,
mattreynolds1a4181f2016-10-05 23:50:00130 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableIOSPhysicalWeb,
131 switches::kDisableIOSPhysicalWeb)},
vabr0215a8e2017-03-28 12:47:34132 {"browser-task-scheduler", flag_descriptions::kBrowserTaskScheduler,
133 flag_descriptions::kBrowserTaskSchedulerDescription, flags_ui::kOsIos,
robliao59eed1a2016-10-28 17:12:16134 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableBrowserTaskScheduler,
135 switches::kDisableBrowserTaskScheduler)},
elawrence816f6790e2017-06-16 18:19:28136 {"mark-non-secure-as", flag_descriptions::kMarkHttpAsName,
137 flag_descriptions::kMarkHttpAsDescription, flags_ui::kOsIos,
138 MULTI_VALUE_TYPE(kMarkHttpAsChoices)},
mathp9b4c11d2017-07-06 20:24:13139 {"web-payments", flag_descriptions::kWebPaymentsName,
140 flag_descriptions::kWebPaymentsDescription, flags_ui::kOsIos,
141 FEATURE_VALUE_TYPE(payments::features::kWebPayments)},
Randall Raymond59766ac2017-08-09 16:28:58142 {"web-payments-native-apps", flag_descriptions::kWebPaymentsNativeAppsName,
143 flag_descriptions::kWebPaymentsNativeAppsDescription, flags_ui::kOsIos,
144 FEATURE_VALUE_TYPE(payments::features::kWebPaymentsNativeApps)},
Mike Doughertya5e5ad52017-08-05 00:11:58145 {"ios-captive-portal", flag_descriptions::kCaptivePortalName,
146 flag_descriptions::kCaptivePortalDescription, flags_ui::kOsIos,
147 FEATURE_VALUE_TYPE(kCaptivePortalFeature)},
Cooper Knaak1e026562017-07-26 05:22:28148 {"in-product-help-demo-mode-choice",
149 flag_descriptions::kInProductHelpDemoModeName,
150 flag_descriptions::kInProductHelpDemoModeDescription, flags_ui::kOsIos,
151 FEATURE_WITH_PARAMS_VALUE_TYPE(
Tommy Nyquistc1d6dea12017-07-26 20:37:23152 feature_engagement::kIPHDemoMode,
153 feature_engagement::kIPHDemoModeChoiceVariations,
Marc Treib2752e8b2017-08-04 14:12:09154 "IPH_DemoMode")},
155 {"use-ddljson-api", flag_descriptions::kUseDdljsonApiName,
156 flag_descriptions::kUseDdljsonApiDescription, flags_ui::kOsIos,
157 FEATURE_WITH_PARAMS_VALUE_TYPE(
158 search_provider_logos::features::kUseDdljsonApi,
159 kUseDdljsonApiVariations,
Justin Donnelly33d712e2017-08-23 21:32:51160 "NTPUseDdljsonApi")},
161 {"omnibox-ui-elide-suggestion-url-after-host",
162 flag_descriptions::kOmniboxUIElideSuggestionUrlAfterHostName,
163 flag_descriptions::kOmniboxUIElideSuggestionUrlAfterHostDescription,
164 flags_ui::kOsIos,
165 FEATURE_VALUE_TYPE(omnibox::kUIExperimentElideSuggestionUrlAfterHost)},
166 {"omnibox-ui-hide-suggestion-url-scheme",
167 flag_descriptions::kOmniboxUIHideSuggestionUrlSchemeName,
168 flag_descriptions::kOmniboxUIHideSuggestionUrlSchemeDescription,
169 flags_ui::kOsIos,
170 FEATURE_VALUE_TYPE(omnibox::kUIExperimentHideSuggestionUrlScheme)},
171 {"omnibox-ui-hide-suggestion-url-trivial-subdomains",
172 flag_descriptions::kOmniboxUIHideSuggestionUrlTrivialSubdomainsName,
173 flag_descriptions::kOmniboxUIHideSuggestionUrlTrivialSubdomainsDescription,
174 flags_ui::kOsIos,
175 FEATURE_VALUE_TYPE(
Marti Wong87d31d72017-08-25 02:44:52176 omnibox::kUIExperimentHideSuggestionUrlTrivialSubdomains)},
177 {"bookmark-new-generation", flag_descriptions::kBookmarkNewGenerationName,
178 flag_descriptions::kBookmarkNewGenerationDescription, flags_ui::kOsIos,
179 FEATURE_VALUE_TYPE(
180 bookmark_new_generation::features::kBookmarkNewGeneration)}};
sdefresne14900ee2015-11-27 14:43:21181
182// Add all switches from experimental flags to |command_line|.
183void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) {
184 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
185
mattreynoldsc9f1df52016-06-29 08:30:04186 // Populate command line flags from PhysicalWebEnabled.
187 NSString* enablePhysicalWebValue =
188 [defaults stringForKey:@"PhysicalWebEnabled"];
189 if ([enablePhysicalWebValue isEqualToString:@"Enabled"]) {
190 command_line->AppendSwitch(switches::kEnableIOSPhysicalWeb);
191 } else if ([enablePhysicalWebValue isEqualToString:@"Disabled"]) {
192 command_line->AppendSwitch(switches::kDisableIOSPhysicalWeb);
193 }
194
sdefresne14900ee2015-11-27 14:43:21195 // Web page replay flags.
196 BOOL webPageReplayEnabled = [defaults boolForKey:@"WebPageReplayEnabled"];
197 NSString* webPageReplayProxy =
198 [defaults stringForKey:@"WebPageReplayProxyAddress"];
199 if (webPageReplayEnabled && [webPageReplayProxy length]) {
200 command_line->AppendSwitch(switches::kIOSIgnoreCertificateErrors);
201 // 80 and 443 are the default ports from web page replay.
202 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpPort, "80");
203 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpsPort, "443");
204 command_line->AppendSwitchASCII(
205 switches::kIOSHostResolverRules,
206 "MAP * " + base::SysNSStringToUTF8(webPageReplayProxy));
207 }
208
sdefresne14900ee2015-11-27 14:43:21209 // Set the UA flag if UseMobileSafariUA is enabled.
210 if ([defaults boolForKey:@"UseMobileSafariUA"]) {
211 // Safari uses "Vesion/", followed by the OS version excluding bugfix, where
212 // Chrome puts its product token.
avi571943672015-12-22 02:12:49213 int32_t major = 0;
214 int32_t minor = 0;
215 int32_t bugfix = 0;
sdefresne14900ee2015-11-27 14:43:21216 base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
217 std::string product = base::StringPrintf("Version/%d.%d", major, minor);
218
219 command_line->AppendSwitchASCII(switches::kUserAgent,
220 web::BuildUserAgentFromProduct(product));
221 }
222
gambardd2e44fb2017-01-25 09:14:21223 // Populate command line flag for Suggestions UI display.
224 NSString* enableSuggestions = [defaults stringForKey:@"EnableSuggestions"];
225 if ([enableSuggestions isEqualToString:@"Enabled"]) {
226 command_line->AppendSwitch(switches::kEnableSuggestionsUI);
227 } else if ([enableSuggestions isEqualToString:@"Disabled"]) {
228 command_line->AppendSwitch(switches::kDisableSuggestionsUI);
229 }
230
jkrcal7d79de52017-05-05 18:13:10231 // Populate command line flag for fetching missing favicons for NTP tiles.
232 NSString* enableMostLikelyFaviconsFromServer =
233 [defaults stringForKey:@"EnableNtpMostLikelyFaviconsFromServer"];
234 if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Enabled"]) {
235 command_line->AppendSwitch(
236 ntp_tiles::switches::kEnableNtpMostLikelyFaviconsFromServer);
237 } else if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Disabled"]) {
238 command_line->AppendSwitch(
239 ntp_tiles::switches::kDisableNtpMostLikelyFaviconsFromServer);
240 }
241
Danyao Wang6ab20f72017-06-20 14:13:41242 // Populate command line flag for the native to WKBackForwardList based
243 // navigation manager experiment.
244 NSString* enableSlimNavigationManager =
245 [defaults stringForKey:@"EnableSlimNavigationManager"];
246 if ([enableSlimNavigationManager isEqualToString:@"Enabled"]) {
247 command_line->AppendSwitch(switches::kEnableSlimNavigationManager);
248 } else if ([enableSlimNavigationManager isEqualToString:@"Disabled"]) {
249 command_line->AppendSwitch(switches::kDisableSlimNavigationManager);
250 }
251
sdefresne14900ee2015-11-27 14:43:21252 // Freeform commandline flags. These are added last, so that any flags added
253 // earlier in this function take precedence.
254 if ([defaults boolForKey:@"EnableFreeformCommandLineFlags"]) {
255 base::CommandLine::StringVector flags;
256 // Append an empty "program" argument.
257 flags.push_back("");
258
259 // The number of flags corresponds to the number of text fields in
260 // Experimental.plist.
261 const int kNumFreeformFlags = 5;
262 for (int i = 1; i <= kNumFreeformFlags; ++i) {
263 NSString* key =
264 [NSString stringWithFormat:@"FreeformCommandLineFlag%d", i];
265 NSString* flag = [defaults stringForKey:key];
266 if ([flag length]) {
267 flags.push_back(base::SysNSStringToUTF8(flag));
268 }
269 }
270
271 base::CommandLine temp_command_line(flags);
272 command_line->AppendArguments(temp_command_line, false);
273 }
msardafc76f662017-02-24 12:46:28274
jlebel2cf54d72017-03-29 13:51:40275 // Populate command line flag for Sign-in promo.
276 NSString* enableSigninPromo = [defaults stringForKey:@"EnableSigninPromo"];
277 if ([enableSigninPromo isEqualToString:@"Enabled"]) {
278 command_line->AppendSwitch(switches::kEnableSigninPromo);
279 } else if ([enableSigninPromo isEqualToString:@"Disabled"]) {
280 command_line->AppendSwitch(switches::kDisableSigninPromo);
281 }
282
justincohendacc85d2017-06-28 23:34:10283 // Populate command line flag for 3rd party keyboard omnibox workaround.
284 NSString* enableThirdPartyKeyboardWorkaround =
285 [defaults stringForKey:@"EnableThirdPartyKeyboardWorkaround"];
286 if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Enabled"]) {
287 command_line->AppendSwitch(switches::kEnableThirdPartyKeyboardWorkaround);
288 } else if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Disabled"]) {
289 command_line->AppendSwitch(switches::kDisableThirdPartyKeyboardWorkaround);
290 }
291
msardafc76f662017-02-24 12:46:28292 ios::GetChromeBrowserProvider()->AppendSwitchesFromExperimentalSettings(
293 defaults, command_line);
sdefresne14900ee2015-11-27 14:43:21294}
295
296bool SkipConditionalFeatureEntry(const flags_ui::FeatureEntry& entry) {
297 return false;
298}
299
300class FlagsStateSingleton {
301 public:
302 FlagsStateSingleton()
303 : flags_state_(kFeatureEntries, arraysize(kFeatureEntries)) {}
304 ~FlagsStateSingleton() {}
305
306 static FlagsStateSingleton* GetInstance() {
307 return base::Singleton<FlagsStateSingleton>::get();
308 }
309
310 static flags_ui::FlagsState* GetFlagsState() {
311 return &GetInstance()->flags_state_;
312 }
313
314 private:
315 flags_ui::FlagsState flags_state_;
316
317 DISALLOW_COPY_AND_ASSIGN(FlagsStateSingleton);
318};
319} // namespace
320
321void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
322 base::CommandLine* command_line) {
323 FlagsStateSingleton::GetFlagsState()->ConvertFlagsToSwitches(
sdefresnec9763902015-12-02 10:30:11324 flags_storage, command_line, flags_ui::kAddSentinels,
325 switches::kEnableIOSFeatures, switches::kDisableIOSFeatures);
sdefresne14900ee2015-11-27 14:43:21326 AppendSwitchesFromExperimentalSettings(command_line);
327}
328
jkrcalbf073372016-07-29 07:21:31329std::vector<std::string> RegisterAllFeatureVariationParameters(
330 flags_ui::FlagsStorage* flags_storage,
331 base::FeatureList* feature_list) {
332 return FlagsStateSingleton::GetFlagsState()
333 ->RegisterAllFeatureVariationParameters(flags_storage, feature_list);
334}
335
sdefresne14900ee2015-11-27 14:43:21336void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
337 flags_ui::FlagAccess access,
338 base::ListValue* supported_entries,
339 base::ListValue* unsupported_entries) {
340 FlagsStateSingleton::GetFlagsState()->GetFlagFeatureEntries(
341 flags_storage, access, supported_entries, unsupported_entries,
342 base::Bind(&SkipConditionalFeatureEntry));
343}
344
345void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
346 const std::string& internal_name,
347 bool enable) {
348 FlagsStateSingleton::GetFlagsState()->SetFeatureEntryEnabled(
349 flags_storage, internal_name, enable);
350}
351
352void ResetAllFlags(flags_ui::FlagsStorage* flags_storage) {
353 FlagsStateSingleton::GetFlagsState()->ResetAllFlags(flags_storage);
354}
355
356namespace testing {
357
358const flags_ui::FeatureEntry* GetFeatureEntries(size_t* count) {
359 *count = arraysize(kFeatureEntries);
360 return kFeatureEntries;
361}
362
363} // namespace testing