Chromium Code Reviews| 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 public SearchAction(SearchActionListener listener, SearchGestureHost host) { | |
| 31 mHost = host; | |
| 32 mNativePointer = nativeInit(); | |
| 33 | |
| 34 mListener = listener; | |
| 35 } | |
| 36 | |
| 37 // ========================================================================= =================== | |
| 38 // Abstract | |
| 39 // ========================================================================= =================== | |
| 40 | |
| 41 public abstract void extractContext(); | |
| 42 | |
| 43 // ========================================================================= =================== | |
| 44 // | |
| 45 // ========================================================================= =================== | |
| 46 | |
| 47 public void dismissAction() { | |
| 48 mHost.dismissGesture(); | |
| 49 } | |
| 50 | |
| 51 public void destroyAction() { | |
| 52 onActionEnded(); | |
| 53 | |
| 54 if (mNativePointer != 0L) { | |
| 55 nativeDestroy(mNativePointer); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 public String getFocusedWord() { | |
| 60 if (mNativePointer == 0L) return null; | |
| 61 | |
| 62 return nativeGetFocusedWord(mNativePointer); | |
| 63 } | |
| 64 | |
| 65 public String getSampleText(int sampleLength) { | |
| 66 if (mNativePointer == 0L) return null; | |
| 67 | |
| 68 return nativeGetSampleText(mNativePointer, sampleLength); | |
| 69 } | |
| 70 | |
| 71 // ========================================================================= =================== | |
| 72 // Suppression | |
| 73 // ========================================================================= =================== | |
| 74 | |
| 75 protected boolean shouldSuppressAction() { | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 // ========================================================================= =================== | |
| 80 // State notification | |
| 81 // ========================================================================= =================== | |
| 82 | |
| 83 protected void notifyContextReady() { | |
| 84 onContextReady(); | |
| 85 } | |
| 86 | |
| 87 // ========================================================================= =================== | |
| 88 // Surrounding Text | |
| 89 // ========================================================================= =================== | |
| 90 | |
| 91 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
| |
| 92 mRequestSurroundingTextStartTime = System.nanoTime(); | |
| 93 WebContents webContents = mHost.getTabWebContents(); | |
| 94 if (webContents != null) { | |
| 95 nativeRequestSurroundingText(mNativePointer, webContents); | |
| 96 } else { | |
| 97 notifyContextReady(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 @CalledByNative | |
| 102 protected void onSurroundingTextResponse() { | |
| 103 long duration = (System.nanoTime() - mRequestSurroundingTextStartTime) / 1000000; | |
| 104 // TODO(donnd) consider logging the duration to UMA or removing this dur ation completely. | |
| 105 System.out.println("ctxs --- onSurroundingTextResponse duration " + dura tion + "ms"); | |
| 106 } | |
| 107 | |
| 108 // ========================================================================= =================== | |
| 109 // SearchAction states | |
| 110 // ========================================================================= =================== | |
| 111 | |
| 112 private void onContextReady() { | |
| 113 mListener.onContextReady(this); | |
| 114 | |
| 115 if (shouldSuppressAction()) { | |
| 116 onActionSuppressed(); | |
| 117 } else { | |
| 118 onActionAccepted(); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 private void onActionAccepted() { | |
| 123 mListener.onActionAccepted(this); | |
| 124 } | |
| 125 | |
| 126 private void onActionSuppressed() { | |
| 127 mListener.onActionSuppressed(this); | |
| 128 | |
| 129 dismissAction(); | |
| 130 } | |
| 131 | |
| 132 private void onActionEnded() { | |
| 133 mListener.onActionEnded(this); | |
| 134 } | |
| 135 | |
| 136 // ========================================================================= =================== | |
| 137 // Internals | |
| 138 // ========================================================================= =================== | |
| 139 | |
| 140 @CalledByNative | |
| 141 private void clearNativePointer() { | |
| 142 assert mNativePointer != 0; | |
| 143 mNativePointer = 0; | |
| 144 } | |
| 145 | |
| 146 // ========================================================================= =================== | |
| 147 // Native methods. | |
| 148 // ========================================================================= =================== | |
| 149 | |
| 150 // Native calls. | |
| 151 private native long nativeInit(); | |
| 152 private native void nativeDestroy(long nativeSearchAction); | |
| 153 | |
| 154 private native void nativeRequestSurroundingText( | |
| 155 long nativeSearchAction, WebContents webContents); | |
| 156 private native String nativeGetFocusedWord(long nativeSearchAction); | |
| 157 private native String nativeGetSampleText(long nativeSearchAction, int sampl eLength); | |
| 158 } | |
| OLD | NEW |