Commit 15faadec authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: Call Tab.nativeDestroy only when nativeInit was called

Tab.nativeInit and nativeDestroy come in pair. This CL ensures that
the latter is executed only when |mIsInitialized| is true by |nativeInit|
beforehand. This helps Tab hide |initializeNative| API from public
interface.

Also added a few asserts against mNativeTabAndroid to make sure Tab
is used in native-initialized state.

Bug: 995903
Change-Id: I8178a4cd064f48dddd330cc5a314f946e579dd50
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1818010
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701006}
parent 422aeeab
...@@ -123,9 +123,6 @@ public class Tab { ...@@ -123,9 +123,6 @@ public class Tab {
/** Gives {@link Tab} a way to interact with the Android window. */ /** Gives {@link Tab} a way to interact with the Android window. */
private WindowAndroid mWindowAndroid; private WindowAndroid mWindowAndroid;
/** Whether or not this {@link Tab} is initialized and should be interacted with. */
private boolean mIsInitialized;
/** The current native page (e.g. chrome-native://newtab), or {@code null} if there is none. */ /** The current native page (e.g. chrome-native://newtab), or {@code null} if there is none. */
private NativePage mNativePage; private NativePage mNativePage;
...@@ -1015,6 +1012,7 @@ public class Tab { ...@@ -1015,6 +1012,7 @@ public class Tab {
// activity. // activity.
maybeShowNativePage(getUrl(), true); maybeShowNativePage(getUrl(), true);
assert mNativeTabAndroid != 0;
TabJni.get().attachDetachedTab(mNativeTabAndroid, Tab.this); TabJni.get().attachDetachedTab(mNativeTabAndroid, Tab.this);
if (getWebContents() != null) { if (getWebContents() != null) {
...@@ -1126,16 +1124,11 @@ public class Tab { ...@@ -1126,16 +1124,11 @@ public class Tab {
} }
/** /**
* Builds the native counterpart to this class. Meant to be overridden by subclasses to build * Builds the native counterpart to this class.
* subclass native counterparts instead. Subclasses should not call this via super and instead
* rely on the native class to create the JNI association.
*
* TODO(dfalcantara): Make this function harder to access.
*/ */
public void initializeNative() { private void initializeNative() {
if (mNativeTabAndroid == 0) TabJni.get().init(Tab.this); if (mNativeTabAndroid == 0) TabJni.get().init(Tab.this);
assert mNativeTabAndroid != 0; assert mNativeTabAndroid != 0;
mIsInitialized = true;
} }
/** /**
...@@ -1250,7 +1243,6 @@ public class Tab { ...@@ -1250,7 +1243,6 @@ public class Tab {
* this method is called. Once this call is made this {@link Tab} should no longer be used. * this method is called. Once this call is made this {@link Tab} should no longer be used.
*/ */
public void destroy() { public void destroy() {
mIsInitialized = false;
// Update the title before destroying the tab. http://b/5783092 // Update the title before destroying the tab. http://b/5783092
updateTitle(); updateTitle();
...@@ -1270,9 +1262,10 @@ public class Tab { ...@@ -1270,9 +1262,10 @@ public class Tab {
// InfoBarContainer. The native tab should be destroyed before the infobar container as // InfoBarContainer. The native tab should be destroyed before the infobar container as
// destroying the native tab cleanups up any remaining infobars. The infobar container // destroying the native tab cleanups up any remaining infobars. The infobar container
// expects all infobars to be cleaned up before its own destruction. // expects all infobars to be cleaned up before its own destruction.
assert mNativeTabAndroid != 0; if (mNativeTabAndroid != 0) {
TabJni.get().destroy(mNativeTabAndroid, Tab.this); TabJni.get().destroy(mNativeTabAndroid, Tab.this);
assert mNativeTabAndroid == 0; assert mNativeTabAndroid == 0;
}
} }
/** /**
...@@ -1280,7 +1273,7 @@ public class Tab { ...@@ -1280,7 +1273,7 @@ public class Tab {
* {@link #initializeNative()} being called or after {@link #destroy()}. * {@link #initializeNative()} being called or after {@link #destroy()}.
*/ */
public boolean isInitialized() { public boolean isInitialized() {
return mIsInitialized; return mNativeTabAndroid != 0;
} }
/** /**
...@@ -1659,6 +1652,7 @@ public class Tab { ...@@ -1659,6 +1652,7 @@ public class Tab {
webContents.setSize(originalWidth, originalHeight); webContents.setSize(originalWidth, originalHeight);
if (!bounds.isEmpty()) { if (!bounds.isEmpty()) {
assert mNativeTabAndroid != 0;
TabJni.get().onPhysicalBackingSizeChanged( TabJni.get().onPhysicalBackingSizeChanged(
mNativeTabAndroid, Tab.this, webContents, bounds.right, bounds.bottom); mNativeTabAndroid, Tab.this, webContents, bounds.right, bounds.bottom);
} }
...@@ -1690,6 +1684,7 @@ public class Tab { ...@@ -1690,6 +1684,7 @@ public class Tab {
@CalledByNative @CalledByNative
private void setNativePtr(long nativePtr) { private void setNativePtr(long nativePtr) {
assert nativePtr != 0;
assert mNativeTabAndroid == 0; assert mNativeTabAndroid == 0;
mNativeTabAndroid = nativePtr; mNativeTabAndroid = nativePtr;
} }
...@@ -1738,6 +1733,7 @@ public class Tab { ...@@ -1738,6 +1733,7 @@ public class Tab {
*/ */
public void createHistoricalTab() { public void createHistoricalTab() {
if (!isFrozen()) { if (!isFrozen()) {
assert mNativeTabAndroid != 0;
TabJni.get().createHistoricalTab(mNativeTabAndroid, Tab.this); TabJni.get().createHistoricalTab(mNativeTabAndroid, Tab.this);
} else if (mFrozenContentsState != null) { } else if (mFrozenContentsState != null) {
mFrozenContentsState.createHistoricalTab(); mFrozenContentsState.createHistoricalTab();
...@@ -1885,6 +1881,7 @@ public class Tab { ...@@ -1885,6 +1881,7 @@ public class Tab {
* @return Whether input events from the renderer are ignored on the browser side. * @return Whether input events from the renderer are ignored on the browser side.
*/ */
public boolean areRendererInputEventsIgnored() { public boolean areRendererInputEventsIgnored() {
assert mNativeTabAndroid != 0;
return TabJni.get().areRendererInputEventsIgnored(mNativeTabAndroid, Tab.this); return TabJni.get().areRendererInputEventsIgnored(mNativeTabAndroid, Tab.this);
} }
......
...@@ -135,14 +135,9 @@ public class TabModelSelectorTabObserverTest { ...@@ -135,14 +135,9 @@ public class TabModelSelectorTabObserverTest {
} }
private Tab createTestTab(boolean incognito) { private Tab createTestTab(boolean incognito) {
return ThreadUtils.runOnUiThreadBlockingNoException(() -> { return ThreadUtils.runOnUiThreadBlockingNoException(
Tab testTab = new TabBuilder() new TabBuilder().setIncognito(incognito).setWindow(
.setIncognito(incognito) mTestRule.getWindowAndroid())::build);
.setWindow(mTestRule.getWindowAndroid())
.build();
testTab.initializeNative();
return testTab;
});
} }
private static void addTab(TabModel tabModel, Tab tab) { private static void addTab(TabModel tabModel, Tab tab) {
......
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