Index: chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/action/SearchAction.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/action/SearchAction.java b/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/action/SearchAction.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6275d8d59c7451e0766eeaed92430f598d41d898 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/action/SearchAction.java |
@@ -0,0 +1,183 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.contextualsearch.action; |
+ |
+import org.chromium.base.annotations.CalledByNative; |
+import org.chromium.chrome.browser.contextualsearch.gesture.SearchGestureHost; |
+import org.chromium.content_public.browser.WebContents; |
+ |
+/** |
+ * 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.
|
+ * Subclasses will exist for a Resolved search action that determines the search based on page text, |
+ * and Verbatim search action that just searches for the literal selection without providing |
+ * context. |
+ * This is part of the 2016-refactoring (crbug.com/624609, go/cs-refactoring-2016). |
+ */ |
+public abstract class SearchAction { |
+ private long mNativePointer; |
+ |
+ protected final SearchActionListener mListener; |
+ |
+ protected SearchGestureHost mHost; |
+ |
+ private String mSurroundingTextSample; |
+ private String mFocusedWord; |
+ |
+ private int mSurroundingTextSampleStart; |
+ |
+ private int mAdjustedSelectionStart; |
+ private int mAdjustedSelectionEnd; |
+ |
+ private int mFocusedWordEnd; |
+ |
+ private int mSelectionDiscrepancyStart; |
+ private int mSelectionDiscrepancyEnd; |
+ |
+ private long mRequestSurroundingTextStartTime; |
+ |
+ // ============================================================================================ |
+ // Constructor |
+ // ============================================================================================ |
+ |
+ public SearchAction(SearchActionListener listener) { |
+ mNativePointer = nativeInit(); |
+ |
+ mListener = listener; |
+ } |
+ |
+ // ============================================================================================ |
+ // Abstract |
+ // ============================================================================================ |
+ |
+ public abstract void extractContext(SearchGestureHost host); |
+ |
+ // ============================================================================================ |
+ // |
+ // ============================================================================================ |
+ |
+ public void dismissAction() { |
+ mHost.dismissGesture(); |
+ } |
+ |
+ public void destroyAction() { |
+ onActionEnded(); |
+ |
+ if (mNativePointer != 0L) { |
+ nativeDestroy(mNativePointer); |
+ } |
+ } |
+ |
+ public String getFocusedWord() { |
+ return mFocusedWord; |
+ } |
+ |
+ public String getTextAfterFocusedWord() { |
+ return mSurroundingTextSample.substring(mFocusedWordEnd - mSurroundingTextSampleStart); |
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
|
+ } |
+ |
+ // ============================================================================================ |
+ // Suppression |
+ // ============================================================================================ |
+ |
+ protected boolean shouldSuppressAction() { |
+ return false; |
+ } |
+ |
+ // ============================================================================================ |
+ // State notification |
+ // ============================================================================================ |
+ |
+ protected void notifyContextReady() { |
+ onContextReady(); |
+ } |
+ |
+ // ============================================================================================ |
+ // Surrounding Text |
+ // ============================================================================================ |
+ |
+ protected void requestSurroundingText() { |
+ mRequestSurroundingTextStartTime = System.nanoTime(); |
+ nativeRequestSurroundingText(mNativePointer, mHost.getTabWebContents()); |
+ } |
+ |
+ @CalledByNative |
+ protected void onSurroundingTextResponse(String surroundingTextSample, int sampleStart, |
+ int focusStart, int focusEnd, int focusedWordStart, int focusedWordEnd) { |
+ long duration = (System.nanoTime() - mRequestSurroundingTextStartTime) / 1000000; |
+ System.out.println("ctxs --- onSurroundingTextResponse duration " + duration + "ms"); |
+ System.out.println("ctxs sample '" + surroundingTextSample + "'"); |
+ |
+ mSurroundingTextSample = surroundingTextSample; |
+ mSurroundingTextSampleStart = sampleStart; |
+ |
+ mAdjustedSelectionStart = focusStart; |
+ mAdjustedSelectionEnd = focusEnd; |
+ |
+ mFocusedWordEnd = focusedWordEnd; |
+ |
+ System.out.println("ctxs surroundingTextSample length " + surroundingTextSample.length()); |
+ System.out.println("ctxs sampleStart " + sampleStart); |
+ System.out.println("ctxs focusStart " + focusStart); |
+ System.out.println("ctxs focusEnd " + focusEnd); |
+ System.out.println("ctxs focusedWordStart " + focusedWordStart); |
+ System.out.println("ctxs focusedWordEnd " + focusedWordEnd); |
+ |
+ mFocusedWord = surroundingTextSample.substring( |
+ focusedWordStart - sampleStart, focusedWordEnd - sampleStart); |
+ } |
+ |
+ // ============================================================================================ |
+ // SearchAction states |
+ // ============================================================================================ |
+ |
+ private void onContextReady() { |
+ mListener.onContextReady(this); |
+ |
+ if (shouldSuppressAction()) { |
+ onActionSuppressed(); |
+ } else { |
+ onActionAccepted(); |
+ } |
+ } |
+ |
+ private void onActionAccepted() { |
+ mListener.onActionAccepted(this); |
+ } |
+ |
+ private void onActionSuppressed() { |
+ mListener.onActionSuppressed(this); |
+ |
+ dismissAction(); |
+ } |
+ |
+ private void onActionEnded() { |
+ mListener.onActionEnded(this); |
+ } |
+ |
+ // ============================================================================================ |
+ // Internals |
+ // ============================================================================================ |
+ |
+ 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.
|
+ mHost = host; |
+ } |
+ |
+ @CalledByNative |
+ private void clearNativePointer() { |
+ assert mNativePointer != 0; |
+ mNativePointer = 0; |
+ } |
+ |
+ // ============================================================================================ |
+ // Native methods. |
+ // ============================================================================================ |
+ |
+ // Native calls. |
+ private native long nativeInit(); |
+ private native void nativeDestroy(long nativeSearchAction); |
+ |
+ private native void nativeRequestSurroundingText( |
+ long nativeSearchAction, WebContents webContents); |
+} |