Allow non default prefetch suggestions to be prefetched

The SearchProvider may mark some suggestions to be prefetched based on
instructions from the suggest server. Currently we prefetch it only if such a
match ranks sufficiently highly. This cl allows to prefetch the suggestion
regardless of its autocomplete rank if allow_prefetch_non_default_match field
trial is enabled.

BUG=386203

Review URL: https://codereview.chromium.org/324273004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278668 0039d316-1c4b-4281-b951-d872f2087c98
parent d7ae8102
......@@ -67,6 +67,7 @@ const uint64 kEmbeddedPageVersionDefault = 2;
const char kHideVerbatimFlagName[] = "hide_verbatim";
const char kPrefetchSearchResultsFlagName[] = "prefetch_results";
const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp";
const char kAllowPrefetchNonDefaultMatch[] = "allow_prefetch_non_default_match";
const char kPrerenderInstantUrlOnOmniboxFocus[] =
"prerender_instant_url_on_omnibox_focus";
......@@ -590,6 +591,15 @@ bool ShouldPrefetchSearchResults() {
kPrefetchSearchResultsFlagName, false, flags);
}
bool ShouldAllowPrefetchNonDefaultMatch() {
if (!ShouldPrefetchSearchResults())
return false;
FieldTrialFlags flags;
return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
kAllowPrefetchNonDefaultMatch, false, flags);
}
bool ShouldPrerenderInstantUrlOnOmniboxFocus() {
if (!ShouldPrefetchSearchResults())
return false;
......
......@@ -166,6 +166,11 @@ GURL GetSearchResultPrefetchBaseURL(Profile* profile);
// prefetch high-confidence search suggestions.
bool ShouldPrefetchSearchResults();
// Returns true if 'allow_prefetch_non_default_match' flag is enabled in field
// trials to allow prefetching the suggestion marked to be prefetched by the
// suggest server even if it is not the default match.
bool ShouldAllowPrefetchNonDefaultMatch();
// Returns true if 'prerender_instant_url_on_omnibox_focus' flag is enabled in
// field trials to prerender Instant search base page when the omnibox is
// focused.
......
......@@ -689,6 +689,41 @@ TEST_F(SearchTest, ShouldPrefetchSearchResults_EnabledViaFieldTrial) {
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest, ShouldPrefetchSearchResults_EnabledViaCommandLine) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kPrefetchSearchResults);
// Command-line enable should override Finch.
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"EmbeddedSearch", "Group1 espv:80 prefetch_results:0"));
EXPECT_TRUE(ShouldPrefetchSearchResults());
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest,
ShouldAllowPrefetchNonDefaultMatch_PrefetchResultsFlagDisabled) {
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"EmbeddedSearch",
"Group1 espv:80 prefetch_results:0 allow_prefetch_non_default_match:1"));
EXPECT_FALSE(ShouldAllowPrefetchNonDefaultMatch());
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest, ShouldAllowPrefetchNonDefaultMatch_DisabledViaFieldTrial) {
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"EmbeddedSearch",
"Group1 espv:89 prefetch_results:1 allow_prefetch_non_default_match:0"));
EXPECT_FALSE(ShouldAllowPrefetchNonDefaultMatch());
EXPECT_EQ(89ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest, ShouldAllowPrefetchNonDefaultMatch_EnabledViaFieldTrial) {
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"EmbeddedSearch",
"Group1 espv:80 prefetch_results:1 allow_prefetch_non_default_match:1"));
EXPECT_TRUE(ShouldAllowPrefetchNonDefaultMatch());
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest,
ShouldPrerenderInstantUrlOnOmniboxFocus_PrefetchResultsFlagDisabled) {
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
......@@ -719,17 +754,6 @@ TEST_F(SearchTest,
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest, ShouldPrefetchSearchResults_EnabledViaCommandLine) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kPrefetchSearchResults);
// Command-line enable should override Finch.
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"EmbeddedSearch",
"Group1 espv:80 prefetch_results:0"));
EXPECT_TRUE(ShouldPrefetchSearchResults());
EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
}
TEST_F(SearchTest,
ShouldReuseInstantSearchBasePage_PrefetchResultsFlagDisabled) {
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
......
......@@ -30,15 +30,24 @@ namespace {
//
// The SearchProvider may mark some suggestions to be prefetched based on
// instructions from the suggest server. If such a match ranks sufficiently
// highly, we'll return it.
// highly or if kAllowPrefetchNonDefaultMatch field trial is enabled, we'll
// return it.
//
// We only care about matches that are the default or the very first entry in
// the dropdown (which can happen for non-default matches only if we're hiding
// a top verbatim match) or the second entry in the dropdown (which can happen
// for non-default matches when a top verbatim match is shown); for other
// matches, we think the likelihood of the user selecting them is low enough
// that prefetching isn't worth doing.
// If the kAllowPrefetchNonDefaultMatch field trial is enabled we return the
// prefetch suggestion even if it is not the default match. Otherwise we only
// care about matches that are the default or the very first entry in the
// dropdown (which can happen for non-default matches only if we're hiding a top
// verbatim match) or the second entry in the dropdown (which can happen for
// non-default matches when a top verbatim match is shown); for other matches,
// we think the likelihood of the user selecting them is low enough that
// prefetching isn't worth doing.
const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
if (chrome::ShouldAllowPrefetchNonDefaultMatch()) {
const AutocompleteResult::const_iterator prefetch_match = std::find_if(
result.begin(), result.end(), SearchProvider::ShouldPrefetch);
return prefetch_match != result.end() ? &(*prefetch_match) : NULL;
}
// If the default match should be prefetched, do that.
const AutocompleteResult::const_iterator default_match(
result.default_match());
......@@ -46,8 +55,8 @@ const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
SearchProvider::ShouldPrefetch(*default_match))
return &(*default_match);
// Otherwise, if the top match is a verbatim match and the very next match is
// prefetchable, fetch that.
// Otherwise, if the top match is a verbatim match and the very next match
// is prefetchable, fetch that.
if ((result.ShouldHideTopMatch() ||
result.TopMatchIsStandaloneVerbatimMatch()) &&
(result.size() > 1) &&
......@@ -86,27 +95,12 @@ void OmniboxController::OnResultChanged(bool default_match_changed) {
if (default_match_changed) {
// The default match has changed, we need to let the OmniboxEditModel know
// about new inline autocomplete text (blue highlight).
const AutocompleteResult& result = this->result();
const AutocompleteResult::const_iterator match(result.default_match());
if (match != result.end()) {
const AutocompleteResult::const_iterator match(result().default_match());
if (match != result().end()) {
current_match_ = *match;
if (!prerender::IsOmniboxEnabled(profile_))
DoPreconnect(*match);
omnibox_edit_model_->OnCurrentMatchChanged();
if (chrome::IsInstantExtendedAPIEnabled()) {
InstantSuggestion prefetch_suggestion;
const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result);
if (match_to_prefetch) {
prefetch_suggestion.text = match_to_prefetch->contents;
prefetch_suggestion.metadata =
SearchProvider::GetSuggestMetadata(*match_to_prefetch);
}
// Send the prefetch suggestion unconditionally to the InstantPage. If
// there is no suggestion to prefetch, we need to send a blank query to
// clear the prefetched results.
omnibox_edit_model_->SetSuggestionToPrefetch(prefetch_suggestion);
}
} else {
InvalidateCurrentMatch();
popup_->OnResultChanged();
......@@ -122,6 +116,22 @@ void OmniboxController::OnResultChanged(bool default_match_changed) {
// to have temporary text when the popup is closed.
omnibox_edit_model_->AcceptTemporaryTextAsUserText();
}
if (chrome::IsInstantExtendedAPIEnabled() &&
((default_match_changed && result().default_match() != result().end()) ||
(chrome::ShouldAllowPrefetchNonDefaultMatch() && !result().empty()))) {
InstantSuggestion prefetch_suggestion;
const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result());
if (match_to_prefetch) {
prefetch_suggestion.text = match_to_prefetch->contents;
prefetch_suggestion.metadata =
SearchProvider::GetSuggestMetadata(*match_to_prefetch);
}
// Send the prefetch suggestion unconditionally to the InstantPage. If
// there is no suggestion to prefetch, we need to send a blank query to
// clear the prefetched results.
omnibox_edit_model_->SetSuggestionToPrefetch(prefetch_suggestion);
}
}
void OmniboxController::InvalidateCurrentMatch() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment