blob: 32f800674fb05d6b02200dd88798e012952f78d3 [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"
mathp9b4c11d2017-07-06 20:24:1331#include "components/payments/core/features.h"
Marc Treib2752e8b2017-08-04 14:12:0932#include "components/search_provider_logos/features.h"
elawrence816f6790e2017-06-16 18:19:2833#include "components/security_state/core/switches.h"
jlebel2cf54d72017-03-29 13:51:4034#include "components/signin/core/common/signin_switches.h"
sdefresne36579782016-02-05 11:08:2535#include "components/strings/grit/components_strings.h"
sdefresne14900ee2015-11-27 14:43:2136#include "ios/chrome/browser/chrome_switches.h"
vabr0215a8e2017-03-28 12:47:3437#include "ios/chrome/browser/ios_chrome_flag_descriptions.h"
sdefresne14900ee2015-11-27 14:43:2138#include "ios/chrome/grit/ios_strings.h"
Mike Dougherty929e2482017-07-19 18:38:2539#include "ios/components/captive_portal/features.h"
msardafc76f662017-02-24 12:46:2840#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
sdefresne14900ee2015-11-27 14:43:2141#include "ios/web/public/user_agent.h"
42#include "ios/web/public/web_view_creation_util.h"
43
44#if !defined(OFFICIAL_BUILD)
45#include "components/variations/variations_switches.h"
vitaliii489217aa2017-01-30 14:50:2246#endif
stkhapuginc1be1792016-12-13 14:30:5347
48#if !defined(__has_feature) || !__has_feature(objc_arc)
49#error "This file requires ARC support."
50#endif
sdefresne14900ee2015-11-27 14:43:2151
elawrence816f6790e2017-06-16 18:19:2852using flags_ui::FeatureEntry;
53
sdefresne14900ee2015-11-27 14:43:2154namespace {
elawrence816f6790e2017-06-16 18:19:2855const FeatureEntry::Choice kMarkHttpAsChoices[] = {
56 {flags_ui::kGenericExperimentChoiceDefault, "", ""},
57 {flag_descriptions::kMarkHttpAsNonSecureAfterEditing,
58 security_state::switches::kMarkHttpAs,
59 security_state::switches::kMarkHttpAsNonSecureAfterEditing},
60 {flag_descriptions::kMarkHttpAsNonSecureWhileIncognito,
61 security_state::switches::kMarkHttpAs,
62 security_state::switches::kMarkHttpAsNonSecureWhileIncognito},
63 {flag_descriptions::kMarkHttpAsNonSecureWhileIncognitoOrEditing,
64 security_state::switches::kMarkHttpAs,
65 security_state::switches::kMarkHttpAsNonSecureWhileIncognitoOrEditing},
66 {flag_descriptions::kMarkHttpAsDangerous,
67 security_state::switches::kMarkHttpAs,
68 security_state::switches::kMarkHttpAsDangerous}};
69
Marc Treib2752e8b2017-08-04 14:12:0970const FeatureEntry::FeatureParam kUseDdljsonApiTest1[] = {
71 {search_provider_logos::features::kDdljsonOverrideUrlParam,
72 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios1.json"}};
73const FeatureEntry::FeatureParam kUseDdljsonApiTest2[] = {
74 {search_provider_logos::features::kDdljsonOverrideUrlParam,
75 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios2.json"}};
76const FeatureEntry::FeatureParam kUseDdljsonApiTest3[] = {
77 {search_provider_logos::features::kDdljsonOverrideUrlParam,
78 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios3.json"}};
79const FeatureEntry::FeatureParam kUseDdljsonApiTest4[] = {
80 {search_provider_logos::features::kDdljsonOverrideUrlParam,
81 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios4.json"}};
82
83const FeatureEntry::FeatureVariation kUseDdljsonApiVariations[] = {
84 {"(force test doodle 1)", kUseDdljsonApiTest1,
85 arraysize(kUseDdljsonApiTest1), nullptr},
86 {"(force test doodle 2)", kUseDdljsonApiTest2,
87 arraysize(kUseDdljsonApiTest2), nullptr},
88 {"(force test doodle 3)", kUseDdljsonApiTest3,
89 arraysize(kUseDdljsonApiTest3), nullptr},
90 {"(force test doodle 4)", kUseDdljsonApiTest4,
91 arraysize(kUseDdljsonApiTest4), nullptr}};
92
Cooper Knaak1e026562017-07-26 05:22:2893// To add a new entry, add to the end of kFeatureEntries. There are four
sdefresne14900ee2015-11-27 14:43:2194// distinct types of entries:
Cooper Knaak1e026562017-07-26 05:22:2895// . ENABLE_DISABLE_VALUE: entry is either enabled, disabled, or uses the
96// default value for this feature. Use the ENABLE_DISABLE_VALUE_TYPE
sdefresne14900ee2015-11-27 14:43:2197// macro for this type supplying the command line to the macro.
98// . MULTI_VALUE: a list of choices, the first of which should correspond to a
99// deactivated state for this lab (i.e. no command line option). To specify
100// this type of entry use the macro MULTI_VALUE_TYPE supplying it the
101// array of choices.
Cooper Knaak1e026562017-07-26 05:22:28102// . FEATURE_VALUE: entry is associated with a base::Feature instance. Entry is
103// either enabled, disabled, or uses the default value of the associated
104// base::Feature instance. To specify this type of entry use the macro
105// FEATURE_VALUE_TYPE supplying it the base::Feature instance.
106// . FEATURE_WITH_PARAM_VALUES: a list of choices associated with a
107// base::Feature instance. Choices corresponding to the default state, a
108// universally enabled state, and a universally disabled state are
109// automatically included. To specify this type of entry use the macro
110// FEATURE_WITH_PARAMS_VALUE_TYPE supplying it the base::Feature instance and
111// the array of choices.
112//
sdefresne14900ee2015-11-27 14:43:21113// See the documentation of FeatureEntry for details on the fields.
114//
115// When adding a new choice, add it to the end of the list.
116const flags_ui::FeatureEntry kFeatureEntries[] = {
vabr0215a8e2017-03-28 12:47:34117 {"contextual-search", flag_descriptions::kContextualSearch,
118 flag_descriptions::kContextualSearchDescription, flags_ui::kOsIos,
sdefresne14900ee2015-11-27 14:43:21119 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableContextualSearch,
120 switches::kDisableContextualSearch)},
vabr0215a8e2017-03-28 12:47:34121 {"ios-physical-web", flag_descriptions::kPhysicalWeb,
122 flag_descriptions::kPhysicalWebDescription, flags_ui::kOsIos,
mattreynolds1a4181f2016-10-05 23:50:00123 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableIOSPhysicalWeb,
124 switches::kDisableIOSPhysicalWeb)},
vabr0215a8e2017-03-28 12:47:34125 {"browser-task-scheduler", flag_descriptions::kBrowserTaskScheduler,
126 flag_descriptions::kBrowserTaskSchedulerDescription, flags_ui::kOsIos,
robliao59eed1a2016-10-28 17:12:16127 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableBrowserTaskScheduler,
128 switches::kDisableBrowserTaskScheduler)},
elawrence816f6790e2017-06-16 18:19:28129 {"mark-non-secure-as", flag_descriptions::kMarkHttpAsName,
130 flag_descriptions::kMarkHttpAsDescription, flags_ui::kOsIos,
131 MULTI_VALUE_TYPE(kMarkHttpAsChoices)},
mathp9b4c11d2017-07-06 20:24:13132 {"web-payments", flag_descriptions::kWebPaymentsName,
133 flag_descriptions::kWebPaymentsDescription, flags_ui::kOsIos,
134 FEATURE_VALUE_TYPE(payments::features::kWebPayments)},
Mike Dougherty929e2482017-07-19 18:38:25135 {"ios-captive-portal", flag_descriptions::kIosCaptivePortalName,
136 flag_descriptions::kIosCaptivePortalDescription, flags_ui::kOsIos,
137 FEATURE_VALUE_TYPE(captive_portal::kIosCaptivePortal)},
Cooper Knaak1e026562017-07-26 05:22:28138 {"in-product-help-demo-mode-choice",
139 flag_descriptions::kInProductHelpDemoModeName,
140 flag_descriptions::kInProductHelpDemoModeDescription, flags_ui::kOsIos,
141 FEATURE_WITH_PARAMS_VALUE_TYPE(
Tommy Nyquistc1d6dea12017-07-26 20:37:23142 feature_engagement::kIPHDemoMode,
143 feature_engagement::kIPHDemoModeChoiceVariations,
Marc Treib2752e8b2017-08-04 14:12:09144 "IPH_DemoMode")},
145 {"use-ddljson-api", flag_descriptions::kUseDdljsonApiName,
146 flag_descriptions::kUseDdljsonApiDescription, flags_ui::kOsIos,
147 FEATURE_WITH_PARAMS_VALUE_TYPE(
148 search_provider_logos::features::kUseDdljsonApi,
149 kUseDdljsonApiVariations,
150 search_provider_logos::features::kUseDdljsonApi.name)}};
sdefresne14900ee2015-11-27 14:43:21151
152// Add all switches from experimental flags to |command_line|.
153void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) {
154 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
155
liaoyukedf4f7e42017-01-07 00:21:10156 // Populate command line flag for the tab strip auto scroll new tabs
157 // experiment from the configuration plist.
158 if ([defaults boolForKey:@"TabStripAutoScrollNewTabsDisabled"])
159 command_line->AppendSwitch(switches::kDisableTabStripAutoScrollNewTabs);
160
sdefresne14900ee2015-11-27 14:43:21161 // Populate command line flags from PasswordGenerationEnabled.
162 NSString* enablePasswordGenerationValue =
163 [defaults stringForKey:@"PasswordGenerationEnabled"];
164 if ([enablePasswordGenerationValue isEqualToString:@"Enabled"]) {
165 command_line->AppendSwitch(switches::kEnableIOSPasswordGeneration);
166 } else if ([enablePasswordGenerationValue isEqualToString:@"Disabled"]) {
167 command_line->AppendSwitch(switches::kDisableIOSPasswordGeneration);
168 }
169
mattreynoldsc9f1df52016-06-29 08:30:04170 // Populate command line flags from PhysicalWebEnabled.
171 NSString* enablePhysicalWebValue =
172 [defaults stringForKey:@"PhysicalWebEnabled"];
173 if ([enablePhysicalWebValue isEqualToString:@"Enabled"]) {
174 command_line->AppendSwitch(switches::kEnableIOSPhysicalWeb);
175 } else if ([enablePhysicalWebValue isEqualToString:@"Disabled"]) {
176 command_line->AppendSwitch(switches::kDisableIOSPhysicalWeb);
177 }
178
sdefresne14900ee2015-11-27 14:43:21179 // Web page replay flags.
180 BOOL webPageReplayEnabled = [defaults boolForKey:@"WebPageReplayEnabled"];
181 NSString* webPageReplayProxy =
182 [defaults stringForKey:@"WebPageReplayProxyAddress"];
183 if (webPageReplayEnabled && [webPageReplayProxy length]) {
184 command_line->AppendSwitch(switches::kIOSIgnoreCertificateErrors);
185 // 80 and 443 are the default ports from web page replay.
186 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpPort, "80");
187 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpsPort, "443");
188 command_line->AppendSwitchASCII(
189 switches::kIOSHostResolverRules,
190 "MAP * " + base::SysNSStringToUTF8(webPageReplayProxy));
191 }
192
sdefresne14900ee2015-11-27 14:43:21193 // Populate command line flags from ReaderModeEnabled.
194 if ([defaults boolForKey:@"ReaderModeEnabled"]) {
195 command_line->AppendSwitch(switches::kEnableReaderModeToolbarIcon);
196
197 // Populate command line from ReaderMode Heuristics detection.
198 NSString* readerModeDetectionHeuristics =
199 [defaults stringForKey:@"ReaderModeDetectionHeuristics"];
200 if (!readerModeDetectionHeuristics) {
noyaudb4a2d62016-09-26 11:04:45201 command_line->AppendSwitchASCII(
202 switches::kReaderModeHeuristics,
203 switches::reader_mode_heuristics::kOGArticle);
sdefresne14900ee2015-11-27 14:43:21204 } else if ([readerModeDetectionHeuristics isEqualToString:@"AdaBoost"]) {
noyaudb4a2d62016-09-26 11:04:45205 command_line->AppendSwitchASCII(
206 switches::kReaderModeHeuristics,
207 switches::reader_mode_heuristics::kAdaBoost);
sdefresne14900ee2015-11-27 14:43:21208 } else {
209 DCHECK([readerModeDetectionHeuristics isEqualToString:@"Off"]);
noyaudb4a2d62016-09-26 11:04:45210 command_line->AppendSwitchASCII(switches::kReaderModeHeuristics,
211 switches::reader_mode_heuristics::kNone);
sdefresne14900ee2015-11-27 14:43:21212 }
213 }
214
sdefresne14900ee2015-11-27 14:43:21215 // Set the UA flag if UseMobileSafariUA is enabled.
216 if ([defaults boolForKey:@"UseMobileSafariUA"]) {
217 // Safari uses "Vesion/", followed by the OS version excluding bugfix, where
218 // Chrome puts its product token.
avi571943672015-12-22 02:12:49219 int32_t major = 0;
220 int32_t minor = 0;
221 int32_t bugfix = 0;
sdefresne14900ee2015-11-27 14:43:21222 base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
223 std::string product = base::StringPrintf("Version/%d.%d", major, minor);
224
225 command_line->AppendSwitchASCII(switches::kUserAgent,
226 web::BuildUserAgentFromProduct(product));
227 }
228
gambardd2e44fb2017-01-25 09:14:21229 // Populate command line flag for Suggestions UI display.
230 NSString* enableSuggestions = [defaults stringForKey:@"EnableSuggestions"];
231 if ([enableSuggestions isEqualToString:@"Enabled"]) {
232 command_line->AppendSwitch(switches::kEnableSuggestionsUI);
233 } else if ([enableSuggestions isEqualToString:@"Disabled"]) {
234 command_line->AppendSwitch(switches::kDisableSuggestionsUI);
235 }
236
jkrcal7d79de52017-05-05 18:13:10237 // Populate command line flag for fetching missing favicons for NTP tiles.
238 NSString* enableMostLikelyFaviconsFromServer =
239 [defaults stringForKey:@"EnableNtpMostLikelyFaviconsFromServer"];
240 if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Enabled"]) {
241 command_line->AppendSwitch(
242 ntp_tiles::switches::kEnableNtpMostLikelyFaviconsFromServer);
243 } else if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Disabled"]) {
244 command_line->AppendSwitch(
245 ntp_tiles::switches::kDisableNtpMostLikelyFaviconsFromServer);
246 }
247
Danyao Wang6ab20f72017-06-20 14:13:41248 // Populate command line flag for the native to WKBackForwardList based
249 // navigation manager experiment.
250 NSString* enableSlimNavigationManager =
251 [defaults stringForKey:@"EnableSlimNavigationManager"];
252 if ([enableSlimNavigationManager isEqualToString:@"Enabled"]) {
253 command_line->AppendSwitch(switches::kEnableSlimNavigationManager);
254 } else if ([enableSlimNavigationManager isEqualToString:@"Disabled"]) {
255 command_line->AppendSwitch(switches::kDisableSlimNavigationManager);
256 }
257
sdefresne14900ee2015-11-27 14:43:21258 // Freeform commandline flags. These are added last, so that any flags added
259 // earlier in this function take precedence.
260 if ([defaults boolForKey:@"EnableFreeformCommandLineFlags"]) {
261 base::CommandLine::StringVector flags;
262 // Append an empty "program" argument.
263 flags.push_back("");
264
265 // The number of flags corresponds to the number of text fields in
266 // Experimental.plist.
267 const int kNumFreeformFlags = 5;
268 for (int i = 1; i <= kNumFreeformFlags; ++i) {
269 NSString* key =
270 [NSString stringWithFormat:@"FreeformCommandLineFlag%d", i];
271 NSString* flag = [defaults stringForKey:key];
272 if ([flag length]) {
273 flags.push_back(base::SysNSStringToUTF8(flag));
274 }
275 }
276
277 base::CommandLine temp_command_line(flags);
278 command_line->AppendArguments(temp_command_line, false);
279 }
msardafc76f662017-02-24 12:46:28280
jlebel2cf54d72017-03-29 13:51:40281 // Populate command line flag for Sign-in promo.
282 NSString* enableSigninPromo = [defaults stringForKey:@"EnableSigninPromo"];
283 if ([enableSigninPromo isEqualToString:@"Enabled"]) {
284 command_line->AppendSwitch(switches::kEnableSigninPromo);
285 } else if ([enableSigninPromo isEqualToString:@"Disabled"]) {
286 command_line->AppendSwitch(switches::kDisableSigninPromo);
287 }
288
martiwed82d452017-06-19 00:10:26289 // Populate command line flag for Bookmark reordering.
290 NSString* enableBookmarkReordering =
291 [defaults stringForKey:@"EnableBookmarkReordering"];
292 if ([enableBookmarkReordering isEqualToString:@"Enabled"]) {
293 command_line->AppendSwitch(switches::kEnableBookmarkReordering);
294 } else if ([enableBookmarkReordering isEqualToString:@"Disabled"]) {
295 command_line->AppendSwitch(switches::kDisableBookmarkReordering);
296 }
297
justincohendacc85d2017-06-28 23:34:10298 // Populate command line flag for 3rd party keyboard omnibox workaround.
299 NSString* enableThirdPartyKeyboardWorkaround =
300 [defaults stringForKey:@"EnableThirdPartyKeyboardWorkaround"];
301 if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Enabled"]) {
302 command_line->AppendSwitch(switches::kEnableThirdPartyKeyboardWorkaround);
303 } else if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Disabled"]) {
304 command_line->AppendSwitch(switches::kDisableThirdPartyKeyboardWorkaround);
305 }
306
msardafc76f662017-02-24 12:46:28307 ios::GetChromeBrowserProvider()->AppendSwitchesFromExperimentalSettings(
308 defaults, command_line);
sdefresne14900ee2015-11-27 14:43:21309}
310
311bool SkipConditionalFeatureEntry(const flags_ui::FeatureEntry& entry) {
312 return false;
313}
314
315class FlagsStateSingleton {
316 public:
317 FlagsStateSingleton()
318 : flags_state_(kFeatureEntries, arraysize(kFeatureEntries)) {}
319 ~FlagsStateSingleton() {}
320
321 static FlagsStateSingleton* GetInstance() {
322 return base::Singleton<FlagsStateSingleton>::get();
323 }
324
325 static flags_ui::FlagsState* GetFlagsState() {
326 return &GetInstance()->flags_state_;
327 }
328
329 private:
330 flags_ui::FlagsState flags_state_;
331
332 DISALLOW_COPY_AND_ASSIGN(FlagsStateSingleton);
333};
334} // namespace
335
336void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
337 base::CommandLine* command_line) {
338 FlagsStateSingleton::GetFlagsState()->ConvertFlagsToSwitches(
sdefresnec9763902015-12-02 10:30:11339 flags_storage, command_line, flags_ui::kAddSentinels,
340 switches::kEnableIOSFeatures, switches::kDisableIOSFeatures);
sdefresne14900ee2015-11-27 14:43:21341 AppendSwitchesFromExperimentalSettings(command_line);
342}
343
jkrcalbf073372016-07-29 07:21:31344std::vector<std::string> RegisterAllFeatureVariationParameters(
345 flags_ui::FlagsStorage* flags_storage,
346 base::FeatureList* feature_list) {
347 return FlagsStateSingleton::GetFlagsState()
348 ->RegisterAllFeatureVariationParameters(flags_storage, feature_list);
349}
350
sdefresne14900ee2015-11-27 14:43:21351void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
352 flags_ui::FlagAccess access,
353 base::ListValue* supported_entries,
354 base::ListValue* unsupported_entries) {
355 FlagsStateSingleton::GetFlagsState()->GetFlagFeatureEntries(
356 flags_storage, access, supported_entries, unsupported_entries,
357 base::Bind(&SkipConditionalFeatureEntry));
358}
359
360void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
361 const std::string& internal_name,
362 bool enable) {
363 FlagsStateSingleton::GetFlagsState()->SetFeatureEntryEnabled(
364 flags_storage, internal_name, enable);
365}
366
367void ResetAllFlags(flags_ui::FlagsStorage* flags_storage) {
368 FlagsStateSingleton::GetFlagsState()->ResetAllFlags(flags_storage);
369}
370
371namespace testing {
372
373const flags_ui::FeatureEntry* GetFeatureEntries(size_t* count) {
374 *count = arraysize(kFeatureEntries);
375 return kFeatureEntries;
376}
377
378} // namespace testing