Skip to content

Commit

Permalink
Keep native autocomplete_match_types in sync with java.
Browse files Browse the repository at this point in the history
Auto-generates the enum values for java autocomplete types. (Gets rid
of java enum overhead as a bonus).
Removes OPEN_HISTORY_PAGE suggestion type which looks like dead
code.
Replaces some switch statements with isUrlSuggestion, which seems to have
identical logic.

BUG=373520

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

Cr-Commit-Position: refs/heads/master@{#357515}
  • Loading branch information
mariakhomenko authored and Commit bot committed Nov 3, 2015
1 parent b24facb commit 73808f9
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 213 deletions.
1 change: 1 addition & 0 deletions chrome/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ android_library("chrome_java") {
"//chrome:content_settings_type_javagen",
"//components/enhanced_bookmarks:enhanced_bookmarks_java_enums_srcjar",
"//components/offline_pages:offline_pages_enums_java",
"//components/omnibox/browser:autocomplete_match_type_javagen",
]

DEPRECATED_java_in_dir = "java/src"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ private void notifyNativeDestroyed() {
* modifying text in the omnibox and selecting a suggestion.
* @param webContents The web contents for the tab where the selected suggestion will be shown.
*/
public void onSuggestionSelected(int selectedIndex, OmniboxSuggestion.Type type,
public void onSuggestionSelected(int selectedIndex, int type,
String currentPageUrl, boolean isQueryInOmnibox, boolean focusedFromFakebox,
long elapsedTimeSinceModified, WebContents webContents) {
// Don't natively log voice suggestion results as we add them in Java.
if (type == OmniboxSuggestion.Type.VOICE_SUGGEST) return;
if (type == OmniboxSuggestionType.VOICE_SUGGEST) return;
nativeOnSuggestionSelected(mNativeAutocompleteControllerAndroid, selectedIndex,
currentPageUrl, isQueryInOmnibox, focusedFromFakebox, elapsedTimeSinceModified,
webContents);
Expand Down Expand Up @@ -264,12 +264,12 @@ private static void addOmniboxSuggestionToList(List<OmniboxSuggestion> suggestio
}

@CalledByNative
private static OmniboxSuggestion buildOmniboxSuggestion(int nativeType, int relevance,
int transition, String text, String description, String answerContents,
private static OmniboxSuggestion buildOmniboxSuggestion(int nativeType, boolean isSearchType,
int relevance, int transition, String text, String description, String answerContents,
String answerType, String fillIntoEdit, String url, String formattedUrl,
boolean isStarred, boolean isDeletable) {
return new OmniboxSuggestion(nativeType, relevance, transition, text, description,
answerContents, answerType, fillIntoEdit, url, formattedUrl, isStarred,
return new OmniboxSuggestion(nativeType, isSearchType, relevance, transition, text,
description, answerContents, answerType, fillIntoEdit, url, formattedUrl, isStarred,
isDeletable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsReceivedListener;
import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxResultItem;
import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxSuggestionDelegate;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestion.Type;
import org.chromium.chrome.browser.omnibox.VoiceSuggestionProvider.VoiceResult;
import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader;
import org.chromium.chrome.browser.omnibox.geo.GeolocationSnackbarController;
Expand Down Expand Up @@ -450,7 +449,7 @@ private void findMatchAndLoadUrl(String urlText) {
// up saving generated URLs as typed URLs, which would then pollute the subsequent
// omnibox results. There is one special case where the suggestion text was pasted,
// where we want the transition type to be LINK.
int transition = suggestionMatch.getType() == OmniboxSuggestion.Type.URL_WHAT_YOU_TYPED
int transition = suggestionMatch.getType() == OmniboxSuggestionType.URL_WHAT_YOU_TYPED
&& mUrlBar.isPastedText() ? PageTransition.LINK
: suggestionMatch.getTransition();

Expand Down Expand Up @@ -1123,29 +1122,11 @@ protected ToolbarDataProvider getToolbarDataProvider() {
}

private static NavigationButtonType suggestionTypeToNavigationButtonType(
OmniboxSuggestion.Type suggestionType) {
switch (suggestionType) {
case NAVSUGGEST:
case HISTORY_URL:
case URL_WHAT_YOU_TYPED:
case HISTORY_TITLE:
case HISTORY_BODY:
case HISTORY_KEYWORD:
return NavigationButtonType.PAGE;
case SEARCH_WHAT_YOU_TYPED:
case SEARCH_HISTORY:
case SEARCH_SUGGEST:
case SEARCH_SUGGEST_ENTITY:
case SEARCH_SUGGEST_TAIL:
case SEARCH_SUGGEST_PERSONALIZED:
case SEARCH_SUGGEST_PROFILE:
case VOICE_SUGGEST:
case SEARCH_OTHER_ENGINE:
case OPEN_HISTORY_PAGE:
return NavigationButtonType.MAGNIFIER;
default:
assert false;
return NavigationButtonType.MAGNIFIER;
OmniboxSuggestion suggestion) {
if (suggestion.isUrlSuggestion()) {
return NavigationButtonType.PAGE;
} else {
return NavigationButtonType.MAGNIFIER;
}
}

Expand All @@ -1156,7 +1137,7 @@ private void updateNavigationButton() {
if (isTablet && !mSuggestionItems.isEmpty()) {
// If there are suggestions showing, show the icon for the default suggestion.
type = suggestionTypeToNavigationButtonType(
mSuggestionItems.get(0).getSuggestion().getType());
mSuggestionItems.get(0).getSuggestion());
} else if (mQueryInTheOmnibox) {
type = NavigationButtonType.MAGNIFIER;
} else if (isTablet) {
Expand Down Expand Up @@ -1570,7 +1551,7 @@ private String updateSuggestionUrlIfNeeded(OmniboxSuggestion suggestion, int sel
updatedUrl = TemplateUrlService.getInstance().replaceSearchTermsInUrl(
query, currentTab.getUrl());
}
} else if (suggestion.getType() != Type.VOICE_SUGGEST) {
} else if (suggestion.getType() != OmniboxSuggestionType.VOICE_SUGGEST) {
// TODO(mariakhomenko): Ideally we want to update match destination URL with new aqs
// for query in the omnibox and voice suggestions, but it's currently difficult to do.
long elapsedTimeSinceInputChange = mNewOmniboxEditSessionTimestamp > 0
Expand Down Expand Up @@ -1808,7 +1789,7 @@ public void onSuggestionsReceived(List<OmniboxSuggestion> newSuggestions,
// Determine whether the suggestions have changed. If not, save some time by not
// redrawing the suggestions UI.
if (suggestion.equals(newSuggestion)
&& suggestion.getType() != OmniboxSuggestion.Type.SEARCH_SUGGEST_TAIL) {
&& suggestion.getType() != OmniboxSuggestionType.SEARCH_SUGGEST_TAIL) {
if (suggestionItem.getMatchedQuery().equals(userText)) {
continue;
} else if (!suggestion.getDisplayText().startsWith(userText)
Expand Down Expand Up @@ -2019,8 +2000,7 @@ public void setIgnoreURLBarModification(boolean value) {
mIgnoreURLBarModification = value;
}

private void loadUrlFromOmniboxMatch(String url, int transition, int matchPosition,
OmniboxSuggestion.Type type) {
private void loadUrlFromOmniboxMatch(String url, int transition, int matchPosition, int type) {
// loadUrl modifies AutocompleteController's state clearing the native
// AutocompleteResults needed by onSuggestionsSelected. Therefore,
// loadUrl should should be invoked last.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@VisibleForTesting
public class OmniboxSuggestion {

private final Type mType;
private final int mType;
private final boolean mIsSearchType;
private final String mDisplayText;
private final String mDescription;
private final String mAnswerContents;
Expand All @@ -28,71 +29,12 @@ public class OmniboxSuggestion {
private final boolean mIsStarred;
private final boolean mIsDeletable;

/**
* This should be kept in sync with AutocompleteMatch::Type
* (see components/omnibox/autocomplete_match_type.h).
* Negative types are specific to Chrome on Android front-end.
*/
public static enum Type {
VOICE_SUGGEST (-100), // A suggested search from the voice recognizer.

URL_WHAT_YOU_TYPED (0), // The input as a URL.
HISTORY_URL (1), // A past page whose URL contains the input.
HISTORY_TITLE (2), // A past page whose title contains the input.
HISTORY_BODY (3), // A past page whose body contains the input.
HISTORY_KEYWORD (4), // A past page whose keyword contains the input.
NAVSUGGEST (5), // A suggested URL.
SEARCH_WHAT_YOU_TYPED (6), // The input as a search query (with the default
// engine).
SEARCH_HISTORY (7), // A past search (with the default engine)
// containing the input.
SEARCH_SUGGEST (8), // A suggested search (with the default engine).
SEARCH_SUGGEST_ENTITY (9), // A suggested search for an entity.
SEARCH_SUGGEST_TAIL (10), // A suggested search (with the default engine)
// to complete the tail part of the input.
SEARCH_SUGGEST_PERSONALIZED (11), // A personalized suggested search.
SEARCH_SUGGEST_PROFILE (12), // A personalized suggested search for a
// Google+ profile.
SEARCH_OTHER_ENGINE (13), // A search with a non-default engine.
OPEN_HISTORY_PAGE (17); // A synthetic result that opens the history page
// to search for the input.

private final int mNativeType;

Type(int nativeType) {
mNativeType = nativeType;
}

static Type getTypeFromNativeType(int nativeType) {
for (Type t : Type.values()) {
if (t.mNativeType == nativeType) return t;
}

return URL_WHAT_YOU_TYPED;
}

public boolean isHistoryUrl() {
return this == HISTORY_URL || this == HISTORY_TITLE
|| this == HISTORY_BODY || this == HISTORY_KEYWORD;
}

public boolean isUrl() {
return this == URL_WHAT_YOU_TYPED || this.isHistoryUrl() || this == NAVSUGGEST;
}

/**
* @return The ID of the type used by the native code.
*/
public int nativeType() {
return mNativeType;
}
}

public OmniboxSuggestion(int nativeType, int relevance, int transition,
public OmniboxSuggestion(int nativeType, boolean isSearchType, int relevance, int transition,
String text, String description, String answerContents,
String answerType, String fillIntoEdit, String url,
String formattedUrl, boolean isStarred, boolean isDeletable) {
mType = Type.getTypeFromNativeType(nativeType);
mType = nativeType;
mIsSearchType = isSearchType;
mRelevance = relevance;
mTransition = transition;
mDisplayText = text;
Expand All @@ -114,7 +56,7 @@ public OmniboxSuggestion(int nativeType, int relevance, int transition,
}
}

public Type getType() {
public int getType() {
return mType;
}

Expand Down Expand Up @@ -158,8 +100,11 @@ public String getFormattedUrl() {
return mFormattedUrl;
}

/**
* @return Whether the suggestion is a URL.
*/
public boolean isUrlSuggestion() {
return mType.isUrl();
return !mIsSearchType;
}

/**
Expand Down Expand Up @@ -187,7 +132,7 @@ public String toString() {

@Override
public int hashCode() {
int hash = 37 * mType.mNativeType + mDisplayText.hashCode() + mFillIntoEdit.hashCode()
int hash = 37 * mType + mDisplayText.hashCode() + mFillIntoEdit.hashCode()
+ (mIsStarred ? 1 : 0) + (mIsDeletable ? 1 : 0);
if (mAnswerContents != null) {
hash = hash + mAnswerContents.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package org.chromium.chrome.browser.omnibox;

import static org.chromium.chrome.browser.omnibox.OmniboxSuggestion.Type.HISTORY_URL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
Expand Down Expand Up @@ -35,7 +33,6 @@
import org.chromium.chrome.R;
import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxResultItem;
import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxSuggestionDelegate;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestion.Type;
import org.chromium.chrome.browser.widget.TintedDrawable;
import org.chromium.ui.base.DeviceFormFactor;

Expand Down Expand Up @@ -185,7 +182,7 @@ protected void drawableStateChanged() {
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (getMeasuredWidth() == 0) return;

if (mSuggestion.getType() != Type.SEARCH_SUGGEST_TAIL) {
if (mSuggestion.getType() != OmniboxSuggestionType.SEARCH_SUGGEST_TAIL) {
mContentsView.resetTextWidths();
}

Expand Down Expand Up @@ -297,64 +294,44 @@ public void init(OmniboxResultItem suggestionItem,

boolean sameAsTyped =
suggestionItem.getMatchedQuery().equalsIgnoreCase(mSuggestion.getDisplayText());
Type suggestionType = mSuggestion.getType();
switch (suggestionType) {
case HISTORY_URL:
case URL_WHAT_YOU_TYPED:
case NAVSUGGEST:
case HISTORY_TITLE:
case HISTORY_BODY:
case HISTORY_KEYWORD:
case OPEN_HISTORY_PAGE:
if (mSuggestion.isStarred()) {
mContentsView.setSuggestionIcon(SuggestionIconType.BOOKMARK, colorsChanged);
} else if (suggestionType == HISTORY_URL) {
mContentsView.setSuggestionIcon(SuggestionIconType.HISTORY, colorsChanged);
} else {
mContentsView.setSuggestionIcon(SuggestionIconType.GLOBE, colorsChanged);
}
boolean urlShown = !TextUtils.isEmpty(mSuggestion.getUrl());
boolean urlHighlighted = false;
if (urlShown) {
urlHighlighted = setUrlText(suggestionItem);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
setSuggestedQuery(suggestionItem, true, urlShown, urlHighlighted);
setRefinable(!sameAsTyped);
break;
case SEARCH_WHAT_YOU_TYPED:
case SEARCH_HISTORY:
case SEARCH_SUGGEST:
case SEARCH_OTHER_ENGINE:
case SEARCH_SUGGEST_ENTITY:
case SEARCH_SUGGEST_TAIL:
case SEARCH_SUGGEST_PERSONALIZED:
case SEARCH_SUGGEST_PROFILE:
case VOICE_SUGGEST:
SuggestionIconType suggestionIcon = SuggestionIconType.MAGNIFIER;
if (suggestionType == Type.VOICE_SUGGEST) {
suggestionIcon = SuggestionIconType.VOICE;
} else if ((suggestionType == Type.SEARCH_SUGGEST_PERSONALIZED)
|| (suggestionType == Type.SEARCH_HISTORY)) {
// Show history icon for suggestions based on user queries.
suggestionIcon = SuggestionIconType.HISTORY;
}
mContentsView.setSuggestionIcon(suggestionIcon, colorsChanged);
setRefinable(!sameAsTyped);
setSuggestedQuery(suggestionItem, false, false, false);
if ((suggestionType == Type.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == Type.SEARCH_SUGGEST_PROFILE)) {
showDescriptionLine(
SpannableString.valueOf(mSuggestion.getDescription()),
getStandardFontColor());
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
break;
default:
assert false : "Suggestion type (" + mSuggestion.getType() + ") is not handled";
break;
int suggestionType = mSuggestion.getType();
if (mSuggestion.isUrlSuggestion()) {
if (mSuggestion.isStarred()) {
mContentsView.setSuggestionIcon(SuggestionIconType.BOOKMARK, colorsChanged);
} else if (suggestionType == OmniboxSuggestionType.HISTORY_URL) {
mContentsView.setSuggestionIcon(SuggestionIconType.HISTORY, colorsChanged);
} else {
mContentsView.setSuggestionIcon(SuggestionIconType.GLOBE, colorsChanged);
}
boolean urlShown = !TextUtils.isEmpty(mSuggestion.getUrl());
boolean urlHighlighted = false;
if (urlShown) {
urlHighlighted = setUrlText(suggestionItem);
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
setSuggestedQuery(suggestionItem, true, urlShown, urlHighlighted);
setRefinable(!sameAsTyped);
} else {
SuggestionIconType suggestionIcon = SuggestionIconType.MAGNIFIER;
if (suggestionType == OmniboxSuggestionType.VOICE_SUGGEST) {
suggestionIcon = SuggestionIconType.VOICE;
} else if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PERSONALIZED)
|| (suggestionType == OmniboxSuggestionType.SEARCH_HISTORY)) {
// Show history icon for suggestions based on user queries.
suggestionIcon = SuggestionIconType.HISTORY;
}
mContentsView.setSuggestionIcon(suggestionIcon, colorsChanged);
setRefinable(!sameAsTyped);
setSuggestedQuery(suggestionItem, false, false, false);
if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PROFILE)) {
showDescriptionLine(
SpannableString.valueOf(mSuggestion.getDescription()),
getStandardFontColor());
} else {
mContentsView.mTextLine2.setVisibility(INVISIBLE);
}
}
}

Expand Down Expand Up @@ -467,7 +444,7 @@ private void setSuggestedQuery(
suggestedQuery = suggestion.getFormattedUrl();
}

if (mSuggestion.getType() == Type.SEARCH_SUGGEST_TAIL) {
if (mSuggestion.getType() == OmniboxSuggestionType.SEARCH_SUGGEST_TAIL) {
String fillIntoEdit = mSuggestion.getFillIntoEdit();
// Data sanity checks.
if (fillIntoEdit.startsWith(userQuery)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ private void addVoiceResultToOmniboxSuggestions(List<OmniboxSuggestion> suggesti
String voiceUrl = TemplateUrlService.getInstance().getUrlForVoiceSearchQuery(
result.getMatch());
suggestions.add(new OmniboxSuggestion(
OmniboxSuggestion.Type.VOICE_SUGGEST.nativeType(),
OmniboxSuggestionType.VOICE_SUGGEST,
true,
0,
1,
result.getMatch(),
Expand Down
Loading

0 comments on commit 73808f9

Please sign in to comment.