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++ functiionality. | |
Theresa
2016/08/16 15:41:49
nit: s/functiionality/functionality
Donn Denman
2016/08/17 04:35:22
Done.
| |
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 | |
23 protected SearchGestureHost mHost; | |
24 | |
25 private String mSurroundingTextSample; | |
26 private String mFocusedWord; | |
27 | |
28 private int mSurroundingTextSampleStart; | |
29 | |
30 private int mAdjustedSelectionStart; | |
31 private int mAdjustedSelectionEnd; | |
32 | |
33 private int mFocusedWordEnd; | |
34 | |
35 private int mSelectionDiscrepancyStart; | |
36 private int mSelectionDiscrepancyEnd; | |
37 | |
38 private long mRequestSurroundingTextStartTime; | |
39 | |
40 // ========================================================================= =================== | |
41 // Constructor | |
42 // ========================================================================= =================== | |
43 | |
44 public SearchAction(SearchActionListener listener) { | |
45 mNativePointer = nativeInit(); | |
46 | |
47 mListener = listener; | |
48 } | |
49 | |
50 // ========================================================================= =================== | |
51 // Abstract | |
52 // ========================================================================= =================== | |
53 | |
54 public abstract void extractContext(SearchGestureHost host); | |
55 | |
56 // ========================================================================= =================== | |
57 // | |
58 // ========================================================================= =================== | |
59 | |
60 public void dismissAction() { | |
61 mHost.dismissGesture(); | |
62 } | |
63 | |
64 public void destroyAction() { | |
65 onActionEnded(); | |
66 | |
67 if (mNativePointer != 0L) { | |
68 nativeDestroy(mNativePointer); | |
69 } | |
70 } | |
71 | |
72 public String getFocusedWord() { | |
73 return mFocusedWord; | |
74 } | |
75 | |
76 public String getTextAfterFocusedWord() { | |
77 return mSurroundingTextSample.substring(mFocusedWordEnd - mSurroundingTe xtSampleStart); | |
Theresa
2016/08/16 15:41:49
Should this just be mFocusedWordEnd?
e.g. mFocuse
Donn Denman
2016/08/17 04:35:22
Oh, this is confusing -- these offsets are within
pedro (no code reviews)
2016/08/22 20:54:17
Goldmine will send us offsets relative to the whol
Donn Denman
2016/08/23 23:21:46
This issue no longer applies -- not sending the su
| |
78 } | |
79 | |
80 // ========================================================================= =================== | |
81 // Suppression | |
82 // ========================================================================= =================== | |
83 | |
84 protected boolean shouldSuppressAction() { | |
85 return false; | |
86 } | |
87 | |
88 // ========================================================================= =================== | |
89 // State notification | |
90 // ========================================================================= =================== | |
91 | |
92 protected void notifyContextReady() { | |
93 onContextReady(); | |
94 } | |
95 | |
96 // ========================================================================= =================== | |
97 // Surrounding Text | |
98 // ========================================================================= =================== | |
99 | |
100 protected void requestSurroundingText() { | |
101 mRequestSurroundingTextStartTime = System.nanoTime(); | |
102 nativeRequestSurroundingText(mNativePointer, mHost.getTabWebContents()); | |
103 } | |
104 | |
105 @CalledByNative | |
106 protected void onSurroundingTextResponse(String surroundingTextSample, int s ampleStart, | |
107 int focusStart, int focusEnd, int focusedWordStart, int focusedWordE nd) { | |
108 long duration = (System.nanoTime() - mRequestSurroundingTextStartTime) / 1000000; | |
109 System.out.println("ctxs --- onSurroundingTextResponse duration " + dura tion + "ms"); | |
110 System.out.println("ctxs sample '" + surroundingTextSample + "'"); | |
111 | |
112 mSurroundingTextSample = surroundingTextSample; | |
113 mSurroundingTextSampleStart = sampleStart; | |
114 | |
115 mAdjustedSelectionStart = focusStart; | |
116 mAdjustedSelectionEnd = focusEnd; | |
117 | |
118 mFocusedWordEnd = focusedWordEnd; | |
119 | |
120 System.out.println("ctxs surroundingTextSample length " + surroundingTex tSample.length()); | |
121 System.out.println("ctxs sampleStart " + sampleStart); | |
122 System.out.println("ctxs focusStart " + focusStart); | |
123 System.out.println("ctxs focusEnd " + focusEnd); | |
124 System.out.println("ctxs focusedWordStart " + focusedWordStart); | |
125 System.out.println("ctxs focusedWordEnd " + focusedWordEnd); | |
126 | |
127 mFocusedWord = surroundingTextSample.substring( | |
128 focusedWordStart - sampleStart, focusedWordEnd - sampleStart); | |
129 } | |
130 | |
131 // ========================================================================= =================== | |
132 // SearchAction states | |
133 // ========================================================================= =================== | |
134 | |
135 private void onContextReady() { | |
136 mListener.onContextReady(this); | |
137 | |
138 if (shouldSuppressAction()) { | |
139 onActionSuppressed(); | |
140 } else { | |
141 onActionAccepted(); | |
142 } | |
143 } | |
144 | |
145 private void onActionAccepted() { | |
146 mListener.onActionAccepted(this); | |
147 } | |
148 | |
149 private void onActionSuppressed() { | |
150 mListener.onActionSuppressed(this); | |
151 | |
152 dismissAction(); | |
153 } | |
154 | |
155 private void onActionEnded() { | |
156 mListener.onActionEnded(this); | |
157 } | |
158 | |
159 // ========================================================================= =================== | |
160 // Internals | |
161 // ========================================================================= =================== | |
162 | |
163 protected void updateState(SearchGestureHost host) { | |
pedro (no code reviews)
2016/08/22 20:54:17
I think this should be kept as private, as stated
Donn Denman
2016/08/23 23:21:46
Done -- removed.
| |
164 mHost = host; | |
165 } | |
166 | |
167 @CalledByNative | |
168 private void clearNativePointer() { | |
169 assert mNativePointer != 0; | |
170 mNativePointer = 0; | |
171 } | |
172 | |
173 // ========================================================================= =================== | |
174 // Native methods. | |
175 // ========================================================================= =================== | |
176 | |
177 // Native calls. | |
178 private native long nativeInit(); | |
179 private native void nativeDestroy(long nativeSearchAction); | |
180 | |
181 private native void nativeRequestSurroundingText( | |
182 long nativeSearchAction, WebContents webContents); | |
183 } | |
OLD | NEW |