Commit c1ffeb3e authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: makes Tab always have a Browser

This avoids error prone null checks.

BUG=none
TEST=covered by tests

Change-Id: Ie0da15f3469c06d56b2feafa9e64ff74347201fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2463806Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815793}
parent 9a52be5f
...@@ -240,8 +240,10 @@ public class BrowserImpl extends IBrowser.Stub implements View.OnAttachStateChan ...@@ -240,8 +240,10 @@ public class BrowserImpl extends IBrowser.Stub implements View.OnAttachStateChan
@Override @Override
public TabImpl createTab() { public TabImpl createTab() {
TabImpl tab = new TabImpl(mProfile, mWindowAndroid); TabImpl tab = new TabImpl(this, mProfile, mWindowAndroid);
addTab(tab); // This needs |alwaysAdd| set to true as the Tab is created with the Browser already set to
// this.
addTab(tab, /* alwaysAdd */ true);
return tab; return tab;
} }
...@@ -291,14 +293,17 @@ public class BrowserImpl extends IBrowser.Stub implements View.OnAttachStateChan ...@@ -291,14 +293,17 @@ public class BrowserImpl extends IBrowser.Stub implements View.OnAttachStateChan
@Override @Override
public void addTab(ITab iTab) { public void addTab(ITab iTab) {
StrictModeWorkaround.apply(); StrictModeWorkaround.apply();
TabImpl tab = (TabImpl) iTab; addTab((TabImpl) iTab, /* alwaysAdd */ false);
if (tab.getBrowser() == this) return; }
private void addTab(TabImpl tab, boolean alwaysAdd) {
if (!alwaysAdd && tab.getBrowser() == this) return;
BrowserImplJni.get().addTab(mNativeBrowser, tab.getNativeTab()); BrowserImplJni.get().addTab(mNativeBrowser, tab.getNativeTab());
} }
@CalledByNative @CalledByNative
private void createJavaTabForNativeTab(long nativeTab) { private void createJavaTabForNativeTab(long nativeTab) {
new TabImpl(mProfile, mWindowAndroid, nativeTab); new TabImpl(this, mProfile, mWindowAndroid, nativeTab);
} }
private void checkPreferences() { private void checkPreferences() {
......
...@@ -19,6 +19,7 @@ import android.view.ViewStructure; ...@@ -19,6 +19,7 @@ import android.view.ViewStructure;
import android.view.autofill.AutofillValue; import android.view.autofill.AutofillValue;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -101,9 +102,8 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -101,9 +102,8 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
private FullscreenCallbackProxy mFullscreenCallbackProxy; private FullscreenCallbackProxy mFullscreenCallbackProxy;
private TabViewAndroidDelegate mViewAndroidDelegate; private TabViewAndroidDelegate mViewAndroidDelegate;
private GoogleAccountsCallbackProxy mGoogleAccountsCallbackProxy; private GoogleAccountsCallbackProxy mGoogleAccountsCallbackProxy;
// BrowserImpl this TabImpl is in. This is null before attached to a Browser. While this is null // BrowserImpl this TabImpl is in.
// before attached, there are code paths that may trigger calling methods before set. @NonNull
@Nullable
private BrowserImpl mBrowser; private BrowserImpl mBrowser;
private LoginPrompt mLoginPrompt; private LoginPrompt mLoginPrompt;
/** /**
...@@ -238,7 +238,8 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -238,7 +238,8 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
return sTabMap.get(tabId); return sTabMap.get(tabId);
} }
public TabImpl(ProfileImpl profile, WindowAndroid windowAndroid) { public TabImpl(BrowserImpl browser, ProfileImpl profile, WindowAndroid windowAndroid) {
mBrowser = browser;
mId = ++sNextId; mId = ++sNextId;
init(profile, windowAndroid, TabImplJni.get().createTab(profile.getNativeProfile(), this)); init(profile, windowAndroid, TabImplJni.get().createTab(profile.getNativeProfile(), this));
} }
...@@ -247,8 +248,10 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -247,8 +248,10 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
* This constructor is called when the native side triggers creation of a TabImpl * This constructor is called when the native side triggers creation of a TabImpl
* (as happens with popups and other scenarios). * (as happens with popups and other scenarios).
*/ */
public TabImpl(ProfileImpl profile, WindowAndroid windowAndroid, long nativeTab) { public TabImpl(
BrowserImpl browser, ProfileImpl profile, WindowAndroid windowAndroid, long nativeTab) {
mId = ++sNextId; mId = ++sNextId;
mBrowser = browser;
TabImplJni.get().setJavaImpl(nativeTab, TabImpl.this); TabImplJni.get().setJavaImpl(nativeTab, TabImpl.this);
init(profile, windowAndroid, nativeTab); init(profile, windowAndroid, nativeTab);
} }
...@@ -336,6 +339,9 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -336,6 +339,9 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
* Sets the BrowserImpl this TabImpl is contained in. * Sets the BrowserImpl this TabImpl is contained in.
*/ */
public void attachToBrowser(BrowserImpl browser) { public void attachToBrowser(BrowserImpl browser) {
// NOTE: during tab creation this is called with |mBrowser| set to |browser|. This happens
// because the tab is created with |mBrowser| already set (to avoid having a bunch of null
// checks).
mBrowser = browser; mBrowser = browser;
updateFromBrowser(); updateFromBrowser();
} }
...@@ -418,7 +424,6 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -418,7 +424,6 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
public void onAttachedToViewController( public void onAttachedToViewController(
long topControlsContainerViewHandle, long bottomControlsContainerViewHandle) { long topControlsContainerViewHandle, long bottomControlsContainerViewHandle) {
// attachToFragment() must be called before activate(). // attachToFragment() must be called before activate().
assert mBrowser != null;
TabImplJni.get().setBrowserControlsContainerViews( TabImplJni.get().setBrowserControlsContainerViews(
mNativeTab, topControlsContainerViewHandle, bottomControlsContainerViewHandle); mNativeTab, topControlsContainerViewHandle, bottomControlsContainerViewHandle);
mInfoBarContainer.onTabAttachedToViewController(); mInfoBarContainer.onTabAttachedToViewController();
...@@ -459,7 +464,7 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -459,7 +464,7 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
} }
private boolean isActiveTab() { private boolean isActiveTab() {
return mBrowser != null && mBrowser.getActiveTab() == this; return mBrowser.getActiveTab() == this;
} }
private void updateWebContentsVisibility() { private void updateWebContentsVisibility() {
...@@ -1043,8 +1048,6 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -1043,8 +1048,6 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
} }
private void onBrowserControlsConstraintUpdated(int constraint) { private void onBrowserControlsConstraintUpdated(int constraint) {
// WARNING: this may be called before attached. This means |mBrowser| may be null.
// If something has overridden the FIP's SHOWN constraint, cancel FIP. This causes FIP to // If something has overridden the FIP's SHOWN constraint, cancel FIP. This causes FIP to
// dismiss when entering fullscreen. // dismiss when entering fullscreen.
if (constraint != BrowserControlsState.SHOWN) { if (constraint != BrowserControlsState.SHOWN) {
......
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