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.Log; | |
11 import org.chromium.base.annotations.CalledByNative; | |
12 import org.chromium.base.annotations.JNINamespace; | |
13 import org.chromium.content_public.browser.WebContents; | |
14 import org.chromium.ui.base.WindowAndroid; | |
15 import org.chromium.ui.touch_selection.SelectionEventType; | |
16 | |
17 import java.lang.annotation.Retention; | |
18 import java.lang.annotation.RetentionPolicy; | |
19 | |
20 /** | |
21 * A class that controls the classification of the textual selection. | |
22 * It requests the selection together with its surrounding text from | |
23 * the focused frame and sends it to ContextSelectionProvider | |
24 * which does the classification itself. | |
25 */ | |
26 @JNINamespace("content") | |
27 public class ContextSelectionClient implements SelectionClient { | |
Tima Vaisburd
2017/03/10 20:42:37
A better naming suggestion is welcome.
| |
28 private static final String TAG = "ContextSelClient"; // 20 char limit | |
29 | |
30 @IntDef({CLASSIFY, SUGGEST_AND_CLASSIFY}) | |
31 @Retention(RetentionPolicy.SOURCE) | |
32 private @interface RequestType {} | |
33 | |
34 private static final int CLASSIFY = 0; | |
boliu
2017/03/11 00:37:52
comment on what these actually does?
Tima Vaisburd
2017/03/20 05:06:04
Not done yet.
Tima Vaisburd
2017/03/21 02:07:01
Done.
| |
35 private static final int SUGGEST_AND_CLASSIFY = 1; | |
36 | |
37 // The callback that would receive the information from ContextSelectionProv ider. | |
38 private class ProviderCallback implements ContextSelectionProvider.ResultCal lback { | |
boliu
2017/03/11 00:37:52
What's the point of this class?
Tima Vaisburd
2017/03/20 05:06:04
See my reply below.
| |
39 @Override | |
40 public void onClassified(ContextSelectionProvider.Result result) { | |
41 mOuterCallback.onClassified(result); | |
42 } | |
43 }; | |
44 | |
45 private long mNativeContextSelectionClient; | |
46 private ContextSelectionProvider mProvider; | |
47 private ProviderCallback mProviderCallback; | |
48 private ContextSelectionProvider.ResultCallback mOuterCallback; | |
49 | |
50 /** | |
51 * Creates the ContextSelectionClient. Returns null in case ContextSelection Provider | |
52 * does not exist in the system. | |
53 */ | |
54 public static ContextSelectionClient create(ContextSelectionProvider.ResultC allback callback, | |
55 WindowAndroid windowAndroid, WebContents webContent) { | |
56 Log.v(TAG, "create"); | |
boliu
2017/03/11 00:37:52
remove random logs
Tima Vaisburd
2017/03/20 05:06:04
Done.
| |
57 | |
58 ContextSelectionClient client = new ContextSelectionClient(callback); | |
59 if (!client.initialize(windowAndroid, webContent)) client = null; | |
60 return client; | |
61 } | |
62 | |
63 private ContextSelectionClient(ContextSelectionProvider.ResultCallback callb ack) { | |
64 mOuterCallback = callback; | |
65 } | |
66 | |
67 private boolean initialize(WindowAndroid windowAndroid, WebContents webConte nts) { | |
68 Log.v(TAG, "initialize"); | |
boliu
2017/03/11 00:37:52
ditto
Tima Vaisburd
2017/03/20 05:06:04
Done.
| |
69 // We make a chain of callbacks: this class receiive result from provide r through | |
70 // mProviderCallback and passes the info to embedder with mOuterCallback . | |
71 mProviderCallback = new ProviderCallback(); | |
72 mProvider = ContentClassFactory.get().createContextSelectionProvider( | |
73 mProviderCallback, windowAndroid); | |
74 | |
75 // ContextSelectionProvider might not exist. | |
76 if (mProvider == null) { | |
77 Log.v(TAG, "initialize: no provider"); | |
78 return false; | |
79 } | |
80 | |
81 mNativeContextSelectionClient = nativeInit(webContents); | |
82 return mNativeContextSelectionClient != 0; | |
boliu
2017/03/11 00:37:52
how can this ever be 0?
you can check null provid
Tima Vaisburd
2017/03/20 05:06:04
When the C++ new fails. I tried to make something
boliu
2017/03/20 20:27:25
yeah, why not just use the same object?
Tima Vaisburd
2017/03/21 02:07:01
Done.
| |
83 } | |
84 | |
85 @CalledByNative | |
86 private void onNativeSideDestroyed(long nativeContextSelectionClient) { | |
boliu
2017/03/11 00:37:52
what if there are pending requests here?
Tima Vaisburd
2017/03/20 05:06:04
They will be executed and eventually passed to Sel
boliu
2017/03/20 20:27:25
Document that you chose this over cancelling pendi
Tima Vaisburd
2017/03/21 02:07:01
When the native side is destroyed we do not care a
| |
87 assert nativeContextSelectionClient == mNativeContextSelectionClient; | |
88 mNativeContextSelectionClient = 0; | |
89 } | |
90 | |
91 // SelectionClient implementation | |
92 @Override | |
93 public void onSelectionChanged(String selection) {} | |
94 | |
95 @Override | |
96 public void onSelectionEvent(int eventType, float posXPix, float posYPix) { | |
97 switch (eventType) { | |
98 case SelectionEventType.SELECTION_HANDLES_SHOWN: | |
boliu
2017/03/11 00:37:52
I think what happens in each case is super unintui
Tima Vaisburd
2017/03/20 05:06:04
I tried to add some comments, but I think this is
| |
99 requestSurroundingText(SUGGEST_AND_CLASSIFY); | |
100 break; | |
101 | |
102 case SelectionEventType.SELECTION_HANDLES_CLEARED: | |
103 cancelAllRequests(); | |
104 break; | |
105 | |
106 case SelectionEventType.SELECTION_HANDLE_DRAG_STOPPED: | |
107 requestSurroundingText(CLASSIFY); | |
108 break; | |
109 | |
110 default: | |
111 break; // ignore | |
112 } | |
113 } | |
114 | |
115 @Override | |
116 public void showUnhandledTapUIIfNeeded(int x, int y) {} | |
117 | |
118 @Override | |
119 public boolean sendsMenuUpdates() { | |
120 return true; | |
121 } | |
122 | |
123 private void cancelAllRequests() { | |
124 if (mNativeContextSelectionClient != 0) { | |
125 nativeCancelAllRequests(mNativeContextSelectionClient); | |
126 } | |
127 | |
128 mProvider.cancelAllRequests(); | |
129 } | |
130 | |
131 private void requestSurroundingText(@RequestType int callbackData) { | |
132 Log.v(TAG, "requestSurroundingText"); | |
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 |