Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.support.annotation.IntDef; | 7 import android.support.annotation.IntDef; |
| 8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
| 9 | 9 |
| 10 import org.chromium.base.Log; | |
| 10 import org.chromium.base.annotations.CalledByNative; | 11 import org.chromium.base.annotations.CalledByNative; |
| 11 import org.chromium.base.annotations.JNINamespace; | 12 import org.chromium.base.annotations.JNINamespace; |
| 13 import org.chromium.content_public.browser.TextClassifierProvider; | |
| 12 import org.chromium.content_public.browser.WebContents; | 14 import org.chromium.content_public.browser.WebContents; |
| 13 import org.chromium.ui.base.WindowAndroid; | |
| 14 import org.chromium.ui.touch_selection.SelectionEventType; | 15 import org.chromium.ui.touch_selection.SelectionEventType; |
| 15 | 16 |
| 16 import java.lang.annotation.Retention; | 17 import java.lang.annotation.Retention; |
| 17 import java.lang.annotation.RetentionPolicy; | 18 import java.lang.annotation.RetentionPolicy; |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * A class that controls the classification of the textual selection. | 21 * A class that controls the classification of the textual selection. |
| 21 * It requests the selection together with its surrounding text from the focused frame and sends it | 22 * It requests the selection together with its surrounding text from the focused frame and sends it |
| 22 * to ContextSelectionProvider which does the classification itself. | 23 * to ContextSelectionProvider which does the classification itself. |
| 23 */ | 24 */ |
| 24 @JNINamespace("content") | 25 @JNINamespace("content") |
| 25 public class ContextSelectionClient implements SelectionClient { | 26 public class ContextSelectionClient implements SelectionClient { |
| 26 @IntDef({CLASSIFY, SUGGEST_AND_CLASSIFY}) | 27 @IntDef({CLASSIFY, SUGGEST_AND_CLASSIFY}) |
| 27 @Retention(RetentionPolicy.SOURCE) | 28 @Retention(RetentionPolicy.SOURCE) |
| 28 private @interface RequestType {} | 29 private @interface RequestType {} |
| 29 | 30 |
| 30 // Request to obtain the type (e.g. phone number, e-mail address) and the mo st | 31 // Request to obtain the type (e.g. phone number, e-mail address) and the mo st |
| 31 // appropriate operation for the selected text. | 32 // appropriate operation for the selected text. |
| 32 private static final int CLASSIFY = 0; | 33 private static final int CLASSIFY = 0; |
| 33 | 34 |
| 34 // Request to obtain the type (e.g. phone number, e-mail address), the most | 35 // Request to obtain the type (e.g. phone number, e-mail address), the most |
| 35 // appropriate operation for the selected text and a better selection bounda ries. | 36 // appropriate operation for the selected text and a better selection bounda ries. |
| 36 private static final int SUGGEST_AND_CLASSIFY = 1; | 37 private static final int SUGGEST_AND_CLASSIFY = 1; |
| 37 | 38 |
| 38 // The maximal number of characters on the left and on the right from the cu rrent selection. | 39 // The maximal number of characters on the left and on the right from the cu rrent selection. |
| 39 // Used for surrounding text request. | 40 // Used for surrounding text request. |
| 40 private static final int NUM_EXTRA_CHARS = 100; | 41 private static final int NUM_EXTRA_CHARS = 100; |
| 41 | 42 |
| 43 private static final String TAG = "ContextSelClient"; // 20 characters limit | |
| 44 | |
| 42 private long mNativeContextSelectionClient; | 45 private long mNativeContextSelectionClient; |
| 43 private ContextSelectionProvider mProvider; | 46 private ContextSelectionProvider mProvider; |
| 44 private ContextSelectionProvider.ResultCallback mCallback; | 47 private ContextSelectionProvider.ResultCallback mCallback; |
| 45 | 48 |
| 46 /** | 49 /** |
| 47 * Creates the ContextSelectionClient. Returns null in case ContextSelection Provider | 50 * Creates the ContextSelectionClient. Returns null in case ContextSelection Provider |
| 48 * does not exist in the system. | 51 * does not exist in the system. |
| 49 */ | 52 */ |
| 50 public static ContextSelectionClient create(ContextSelectionProvider.ResultC allback callback, | 53 public static ContextSelectionClient create( |
| 51 WindowAndroid windowAndroid, WebContents webContents) { | 54 ContextSelectionProvider.ResultCallback callback, WebContents webCon tents) { |
| 55 TextClassifierProvider tc_provider = webContents.getTextClassifierProvid er(); | |
|
boliu
2017/04/25 20:30:28
camelCase
| |
| 56 if (tc_provider == null) { | |
| 57 Log.e(TAG, "Cannot create object: no TextClassifierProvider"); | |
|
boliu
2017/04/25 20:30:28
assert, don't log
| |
| 58 return null; | |
| 59 } | |
| 52 ContextSelectionProvider provider = | 60 ContextSelectionProvider provider = |
| 53 ContentClassFactory.get().createContextSelectionProvider(callbac k, windowAndroid); | 61 ContentClassFactory.get().createContextSelectionProvider(callbac k, tc_provider); |
| 54 | 62 |
| 55 // ContextSelectionProvider might not exist. | 63 // ContextSelectionProvider might not exist. |
| 56 if (provider == null) return null; | 64 if (provider == null) return null; |
| 57 | 65 |
| 58 return new ContextSelectionClient(provider, callback, webContents); | 66 return new ContextSelectionClient(provider, callback, webContents); |
| 59 } | 67 } |
| 60 | 68 |
| 61 private ContextSelectionClient(ContextSelectionProvider provider, | 69 private ContextSelectionClient(ContextSelectionProvider provider, |
| 62 ContextSelectionProvider.ResultCallback callback, WebContents webCon tents) { | 70 ContextSelectionProvider.ResultCallback callback, WebContents webCon tents) { |
| 63 mProvider = provider; | 71 mProvider = provider; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 assert false : "Unexpected callback data"; | 156 assert false : "Unexpected callback data"; |
| 149 break; | 157 break; |
| 150 } | 158 } |
| 151 } | 159 } |
| 152 | 160 |
| 153 private native long nativeInit(WebContents webContents); | 161 private native long nativeInit(WebContents webContents); |
| 154 private native void nativeRequestSurroundingText( | 162 private native void nativeRequestSurroundingText( |
| 155 long nativeContextSelectionClient, int numExtraCharacters, int callb ackData); | 163 long nativeContextSelectionClient, int numExtraCharacters, int callb ackData); |
| 156 private native void nativeCancelAllRequests(long nativeContextSelectionClien t); | 164 private native void nativeCancelAllRequests(long nativeContextSelectionClien t); |
| 157 } | 165 } |
| OLD | NEW |