blob: a1022464eff0c0b3da91ebdb3db1c4aab0e8eaab [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"
Mike Doughertya5e5ad52017-08-05 00:11:5838#include "ios/chrome/browser/ssl/captive_portal_features.h"
sdefresne14900ee2015-11-27 14:43:2139#include "ios/chrome/grit/ios_strings.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 Treib1aa164b2017-08-08 15:37:4070const FeatureEntry::FeatureParam kUseDdljsonApiTest0[] = {
71 {search_provider_logos::features::kDdljsonOverrideUrlParam,
72 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios0.json"}};
Marc Treib2752e8b2017-08-04 14:12:0973const FeatureEntry::FeatureParam kUseDdljsonApiTest1[] = {
74 {search_provider_logos::features::kDdljsonOverrideUrlParam,
75 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios1.json"}};
76const FeatureEntry::FeatureParam kUseDdljsonApiTest2[] = {
77 {search_provider_logos::features::kDdljsonOverrideUrlParam,
78 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios2.json"}};
79const FeatureEntry::FeatureParam kUseDdljsonApiTest3[] = {
80 {search_provider_logos::features::kDdljsonOverrideUrlParam,
81 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios3.json"}};
82const FeatureEntry::FeatureParam kUseDdljsonApiTest4[] = {
83 {search_provider_logos::features::kDdljsonOverrideUrlParam,
84 "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_ios4.json"}};
85
86const FeatureEntry::FeatureVariation kUseDdljsonApiVariations[] = {
Marc Treib1aa164b2017-08-08 15:37:4087 {"(force test doodle 0)", kUseDdljsonApiTest0,
88 arraysize(kUseDdljsonApiTest0), nullptr},
Marc Treib2752e8b2017-08-04 14:12:0989 {"(force test doodle 1)", kUseDdljsonApiTest1,
90 arraysize(kUseDdljsonApiTest1), nullptr},
91 {"(force test doodle 2)", kUseDdljsonApiTest2,
92 arraysize(kUseDdljsonApiTest2), nullptr},
93 {"(force test doodle 3)", kUseDdljsonApiTest3,
94 arraysize(kUseDdljsonApiTest3), nullptr},
95 {"(force test doodle 4)", kUseDdljsonApiTest4,
96 arraysize(kUseDdljsonApiTest4), nullptr}};
97
Cooper Knaak1e026562017-07-26 05:22:2898// To add a new entry, add to the end of kFeatureEntries. There are four
sdefresne14900ee2015-11-27 14:43:2199// distinct types of entries:
Cooper Knaak1e026562017-07-26 05:22:28100// . ENABLE_DISABLE_VALUE: entry is either enabled, disabled, or uses the
101// default value for this feature. Use the ENABLE_DISABLE_VALUE_TYPE
sdefresne14900ee2015-11-27 14:43:21102// macro for this type supplying the command line to the macro.
103// . MULTI_VALUE: a list of choices, the first of which should correspond to a
104// deactivated state for this lab (i.e. no command line option). To specify
105// this type of entry use the macro MULTI_VALUE_TYPE supplying it the
106// array of choices.
Cooper Knaak1e026562017-07-26 05:22:28107// . FEATURE_VALUE: entry is associated with a base::Feature instance. Entry is
108// either enabled, disabled, or uses the default value of the associated
109// base::Feature instance. To specify this type of entry use the macro
110// FEATURE_VALUE_TYPE supplying it the base::Feature instance.
111// . FEATURE_WITH_PARAM_VALUES: a list of choices associated with a
112// base::Feature instance. Choices corresponding to the default state, a
113// universally enabled state, and a universally disabled state are
114// automatically included. To specify this type of entry use the macro
115// FEATURE_WITH_PARAMS_VALUE_TYPE supplying it the base::Feature instance and
116// the array of choices.
117//
sdefresne14900ee2015-11-27 14:43:21118// See the documentation of FeatureEntry for details on the fields.
119//
120// When adding a new choice, add it to the end of the list.
121const flags_ui::FeatureEntry kFeatureEntries[] = {
vabr0215a8e2017-03-28 12:47:34122 {"contextual-search", flag_descriptions::kContextualSearch,
123 flag_descriptions::kContextualSearchDescription, flags_ui::kOsIos,
sdefresne14900ee2015-11-27 14:43:21124 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableContextualSearch,
125 switches::kDisableContextualSearch)},
vabr0215a8e2017-03-28 12:47:34126 {"ios-physical-web", flag_descriptions::kPhysicalWeb,
127 flag_descriptions::kPhysicalWebDescription, flags_ui::kOsIos,
mattreynolds1a4181f2016-10-05 23:50:00128 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableIOSPhysicalWeb,
129 switches::kDisableIOSPhysicalWeb)},
vabr0215a8e2017-03-28 12:47:34130 {"browser-task-scheduler", flag_descriptions::kBrowserTaskScheduler,
131 flag_descriptions::kBrowserTaskSchedulerDescription, flags_ui::kOsIos,
robliao59eed1a2016-10-28 17:12:16132 ENABLE_DISABLE_VALUE_TYPE(switches::kEnableBrowserTaskScheduler,
133 switches::kDisableBrowserTaskScheduler)},
elawrence816f6790e2017-06-16 18:19:28134 {"mark-non-secure-as", flag_descriptions::kMarkHttpAsName,
135 flag_descriptions::kMarkHttpAsDescription, flags_ui::kOsIos,
136 MULTI_VALUE_TYPE(kMarkHttpAsChoices)},
mathp9b4c11d2017-07-06 20:24:13137 {"web-payments", flag_descriptions::kWebPaymentsName,
138 flag_descriptions::kWebPaymentsDescription, flags_ui::kOsIos,
139 FEATURE_VALUE_TYPE(payments::features::kWebPayments)},
Mike Doughertya5e5ad52017-08-05 00:11:58140 {"ios-captive-portal", flag_descriptions::kCaptivePortalName,
141 flag_descriptions::kCaptivePortalDescription, flags_ui::kOsIos,
142 FEATURE_VALUE_TYPE(kCaptivePortalFeature)},
Cooper Knaak1e026562017-07-26 05:22:28143 {"in-product-help-demo-mode-choice",
144 flag_descriptions::kInProductHelpDemoModeName,
145 flag_descriptions::kInProductHelpDemoModeDescription, flags_ui::kOsIos,
146 FEATURE_WITH_PARAMS_VALUE_TYPE(
Tommy Nyquistc1d6dea12017-07-26 20:37:23147 feature_engagement::kIPHDemoMode,
148 feature_engagement::kIPHDemoModeChoiceVariations,
Marc Treib2752e8b2017-08-04 14:12:09149 "IPH_DemoMode")},
150 {"use-ddljson-api", flag_descriptions::kUseDdljsonApiName,
151 flag_descriptions::kUseDdljsonApiDescription, flags_ui::kOsIos,
152 FEATURE_WITH_PARAMS_VALUE_TYPE(
153 search_provider_logos::features::kUseDdljsonApi,
154 kUseDdljsonApiVariations,
Marc Treib46d33ef82017-08-07 15:06:53155 "NTPUseDdljsonApi")}};
sdefresne14900ee2015-11-27 14:43:21156
157// Add all switches from experimental flags to |command_line|.
158void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) {
159 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
160
mattreynoldsc9f1df52016-06-29 08:30:04161 // Populate command line flags from PhysicalWebEnabled.
162 NSString* enablePhysicalWebValue =
163 [defaults stringForKey:@"PhysicalWebEnabled"];
164 if ([enablePhysicalWebValue isEqualToString:@"Enabled"]) {
165 command_line->AppendSwitch(switches::kEnableIOSPhysicalWeb);
166 } else if ([enablePhysicalWebValue isEqualToString:@"Disabled"]) {
167 command_line->AppendSwitch(switches::kDisableIOSPhysicalWeb);
168 }
169
sdefresne14900ee2015-11-27 14:43:21170 // Web page replay flags.
171 BOOL webPageReplayEnabled = [defaults boolForKey:@"WebPageReplayEnabled"];
172 NSString* webPageReplayProxy =
173 [defaults stringForKey:@"WebPageReplayProxyAddress"];
174 if (webPageReplayEnabled && [webPageReplayProxy length]) {
175 command_line->AppendSwitch(switches::kIOSIgnoreCertificateErrors);
176 // 80 and 443 are the default ports from web page replay.
177 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpPort, "80");
178 command_line->AppendSwitchASCII(switches::kIOSTestingFixedHttpsPort, "443");
179 command_line->AppendSwitchASCII(
180 switches::kIOSHostResolverRules,
181 "MAP * " + base::SysNSStringToUTF8(webPageReplayProxy));
182 }
183
sdefresne14900ee2015-11-27 14:43:21184 // Populate command line flags from ReaderModeEnabled.
185 if ([defaults boolForKey:@"ReaderModeEnabled"]) {
186 command_line->AppendSwitch(switches::kEnableReaderModeToolbarIcon);
187
188 // Populate command line from ReaderMode Heuristics detection.
189 NSString* readerModeDetectionHeuristics =
190 [defaults stringForKey:@"ReaderModeDetectionHeuristics"];
191 if (!readerModeDetectionHeuristics) {
noyaudb4a2d62016-09-26 11:04:45192 command_line->AppendSwitchASCII(
193 switches::kReaderModeHeuristics,
194 switches::reader_mode_heuristics::kOGArticle);
sdefresne14900ee2015-11-27 14:43:21195 } else if ([readerModeDetectionHeuristics isEqualToString:@"AdaBoost"]) {
noyaudb4a2d62016-09-26 11:04:45196 command_line->AppendSwitchASCII(
197 switches::kReaderModeHeuristics,
198 switches::reader_mode_heuristics::kAdaBoost);
sdefresne14900ee2015-11-27 14:43:21199 } else {
200 DCHECK([readerModeDetectionHeuristics isEqualToString:@"Off"]);
noyaudb4a2d62016-09-26 11:04:45201 command_line->AppendSwitchASCII(switches::kReaderModeHeuristics,
202 switches::reader_mode_heuristics::kNone);
sdefresne14900ee2015-11-27 14:43:21203 }
204 }
205
sdefresne14900ee2015-11-27 14:43:21206 // Set the UA flag if UseMobileSafariUA is enabled.
207 if ([defaults boolForKey:@"UseMobileSafariUA"]) {
208 // Safari uses "Vesion/", followed by the OS version excluding bugfix, where
209 // Chrome puts its product token.
avi571943672015-12-22 02:12:49210 int32_t major = 0;
211 int32_t minor = 0;
212 int32_t bugfix = 0;
sdefresne14900ee2015-11-27 14:43:21213 base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
214 std::string product = base::StringPrintf("Version/%d.%d", major, minor);
215
216 command_line->AppendSwitchASCII(switches::kUserAgent,
217 web::BuildUserAgentFromProduct(product));
218 }
219
gambardd2e44fb2017-01-25 09:14:21220 // Populate command line flag for Suggestions UI display.
221 NSString* enableSuggestions = [defaults stringForKey:@"EnableSuggestions"];
222 if ([enableSuggestions isEqualToString:@"Enabled"]) {
223 command_line->AppendSwitch(switches::kEnableSuggestionsUI);
224 } else if ([enableSuggestions isEqualToString:@"Disabled"]) {
225 command_line->AppendSwitch(switches::kDisableSuggestionsUI);
226 }
227
jkrcal7d79de52017-05-05 18:13:10228 // Populate command line flag for fetching missing favicons for NTP tiles.
229 NSString* enableMostLikelyFaviconsFromServer =
230 [defaults stringForKey:@"EnableNtpMostLikelyFaviconsFromServer"];
231 if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Enabled"]) {
232 command_line->AppendSwitch(
233 ntp_tiles::switches::kEnableNtpMostLikelyFaviconsFromServer);
234 } else if ([enableMostLikelyFaviconsFromServer isEqualToString:@"Disabled"]) {
235 command_line->AppendSwitch(
236 ntp_tiles::switches::kDisableNtpMostLikelyFaviconsFromServer);
237 }
238
Danyao Wang6ab20f72017-06-20 14:13:41239 // Populate command line flag for the native to WKBackForwardList based
240 // navigation manager experiment.
241 NSString* enableSlimNavigationManager =
242 [defaults stringForKey:@"EnableSlimNavigationManager"];
243 if ([enableSlimNavigationManager isEqualToString:@"Enabled"]) {
244 command_line->AppendSwitch(switches::kEnableSlimNavigationManager);
245 } else if ([enableSlimNavigationManager isEqualToString:@"Disabled"]) {
246 command_line->AppendSwitch(switches::kDisableSlimNavigationManager);
247 }
248
sdefresne14900ee2015-11-27 14:43:21249 // Freeform commandline flags. These are added last, so that any flags added
250 // earlier in this function take precedence.
251 if ([defaults boolForKey:@"EnableFreeformCommandLineFlags"]) {
252 base::CommandLine::StringVector flags;
253 // Append an empty "program" argument.
254 flags.push_back("");
255
256 // The number of flags corresponds to the number of text fields in
257 // Experimental.plist.
258 const int kNumFreeformFlags = 5;
259 for (int i = 1; i <= kNumFreeformFlags; ++i) {
260 NSString* key =
261 [NSString stringWithFormat:@"FreeformCommandLineFlag%d", i];
262 NSString* flag = [defaults stringForKey:key];
263 if ([flag length]) {
264 flags.push_back(base::SysNSStringToUTF8(flag));
265 }
266 }
267
268 base::CommandLine temp_command_line(flags);
269 command_line->AppendArguments(temp_command_line, false);
270 }
msardafc76f662017-02-24 12:46:28271
jlebel2cf54d72017-03-29 13:51:40272 // Populate command line flag for Sign-in promo.
273 NSString* enableSigninPromo = [defaults stringForKey:@"EnableSigninPromo"];
274 if ([enableSigninPromo isEqualToString:@"Enabled"]) {
275 command_line->AppendSwitch(switches::kEnableSigninPromo);
276 } else if ([enableSigninPromo isEqualToString:@"Disabled"]) {
277 command_line->AppendSwitch(switches::kDisableSigninPromo);
278 }
279
martiwed82d452017-06-19 00:10:26280 // Populate command line flag for Bookmark reordering.
281 NSString* enableBookmarkReordering =
282 [defaults stringForKey:@"EnableBookmarkReordering"];
283 if ([enableBookmarkReordering isEqualToString:@"Enabled"]) {
284 command_line->AppendSwitch(switches::kEnableBookmarkReordering);
285 } else if ([enableBookmarkReordering isEqualToString:@"Disabled"]) {
286 command_line->AppendSwitch(switches::kDisableBookmarkReordering);
287 }
288
justincohendacc85d2017-06-28 23:34:10289 // Populate command line flag for 3rd party keyboard omnibox workaround.
290 NSString* enableThirdPartyKeyboardWorkaround =
291 [defaults stringForKey:@"EnableThirdPartyKeyboardWorkaround"];
292 if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Enabled"]) {
293 command_line->AppendSwitch(switches::kEnableThirdPartyKeyboardWorkaround);
294 } else if ([enableThirdPartyKeyboardWorkaround isEqualToString:@"Disabled"]) {
295 command_line->AppendSwitch(switches::kDisableThirdPartyKeyboardWorkaround);
296 }
297
msardafc76f662017-02-24 12:46:28298 ios::GetChromeBrowserProvider()->AppendSwitchesFromExperimentalSettings(
299 defaults, command_line);
sdefresne14900ee2015-11-27 14:43:21300}
301
302bool SkipConditionalFeatureEntry(const flags_ui::FeatureEntry& entry) {
303 return false;
304}
305
306class FlagsStateSingleton {
307 public:
308 FlagsStateSingleton()
309 : flags_state_(kFeatureEntries, arraysize(kFeatureEntries)) {}
310 ~FlagsStateSingleton() {}
311
312 static FlagsStateSingleton* GetInstance() {
313 return base::Singleton<FlagsStateSingleton>::get();
314 }
315
316 static flags_ui::FlagsState* GetFlagsState() {
317 return &GetInstance()->flags_state_;
318 }
319
320 private:
321 flags_ui::FlagsState flags_state_;
322
323 DISALLOW_COPY_AND_ASSIGN(FlagsStateSingleton);
324};
325} // namespace
326
327void ConvertFlagsToSwitches(flags_ui::FlagsStorage* flags_storage,
328 base::CommandLine* command_line) {
329 FlagsStateSingleton::GetFlagsState()->ConvertFlagsToSwitches(
sdefresnec9763902015-12-02 10:30:11330 flags_storage, command_line, flags_ui::kAddSentinels,
331 switches::kEnableIOSFeatures, switches::kDisableIOSFeatures);
sdefresne14900ee2015-11-27 14:43:21332 AppendSwitchesFromExperimentalSettings(command_line);
333}
334
jkrcalbf073372016-07-29 07:21:31335std::vector<std::string> RegisterAllFeatureVariationParameters(
336 flags_ui::FlagsStorage* flags_storage,
337 base::FeatureList* feature_list) {
338 return FlagsStateSingleton::GetFlagsState()
339 ->RegisterAllFeatureVariationParameters(flags_storage, feature_list);
340}
341
sdefresne14900ee2015-11-27 14:43:21342void GetFlagFeatureEntries(flags_ui::FlagsStorage* flags_storage,
343 flags_ui::FlagAccess access,
344 base::ListValue* supported_entries,
345 base::ListValue* unsupported_entries) {
346 FlagsStateSingleton::GetFlagsState()->GetFlagFeatureEntries(
347 flags_storage, access, supported_entries, unsupported_entries,
348 base::Bind(&SkipConditionalFeatureEntry));
349}
350
351void SetFeatureEntryEnabled(flags_ui::FlagsStorage* flags_storage,
352 const std::string& internal_name,
353 bool enable) {
354 FlagsStateSingleton::GetFlagsState()->SetFeatureEntryEnabled(
355 flags_storage, internal_name, enable);
356}
357
358void ResetAllFlags(flags_ui::FlagsStorage* flags_storage) {
359 FlagsStateSingleton::GetFlagsState()->ResetAllFlags(flags_storage);
360}
361
362namespace testing {
363
364const flags_ui::FeatureEntry* GetFeatureEntries(size_t* count) {
365 *count = arraysize(kFeatureEntries);
366 return kFeatureEntries;
367}
368
369} // namespace testing