Commit 6a600583 authored by manuk's avatar manuk Committed by Commit Bot

[Omnibox] When in keyword mode, don't allow shortcut matches of different...

[Omnibox] When in keyword mode, don't allow shortcut matches of different keywords to be the default match.

Prior to this cl, when the user has searched for `searchProvder searchTerm` (not in keyword mode) then typing in `searchProvider `, entering keyword mode, and typing `s` could result in the shortcut match for the earlier search becoming the default match and causing him to leave keyword mode.

Bug: 632215
Change-Id: I19ee38e3e5c9ee68f8f7703afccef1276027b521
Reviewed-on: https://chromium-review.googlesource.com/c/1182295
Commit-Queue: manuk hovanesian <manukh@chromium.org>
Reviewed-by: default avatarMark Pearson <mpearson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597202}
parent f9bb833d
......@@ -41,7 +41,7 @@ struct TestShortcutData shortcut_test_db[] = {
{"BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo",
"chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo",
"Run Echo command: echo", "0,0", "Echo", "0,4", ui::PAGE_TRANSITION_TYPED,
AutocompleteMatchType::EXTENSION_APP_DEPRECATED, "echo", 1, 1},
AutocompleteMatchType::EXTENSION_APP_DEPRECATED, "", 1, 1},
};
} // namespace
......
......@@ -17,6 +17,7 @@
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
......@@ -388,37 +389,54 @@ AutocompleteMatch ShortcutsProvider::ShortcutToACMatch(
base::UTF16ToUTF8(shortcut.text));
// Set |inline_autocompletion| and |allowed_to_be_default_match| if possible.
// If the match is a search query this is easy: simply check whether the
// user text is a prefix of the query. If the match is a navigation, we
// assume the fill_into_edit looks something like a URL, so we use
// URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes
// that the user might not think would change the meaning, but would
// otherwise prevent inline autocompletion. This allows, for example, the
// input of "foo.c" to autocomplete to "foo.com" for a fill_into_edit of
// "http://foo.com".
// If the input is in keyword mode, navigation matches cannot be the default
// match, and search query matches can only be the default match if their
// keywords matches the input's keyword, as otherwise, default,
// different-keyword matches may result in leaving keyword mode. Additionally,
// if the match is a search query, check whether the user text is a prefix of
// the query. If the match is a navigation, we assume the fill_into_edit looks
// something like a URL, so we use URLPrefix::GetInlineAutocompleteOffset() to
// try and strip off any prefixes that the user might not think would change
// the meaning, but would otherwise prevent inline autocompletion. This
// allows, for example, the input of "foo.c" to autocomplete to "foo.com" for
// a fill_into_edit of "http://foo.com".
const bool is_search_type = AutocompleteMatch::IsSearchType(match.type);
if (is_search_type) {
if (match.fill_into_edit.size() >= input.text().size() &&
std::equal(match.fill_into_edit.begin(),
match.fill_into_edit.begin() + input.text().size(),
input.text().begin(),
SimpleCaseInsensitiveCompareUCS2())) {
match.inline_autocompletion =
match.fill_into_edit.substr(input.text().length());
match.allowed_to_be_default_match =
!input.prevent_inline_autocomplete() ||
match.inline_autocompletion.empty();
}
} else {
const size_t inline_autocomplete_offset =
URLPrefix::GetInlineAutocompleteOffset(
input.text(), fixed_up_input_text, true, match.fill_into_edit);
if (inline_autocomplete_offset != base::string16::npos) {
match.inline_autocompletion =
match.fill_into_edit.substr(inline_autocomplete_offset);
match.allowed_to_be_default_match =
!HistoryProvider::PreventInlineAutocomplete(input) ||
match.inline_autocompletion.empty();
DCHECK(is_search_type != match.keyword.empty());
// True if input is in keyword mode and the match is a URL suggestion or the
// match has a different keyword.
bool would_cause_leaving_keyboard_mode =
input.prefer_keyword() &&
(!is_search_type ||
!base::StartsWith(base::UTF16ToUTF8(input.text()),
base::StrCat({base::UTF16ToUTF8(match.keyword), " "}),
base::CompareCase::INSENSITIVE_ASCII));
if (!would_cause_leaving_keyboard_mode) {
if (is_search_type) {
if (match.fill_into_edit.size() >= input.text().size() &&
std::equal(match.fill_into_edit.begin(),
match.fill_into_edit.begin() + input.text().size(),
input.text().begin(),
SimpleCaseInsensitiveCompareUCS2())) {
match.inline_autocompletion =
match.fill_into_edit.substr(input.text().length());
match.allowed_to_be_default_match =
!input.prevent_inline_autocomplete() ||
match.inline_autocompletion.empty();
}
} else {
const size_t inline_autocomplete_offset =
URLPrefix::GetInlineAutocompleteOffset(
input.text(), fixed_up_input_text, true, match.fill_into_edit);
if (inline_autocomplete_offset != base::string16::npos) {
match.inline_autocompletion =
match.fill_into_edit.substr(inline_autocomplete_offset);
match.allowed_to_be_default_match =
!HistoryProvider::PreventInlineAutocomplete(input) ||
match.inline_autocompletion.empty();
}
}
}
......
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