Check the validity of selection range for SmartText selection
It is possible that by the time the surrounding text request reaches
the Renderer the selection range becomes empty (e.g. we have an input
caret). The empty selection range is a valid situation for surrounding
text algorithm which returns a text around the input caret, but
invalid for Android TextClassifier.
This CL checks the indices of the selection in the surrounding
text responce from Blink and treats an empty or invalid range
the same way as an empty text, i.e. bypasses classification.
BUG=751397
Change-Id: I3646a16d701e04627e4729f5226850d2b83993b5
Reviewed-on: https://chromium-review.googlesource.com/601256
Commit-Queue: Tima Vaisburd <[email protected]>
Reviewed-by: Bo <[email protected]>
Reviewed-by: Pedro Amaral <[email protected]>
Cr-Commit-Position: refs/heads/master@{#493201}
diff --git a/content/public/android/java/src/org/chromium/content/browser/SmartSelectionClient.java b/content/public/android/java/src/org/chromium/content/browser/SmartSelectionClient.java
index ac1a2af..1990bca 100644
--- a/content/public/android/java/src/org/chromium/content/browser/SmartSelectionClient.java
+++ b/content/public/android/java/src/org/chromium/content/browser/SmartSelectionClient.java
@@ -130,7 +130,7 @@
@CalledByNative
private void onSurroundingTextReceived(
@RequestType int callbackData, String text, int start, int end) {
- if (TextUtils.isEmpty(text)) {
+ if (!textHasValidSelection(text, start, end)) {
mCallback.onClassified(new SmartSelectionProvider.Result());
return;
}
@@ -150,6 +150,10 @@
}
}
+ private boolean textHasValidSelection(String text, int start, int end) {
+ return !TextUtils.isEmpty(text) && 0 <= start && start < end && end <= text.length();
+ }
+
private native long nativeInit(WebContents webContents);
private native void nativeRequestSurroundingText(
long nativeSmartSelectionClient, int numExtraCharacters, int callbackData);