OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser; | |
6 | |
7 import android.support.annotation.IntDef; | |
8 import android.text.TextUtils; | |
9 | |
10 import org.chromium.base.annotations.CalledByNative; | |
11 import org.chromium.base.annotations.JNINamespace; | |
12 import org.chromium.content_public.browser.WebContents; | |
13 import org.chromium.ui.base.WindowAndroid; | |
14 import org.chromium.ui.touch_selection.SelectionEventType; | |
15 | |
16 import java.lang.annotation.Retention; | |
17 import java.lang.annotation.RetentionPolicy; | |
18 | |
19 /** | |
20 * A class that controls the classification of the textual selection. | |
21 * It requests the selection together with its surrounding text from | |
22 * the focused frame and sends it to ContextSelectionProvider | |
23 * which does the classification itself. | |
24 */ | |
25 @JNINamespace("content") | |
26 public class ContextSelectionClient implements SelectionClient { | |
27 private static final String TAG = "ContextSelClient"; // 20 char limit | |
boliu
2017/03/20 20:27:26
not used
Tima Vaisburd
2017/03/21 02:07:02
Removed.
| |
28 | |
29 @IntDef({CLASSIFY, SUGGEST_AND_CLASSIFY}) | |
30 @Retention(RetentionPolicy.SOURCE) | |
31 private @interface RequestType {} | |
32 | |
33 private static final int CLASSIFY = 0; | |
34 private static final int SUGGEST_AND_CLASSIFY = 1; | |
35 | |
36 // The callback that would receive the information from ContextSelectionProv ider. | |
37 private class ProviderCallback implements ContextSelectionProvider.ResultCal lback { | |
38 @Override | |
39 public void onClassified(ContextSelectionProvider.Result result) { | |
40 mOuterCallback.onClassified(result); | |
41 } | |
42 }; | |
43 | |
44 private long mNativeContextSelectionClient; | |
45 private ContextSelectionProvider mProvider; | |
46 private ProviderCallback mProviderCallback; | |
47 private ContextSelectionProvider.ResultCallback mOuterCallback; | |
48 | |
49 /** | |
50 * Creates the ContextSelectionClient. Returns null in case ContextSelection Provider | |
51 * does not exist in the system. | |
52 */ | |
53 public static ContextSelectionClient create(ContextSelectionProvider.ResultC allback callback, | |
54 WindowAndroid windowAndroid, WebContents webContent) { | |
55 ContextSelectionClient client = new ContextSelectionClient(callback); | |
56 if (!client.initialize(windowAndroid, webContent)) client = null; | |
57 return client; | |
58 } | |
59 | |
60 private ContextSelectionClient(ContextSelectionProvider.ResultCallback callb ack) { | |
61 mOuterCallback = callback; | |
62 } | |
63 | |
64 private boolean initialize(WindowAndroid windowAndroid, WebContents webConte nts) { | |
65 // We make a chain of callbacks: this class receiive result from provide r through | |
66 // mProviderCallback and passes the info to embedder with mOuterCallback . | |
67 mProviderCallback = new ProviderCallback(); | |
68 mProvider = ContentClassFactory.get().createContextSelectionProvider( | |
69 mProviderCallback, windowAndroid); | |
70 | |
71 // ContextSelectionProvider might not exist. | |
72 if (mProvider == null) { | |
73 return false; | |
74 } | |
75 | |
76 mNativeContextSelectionClient = nativeInit(webContents); | |
77 return true; | |
78 } | |
79 | |
80 @CalledByNative | |
81 private void onNativeSideDestroyed(long nativeContextSelectionClient) { | |
82 assert nativeContextSelectionClient == mNativeContextSelectionClient; | |
83 mNativeContextSelectionClient = 0; | |
84 } | |
85 | |
86 // SelectionClient implementation | |
87 @Override | |
88 public void onSelectionChanged(String selection) {} | |
89 | |
90 @Override | |
91 public void onSelectionEvent(int eventType, float posXPix, float posYPix) { | |
92 switch (eventType) { | |
93 case SelectionEventType.SELECTION_HANDLES_SHOWN: | |
94 // This event is sent when the long press is detected which caus es | |
95 // selection to appear for the first time. Temporarily hiding th e | |
96 // handles that happens e.g. during scroll does not affect this event. | |
97 requestSurroundingText(SUGGEST_AND_CLASSIFY); | |
98 break; | |
99 | |
100 case SelectionEventType.SELECTION_HANDLES_CLEARED: | |
101 // The ActionMode should be stopped when this event comes. | |
102 cancelAllRequests(); | |
103 break; | |
104 | |
105 case SelectionEventType.SELECTION_HANDLE_DRAG_STOPPED: | |
106 // This event is sent after a user stopped dragging one of the | |
107 // selection handles, i.e. stopped modifying the selection. | |
108 requestSurroundingText(CLASSIFY); | |
109 break; | |
110 | |
111 default: | |
112 break; // ignore | |
113 } | |
114 } | |
115 | |
116 @Override | |
117 public void showUnhandledTapUIIfNeeded(int x, int y) {} | |
118 | |
119 @Override | |
120 public boolean sendsMenuUpdates() { | |
121 return true; | |
122 } | |
123 | |
124 private void cancelAllRequests() { | |
125 if (mNativeContextSelectionClient != 0) { | |
126 nativeCancelAllRequests(mNativeContextSelectionClient); | |
127 } | |
128 | |
129 mProvider.cancelAllRequests(); | |
130 } | |
131 | |
132 private void requestSurroundingText(@RequestType int callbackData) { | |
133 if (mNativeContextSelectionClient == 0) { | |
134 onSurroundingTextReceived(callbackData, "", 0, 0); | |
135 return; | |
136 } | |
137 | |
138 nativeRequestSurroundingText(mNativeContextSelectionClient, callbackData ); | |
139 } | |
140 | |
141 @CalledByNative | |
142 private void onSurroundingTextReceived( | |
143 @RequestType int callbackData, String text, int start, int end) { | |
144 if (TextUtils.isEmpty(text)) { | |
145 mOuterCallback.onClassified(new ContextSelectionProvider.Result()); | |
146 return; | |
147 } | |
148 | |
149 switch (callbackData) { | |
150 case SUGGEST_AND_CLASSIFY: | |
151 mProvider.sendSuggestAndClassifyRequest(text, start, end); | |
152 break; | |
153 | |
154 case CLASSIFY: | |
155 mProvider.sendClassifyRequest(text, start, end); | |
156 break; | |
157 | |
158 default: | |
159 assert false : "Unexpected callback data"; | |
160 break; | |
161 } | |
162 } | |
163 | |
164 private native long nativeInit(WebContents webContents); | |
165 private native void nativeRequestSurroundingText( | |
166 long nativeContextSelectionClient, int callbackData); | |
167 private native void nativeCancelAllRequests(long nativeContextSelectionClien t); | |
168 } | |
OLD | NEW |