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 // =========================================================================
=================== |
| 25 // Constructor |
| 26 // =========================================================================
=================== |
| 27 |
| 28 /** |
| 29 * Constructs an action that knows how to Search. Current implementation is
limited to |
| 30 * gathering the text on a Tap gesture in order to determine whether the Tap
should be |
| 31 * suppressed or a search should be done or not, implemented by the |
| 32 * {@class ResolvedSearchAction} subclass. |
| 33 * @param listener The object to notify when the {@link SearchAction} state
changes. |
| 34 * @param host The host object, which provides environment data. |
| 35 */ |
| 36 public SearchAction(SearchActionListener listener, SearchGestureHost host) { |
| 37 mHost = host; |
| 38 mNativePointer = nativeInit(); |
| 39 |
| 40 mListener = listener; |
| 41 } |
| 42 |
| 43 // =========================================================================
=================== |
| 44 // Abstract |
| 45 // =========================================================================
=================== |
| 46 |
| 47 /** |
| 48 * Extracts the context for the current search -- text surrounding the locat
ion of the Tap |
| 49 * gesture. |
| 50 */ |
| 51 public abstract void extractContext(); |
| 52 |
| 53 // =========================================================================
=================== |
| 54 // |
| 55 // =========================================================================
=================== |
| 56 |
| 57 /** |
| 58 * Called when the system determines that this action will not be acted upon
. |
| 59 */ |
| 60 public void dismissAction() { |
| 61 mHost.dismissGesture(); |
| 62 } |
| 63 |
| 64 /** |
| 65 * Should be called when this object is no longer needed to clean up storage
. |
| 66 */ |
| 67 public void destroyAction() { |
| 68 onActionEnded(); |
| 69 |
| 70 if (mNativePointer != 0L) { |
| 71 nativeDestroy(mNativePointer); |
| 72 } |
| 73 } |
| 74 |
| 75 // =========================================================================
=================== |
| 76 // Suppression |
| 77 // =========================================================================
=================== |
| 78 |
| 79 /** |
| 80 * @return Whether this action should be suppressed. |
| 81 */ |
| 82 protected boolean shouldSuppressAction() { |
| 83 // TODO(donnd): integrate with native tap suppression. |
| 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 WebContents webContents = mHost.getTabWebContents(); |
| 107 if (webContents != null) { |
| 108 nativeRequestSurroundingText(mNativePointer, webContents); |
| 109 // TODO(donnd): consider reusing this surrounding text for the resol
ve action too. |
| 110 // Currently we make an additional request for the surroundings afte
r the UX selects the |
| 111 // word tapped, in order to resolve the search term based on that se
lection. |
| 112 } else { |
| 113 notifyContextReady(); |
| 114 } |
| 115 } |
| 116 |
| 117 @CalledByNative |
| 118 protected void onSurroundingTextReady() { |
| 119 // No base class action here, subclass may override and take action. |
| 120 } |
| 121 |
| 122 // =========================================================================
=================== |
| 123 // SearchAction states |
| 124 // =========================================================================
=================== |
| 125 |
| 126 /** |
| 127 * Called to notify that the current context is ready. |
| 128 */ |
| 129 private void onContextReady() { |
| 130 mListener.onContextReady(this); |
| 131 |
| 132 if (shouldSuppressAction()) { |
| 133 onActionSuppressed(); |
| 134 } else { |
| 135 onActionAccepted(); |
| 136 } |
| 137 } |
| 138 |
| 139 /** |
| 140 * Called when an action has been accepted to notify the listener. |
| 141 */ |
| 142 private void onActionAccepted() { |
| 143 mListener.onActionAccepted(this); |
| 144 } |
| 145 |
| 146 /** |
| 147 * Called when an action has been suppressed to notify the listener. |
| 148 */ |
| 149 private void onActionSuppressed() { |
| 150 mListener.onActionSuppressed(this); |
| 151 |
| 152 dismissAction(); |
| 153 } |
| 154 |
| 155 /** |
| 156 * Called when an action has ended to notify the listener. |
| 157 */ |
| 158 private void onActionEnded() { |
| 159 mListener.onActionEnded(this); |
| 160 } |
| 161 |
| 162 // =========================================================================
=================== |
| 163 // Internals |
| 164 // =========================================================================
=================== |
| 165 |
| 166 @CalledByNative |
| 167 private void clearNativePointer() { |
| 168 assert mNativePointer != 0; |
| 169 mNativePointer = 0; |
| 170 } |
| 171 |
| 172 // =========================================================================
=================== |
| 173 // Native methods. |
| 174 // =========================================================================
=================== |
| 175 |
| 176 // Native calls. |
| 177 private native long nativeInit(); |
| 178 private native void nativeDestroy(long nativeSearchAction); |
| 179 |
| 180 private native void nativeRequestSurroundingText( |
| 181 long nativeSearchAction, WebContents webContents); |
| 182 } |
OLD | NEW |