OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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.chrome.browser.contextualsearch.action; | |
6 | |
7 import org.chromium.base.annotations.CalledByNative; | |
8 import org.chromium.chrome.browser.contextualsearch.gesture.SearchGestureHost; | |
9 import org.chromium.content_public.browser.WebContents; | |
10 | |
11 /** | |
12 * Represents an abstract action to do a Contextual Search, and supports native C++ functionality. | |
13 * Subclasses will exist for a Resolved search action that determines the search based on page text, | |
14 * and Verbatim search action that just searches for the literal selection witho ut providing | |
15 * context. | |
16 * This is part of the 2016-refactoring (crbug.com/624609, go/cs-refactoring-201 6). | |
17 */ | |
18 public abstract class SearchAction { | |
19 private long mNativePointer; | |
20 | |
21 protected final SearchActionListener mListener; | |
22 protected final SearchGestureHost mHost; | |
23 | |
24 private long mRequestSurroundingTextStartTime; | |
25 | |
26 // ========================================================================= =================== | |
27 // Constructor | |
28 // ========================================================================= =================== | |
29 | |
30 /** | |
31 * Constructs an action that knows how to Search. Current implementation is limited to | |
32 * gathering the text surrounding a Tap gesture in order to determine whethe r a search should | |
Theresa
2016/09/01 16:49:36
nit: ... text on a Tap....
..... determine wh
Donn Denman
2016/09/02 16:40:43
Done.
| |
33 * be done or not, implemented by the {@class ResolvedSearchAction} subclass . | |
34 * @param listener The object to notify when the {@link SearchAction} state changes. | |
35 * @param host The host object, which provides environment data. | |
36 */ | |
37 public SearchAction(SearchActionListener listener, SearchGestureHost host) { | |
38 mHost = host; | |
39 mNativePointer = nativeInit(); | |
40 | |
41 mListener = listener; | |
42 } | |
43 | |
44 // ========================================================================= =================== | |
45 // Abstract | |
46 // ========================================================================= =================== | |
47 | |
48 /** | |
49 * Extracts the context for the current search -- text surrounding the locat ion of the Tap | |
50 * gesture. | |
51 */ | |
52 public abstract void extractContext(); | |
53 | |
54 // ========================================================================= =================== | |
55 // | |
56 // ========================================================================= =================== | |
57 | |
58 /** | |
59 * Called when the system determines that this action will not be acted upon . | |
60 */ | |
61 public void dismissAction() { | |
62 mHost.dismissGesture(); | |
63 } | |
64 | |
65 /** | |
66 * Should be called when this object is no longer needed to clean up storage . | |
67 */ | |
68 public void destroyAction() { | |
69 onActionEnded(); | |
70 | |
71 if (mNativePointer != 0L) { | |
72 nativeDestroy(mNativePointer); | |
73 } | |
74 } | |
75 | |
76 // ========================================================================= =================== | |
77 // Suppression | |
78 // ========================================================================= =================== | |
79 | |
80 /** | |
81 * @return Whether this action should be suppressed. | |
82 */ | |
83 protected boolean shouldSuppressAction() { | |
pedro (no code reviews)
2016/08/31 22:38:22
Is this being used? This is where the tap suppress
Theresa
2016/09/01 16:49:36
I think ultimately we want this to come from nativ
Donn Denman
2016/09/02 16:40:43
Added a TODO to integrate with the native tap supp
Donn Denman
2016/09/02 16:40:43
I'd rather wait until we take the next step in int
| |
84 return false; | |
85 } | |
86 | |
87 // ========================================================================= =================== | |
88 // State notification | |
89 // ========================================================================= =================== | |
90 | |
91 /** | |
92 * Sends notification that the context is ready for use now. | |
93 */ | |
94 protected void notifyContextReady() { | |
95 onContextReady(); | |
96 } | |
97 | |
98 // ========================================================================= =================== | |
99 // Surrounding Text | |
100 // ========================================================================= =================== | |
101 | |
102 /** | |
103 * Requests text surrounding the location of the caret. | |
104 */ | |
105 protected void requestSurroundingText() { | |
106 mRequestSurroundingTextStartTime = System.nanoTime(); | |
107 WebContents webContents = mHost.getTabWebContents(); | |
108 if (webContents != null) { | |
109 nativeRequestSurroundingText(mNativePointer, webContents); | |
Theresa
2016/09/01 16:49:36
Is there a cost associated with gathering the surr
Donn Denman
2016/09/02 16:40:43
I'm not concerned about this. Looks like the cost
Theresa
2016/09/02 17:04:07
I was thinking more about the blink operation that
Donn Denman
2016/09/02 20:48:35
Done. It's actually stored in the native context
| |
110 } else { | |
111 notifyContextReady(); | |
112 } | |
113 } | |
114 | |
115 @CalledByNative | |
116 protected void onSurroundingTextResponse() { | |
Theresa
2016/09/01 16:49:37
Should this be onSurroundingTextReady() since it's
Donn Denman
2016/09/02 16:40:43
Done.
| |
117 long duration = (System.nanoTime() - mRequestSurroundingTextStartTime) / 1000000; | |
Theresa
2016/09/01 16:49:37
Findbugs will probably complain about this unused
Donn Denman
2016/09/02 16:40:43
OK, removed this code.
| |
118 // TODO(donnd) consider logging the duration to UMA or removing this dur ation completely. | |
119 } | |
120 | |
121 // ========================================================================= =================== | |
122 // SearchAction states | |
123 // ========================================================================= =================== | |
124 | |
125 /** | |
126 * Called to notify that the current context is ready. | |
127 */ | |
128 private void onContextReady() { | |
129 mListener.onContextReady(this); | |
130 | |
131 if (shouldSuppressAction()) { | |
132 onActionSuppressed(); | |
133 } else { | |
134 onActionAccepted(); | |
135 } | |
136 } | |
137 | |
138 /** | |
139 * Called when an action has been accepted to notify the listener. | |
140 */ | |
141 private void onActionAccepted() { | |
142 mListener.onActionAccepted(this); | |
143 } | |
144 | |
145 /** | |
146 * Called when an action has been suppressed to notify the listener. | |
147 */ | |
148 private void onActionSuppressed() { | |
149 mListener.onActionSuppressed(this); | |
150 | |
151 dismissAction(); | |
152 } | |
153 | |
154 /** | |
155 * Called when an action has ended to notify the listener. | |
156 */ | |
157 private void onActionEnded() { | |
158 mListener.onActionEnded(this); | |
159 } | |
160 | |
161 // ========================================================================= =================== | |
162 // Internals | |
163 // ========================================================================= =================== | |
164 | |
165 @CalledByNative | |
166 private void clearNativePointer() { | |
167 assert mNativePointer != 0; | |
168 mNativePointer = 0; | |
169 } | |
170 | |
171 // ========================================================================= =================== | |
172 // Native methods. | |
173 // ========================================================================= =================== | |
174 | |
175 // Native calls. | |
176 private native long nativeInit(); | |
177 private native void nativeDestroy(long nativeSearchAction); | |
178 | |
179 private native void nativeRequestSurroundingText( | |
180 long nativeSearchAction, WebContents webContents); | |
181 } | |
OLD | NEW |