Commit 97dbb8fa authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

JNI refactor: @NativeMethods conversion (AutocompleteController).

This CL was partially created by
//base/android/jni_generator/jni_refactorer.py.

The conversion also required converting unit tests to use the new JNI
mocking approach provided by JniMocker.

Bug: 929661
Change-Id: I57cc3e19641af73e745ea1b224947586ba2287bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1834035
Auto-Submit: Eric Stevenson <estevenson@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707384}
parent f402914b
...@@ -11,6 +11,7 @@ import androidx.annotation.Nullable; ...@@ -11,6 +11,7 @@ import androidx.annotation.Nullable;
import org.chromium.base.Log; import org.chromium.base.Log;
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.annotations.NativeMethods;
import org.chromium.chrome.browser.WarmupManager; import org.chromium.chrome.browser.WarmupManager;
import org.chromium.chrome.browser.ntp.NewTabPage; import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.omnibox.LocationBarVoiceRecognitionHandler.VoiceResult; import org.chromium.chrome.browser.omnibox.LocationBarVoiceRecognitionHandler.VoiceResult;
...@@ -54,7 +55,8 @@ public class AutocompleteController { ...@@ -54,7 +55,8 @@ public class AutocompleteController {
public AutocompleteController(Profile profile, OnSuggestionsReceivedListener listener) { public AutocompleteController(Profile profile, OnSuggestionsReceivedListener listener) {
if (profile != null) { if (profile != null) {
mNativeAutocompleteControllerAndroid = nativeInit(profile); mNativeAutocompleteControllerAndroid =
AutocompleteControllerJni.get().init(AutocompleteController.this, profile);
} }
mListener = listener; mListener = listener;
} }
...@@ -76,7 +78,8 @@ public class AutocompleteController { ...@@ -76,7 +78,8 @@ public class AutocompleteController {
return; return;
} }
mNativeAutocompleteControllerAndroid = nativeInit(profile); mNativeAutocompleteControllerAndroid =
AutocompleteControllerJni.get().init(AutocompleteController.this, profile);
} }
/** /**
...@@ -108,10 +111,12 @@ public class AutocompleteController { ...@@ -108,10 +111,12 @@ public class AutocompleteController {
TextUtils.isEmpty(url)); TextUtils.isEmpty(url));
if (profile == null || TextUtils.isEmpty(url)) return; if (profile == null || TextUtils.isEmpty(url)) return;
mNativeAutocompleteControllerAndroid = nativeInit(profile); mNativeAutocompleteControllerAndroid =
AutocompleteControllerJni.get().init(AutocompleteController.this, profile);
// Initializing the native counterpart might still fail. // Initializing the native counterpart might still fail.
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
nativeStart(mNativeAutocompleteControllerAndroid, text, cursorPosition, null, url, AutocompleteControllerJni.get().start(mNativeAutocompleteControllerAndroid,
AutocompleteController.this, text, cursorPosition, null, url,
pageClassification, preventInlineAutocomplete, false, false, true); pageClassification, preventInlineAutocomplete, false, false, true);
mWaitingForSuggestionsToCache = false; mWaitingForSuggestionsToCache = false;
} }
...@@ -134,7 +139,8 @@ public class AutocompleteController { ...@@ -134,7 +139,8 @@ public class AutocompleteController {
*/ */
public OmniboxSuggestion classify(String text, boolean focusedFromFakebox) { public OmniboxSuggestion classify(String text, boolean focusedFromFakebox) {
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
return nativeClassify(mNativeAutocompleteControllerAndroid, text, focusedFromFakebox); return AutocompleteControllerJni.get().classify(mNativeAutocompleteControllerAndroid,
AutocompleteController.this, text, focusedFromFakebox);
} }
return null; return null;
} }
...@@ -157,11 +163,12 @@ public class AutocompleteController { ...@@ -157,11 +163,12 @@ public class AutocompleteController {
// especially if a Service Worker is used. // especially if a Service Worker is used.
WarmupManager.getInstance().createSpareRenderProcessHost(profile); WarmupManager.getInstance().createSpareRenderProcessHost(profile);
} }
mNativeAutocompleteControllerAndroid = nativeInit(profile); mNativeAutocompleteControllerAndroid =
AutocompleteControllerJni.get().init(AutocompleteController.this, profile);
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
if (mUseCachedZeroSuggestResults) mWaitingForSuggestionsToCache = true; if (mUseCachedZeroSuggestResults) mWaitingForSuggestionsToCache = true;
nativeOnOmniboxFocused(mNativeAutocompleteControllerAndroid, omniboxText, url, AutocompleteControllerJni.get().onOmniboxFocused(mNativeAutocompleteControllerAndroid,
pageClassification, title); AutocompleteController.this, omniboxText, url, pageClassification, title);
} }
} }
...@@ -183,7 +190,8 @@ public class AutocompleteController { ...@@ -183,7 +190,8 @@ public class AutocompleteController {
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
// crbug.com/764749 // crbug.com/764749
Log.w(TAG, "stopping autocomplete."); Log.w(TAG, "stopping autocomplete.");
nativeStop(mNativeAutocompleteControllerAndroid, clear); AutocompleteControllerJni.get().stop(
mNativeAutocompleteControllerAndroid, AutocompleteController.this, clear);
} }
} }
...@@ -193,7 +201,8 @@ public class AutocompleteController { ...@@ -193,7 +201,8 @@ public class AutocompleteController {
*/ */
void resetSession() { void resetSession() {
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
nativeResetSession(mNativeAutocompleteControllerAndroid); AutocompleteControllerJni.get().resetSession(
mNativeAutocompleteControllerAndroid, AutocompleteController.this);
} }
} }
...@@ -203,7 +212,8 @@ public class AutocompleteController { ...@@ -203,7 +212,8 @@ public class AutocompleteController {
*/ */
void deleteSuggestion(int position, int hashCode) { void deleteSuggestion(int position, int hashCode) {
if (mNativeAutocompleteControllerAndroid != 0) { if (mNativeAutocompleteControllerAndroid != 0) {
nativeDeleteSuggestion(mNativeAutocompleteControllerAndroid, position, hashCode); AutocompleteControllerJni.get().deleteSuggestion(mNativeAutocompleteControllerAndroid,
AutocompleteController.this, position, hashCode);
} }
} }
...@@ -256,9 +266,9 @@ public class AutocompleteController { ...@@ -256,9 +266,9 @@ public class AutocompleteController {
assert mNativeAutocompleteControllerAndroid != 0; assert mNativeAutocompleteControllerAndroid != 0;
// Don't natively log voice suggestion results as we add them in Java. // Don't natively log voice suggestion results as we add them in Java.
if (type == OmniboxSuggestionType.VOICE_SUGGEST) return; if (type == OmniboxSuggestionType.VOICE_SUGGEST) return;
nativeOnSuggestionSelected(mNativeAutocompleteControllerAndroid, selectedIndex, hashCode, AutocompleteControllerJni.get().onSuggestionSelected(mNativeAutocompleteControllerAndroid,
currentPageUrl, pageClassification, elapsedTimeSinceModified, completedLength, AutocompleteController.this, selectedIndex, hashCode, currentPageUrl,
webContents); pageClassification, elapsedTimeSinceModified, completedLength, webContents);
} }
/** /**
...@@ -332,32 +342,35 @@ public class AutocompleteController { ...@@ -332,32 +342,35 @@ public class AutocompleteController {
*/ */
String updateMatchDestinationUrlWithQueryFormulationTime( String updateMatchDestinationUrlWithQueryFormulationTime(
int selectedIndex, int hashCode, long elapsedTimeSinceInputChange) { int selectedIndex, int hashCode, long elapsedTimeSinceInputChange) {
return nativeUpdateMatchDestinationURLWithQueryFormulationTime( return AutocompleteControllerJni.get().updateMatchDestinationURLWithQueryFormulationTime(
mNativeAutocompleteControllerAndroid, selectedIndex, hashCode, mNativeAutocompleteControllerAndroid, AutocompleteController.this, selectedIndex,
elapsedTimeSinceInputChange); hashCode, elapsedTimeSinceInputChange);
} }
@VisibleForTesting @NativeMethods
protected native long nativeInit(Profile profile); interface Natives {
private native void nativeStart(long nativeAutocompleteControllerAndroid, String text, long init(AutocompleteController caller, Profile profile);
int cursorPosition, String desiredTld, String currentUrl, int pageClassification, void start(long nativeAutocompleteControllerAndroid, AutocompleteController caller,
boolean preventInlineAutocomplete, boolean preferKeyword, String text, int cursorPosition, String desiredTld, String currentUrl,
int pageClassification, boolean preventInlineAutocomplete, boolean preferKeyword,
boolean allowExactKeywordMatch, boolean wantAsynchronousMatches); boolean allowExactKeywordMatch, boolean wantAsynchronousMatches);
private native OmniboxSuggestion nativeClassify( OmniboxSuggestion classify(long nativeAutocompleteControllerAndroid,
long nativeAutocompleteControllerAndroid, String text, boolean focusedFromFakebox); AutocompleteController caller, String text, boolean focusedFromFakebox);
private native void nativeStop(long nativeAutocompleteControllerAndroid, boolean clearResults); void stop(long nativeAutocompleteControllerAndroid, AutocompleteController caller,
private native void nativeResetSession(long nativeAutocompleteControllerAndroid); boolean clearResults);
private native void nativeOnSuggestionSelected(long nativeAutocompleteControllerAndroid, void resetSession(long nativeAutocompleteControllerAndroid, AutocompleteController caller);
int selectedIndex, int hashCode, String currentPageUrl, int pageClassification, void onSuggestionSelected(long nativeAutocompleteControllerAndroid,
long elapsedTimeSinceModified, int completedLength, WebContents webContents); AutocompleteController caller, int selectedIndex, int hashCode,
private native void nativeOnOmniboxFocused(long nativeAutocompleteControllerAndroid, String currentPageUrl, int pageClassification, long elapsedTimeSinceModified,
String omniboxText, String currentUrl, int pageClassification, String currentTitle); int completedLength, WebContents webContents);
private native void nativeDeleteSuggestion( void onOmniboxFocused(long nativeAutocompleteControllerAndroid,
long nativeAutocompleteControllerAndroid, int selectedIndex, int hashCode); AutocompleteController caller, String omniboxText, String currentUrl,
private native String nativeUpdateMatchDestinationURLWithQueryFormulationTime( int pageClassification, String currentTitle);
long nativeAutocompleteControllerAndroid, int selectedIndex, int hashCode, void deleteSuggestion(long nativeAutocompleteControllerAndroid,
long elapsedTimeSinceInputChange); AutocompleteController caller, int selectedIndex, int hashCode);
String updateMatchDestinationURLWithQueryFormulationTime(
long nativeAutocompleteControllerAndroid, AutocompleteController caller,
int selectedIndex, int hashCode, long elapsedTimeSinceInputChange);
/** /**
* Given a search query, this will attempt to see if the query appears to be portion of a * Given a search query, this will attempt to see if the query appears to be portion of a
* properly formed URL. If it appears to be a URL, this will return the fully qualified * properly formed URL. If it appears to be a URL, this will return the fully qualified
...@@ -367,10 +380,11 @@ public class AutocompleteController { ...@@ -367,10 +380,11 @@ public class AutocompleteController {
* @param query The query to be expanded into a fully qualified URL if appropriate. * @param query The query to be expanded into a fully qualified URL if appropriate.
* @return The fully qualified URL or null. * @return The fully qualified URL or null.
*/ */
static native String nativeQualifyPartialURLQuery(String query); String qualifyPartialURLQuery(String query);
/** /**
* Sends a zero suggest request to the server in order to pre-populate the result cache. * Sends a zero suggest request to the server in order to pre-populate the result cache.
*/ */
static native void nativePrefetchZeroSuggestResults(); void prefetchZeroSuggestResults();
}
} }
...@@ -32,7 +32,7 @@ public class AutocompleteCoordinatorFactory { ...@@ -32,7 +32,7 @@ public class AutocompleteCoordinatorFactory {
/** /**
* Temporary shortcut for {@link org.chromium.chrome.browser.IntentHandler} to access * Temporary shortcut for {@link org.chromium.chrome.browser.IntentHandler} to access
* {@link AutocompleteController#nativeQualifyPartialURLQuery(String)} without having an * {@link AutocompleteControllerJni.get().qualifyPartialURLQuery(String)} without having an
* instance of {@link AutocompleteCoordinator}. * instance of {@link AutocompleteCoordinator}.
* *
* TODO(crbug.com/966424): Fix the dependency issue and remove this method. * TODO(crbug.com/966424): Fix the dependency issue and remove this method.
...@@ -42,6 +42,6 @@ public class AutocompleteCoordinatorFactory { ...@@ -42,6 +42,6 @@ public class AutocompleteCoordinatorFactory {
*/ */
@Deprecated @Deprecated
public static String qualifyPartialURLQuery(String query) { public static String qualifyPartialURLQuery(String query) {
return AutocompleteController.nativeQualifyPartialURLQuery(query); return AutocompleteControllerJni.get().qualifyPartialURLQuery(query);
} }
} }
...@@ -265,12 +265,12 @@ public class AutocompleteCoordinatorImpl implements AutocompleteCoordinator { ...@@ -265,12 +265,12 @@ public class AutocompleteCoordinatorImpl implements AutocompleteCoordinator {
@Override @Override
public String qualifyPartialURLQuery(String query) { public String qualifyPartialURLQuery(String query) {
return AutocompleteController.nativeQualifyPartialURLQuery(query); return AutocompleteControllerJni.get().qualifyPartialURLQuery(query);
} }
@Override @Override
public void prefetchZeroSuggestResults() { public void prefetchZeroSuggestResults() {
AutocompleteController.nativePrefetchZeroSuggestResults(); AutocompleteControllerJni.get().prefetchZeroSuggestResults();
} }
@VisibleForTesting @VisibleForTesting
......
...@@ -381,7 +381,7 @@ class AutocompleteMediator ...@@ -381,7 +381,7 @@ class AutocompleteMediator
private void maybeTriggerCacheRefresh(String url) { private void maybeTriggerCacheRefresh(String url) {
if (url == null) return; if (url == null) return;
if (!UrlConstants.NTP_URL.equals(url)) return; if (!UrlConstants.NTP_URL.equals(url)) return;
AutocompleteController.nativePrefetchZeroSuggestResults(); AutocompleteControllerJni.get().prefetchZeroSuggestResults();
} }
}; };
} }
......
...@@ -185,11 +185,6 @@ public class OmniboxTestUtils { ...@@ -185,11 +185,6 @@ public class OmniboxTestUtils {
mSuggestionsDispatcher = null; mSuggestionsDispatcher = null;
} }
@Override
protected long nativeInit(Profile profile) {
return 1;
}
@Override @Override
public void setProfile(Profile profile) {} public void setProfile(Profile profile) {}
} }
...@@ -219,11 +214,6 @@ public class OmniboxTestUtils { ...@@ -219,11 +214,6 @@ public class OmniboxTestUtils {
@Override @Override
public void stop(boolean clear) {} public void stop(boolean clear) {}
@Override
protected long nativeInit(Profile profile) {
return 1;
}
@Override @Override
public void setProfile(Profile profile) {} public void setProfile(Profile profile) {}
} }
......
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