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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/omnibox/AutocompleteController.java

Issue 2738583002: Add a way to cache and show zero suggest results before native (Closed)
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.omnibox; 5 package org.chromium.chrome.browser.omnibox;
6 6
7 import android.os.Bundle; 7 import android.os.Bundle;
8 import android.text.TextUtils; 8 import android.text.TextUtils;
9 9
10 import org.chromium.base.VisibleForTesting; 10 import org.chromium.base.VisibleForTesting;
(...skipping 15 matching lines...) Expand all
26 26
27 // Maximum number of voice suggestions to show. 27 // Maximum number of voice suggestions to show.
28 private static final int MAX_VOICE_SUGGESTION_COUNT = 3; 28 private static final int MAX_VOICE_SUGGESTION_COUNT = 3;
29 29
30 private long mNativeAutocompleteControllerAndroid; 30 private long mNativeAutocompleteControllerAndroid;
31 private long mCurrentNativeAutocompleteResult; 31 private long mCurrentNativeAutocompleteResult;
32 private final OnSuggestionsReceivedListener mListener; 32 private final OnSuggestionsReceivedListener mListener;
33 private final VoiceSuggestionProvider mVoiceSuggestionProvider = new VoiceSu ggestionProvider(); 33 private final VoiceSuggestionProvider mVoiceSuggestionProvider = new VoiceSu ggestionProvider();
34 34
35 35
36 private boolean mUseCachedZeroSuggestResults;
37 private boolean mWaitingForSuggestionsToCache;
38
36 /** 39 /**
37 * Listener for receiving OmniboxSuggestions. 40 * Listener for receiving OmniboxSuggestions.
38 */ 41 */
39 public static interface OnSuggestionsReceivedListener { 42 public static interface OnSuggestionsReceivedListener {
40 void onSuggestionsReceived(List<OmniboxSuggestion> suggestions, 43 void onSuggestionsReceived(List<OmniboxSuggestion> suggestions,
41 String inlineAutocompleteText); 44 String inlineAutocompleteText);
42 } 45 }
43 46
44 public AutocompleteController(OnSuggestionsReceivedListener listener) { 47 public AutocompleteController(OnSuggestionsReceivedListener listener) {
45 this(null, listener); 48 this(null, listener);
(...skipping 20 matching lines...) Expand all
66 stop(true); 69 stop(true);
67 if (profile == null) { 70 if (profile == null) {
68 mNativeAutocompleteControllerAndroid = 0; 71 mNativeAutocompleteControllerAndroid = 0;
69 return; 72 return;
70 } 73 }
71 74
72 mNativeAutocompleteControllerAndroid = nativeInit(profile); 75 mNativeAutocompleteControllerAndroid = nativeInit(profile);
73 } 76 }
74 77
75 /** 78 /**
79 * Sets to use any cached zero suggest results if there are any available an d start caching them
80 * for all zero suggest updates.
81 */
82 public void setUseCachedZeroSuggestResults() {
Maria 2017/03/08 06:48:08 nit: I don't know why but I have a lot of trouble
Yusuf 2017/03/08 20:10:46 Done.
83 mUseCachedZeroSuggestResults = true;
84 List<OmniboxSuggestion> suggestions =
85 OmniboxSuggestion.getCachedOmniboxSuggestionsForZeroSuggest();
86 if (suggestions != null) mListener.onSuggestionsReceived(suggestions, "" );
Maria 2017/03/08 06:48:08 I wonder if we can hit some subtle assumptions her
Yusuf 2017/03/08 20:10:46 Acknowledged.
87 }
88
89 /**
76 * Starts querying for omnibox suggestions for a given text. 90 * Starts querying for omnibox suggestions for a given text.
77 * 91 *
78 * @param profile The profile to use for starting the AutocompleteController 92 * @param profile The profile to use for starting the AutocompleteController
79 * @param url The URL of the current tab, used to suggest query refinements. 93 * @param url The URL of the current tab, used to suggest query refinements.
80 * @param text The text to query autocomplete suggestions for. 94 * @param text The text to query autocomplete suggestions for.
81 * @param preventInlineAutocomplete Whether autocomplete suggestions should be prevented. 95 * @param preventInlineAutocomplete Whether autocomplete suggestions should be prevented.
82 */ 96 */
83 public void start(Profile profile, String url, String text, 97 public void start(Profile profile, String url, String text,
84 boolean preventInlineAutocomplete) { 98 boolean preventInlineAutocomplete) {
85 start(profile, url, text, -1, preventInlineAutocomplete); 99 start(profile, url, text, -1, preventInlineAutocomplete);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 * @param omniboxText The text displayed in the omnibox. 147 * @param omniboxText The text displayed in the omnibox.
134 * @param url The url of the currently loaded web page. 148 * @param url The url of the currently loaded web page.
135 * @param focusedFromFakebox Whether the user entered the omnibox by tapping the fakebox on the 149 * @param focusedFromFakebox Whether the user entered the omnibox by tapping the fakebox on the
136 * native NTP. This should be false on all other p ages. 150 * native NTP. This should be false on all other p ages.
137 */ 151 */
138 public void startZeroSuggest(Profile profile, String omniboxText, String url , 152 public void startZeroSuggest(Profile profile, String omniboxText, String url ,
139 boolean focusedFromFakebox) { 153 boolean focusedFromFakebox) {
140 if (profile == null || TextUtils.isEmpty(url)) return; 154 if (profile == null || TextUtils.isEmpty(url)) return;
141 mNativeAutocompleteControllerAndroid = nativeInit(profile); 155 mNativeAutocompleteControllerAndroid = nativeInit(profile);
142 if (mNativeAutocompleteControllerAndroid != 0) { 156 if (mNativeAutocompleteControllerAndroid != 0) {
157 if (mUseCachedZeroSuggestResults) mWaitingForSuggestionsToCache = tr ue;
143 nativeOnOmniboxFocused(mNativeAutocompleteControllerAndroid, omnibox Text, url, 158 nativeOnOmniboxFocused(mNativeAutocompleteControllerAndroid, omnibox Text, url,
144 focusedFromFakebox); 159 focusedFromFakebox);
145 } 160 }
146 } 161 }
147 162
148 /** 163 /**
149 * Stops generating autocomplete suggestions for the currently specified tex t from 164 * Stops generating autocomplete suggestions for the currently specified tex t from
150 * {@link #start(Profile,String, String, boolean)}. 165 * {@link #start(Profile,String, String, boolean)}.
151 * 166 *
152 * <p> 167 * <p>
153 * Calling this method with {@code false}, will result in 168 * Calling this method with {@code false}, will result in
154 * {@link #onSuggestionsReceived(List, String, long)} being called with an e mpty 169 * {@link #onSuggestionsReceived(List, String, long)} being called with an e mpty
155 * result set. 170 * result set.
156 * 171 *
157 * @param clear Whether to clear the most recent autocomplete results. 172 * @param clear Whether to clear the most recent autocomplete results.
158 */ 173 */
159 public void stop(boolean clear) { 174 public void stop(boolean clear) {
160 if (clear) mVoiceSuggestionProvider.clearVoiceSearchResults(); 175 if (clear) mVoiceSuggestionProvider.clearVoiceSearchResults();
161 mCurrentNativeAutocompleteResult = 0; 176 mCurrentNativeAutocompleteResult = 0;
177 mWaitingForSuggestionsToCache = false;
162 if (mNativeAutocompleteControllerAndroid != 0) { 178 if (mNativeAutocompleteControllerAndroid != 0) {
163 nativeStop(mNativeAutocompleteControllerAndroid, clear); 179 nativeStop(mNativeAutocompleteControllerAndroid, clear);
164 } 180 }
165 } 181 }
166 182
167 /** 183 /**
168 * Resets session for autocomplete controller. This happens every time we st art typing 184 * Resets session for autocomplete controller. This happens every time we st art typing
169 * new input into the omnibox. 185 * new input into the omnibox.
170 */ 186 */
171 public void resetSession() { 187 public void resetSession() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 219 }
204 220
205 // Run through new providers to get an updated list of suggestions. 221 // Run through new providers to get an updated list of suggestions.
206 suggestions = mVoiceSuggestionProvider.addVoiceSuggestions( 222 suggestions = mVoiceSuggestionProvider.addVoiceSuggestions(
207 suggestions, MAX_VOICE_SUGGESTION_COUNT); 223 suggestions, MAX_VOICE_SUGGESTION_COUNT);
208 224
209 mCurrentNativeAutocompleteResult = currentNativeAutocompleteResult; 225 mCurrentNativeAutocompleteResult = currentNativeAutocompleteResult;
210 226
211 // Notify callbacks of suggestions. 227 // Notify callbacks of suggestions.
212 mListener.onSuggestionsReceived(suggestions, inlineAutocompleteText); 228 mListener.onSuggestionsReceived(suggestions, inlineAutocompleteText);
229 if (mWaitingForSuggestionsToCache) {
Maria 2017/03/08 06:48:08 This is a little fragile because it relies on zero
Yusuf 2017/03/08 20:10:46 On the omnibox side, we never combine these calls
Maria 2017/03/09 17:55:09 Correct. Each onSuggestionReceived set is newly ra
Yusuf 2017/03/09 19:29:38 Done.
230 OmniboxSuggestion.cacheOmniboxSuggestionListForZeroSuggest(suggestio ns);
231 mWaitingForSuggestionsToCache = false;
232 }
213 } 233 }
214 234
215 @CalledByNative 235 @CalledByNative
216 private void notifyNativeDestroyed() { 236 private void notifyNativeDestroyed() {
217 mNativeAutocompleteControllerAndroid = 0; 237 mNativeAutocompleteControllerAndroid = 0;
218 } 238 }
219 239
220 /** 240 /**
221 * Called whenever a navigation happens from the omnibox to record metrics a bout the user's 241 * Called whenever a navigation happens from the omnibox to record metrics a bout the user's
222 * interaction with the omnibox. 242 * interaction with the omnibox.
223 * 243 *
224 * @param selectedIndex The index of the suggestion that was selected. 244 * @param selectedIndex The index of the suggestion that was selected.
225 * @param type The type of the selected suggestion. 245 * @param type The type of the selected suggestion.
226 * @param currentPageUrl The URL of the current page. 246 * @param currentPageUrl The URL of the current page.
227 * @param focusedFromFakebox Whether the user entered the omnibox by tapping the fakebox on the 247 * @param focusedFromFakebox Whether the user entered the omnibox by tapping the fakebox on the
228 * native NTP. This should be false on all other p ages. 248 * native NTP. This should be false on all other p ages.
229 * @param elapsedTimeSinceModified The number of ms that passed between the user first 249 * @param elapsedTimeSinceModified The number of ms that passed between the user first
230 * modifying text in the omnibox and selecti ng a suggestion. 250 * modifying text in the omnibox and selecti ng a suggestion.
231 * @param completedLength The length of the default match's inline autocompl etion if any. 251 * @param completedLength The length of the default match's inline autocompl etion if any.
232 * @param webContents The web contents for the tab where the selected sugges tion will be shown. 252 * @param webContents The web contents for the tab where the selected sugges tion will be shown.
233 */ 253 */
234 public void onSuggestionSelected(int selectedIndex, int type, 254 public void onSuggestionSelected(int selectedIndex, int type,
235 String currentPageUrl, boolean focusedFromFakebox, long elapsedTimeS inceModified, 255 String currentPageUrl, boolean focusedFromFakebox, long elapsedTimeS inceModified,
236 int completedLength, WebContents webContents) { 256 int completedLength, WebContents webContents) {
257 assert mNativeAutocompleteControllerAndroid != 0;
237 // Don't natively log voice suggestion results as we add them in Java. 258 // Don't natively log voice suggestion results as we add them in Java.
238 if (type == OmniboxSuggestionType.VOICE_SUGGEST) return; 259 if (type == OmniboxSuggestionType.VOICE_SUGGEST) return;
239 nativeOnSuggestionSelected(mNativeAutocompleteControllerAndroid, selecte dIndex, 260 nativeOnSuggestionSelected(mNativeAutocompleteControllerAndroid, selecte dIndex,
240 currentPageUrl, focusedFromFakebox, elapsedTimeSinceModified, 261 currentPageUrl, focusedFromFakebox, elapsedTimeSinceModified,
241 completedLength, webContents); 262 completedLength, webContents);
242 } 263 }
243 264
244 /** 265 /**
245 * Pass the autocomplete controller a {@link Bundle} representing the result s of a voice 266 * Pass the autocomplete controller a {@link Bundle} representing the result s of a voice
246 * recognition. 267 * recognition.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 * 321 *
301 * @param selectedIndex The index of the autocomplete entry selected. 322 * @param selectedIndex The index of the autocomplete entry selected.
302 * @param elapsedTimeSinceInputChange The number of ms between the time the user started 323 * @param elapsedTimeSinceInputChange The number of ms between the time the user started
303 * typing in the omnibox and the time the user has selected 324 * typing in the omnibox and the time the user has selected
304 * a suggestion. 325 * a suggestion.
305 * @return The url to navigate to for this match with aqs parameter updated, if we are 326 * @return The url to navigate to for this match with aqs parameter updated, if we are
306 * making a Google search query. 327 * making a Google search query.
307 */ 328 */
308 public String updateMatchDestinationUrlWithQueryFormulationTime(int selected Index, 329 public String updateMatchDestinationUrlWithQueryFormulationTime(int selected Index,
309 long elapsedTimeSinceInputChange) { 330 long elapsedTimeSinceInputChange) {
331 if (mNativeAutocompleteControllerAndroid == 0) return null;
Maria 2017/03/08 06:48:08 I thought this couldn't get called until native is
Yusuf 2017/03/08 20:10:46 Done. Yeah this seems to be a leftover from initia
310 return nativeUpdateMatchDestinationURLWithQueryFormulationTime( 332 return nativeUpdateMatchDestinationURLWithQueryFormulationTime(
311 mNativeAutocompleteControllerAndroid, selectedIndex, elapsedTime SinceInputChange); 333 mNativeAutocompleteControllerAndroid, selectedIndex, elapsedTime SinceInputChange);
312 } 334 }
313 335
314 @VisibleForTesting 336 @VisibleForTesting
315 protected native long nativeInit(Profile profile); 337 protected native long nativeInit(Profile profile);
316 private native void nativeStart(long nativeAutocompleteControllerAndroid, St ring text, 338 private native void nativeStart(long nativeAutocompleteControllerAndroid, St ring text,
317 int cursorPosition, String desiredTld, String currentUrl, 339 int cursorPosition, String desiredTld, String currentUrl,
318 boolean preventInlineAutocomplete, boolean preferKeyword, 340 boolean preventInlineAutocomplete, boolean preferKeyword,
319 boolean allowExactKeywordMatch, boolean wantAsynchronousMatches); 341 boolean allowExactKeywordMatch, boolean wantAsynchronousMatches);
(...skipping 22 matching lines...) Expand all
342 * @param query The query to be expanded into a fully qualified URL if appro priate. 364 * @param query The query to be expanded into a fully qualified URL if appro priate.
343 * @return The fully qualified URL or null. 365 * @return The fully qualified URL or null.
344 */ 366 */
345 public static native String nativeQualifyPartialURLQuery(String query); 367 public static native String nativeQualifyPartialURLQuery(String query);
346 368
347 /** 369 /**
348 * Sends a zero suggest request to the server in order to pre-populate the r esult cache. 370 * Sends a zero suggest request to the server in order to pre-populate the r esult cache.
349 */ 371 */
350 public static native void nativePrefetchZeroSuggestResults(); 372 public static native void nativePrefetchZeroSuggestResults();
351 } 373 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698