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; ...@@ -13,7 +13,6 @@ import android.view.ViewGroup;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.content.browser.input.SelectPopup; import org.chromium.content.browser.input.SelectPopup;
import org.chromium.content.browser.input.TextSuggestionHost;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.ViewAndroidDelegate; import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid; import org.chromium.ui.base.WindowAndroid;
...@@ -378,15 +377,6 @@ public interface ContentViewCore { ...@@ -378,15 +377,6 @@ public interface ContentViewCore {
// Test-only methods // 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 * @return The amount of the top controls height if controls are in the state
* of shrinking Blink's view size, otherwise 0. * of shrinking Blink's view size, otherwise 0.
......
...@@ -158,8 +158,6 @@ public class ContentViewCoreImpl ...@@ -158,8 +158,6 @@ public class ContentViewCoreImpl
private SelectPopup mSelectPopup; private SelectPopup mSelectPopup;
private long mNativeSelectPopupSourceFrame; private long mNativeSelectPopupSourceFrame;
private TextSuggestionHost mTextSuggestionHost;
// Cached copy of all positions and scales as reported by the renderer. // Cached copy of all positions and scales as reported by the renderer.
private RenderCoordinates mRenderCoordinates; private RenderCoordinates mRenderCoordinates;
...@@ -253,23 +251,11 @@ public class ContentViewCoreImpl ...@@ -253,23 +251,11 @@ public class ContentViewCoreImpl
return nativeGetJavaWindowAndroid(mNativeContentViewCore); return nativeGetJavaWindowAndroid(mNativeContentViewCore);
} }
@VisibleForTesting
@Override
public TextSuggestionHost getTextSuggestionHostForTesting() {
return mTextSuggestionHost;
}
@VisibleForTesting @VisibleForTesting
void setWebContentsForTesting(WebContentsImpl webContents) { void setWebContentsForTesting(WebContentsImpl webContents) {
mWebContents = webContents; mWebContents = webContents;
} }
@VisibleForTesting
@Override
public void setTextSuggestionHostForTesting(TextSuggestionHost textSuggestionHost) {
mTextSuggestionHost = textSuggestionHost;
}
/** /**
* Add {@link WindowAndroidChangeObserver} object. * Add {@link WindowAndroidChangeObserver} object.
* @param observer Observer instance to add. * @param observer Observer instance to add.
...@@ -321,9 +307,9 @@ public class ContentViewCoreImpl ...@@ -321,9 +307,9 @@ public class ContentViewCoreImpl
mWebContents, mContainerView, new InputMethodManagerWrapper(mContext)); mWebContents, mContainerView, new InputMethodManagerWrapper(mContext));
imeAdapter.addEventObserver(this); imeAdapter.addEventObserver(this);
imeAdapter.addEventObserver(TapDisambiguator.create(mContext, mWebContents, containerView)); imeAdapter.addEventObserver(TapDisambiguator.create(mContext, mWebContents, containerView));
mTextSuggestionHost = TextSuggestionHost textSuggestionHost =
new TextSuggestionHost(mContext, mWebContents, windowAndroid, mContainerView); TextSuggestionHost.create(mContext, mWebContents, windowAndroid, containerView);
addWindowAndroidChangedObserver(mTextSuggestionHost); addWindowAndroidChangedObserver(textSuggestionHost);
mWebContentsObserver = new ContentViewWebContentsObserver(this); mWebContentsObserver = new ContentViewWebContentsObserver(this);
...@@ -334,7 +320,7 @@ public class ContentViewCoreImpl ...@@ -334,7 +320,7 @@ public class ContentViewCoreImpl
mWindowEventObservers.addObserver(controller); mWindowEventObservers.addObserver(controller);
mWindowEventObservers.addObserver(getGestureListenerManager()); mWindowEventObservers.addObserver(getGestureListenerManager());
mWindowEventObservers.addObserver(mTextSuggestionHost); mWindowEventObservers.addObserver(textSuggestionHost);
mWindowEventObservers.addObserver(imeAdapter); mWindowEventObservers.addObserver(imeAdapter);
mWindowEventObservers.addObserver(wcax); mWindowEventObservers.addObserver(wcax);
} }
...@@ -385,7 +371,7 @@ public class ContentViewCoreImpl ...@@ -385,7 +371,7 @@ public class ContentViewCoreImpl
if (mContainerView != null) { if (mContainerView != null) {
hideSelectPopupWithCancelMessage(); hideSelectPopupWithCancelMessage();
getImeAdapter().setContainerView(containerView); getImeAdapter().setContainerView(containerView);
mTextSuggestionHost.setContainerView(containerView); getTextSuggestionHost().setContainerView(containerView);
} }
mContainerView = containerView; mContainerView = containerView;
...@@ -416,6 +402,10 @@ public class ContentViewCoreImpl ...@@ -416,6 +402,10 @@ public class ContentViewCoreImpl
return WebContentsAccessibilityImpl.fromWebContents(mWebContents); return WebContentsAccessibilityImpl.fromWebContents(mWebContents);
} }
private TextSuggestionHost getTextSuggestionHost() {
return TextSuggestionHost.fromWebContents(mWebContents);
}
@CalledByNative @CalledByNative
private void onNativeContentViewCoreDestroyed(long nativeContentViewCore) { private void onNativeContentViewCoreDestroyed(long nativeContentViewCore) {
assert nativeContentViewCore == mNativeContentViewCore; assert nativeContentViewCore == mNativeContentViewCore;
...@@ -436,7 +426,7 @@ public class ContentViewCoreImpl ...@@ -436,7 +426,7 @@ public class ContentViewCoreImpl
mWebContentsObserver.destroy(); mWebContentsObserver.destroy();
mWebContentsObserver = null; mWebContentsObserver = null;
getImeAdapter().resetAndHideKeyboard(); getImeAdapter().resetAndHideKeyboard();
removeWindowAndroidChangedObserver(mTextSuggestionHost); removeWindowAndroidChangedObserver(getTextSuggestionHost());
mWindowEventObservers.clear(); mWindowEventObservers.clear();
hidePopupsAndPreserveSelection(); hidePopupsAndPreserveSelection();
mWebContents = null; mWebContents = null;
...@@ -558,8 +548,6 @@ public class ContentViewCoreImpl ...@@ -558,8 +548,6 @@ public class ContentViewCoreImpl
private void hidePopupsAndClearSelection() { private void hidePopupsAndClearSelection() {
if (mWebContents != null) { if (mWebContents != null) {
getSelectionPopupController().destroyActionModeAndUnselect(); getSelectionPopupController().destroyActionModeAndUnselect();
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
mWebContents.dismissTextHandles(); mWebContents.dismissTextHandles();
} }
hidePopups(); hidePopups();
...@@ -569,15 +557,17 @@ public class ContentViewCoreImpl ...@@ -569,15 +557,17 @@ public class ContentViewCoreImpl
private void hidePopupsAndPreserveSelection() { private void hidePopupsAndPreserveSelection() {
if (mWebContents != null) { if (mWebContents != null) {
getSelectionPopupController().destroyActionModeAndKeepSelection(); getSelectionPopupController().destroyActionModeAndKeepSelection();
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
} }
hidePopups(); hidePopups();
} }
private void hidePopups() { private void hidePopups() {
if (mWebContents != null) {
destroyPastePopup();
getTapDisambiguator().hidePopup(false);
getTextSuggestionHost().hidePopups();
}
hideSelectPopupWithCancelMessage(); hideSelectPopupWithCancelMessage();
mTextSuggestionHost.hidePopups();
} }
private void restoreSelectionPopupsIfNecessary() { private void restoreSelectionPopupsIfNecessary() {
...@@ -1141,7 +1131,7 @@ public class ContentViewCoreImpl ...@@ -1141,7 +1131,7 @@ public class ContentViewCoreImpl
hidePopupsAndPreserveSelection(); hidePopupsAndPreserveSelection();
getSelectionPopupController().showActionModeOrClearOnFailure(); getSelectionPopupController().showActionModeOrClearOnFailure();
} }
mTextSuggestionHost.hidePopups(); getTextSuggestionHost().hidePopups();
int rotationDegrees = 0; int rotationDegrees = 0;
switch (rotation) { switch (rotation) {
......
...@@ -13,6 +13,8 @@ import org.chromium.base.annotations.JNINamespace; ...@@ -13,6 +13,8 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.content.browser.WindowAndroidChangedObserver; import org.chromium.content.browser.WindowAndroidChangedObserver;
import org.chromium.content.browser.WindowEventObserver; import org.chromium.content.browser.WindowEventObserver;
import org.chromium.content.browser.webcontents.WebContentsImpl; 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.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid; import org.chromium.ui.base.WindowAndroid;
...@@ -24,9 +26,9 @@ import org.chromium.ui.base.WindowAndroid; ...@@ -24,9 +26,9 @@ import org.chromium.ui.base.WindowAndroid;
@JNINamespace("content") @JNINamespace("content")
public class TextSuggestionHost implements WindowEventObserver, WindowAndroidChangedObserver { public class TextSuggestionHost implements WindowEventObserver, WindowAndroidChangedObserver {
private long mNativeTextSuggestionHost; private long mNativeTextSuggestionHost;
private final Context mContext;
private final WebContentsImpl mWebContents; private final WebContentsImpl mWebContents;
private Context mContext;
private View mContainerView; private View mContainerView;
private boolean mIsAttachedToWindow; private boolean mIsAttachedToWindow;
private WindowAndroid mWindowAndroid; private WindowAndroid mWindowAndroid;
...@@ -34,13 +36,58 @@ public class TextSuggestionHost implements WindowEventObserver, WindowAndroidCha ...@@ -34,13 +36,58 @@ public class TextSuggestionHost implements WindowEventObserver, WindowAndroidCha
private SpellCheckPopupWindow mSpellCheckPopupWindow; private SpellCheckPopupWindow mSpellCheckPopupWindow;
private TextSuggestionsPopupWindow mTextSuggestionsPopupWindow; private TextSuggestionsPopupWindow mTextSuggestionsPopupWindow;
public TextSuggestionHost( private boolean mInitialized;
Context context, WebContentsImpl webContents, WindowAndroid windowAndroid, View view) {
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; mContext = context;
mWebContents = webContents;
mWindowAndroid = windowAndroid; mWindowAndroid = windowAndroid;
mContainerView = view; mContainerView = view;
mNativeTextSuggestionHost = nativeInit(webContents); mNativeTextSuggestionHost = nativeInit(mWebContents);
mInitialized = true;
}
private boolean initialized() {
return mInitialized;
} }
private float getContentOffsetYPix() { private float getContentOffsetYPix() {
......
...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith; ...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature; 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.ContentJUnit4ClassRunner;
import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper; import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
import org.chromium.content.browser.webcontents.WebContentsImpl; import org.chromium.content.browser.webcontents.WebContentsImpl;
...@@ -108,8 +107,6 @@ public class PopupZoomerTest { ...@@ -108,8 +107,6 @@ public class PopupZoomerTest {
mPopupZoomer = createPopupZoomerForTest( mPopupZoomer = createPopupZoomerForTest(
InstrumentationRegistry.getTargetContext(), containerView); InstrumentationRegistry.getTargetContext(), containerView);
TapDisambiguator.fromWebContents(webContents).setPopupZoomerForTest(mPopupZoomer); TapDisambiguator.fromWebContents(webContents).setPopupZoomerForTest(mPopupZoomer);
mContentViewCore.setTextSuggestionHostForTesting(new TextSuggestionHost(
context, (WebContentsImpl) webContents, null, containerView));
} }
}); });
} }
......
...@@ -13,7 +13,6 @@ import android.view.ViewGroup; ...@@ -13,7 +13,6 @@ import android.view.ViewGroup;
import org.chromium.content.browser.ContentViewCore; import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate; import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate;
import org.chromium.content.browser.input.SelectPopup; import org.chromium.content.browser.input.SelectPopup;
import org.chromium.content.browser.input.TextSuggestionHost;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.ViewAndroidDelegate; import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid; import org.chromium.ui.base.WindowAndroid;
...@@ -45,14 +44,6 @@ public class TestContentViewCore implements ContentViewCore { ...@@ -45,14 +44,6 @@ public class TestContentViewCore implements ContentViewCore {
return null; return null;
} }
@Override
public TextSuggestionHost getTextSuggestionHostForTesting() {
return null;
}
@Override
public void setTextSuggestionHostForTesting(TextSuggestionHost textSuggestionHost) {}
@Override @Override
public void initialize(ViewAndroidDelegate viewDelegate, public void initialize(ViewAndroidDelegate viewDelegate,
InternalAccessDelegate internalDispatcher, WebContents webContents, 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