Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3055)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java

Issue 2738583002: Add a way to cache and show zero suggest results before native (Closed)
Patch Set: reset on start call as well Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java
index 26dcfc45efb8d8bf3f1bf522d27d383d40fe527c..0ff7e039b7793b56660e25b9fb9099fb95747932 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java
@@ -226,6 +226,36 @@ public class LocationBarLayout extends FrameLayout
private Runnable mShowSuggestions;
+ private boolean mShowCachedZeroSuggestResults;
+
+ private DeferredOnSelectionRunnable mDeferredOnSelection;
+
+ private abstract class DeferredOnSelectionRunnable implements Runnable {
+ protected final OmniboxSuggestion mSuggestion;
+ protected final int mPosition;
+ protected boolean mShouldLog;
+
+ public DeferredOnSelectionRunnable(OmniboxSuggestion suggestion, int position) {
+ this.mSuggestion = suggestion;
+ this.mPosition = position;
+ }
+
+ /**
+ * Set whether the selection matches with native results for logging to make sense.
+ * @param log Whether the selection should be logged in native code.
+ */
+ public void setShouldLog(boolean log) {
+ mShouldLog = log;
+ }
+
+ /**
+ * @return Whether the selection should be logged in native code.
+ */
+ public boolean shouldLog() {
+ return mShouldLog;
+ }
+ }
+
/**
* Listener for receiving the messages related with interacting with the omnibox during startup.
*/
@@ -761,6 +791,29 @@ public class LocationBarLayout extends FrameLayout
// If the user focused the omnibox prior to the native libraries being initialized,
// autocomplete will not always be enabled, so we force it enabled in that case.
mUrlBar.setIgnoreTextChangesForAutocomplete(false);
+ mAutocomplete = new AutocompleteController(this);
+ }
+
+ /**
+ * Sets to show cached zero suggest results. This will start both caching zero suggest results
+ * in shared preferences and also attempt to show them when appropriate without needing native
+ * initialization. See {@link LocationBarLayout#showCachedZeroSuggestResultsIfAvailable()} for
+ * showing the loaded results before native initialization.
+ * @param showCachedZeroSuggestResults Whether cached zero suggest should be shown.
+ */
+ public void setShowCachedZeroSuggestResults(boolean showCachedZeroSuggestResults) {
+ mShowCachedZeroSuggestResults = showCachedZeroSuggestResults;
+ if (mShowCachedZeroSuggestResults) mAutocomplete.startCachedZeroSuggest();
+ }
+
+ /**
+ * Signals the omnibox to shows the cached zero suggest results if they have been loaded from
+ * cache successfully.
+ */
+ public void showCachedZeroSuggestResultsIfAvailable() {
+ if (!mShowCachedZeroSuggestResults || mSuggestionList == null) return;
+ setSuggestionsListVisibility(true);
+ mSuggestionList.updateLayoutParams();
}
/**
@@ -794,8 +847,6 @@ public class LocationBarLayout extends FrameLayout
mDeleteButton.setOnClickListener(this);
mMicButton.setOnClickListener(this);
- mAutocomplete = new AutocompleteController(this);
-
mOmniboxPrerender = new OmniboxPrerender();
for (Runnable deferredRunnable : mDeferredNativeRunnables) {
@@ -1548,6 +1599,15 @@ public class LocationBarLayout extends FrameLayout
@Override
public void onSelection(OmniboxSuggestion suggestion, int position) {
mSuggestionSelectionInProgress = true;
+ if (mShowCachedZeroSuggestResults && !mNativeInitialized) {
+ mDeferredOnSelection = new DeferredOnSelectionRunnable(suggestion, position) {
+ @Override
+ public void run() {
+ onSelection(this.mSuggestion, this.mPosition);
+ }
+ };
+ return;
+ }
String suggestionMatchUrl = updateSuggestionUrlIfNeeded(
suggestion, position, false);
loadUrlFromOmniboxMatch(suggestionMatchUrl, suggestion.getTransition(), position,
@@ -1720,7 +1780,7 @@ public class LocationBarLayout extends FrameLayout
*/
@Override
public void hideSuggestions() {
- if (mAutocomplete == null) return;
+ if (mAutocomplete == null || !mNativeInitialized) return;
if (mShowSuggestions != null) removeCallbacks(mShowSuggestions);
@@ -1861,12 +1921,13 @@ public class LocationBarLayout extends FrameLayout
// so can only be called once the native side is set up.
assert mNativeInitialized : "Suggestions received before native side intialialized";
- if (getCurrentTab() == null) {
- // If the current tab is not available, drop the suggestions and hide the autocomplete.
- hideSuggestions();
- return;
+ if (mDeferredOnSelection != null) {
+ mDeferredOnSelection.setShouldLog(newSuggestions.size() > mDeferredOnSelection.mPosition
+ && mDeferredOnSelection.mSuggestion.equals(
+ newSuggestions.get(mDeferredOnSelection.mPosition)));
+ mDeferredOnSelection.run();
+ mDeferredOnSelection = null;
}
-
String userText = mUrlBar.getTextWithoutAutocomplete();
mUrlTextAfterSuggestionsReceived = userText + inlineAutocompleteText;
@@ -1945,7 +2006,8 @@ public class LocationBarLayout extends FrameLayout
// Update the navigation button to show the default suggestion's icon.
updateNavigationButton();
- if (!CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_INSTANT)
+ if (mNativeInitialized
+ && !CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_INSTANT)
&& PrivacyPreferencesManager.getInstance().shouldPrerender()) {
mOmniboxPrerender.prerenderMaybe(
userText,
@@ -2092,9 +2154,15 @@ public class LocationBarLayout extends FrameLayout
WebContents webContents = currentTab != null ? currentTab.getWebContents() : null;
long elapsedTimeSinceModified = mNewOmniboxEditSessionTimestamp > 0
? (SystemClock.elapsedRealtime() - mNewOmniboxEditSessionTimestamp) : -1;
- mAutocomplete.onSuggestionSelected(matchPosition, type, currentPageUrl,
- mUrlFocusedFromFakebox, elapsedTimeSinceModified, mUrlBar.getAutocompleteLength(),
- webContents);
+ boolean shouldSkipNativeLog = mShowCachedZeroSuggestResults
+ && (mDeferredOnSelection != null)
+ && !mDeferredOnSelection.shouldLog();
+ if (!shouldSkipNativeLog) {
+ mAutocomplete.onSuggestionSelected(matchPosition, type, currentPageUrl,
+ mUrlFocusedFromFakebox, elapsedTimeSinceModified,
+ mUrlBar.getAutocompleteLength(),
+ webContents);
+ }
loadUrl(url, transition);
}

Powered by Google App Engine
This is Rietveld 408576698