Commit fbc7c1e4 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

TextSuggestionHost on WebContentsUserData

This CL moves Java TextSuggestionHost to be managed by
WebContentsUserData so ContentViewCore doesn't have to keep
a reference to the object, and the object can be obtained
easily by TextSuggestionHost.fromWebContents(). It helps
decouple the class from CVC.

Bug: 789000
Change-Id: Iefdeb0a727fc975323b7f8f24d4eb187ad38601b
Reviewed-on: https://chromium-review.googlesource.com/892180Reviewed-by: default avatarRyan Landay <rlanday@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533068}
parent b17acb28
......@@ -13,7 +13,6 @@ import android.view.ViewGroup;
import org.chromium.base.VisibleForTesting;
import org.chromium.content.browser.input.SelectPopup;
import org.chromium.content.browser.input.TextSuggestionHost;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid;
......@@ -378,15 +377,6 @@ public interface ContentViewCore {
// Test-only methods
/**
* @return The TextSuggestionHost that handles displaying the text suggestion menu.
*/
@VisibleForTesting
TextSuggestionHost getTextSuggestionHostForTesting();
@VisibleForTesting
void setTextSuggestionHostForTesting(TextSuggestionHost textSuggestionHost);
/**
* @return The amount of the top controls height if controls are in the state
* of shrinking Blink's view size, otherwise 0.
......
......@@ -158,8 +158,6 @@ public class ContentViewCoreImpl
private SelectPopup mSelectPopup;
private long mNativeSelectPopupSourceFrame;
private TextSuggestionHost mTextSuggestionHost;
// Cached copy of all positions and scales as reported by the renderer.
private RenderCoordinates mRenderCoordinates;
......@@ -253,23 +251,11 @@ public class ContentViewCoreImpl
return nativeGetJavaWindowAndroid(mNativeContentViewCore);
}
@VisibleForTesting
@Override
public TextSuggestionHost getTextSuggestionHostForTesting() {
return mTextSuggestionHost;
}
@VisibleForTesting
void setWebContentsForTesting(WebContentsImpl webContents) {
mWebContents = webContents;
}
@VisibleForTesting
@Override
public void setTextSuggestionHostForTesting(TextSuggestionHost textSuggestionHost) {
mTextSuggestionHost = textSuggestionHost;
}
/**
* Add {@link WindowAndroidChangeObserver} object.
* @param observer Observer instance to add.
......@@ -321,9 +307,9 @@ public class ContentViewCoreImpl
mWebContents, mContainerView, new InputMethodManagerWrapper(mContext));
imeAdapter.addEventObserver(this);
imeAdapter.addEventObserver(TapDisambiguator.create(mContext, mWebContents, containerView));
mTextSuggestionHost =
new TextSuggestionHost(mContext, mWebContents, windowAndroid, mContainerView);
addWindowAndroidChangedObserver(mTextSuggestionHost);
TextSuggestionHost textSuggestionHost =
TextSuggestionHost.create(mContext, mWebContents, windowAndroid, containerView);
addWindowAndroidChangedObserver(textSuggestionHost);
mWebContentsObserver = new ContentViewWebContentsObserver(this);
......@@ -334,7 +320,7 @@ public class ContentViewCoreImpl
mWindowEventObservers.addObserver(controller);
mWindowEventObservers.addObserver(getGestureListenerManager());
mWindowEventObservers.addObserver(mTextSuggestionHost);
mWindowEventObservers.addObserver(textSuggestionHost);
mWindowEventObservers.addObserver(imeAdapter);
mWindowEventObservers.addObserver(wcax);
}
......@@ -385,7 +371,7 @@ public class ContentViewCoreImpl
if (mContainerView != null) {
hideSelectPopupWithCancelMessage();
getImeAdapter().setContainerView(containerView);
mTextSuggestionHost.setContainerView(containerView);
getTextSuggestionHost().setContainerView(containerView);
}
mContainerView = containerView;
......@@ -416,6 +402,10 @@ public class ContentViewCoreImpl
return WebContentsAccessibilityImpl.fromWebContents(mWebContents);
}
private TextSuggestionHost getTextSuggestionHost() {
return TextSuggestionHost.fromWebContents(mWebContents);
}
@CalledByNative
private void onNativeContentViewCoreDestroyed(long nativeContentViewCore) {
assert nativeContentViewCore == mNativeContentViewCore;
......@@ -436,7 +426,7 @@ public class ContentViewCoreImpl
mWebContentsObserver.destroy();
mWebContentsObserver = null;
getImeAdapter().resetAndHideKeyboard();
removeWindowAndroidChangedObserver(mTextSuggestionHost);
removeWindowAndroidChangedObserver(getTextSuggestionHost());
mWindowEventObservers.clear();
hidePopupsAndPreserveSelection();
mWebContents = null;
......@@ -558,8 +548,6 @@ public class ContentViewCoreImpl
private void hidePopupsAndClearSelection() {
if (mWebContents != null) {
getSelectionPopupController().destroyActionModeAndUnselect();
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
mWebContents.dismissTextHandles();
}
hidePopups();
......@@ -569,15 +557,17 @@ public class ContentViewCoreImpl
private void hidePopupsAndPreserveSelection() {
if (mWebContents != null) {
getSelectionPopupController().destroyActionModeAndKeepSelection();
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
}
hidePopups();
}
private void hidePopups() {
if (mWebContents != null) {
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
getTextSuggestionHost().hidePopups();
}
hideSelectPopupWithCancelMessage();
mTextSuggestionHost.hidePopups();
}
private void restoreSelectionPopupsIfNecessary() {
......@@ -1141,7 +1131,7 @@ public class ContentViewCoreImpl
hidePopupsAndPreserveSelection();
getSelectionPopupController().showActionModeOrClearOnFailure();
}
mTextSuggestionHost.hidePopups();
getTextSuggestionHost().hidePopups();
int rotationDegrees = 0;
switch (rotation) {
......
......@@ -13,6 +13,8 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.content.browser.WindowAndroidChangedObserver;
import org.chromium.content.browser.WindowEventObserver;
import org.chromium.content.browser.webcontents.WebContentsImpl;
import org.chromium.content.browser.webcontents.WebContentsUserData;
import org.chromium.content.browser.webcontents.WebContentsUserData.UserDataFactory;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
......@@ -24,9 +26,9 @@ import org.chromium.ui.base.WindowAndroid;
@JNINamespace("content")
public class TextSuggestionHost implements WindowEventObserver, WindowAndroidChangedObserver {
private long mNativeTextSuggestionHost;
private final Context mContext;
private final WebContentsImpl mWebContents;
private Context mContext;
private View mContainerView;
private boolean mIsAttachedToWindow;
private WindowAndroid mWindowAndroid;
......@@ -34,13 +36,58 @@ public class TextSuggestionHost implements WindowEventObserver, WindowAndroidCha
private SpellCheckPopupWindow mSpellCheckPopupWindow;
private TextSuggestionsPopupWindow mTextSuggestionsPopupWindow;
public TextSuggestionHost(
Context context, WebContentsImpl webContents, WindowAndroid windowAndroid, View view) {
private boolean mInitialized;
private static final class UserDataFactoryLazyHolder {
private static final UserDataFactory<TextSuggestionHost> INSTANCE = TextSuggestionHost::new;
}
/**
* Create {@link TextSuggestionHost} instance.
* @param context Context for action mode.
* @param webContents WebContents instance.
* @param windowAndroid The current WindowAndroid instance.
* @param view Container view.
*/
public static TextSuggestionHost create(
Context context, WebContents webContents, WindowAndroid windowAndroid, View view) {
TextSuggestionHost host = WebContentsUserData.fromWebContents(
webContents, TextSuggestionHost.class, UserDataFactoryLazyHolder.INSTANCE);
assert host != null;
assert !host.initialized();
host.init(context, windowAndroid, view);
return host;
}
/**
* Get {@link TextSuggestionHost} object used for the give WebContents.
* {@link #create()} should precede any calls to this.
* @param webContents {@link WebContents} object.
* @return {@link TextSuggestionHost} object. {@code null} if not available because
* {@link #create()} is not called yet.
*/
public static TextSuggestionHost fromWebContents(WebContents webContents) {
return WebContentsUserData.fromWebContents(webContents, TextSuggestionHost.class, null);
}
/**
* Create {@link TextSuggestionHost} instance.
* @param webContents WebContents instance.
*/
public TextSuggestionHost(WebContents webContents) {
mWebContents = (WebContentsImpl) webContents;
}
private void init(Context context, WindowAndroid windowAndroid, View view) {
mContext = context;
mWebContents = webContents;
mWindowAndroid = windowAndroid;
mContainerView = view;
mNativeTextSuggestionHost = nativeInit(webContents);
mNativeTextSuggestionHost = nativeInit(mWebContents);
mInitialized = true;
}
private boolean initialized() {
return mInitialized;
}
private float getContentOffsetYPix() {
......
......@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.input.TextSuggestionHost;
import org.chromium.content.browser.test.ContentJUnit4ClassRunner;
import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
import org.chromium.content.browser.webcontents.WebContentsImpl;
......@@ -108,8 +107,6 @@ public class PopupZoomerTest {
mPopupZoomer = createPopupZoomerForTest(
InstrumentationRegistry.getTargetContext(), containerView);
TapDisambiguator.fromWebContents(webContents).setPopupZoomerForTest(mPopupZoomer);
mContentViewCore.setTextSuggestionHostForTesting(new TextSuggestionHost(
context, (WebContentsImpl) webContents, null, containerView));
}
});
}
......
......@@ -13,7 +13,6 @@ import android.view.ViewGroup;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate;
import org.chromium.content.browser.input.SelectPopup;
import org.chromium.content.browser.input.TextSuggestionHost;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid;
......@@ -45,14 +44,6 @@ public class TestContentViewCore implements ContentViewCore {
return null;
}
@Override
public TextSuggestionHost getTextSuggestionHostForTesting() {
return null;
}
@Override
public void setTextSuggestionHostForTesting(TextSuggestionHost textSuggestionHost) {}
@Override
public void initialize(ViewAndroidDelegate viewDelegate,
InternalAccessDelegate internalDispatcher, WebContents webContents,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment