Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 4b79d93

Browse files
rohitraoCommit bot
rohitrao
authored and
Commit bot
committed
Adds an IOSWebViewTypeParam() method to SearchTermsData.
This method returns the type of the iOS webview currently in use, formatted as a cgi param suitable for appending to omnibox queries. This CL also updates the corresponding experimental flags methods to cache the current experiment state, since the state will soon be queried often when constructing omnibox results. BUG=551851 TEST=None Review URL: https://codereview.chromium.org/1495643002 Cr-Commit-Position: refs/heads/master@{#362773}
1 parent e250e6a commit 4b79d93

File tree

6 files changed

+91
-27
lines changed

6 files changed

+91
-27
lines changed

components/search_engines/search_terms_data.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ std::string SearchTermsData::NTPIsThemedParam() const {
7676
return std::string();
7777
}
7878

79+
std::string SearchTermsData::IOSWebViewTypeParam() const {
80+
return std::string();
81+
}
82+
7983
std::string SearchTermsData::GoogleImageSearchSource() const {
8084
return std::string();
8185
}

components/search_engines/search_terms_data.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class SearchTermsData {
7070
// non-empty for UIThreadSearchTermsData.
7171
virtual std::string NTPIsThemedParam() const;
7272

73+
// Returns a string indicating which webview is currently in use on iOS,
74+
// suitable for adding as a query string param to search requests. Returns an
75+
// empty string if no parameter should be passed along with search requests.
76+
virtual std::string IOSWebViewTypeParam() const;
77+
7378
// Returns the value to use for replacements of type
7479
// GOOGLE_IMAGE_SEARCH_SOURCE.
7580
virtual std::string GoogleImageSearchSource() const;

ios/chrome/browser/experimental_flags.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef IOS_CHROME_BROWSER_EXPERIMENTAL_FLAGS_H_
66
#define IOS_CHROME_BROWSER_EXPERIMENTAL_FLAGS_H_
77

8+
#include <string>
9+
810
// This file can be empty. Its purpose is to contain the relatively short lived
911
// declarations required for experimental flags.
1012

@@ -30,6 +32,10 @@ bool IsLRUSnapshotCacheEnabled();
3032
// The returned value will not change within a given session.
3133
bool IsWKWebViewEnabled();
3234

35+
// Returns a string containing extra params that should be sent along with
36+
// omnibox search requests. The returned value contains a leading "&".
37+
std::string GetWKWebViewSearchParams();
38+
3339
// Whether keyboard commands are supported.
3440
bool AreKeyboardCommandsEnabled();
3541

ios/chrome/browser/experimental_flags.mm

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "ios/chrome/browser/experimental_flags.h"
99

10+
#include <dispatch/dispatch.h>
1011
#import <Foundation/Foundation.h>
1112

1213
#include <string>
@@ -74,49 +75,84 @@ bool IsLRUSnapshotCacheEnabled() {
7475
base::CompareCase::INSENSITIVE_ASCII);
7576
}
7677

77-
bool IsWKWebViewEnabled() {
78-
// If g_wkwebview_trial_eligibility hasn't been set, default it to
79-
// ineligibile. This ensures future calls to try to set it will DCHECK.
80-
if (g_wkwebview_trial_eligibility == WKWebViewEligibility::UNSET) {
81-
g_wkwebview_trial_eligibility = WKWebViewEligibility::INELIGIBLE;
82-
}
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+
}
83125

84-
// If WKWebView isn't supported, don't activate the experiment at all. This
85-
// avoids someone being slotted into the WKWebView bucket (and thus reporting
86-
// as WKWebView), but actually running UIWebView.
87-
if (!web::IsWKWebViewSupported())
126+
bool IsWKWebViewEnabled() {
127+
if (!CanCheckWKWebViewExperiment()) {
88128
return false;
129+
}
89130

90-
// Check for a flag forcing a specific group.
131+
// Check if the experimental flag is turned on.
91132
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
92-
bool force_enable = command_line->HasSwitch(switches::kEnableIOSWKWebView);
93-
bool force_disable = command_line->HasSwitch(switches::kDisableIOSWKWebView);
94-
bool trial_overridden = force_enable || force_disable;
95-
96-
// If the user isn't eligible for the trial (i.e., their state is such that
97-
// they should not be automatically selected for the group), and there's no
98-
// explicit override, don't check the group (again, to avoid having them
99-
// report as part of a group at all).
100-
if (g_wkwebview_trial_eligibility == WKWebViewEligibility::INELIGIBLE &&
101-
!trial_overridden)
133+
if (command_line->HasSwitch(switches::kEnableIOSWKWebView))
134+
return true;
135+
else if (command_line->HasSwitch(switches::kDisableIOSWKWebView))
102136
return false;
103137

104138
// Now that it's been established that user is a candidate, set up the trial
105139
// by checking the group.
106140
std::string group_name =
107141
base::FieldTrialList::FindFullName("IOSUseWKWebView");
108142

109-
// Check if the experimental flag is turned on.
110-
if (force_enable)
111-
return true;
112-
else if (force_disable)
113-
return false;
114-
115143
// Check if the finch experiment is turned on.
116144
return base::StartsWith(group_name, "Enabled",
117145
base::CompareCase::INSENSITIVE_ASCII);
118146
}
119147

148+
std::string GetWKWebViewSearchParams() {
149+
if (!CanCheckWKWebViewExperiment()) {
150+
return std::string();
151+
}
152+
153+
return variations::GetVariationParamValue("IOSUseWKWebView", "esrch");
154+
}
155+
120156
bool AreKeyboardCommandsEnabled() {
121157
return !base::CommandLine::ForCurrentProcess()->HasSwitch(
122158
switches::kDisableKeyboardCommands);

ios/chrome/browser/search_engines/ui_thread_search_terms_data.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
#include "components/search/search.h"
1313
#include "components/version_info/version_info.h"
1414
#include "ios/chrome/browser/application_context.h"
15+
#include "ios/chrome/browser/experimental_flags.h"
1516
#include "ios/chrome/browser/google/google_brand.h"
1617
#include "ios/chrome/browser/google/google_url_tracker_factory.h"
1718
#include "ios/chrome/common/channel_info.h"
1819
#include "ios/web/public/web_thread.h"
20+
#include "net/base/escape.h"
1921
#include "url/gurl.h"
2022

2123
#if defined(ENABLE_RLZ)
@@ -119,6 +121,16 @@ std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
119121
return std::string();
120122
}
121123

124+
std::string UIThreadSearchTermsData::IOSWebViewTypeParam() const {
125+
DCHECK(thread_checker_.CalledOnValidThread());
126+
std::string param = experimental_flags::GetWKWebViewSearchParams();
127+
if (param.empty()) {
128+
return std::string();
129+
}
130+
131+
return "&esrch=" + net::EscapeQueryParamValue(param, true);
132+
}
133+
122134
std::string UIThreadSearchTermsData::GoogleImageSearchSource() const {
123135
DCHECK(thread_checker_.CalledOnValidThread());
124136
std::string version(version_info::GetProductName() + " " +

ios/chrome/browser/search_engines/ui_thread_search_terms_data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class UIThreadSearchTermsData : public SearchTermsData {
3131
std::string ForceInstantResultsParam(bool for_prerender) const override;
3232
int OmniboxStartMargin() const override;
3333
std::string NTPIsThemedParam() const override;
34+
std::string IOSWebViewTypeParam() const override;
3435
std::string GoogleImageSearchSource() const override;
3536

3637
private:

0 commit comments

Comments
 (0)