From 252be8bbbeee1029e1fa2e1be06d1609ab216b40 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Mon, 28 Oct 2024 17:24:30 +0200 Subject: [PATCH] Fixed crash of access null profile from EmptyTabModel fixes brave/brave-browser#41922 --- .../BraveTabGroupUiCoordinator.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/BraveTabGroupUiCoordinator.java b/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/BraveTabGroupUiCoordinator.java index 8e285416e49e..c9c4ec68b821 100644 --- a/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/BraveTabGroupUiCoordinator.java +++ b/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/BraveTabGroupUiCoordinator.java @@ -23,6 +23,7 @@ import org.chromium.chrome.browser.tabmodel.IncognitoStateProvider.IncognitoStateObserver; import org.chromium.chrome.browser.tabmodel.TabCreatorManager; import org.chromium.chrome.browser.tabmodel.TabModelSelector; +import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver; import org.chromium.chrome.browser.toolbar.bottom.BottomControlsCoordinator; import org.chromium.chrome.tab_ui.R; import org.chromium.components.browser_ui.bottomsheet.BottomSheetController; @@ -37,6 +38,7 @@ public class BraveTabGroupUiCoordinator extends TabGroupUiCoordinator { // Own members. private IncognitoStateProvider mIncognitoStateProvider; private IncognitoStateObserver mIncognitoStateObserver; + private TabModelSelector mTabModelSelector; public BraveTabGroupUiCoordinator( @NonNull Activity activity, @@ -68,6 +70,7 @@ public BraveTabGroupUiCoordinator( modalDialogManager); mIncognitoStateProvider = incognitoStateProvider; + mTabModelSelector = tabModelSelector; assert mToolbarView != null : "Make sure mToolbarView is properly patched in bytecode."; ChromeImageView fadingEdgeStart = @@ -94,6 +97,39 @@ public void initializeWithNative( Activity activity, BottomControlsCoordinator.BottomControlsVisibilityController visibilityController, Callback onModelTokenChange) { + // Fix for the null object crash at TabGroupSyncFeatures.isTabGroupSyncEnabled + // Stack: + // at TabGroupSyncFeatures.isTabGroupSyncEnabled (TabGroupSyncFeatures.java:18) + // at TabGroupUiMediator. (TabGroupUiMediator.java:143) + // at TabGroupUiCoordinator.initializeWithNative (TabGroupUiCoordinator.java:238) + // at BraveTabGroupUiCoordinator.initializeWithNative (BraveTabGroupUiCoordinator.java:97) + // at BottomControlsCoordinator.lambda$new$1 (BottomControlsCoordinator.java:148) + // + // TabModelSelector.getModel returns EmptyTabModel with null profile when the model is not + // found. + // Upstream does not have this issue because they don't use BottomControlsCoordinator + if (mTabModelSelector.getModels().size() < 2) { + mTabModelSelector.addObserver( + new TabModelSelectorObserver() { + @Override + public void onChange() { + if (mTabModelSelector.getModels().size() >= 2) { + callSuperInitializeWithNative( + activity, visibilityController, onModelTokenChange); + + mTabModelSelector.removeObserver(this); + } + } + }); + } else { + callSuperInitializeWithNative(activity, visibilityController, onModelTokenChange); + } + } + + private void callSuperInitializeWithNative( + Activity activity, + BottomControlsCoordinator.BottomControlsVisibilityController visibilityController, + Callback onModelTokenChange) { super.initializeWithNative(activity, visibilityController, onModelTokenChange); mIncognitoStateObserver =