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

Commit 5d34f8f

Browse files
author
Stuart Morgan
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} (cherry picked from commit 4b79d93) Review URL: https://codereview.chromium.org/1492163004 . Cr-Commit-Position: refs/branch-heads/2564@{#218} Cr-Branched-From: 1283eca-refs/heads/master@{#359700}
1 parent 35bd3cd commit 5d34f8f

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>
@@ -70,49 +71,84 @@ bool IsLRUSnapshotCacheEnabled() {
7071
base::CompareCase::INSENSITIVE_ASCII);
7172
}
7273

73-
bool IsWKWebViewEnabled() {
74-
// If g_wkwebview_trial_eligibility hasn't been set, default it to
75-
// ineligibile. This ensures future calls to try to set it will DCHECK.
76-
if (g_wkwebview_trial_eligibility == WKWebViewEligibility::UNSET) {
77-
g_wkwebview_trial_eligibility = WKWebViewEligibility::INELIGIBLE;
78-
}
74+
// Helper method that returns true if it is safe to check the finch group for
75+
// the IOSUseWKWebView experiment. Some users are ineligible to be in the
76+
// trial, so for those users, this method returns false. If this method returns
77+
// false, do not check for the current finch group, as doing so will incorrectly
78+
// mark the current user as being in the experiment.
79+
bool CanCheckWKWebViewExperiment() {
80+
// True if this user is eligible for the WKWebView experiment and it is ok to
81+
// check the experiment group.
82+
static bool ok_to_check_finch = false;
83+
static dispatch_once_t once;
84+
dispatch_once(&once, ^{
85+
// If g_wkwebview_trial_eligibility hasn't been set, default it to
86+
// ineligible. This ensures future calls to try to set it will DCHECK.
87+
if (g_wkwebview_trial_eligibility == WKWebViewEligibility::UNSET) {
88+
g_wkwebview_trial_eligibility = WKWebViewEligibility::INELIGIBLE;
89+
}
90+
91+
// If WKWebView isn't supported, don't activate the experiment at all. This
92+
// avoids someone being slotted into the WKWebView bucket (and thus
93+
// reporting as WKWebView), but actually running UIWebView.
94+
if (!web::IsWKWebViewSupported()) {
95+
ok_to_check_finch = false;
96+
return;
97+
}
98+
99+
// Check for a flag forcing a specific group. Even ineligible users can be
100+
// opted into WKWebView if an override flag is set.
101+
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
102+
bool trial_overridden =
103+
command_line->HasSwitch(switches::kEnableIOSWKWebView) ||
104+
command_line->HasSwitch(switches::kDisableIOSWKWebView);
105+
106+
// If the user isn't eligible for the trial (i.e., their state is such that
107+
// they should not be automatically selected for the group), and there's no
108+
// explicit override, don't check the group (again, to avoid having them
109+
// report as part of a group at all).
110+
if (g_wkwebview_trial_eligibility == WKWebViewEligibility::INELIGIBLE &&
111+
!trial_overridden) {
112+
ok_to_check_finch = false;
113+
return;
114+
}
115+
116+
ok_to_check_finch = true;
117+
});
118+
119+
return ok_to_check_finch;
120+
}
79121

80-
// If WKWebView isn't supported, don't activate the experiment at all. This
81-
// avoids someone being slotted into the WKWebView bucket (and thus reporting
82-
// as WKWebView), but actually running UIWebView.
83-
if (!web::IsWKWebViewSupported())
122+
bool IsWKWebViewEnabled() {
123+
if (!CanCheckWKWebViewExperiment()) {
84124
return false;
125+
}
85126

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

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

105-
// Check if the experimental flag is turned on.
106-
if (force_enable)
107-
return true;
108-
else if (force_disable)
109-
return false;
110-
111139
// Check if the finch experiment is turned on.
112140
return base::StartsWith(group_name, "Enabled",
113141
base::CompareCase::INSENSITIVE_ASCII);
114142
}
115143

144+
std::string GetWKWebViewSearchParams() {
145+
if (!CanCheckWKWebViewExperiment()) {
146+
return std::string();
147+
}
148+
149+
return variations::GetVariationParamValue("IOSUseWKWebView", "esrch");
150+
}
151+
116152
bool AreKeyboardCommandsEnabled() {
117153
return !base::CommandLine::ForCurrentProcess()->HasSwitch(
118154
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)