Commit 7f389197 authored by Tommy C. Li's avatar Tommy C. Li Committed by Commit Bot

Omnibox: Make QueryInOmnibox on Android use native implementation

This CL removes the Java implementation of Query in Omnibox, and makes
Chrome for Android use the native C++ implementation.

This native C++ implementation will be shared with Desktop.

Bug: 874592
Change-Id: Idfe296090c43d5d3aefe4ce166a4f8859d5699f1
Reviewed-on: https://chromium-review.googlesource.com/1204413Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Tommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588976}
parent 3460c477
...@@ -63,6 +63,7 @@ import org.chromium.chrome.browser.ntp.IncognitoNewTabPage; ...@@ -63,6 +63,7 @@ import org.chromium.chrome.browser.ntp.IncognitoNewTabPage;
import org.chromium.chrome.browser.ntp.NewTabPage; import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; import org.chromium.chrome.browser.offlinepages.OfflinePageBridge;
import org.chromium.chrome.browser.omnibox.LocationBar; import org.chromium.chrome.browser.omnibox.LocationBar;
import org.chromium.chrome.browser.omnibox.QueryInOmnibox;
import org.chromium.chrome.browser.omnibox.UrlFocusChangeListener; import org.chromium.chrome.browser.omnibox.UrlFocusChangeListener;
import org.chromium.chrome.browser.partnercustomizations.HomepageManager; import org.chromium.chrome.browser.partnercustomizations.HomepageManager;
import org.chromium.chrome.browser.partnercustomizations.HomepageManager.HomepageStateListener; import org.chromium.chrome.browser.partnercustomizations.HomepageManager.HomepageStateListener;
...@@ -353,10 +354,10 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe ...@@ -353,10 +354,10 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe
mTabObserver = new EmptyTabObserver() { mTabObserver = new EmptyTabObserver() {
@Override @Override
public void onSSLStateUpdated(Tab tab) { public void onSSLStateUpdated(Tab tab) {
setModelShouldIgnoreSecurityLevelForSearchTerms(false);
if (mToolbarModel.getTab() == null) return; if (mToolbarModel.getTab() == null) return;
assert tab == mToolbarModel.getTab(); assert tab == mToolbarModel.getTab();
QueryInOmnibox.setIgnoreSecurityLevelForSearchTerms(tab.getProfile(), false);
mLocationBar.updateSecurityIcon(); mLocationBar.updateSecurityIcon();
mLocationBar.setUrlToPageUrl(); mLocationBar.setUrlToPageUrl();
} }
...@@ -392,12 +393,12 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe ...@@ -392,12 +393,12 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe
@Override @Override
public void onPageLoadStarted(Tab tab, String url) { public void onPageLoadStarted(Tab tab, String url) {
mToolbarModel.setIgnoreSecurityLevelForSearchTerms(true); QueryInOmnibox.setIgnoreSecurityLevelForSearchTerms(tab.getProfile(), true);
} }
@Override @Override
public void onPageLoadFinished(Tab tab) { public void onPageLoadFinished(Tab tab) {
mToolbarModel.setIgnoreSecurityLevelForSearchTerms(false); QueryInOmnibox.setIgnoreSecurityLevelForSearchTerms(tab.getProfile(), false);
if (tab.isShowingErrorPage()) { if (tab.isShowingErrorPage()) {
handleIPHForErrorPageShown(tab); handleIPHForErrorPageShown(tab);
return; return;
...@@ -409,7 +410,7 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe ...@@ -409,7 +410,7 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe
@Override @Override
public void onLoadStarted(Tab tab, boolean toDifferentDocument) { public void onLoadStarted(Tab tab, boolean toDifferentDocument) {
if (!toDifferentDocument) return; if (!toDifferentDocument) return;
mToolbarModel.setIgnoreSecurityLevelForSearchTerms(true); QueryInOmnibox.setIgnoreSecurityLevelForSearchTerms(tab.getProfile(), true);
updateButtonStatus(); updateButtonStatus();
updateTabLoadingState(true); updateTabLoadingState(true);
} }
...@@ -417,7 +418,7 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe ...@@ -417,7 +418,7 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe
@Override @Override
public void onLoadStopped(Tab tab, boolean toDifferentDocument) { public void onLoadStopped(Tab tab, boolean toDifferentDocument) {
if (!toDifferentDocument) return; if (!toDifferentDocument) return;
mToolbarModel.setIgnoreSecurityLevelForSearchTerms(false); QueryInOmnibox.setIgnoreSecurityLevelForSearchTerms(tab.getProfile(), false);
updateTabLoadingState(true); updateTabLoadingState(true);
// If we made some progress, fast-forward to complete, otherwise just dismiss any // If we made some progress, fast-forward to complete, otherwise just dismiss any
...@@ -1688,23 +1689,6 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe ...@@ -1688,23 +1689,6 @@ public class ToolbarManager implements ToolbarTabController, UrlFocusChangeListe
== Configuration.KEYBOARD_QWERTY; == Configuration.KEYBOARD_QWERTY;
} }
/**
* Notifies the toolbar model that it should ignore the security level when determining whether
* or not to display search terms in the URL bar. This is useful for the interim period between
* loading a new page and getting proper security info, to avoid having the full URL flicker
* before displaying search terms.
*
* @param shouldIgnore Whether or not the toolbar model should ignore the security level.
*/
private void setModelShouldIgnoreSecurityLevelForSearchTerms(boolean shouldIgnore) {
assert mToolbar != null;
boolean wasShowingSearchTerms = mToolbarModel.shouldDisplaySearchTerms();
mToolbarModel.setIgnoreSecurityLevelForSearchTerms(shouldIgnore);
if (wasShowingSearchTerms != mToolbarModel.shouldDisplaySearchTerms()) {
mLocationBar.setUrlToPageUrl();
}
}
private static class LoadProgressSimulator { private static class LoadProgressSimulator {
private static final int MSG_ID_UPDATE_PROGRESS = 1; private static final int MSG_ID_UPDATE_PROGRESS = 1;
......
...@@ -15,6 +15,7 @@ import android.text.TextUtils; ...@@ -15,6 +15,7 @@ import android.text.TextUtils;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeFeatureList; import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.ChromeTabbedActivity; import org.chromium.chrome.browser.ChromeTabbedActivity;
...@@ -24,12 +25,11 @@ import org.chromium.chrome.browser.dom_distiller.DomDistillerTabUtils; ...@@ -24,12 +25,11 @@ import org.chromium.chrome.browser.dom_distiller.DomDistillerTabUtils;
import org.chromium.chrome.browser.native_page.NativePageFactory; import org.chromium.chrome.browser.native_page.NativePageFactory;
import org.chromium.chrome.browser.ntp.NewTabPage; import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.offlinepages.OfflinePageUtils; import org.chromium.chrome.browser.offlinepages.OfflinePageUtils;
import org.chromium.chrome.browser.omnibox.AutocompleteController;
import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer; import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer;
import org.chromium.chrome.browser.omnibox.QueryInOmnibox;
import org.chromium.chrome.browser.omnibox.UrlBarData; import org.chromium.chrome.browser.omnibox.UrlBarData;
import org.chromium.chrome.browser.preferences.ChromePreferenceManager; import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.util.ColorUtils; import org.chromium.chrome.browser.util.ColorUtils;
import org.chromium.chrome.browser.util.FeatureUtilities; import org.chromium.chrome.browser.util.FeatureUtilities;
...@@ -38,6 +38,7 @@ import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet; ...@@ -38,6 +38,7 @@ import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet;
import org.chromium.components.dom_distiller.core.DomDistillerService; import org.chromium.components.dom_distiller.core.DomDistillerService;
import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils;
import org.chromium.components.security_state.ConnectionSecurityLevel; import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import java.net.URI; import java.net.URI;
...@@ -59,8 +60,6 @@ public class ToolbarModel implements ToolbarDataProvider { ...@@ -59,8 +60,6 @@ public class ToolbarModel implements ToolbarDataProvider {
private boolean mIsIncognito; private boolean mIsIncognito;
private int mPrimaryColor; private int mPrimaryColor;
private boolean mIsUsingBrandColor; private boolean mIsUsingBrandColor;
/** Query in Omnibox cached values to avoid unnecessary expensive calls. */
private boolean mQueryInOmniboxEnabled;
private String mPreviousUrl; private String mPreviousUrl;
@ConnectionSecurityLevel @ConnectionSecurityLevel
private int mPreviousSecurityLevel; private int mPreviousSecurityLevel;
...@@ -91,7 +90,6 @@ public class ToolbarModel implements ToolbarDataProvider { ...@@ -91,7 +90,6 @@ public class ToolbarModel implements ToolbarDataProvider {
*/ */
public void initializeWithNative() { public void initializeWithNative() {
mNativeToolbarModelAndroid = nativeInit(); mNativeToolbarModelAndroid = nativeInit();
mQueryInOmniboxEnabled = ChromeFeatureList.isEnabled(ChromeFeatureList.QUERY_IN_OMNIBOX);
mIsNativeLibraryReady = true; mIsNativeLibraryReady = true;
} }
...@@ -201,9 +199,10 @@ public class ToolbarModel implements ToolbarDataProvider { ...@@ -201,9 +199,10 @@ public class ToolbarModel implements ToolbarDataProvider {
return buildUrlBarData(url, formattedUrl); return buildUrlBarData(url, formattedUrl);
} }
if (shouldDisplaySearchTerms()) { String searchTerms = getDisplaySearchTerms();
if (searchTerms != null) {
// Show the search terms in the omnibox instead of the URL if this is a DSE search URL. // Show the search terms in the omnibox instead of the URL if this is a DSE search URL.
return buildUrlBarData(url, getDisplaySearchTerms()); return buildUrlBarData(url, searchTerms);
} }
if (ChromeFeatureList.isEnabled( if (ChromeFeatureList.isEnabled(
...@@ -448,87 +447,27 @@ public class ToolbarModel implements ToolbarDataProvider { ...@@ -448,87 +447,27 @@ public class ToolbarModel implements ToolbarDataProvider {
@Override @Override
public boolean shouldDisplaySearchTerms() { public boolean shouldDisplaySearchTerms() {
if (!securityLevelSafeForQueryInOmnibox() && !mIgnoreSecurityLevelForSearchTerms) { return getDisplaySearchTerms() != null;
return false;
}
if (!mQueryInOmniboxEnabled) return false;
if (mTab != null && !(mTab.getActivity() instanceof ChromeTabbedActivity)) return false;
if (TextUtils.isEmpty(getDisplaySearchTerms())) return false;
return true;
}
private boolean securityLevelSafeForQueryInOmnibox() {
int securityLevel = getSecurityLevel();
return securityLevel == ConnectionSecurityLevel.SECURE
|| securityLevel == ConnectionSecurityLevel.EV_SECURE;
} }
/** /**
* Extracts query terms from the current URL if it's a SRP URL from the default search engine, * If the current tab state is eligible for displaying the search query terms instead of the
* caching the result of the more expensive call to {@link #getDisplaySearchTermsInternal}. * URL, this extracts the query terms from the current URL. See {@link QueryInOmnibox}.
* *
* @return The search terms. Returns null if not a DSE SRP URL or there are no search terms to * @return The search terms. Returns null if the tab is ineligible to display the search terms
* extract, if query in omnibox is disabled, or if the security level is insufficient to * instead of the URL.
* display search terms in place of SRP URL. This will also return nothing if the search
* terms look too much like a URL, since we don't want to display URL look-a-likes with
* "Query in Omnibox" to avoid confusion.
*/ */
private String getDisplaySearchTerms() { private String getDisplaySearchTerms() {
String url = getCurrentUrl(); if (mTab != null && !(mTab.getActivity() instanceof ChromeTabbedActivity)) return null;
if (url == null) {
mCachedSearchTerms = null;
} else {
@ConnectionSecurityLevel
int securityLevel = getSecurityLevel();
if (!url.equals(mPreviousUrl) || securityLevel != mPreviousSecurityLevel) {
mCachedSearchTerms = getDisplaySearchTermsInternal(url);
mPreviousUrl = url;
mPreviousSecurityLevel = securityLevel;
}
}
return mCachedSearchTerms;
}
/**
* Extracts query terms from a URL if it's a SRP URL from the default search engine, returning
* the cached result of the previous call if this call is being performed for the same URL and
* security level as last time.
*
* @param url The URL to extract search terms from.
* @return The search terms. Returns null if not a DSE SRP URL or there are no search terms to
* extract, if query in omnibox is disabled, or if the security level is insufficient to
* display search terms in place of SRP URL.
*/
private String getDisplaySearchTermsInternal(String url) {
TemplateUrlService templateUrlService = TemplateUrlService.getInstance();
if (!templateUrlService.isSearchResultsPageFromDefaultSearchProvider(url)) return null;
String searchTerms = templateUrlService.extractSearchTermsFromUrl(url);
// Avoid showing search terms that would be interpreted as a URL if typed into the
// omnibox.
return looksLikeUrl(searchTerms) ? null : searchTerms;
}
/** // We can't fetch the Profile while the browser is still starting.
* Checks if the provided {@link String} look like a URL. if (!BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
* .isStartupSuccessfullyCompleted()) {
* @param input The {@link String} to check. return null;
* @return Whether or not the {@link String} appeared to be a URL. }
*/
private boolean looksLikeUrl(String input) {
return !TextUtils.isEmpty(input)
&& !TextUtils.isEmpty(AutocompleteController.nativeQualifyPartialURLQuery(input));
}
/** return QueryInOmnibox.getDisplaySearchTerms(
* Sets a flag telling the model to ignore the security level in its check for whether to getProfile(), getSecurityLevel(), getCurrentUrl());
* display search terms or not. This is useful for avoiding the flicker that occurs when loading
* a SRP URL before our SSL state updates.
*
* @param ignore Whether or not we should ignore the security level.
*/
protected void setIgnoreSecurityLevelForSearchTerms(boolean ignore) {
mIgnoreSecurityLevelForSearchTerms = ignore;
} }
/** @return The formatted URL suitable for editing. */ /** @return The formatted URL suitable for editing. */
......
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