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..9bacc20606eeaae7b15cebdd942492d6f1f0d1fa |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/action/SearchAction.java |
@@ -0,0 +1,158 @@ |
+// 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++ functionality. |
+ * 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 final SearchGestureHost mHost; |
+ |
+ private long mRequestSurroundingTextStartTime; |
+ |
+ // ============================================================================================ |
+ // Constructor |
+ // ============================================================================================ |
+ |
+ public SearchAction(SearchActionListener listener, SearchGestureHost host) { |
+ mHost = host; |
+ mNativePointer = nativeInit(); |
+ |
+ mListener = listener; |
+ } |
+ |
+ // ============================================================================================ |
+ // Abstract |
+ // ============================================================================================ |
+ |
+ public abstract void extractContext(); |
+ |
+ // ============================================================================================ |
+ // |
+ // ============================================================================================ |
+ |
+ public void dismissAction() { |
+ mHost.dismissGesture(); |
+ } |
+ |
+ public void destroyAction() { |
+ onActionEnded(); |
+ |
+ if (mNativePointer != 0L) { |
+ nativeDestroy(mNativePointer); |
+ } |
+ } |
+ |
+ public String getFocusedWord() { |
+ if (mNativePointer == 0L) return null; |
+ |
+ return nativeGetFocusedWord(mNativePointer); |
+ } |
+ |
+ public String getSampleText(int sampleLength) { |
+ if (mNativePointer == 0L) return null; |
+ |
+ return nativeGetSampleText(mNativePointer, sampleLength); |
+ } |
+ |
+ // ============================================================================================ |
+ // Suppression |
+ // ============================================================================================ |
+ |
+ protected boolean shouldSuppressAction() { |
+ return false; |
+ } |
+ |
+ // ============================================================================================ |
+ // State notification |
+ // ============================================================================================ |
+ |
+ protected void notifyContextReady() { |
+ onContextReady(); |
+ } |
+ |
+ // ============================================================================================ |
+ // Surrounding Text |
+ // ============================================================================================ |
+ |
+ protected void requestSurroundingText() { |
Theresa
2016/08/26 16:39:07
Under what scenarios do we want the surrounding te
Donn Denman
2016/08/26 19:25:02
That's a good question -- we don't yet have usage
|
+ mRequestSurroundingTextStartTime = System.nanoTime(); |
+ WebContents webContents = mHost.getTabWebContents(); |
+ if (webContents != null) { |
+ nativeRequestSurroundingText(mNativePointer, webContents); |
+ } else { |
+ notifyContextReady(); |
+ } |
+ } |
+ |
+ @CalledByNative |
+ protected void onSurroundingTextResponse() { |
+ long duration = (System.nanoTime() - mRequestSurroundingTextStartTime) / 1000000; |
+ // TODO(donnd) consider logging the duration to UMA or removing this duration completely. |
+ System.out.println("ctxs --- onSurroundingTextResponse duration " + duration + "ms"); |
+ } |
+ |
+ // ============================================================================================ |
+ // 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 |
+ // ============================================================================================ |
+ |
+ @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); |
+ private native String nativeGetFocusedWord(long nativeSearchAction); |
+ private native String nativeGetSampleText(long nativeSearchAction, int sampleLength); |
+} |