3939import java .io .Writer ;
4040import java .lang .reflect .Type ;
4141import java .util .ArrayList ;
42+ import java .util .Arrays ;
43+ import java .util .Collections ;
4244import java .util .List ;
4345import java .util .stream .Collectors ;
4446
@@ -677,13 +679,17 @@ void updateMaxWindowScales() {
677679 }
678680 }
679681
682+ public ArrayList <WindowWidget > getCurrentWindows (boolean privateMode ) {
683+ return privateMode ? mPrivateWindows : mRegularWindows ;
684+ }
685+
680686 public ArrayList <WindowWidget > getCurrentWindows () {
681- return mPrivateMode ? mPrivateWindows : mRegularWindows ;
687+ return getCurrentWindows ( mPrivateMode ) ;
682688 }
683689
684690 @ Nullable
685- private WindowWidget getWindowWithPlacement (WindowPlacement aPlacement ) {
686- for (WindowWidget window : getCurrentWindows () ) {
691+ private WindowWidget getWindowWithPlacement (WindowPlacement aPlacement , boolean privateMode ) {
692+ for (WindowWidget window : privateMode ? mPrivateWindows : mRegularWindows ) {
687693 if (window .getWindowPlacement () == aPlacement ) {
688694 return window ;
689695 }
@@ -692,11 +698,21 @@ private WindowWidget getWindowWithPlacement(WindowPlacement aPlacement) {
692698 }
693699
694700 @ Nullable
695- private WindowWidget getFrontWindow () {
701+ private WindowWidget getWindowWithPlacement (WindowPlacement aPlacement ) {
702+ return getWindowWithPlacement (aPlacement , mPrivateMode );
703+ }
704+
705+ @ Nullable
706+ private WindowWidget getFrontWindow (boolean privateMode ) {
696707 if (mFullscreenWindow != null ) {
697708 return mFullscreenWindow ;
698709 }
699- return getWindowWithPlacement (WindowPlacement .FRONT );
710+ return getWindowWithPlacement (WindowPlacement .FRONT , privateMode );
711+ }
712+
713+ @ Nullable
714+ private WindowWidget getFrontWindow () {
715+ return getFrontWindow (mPrivateMode );
700716 }
701717
702718 @ Nullable
@@ -993,7 +1009,9 @@ public void onAuthenticated(@NonNull OAuthAccount oAuthAccount, @NonNull AuthTyp
9931009
9941010 Session fxaSession = SessionStore .get ().getSession (mAccounts .getOriginSessionId ());
9951011 if (fxaSession != null ) {
996- fxaSession .loadUri (mAccounts .getConnectionSuccessURL (), GeckoSession .LOAD_FLAGS_REPLACE_HISTORY );
1012+ Session originSession = SessionStore .get ().getSession (fxaSession .getId ());
1013+ closeTabs (Collections .singletonList (originSession ), fxaSession .isPrivateMode ());
1014+ addTab (getFocusedWindow (), mAccounts .getConnectionSuccessURL ());
9971015 }
9981016
9991017 switch (mAccounts .getLoginOrigin ()) {
@@ -1182,15 +1200,20 @@ private WindowWidget getWindowWithSession(GeckoSession aSession) {
11821200 }
11831201
11841202 @ Nullable
1185- private WindowWidget getWindowWithSession (Session aSession ) {
1186- for (WindowWidget window : getCurrentWindows ()) {
1203+ private WindowWidget getWindowWithSession (Session aSession , boolean privateMode ) {
1204+ for (WindowWidget window : getCurrentWindows (privateMode )) {
11871205 if (window .getSession () == aSession ) {
11881206 return window ;
11891207 }
11901208 }
11911209 return null ;
11921210 }
11931211
1212+ @ Nullable
1213+ private WindowWidget getWindowWithSession (Session aSession ) {
1214+ return getWindowWithSession (aSession , mPrivateMode );
1215+ }
1216+
11941217 // WindowWidget.Delegate
11951218 @ Override
11961219 public void onFocusRequest (@ NonNull WindowWidget aWindow ) {
@@ -1297,8 +1320,6 @@ public void addTab(@NonNull WindowWidget targetWindow, @Nullable String aUri) {
12971320 targetWindow .setSession (session , WindowWidget .DEACTIVATE_CURRENT_SESSION );
12981321 if (aUri == null || aUri .isEmpty ()) {
12991322 session .loadHomePage ();
1300- } else {
1301- session .loadUri (aUri );
13021323 }
13031324 }
13041325
@@ -1316,16 +1337,20 @@ public void onTabAdd() {
13161337 }
13171338
13181339 @ Override
1319- public void onTabsClose (ArrayList <Session > aTabs ) {
1340+ public void onTabsClose (List <Session > aTabs ) {
1341+ closeTabs (aTabs , mPrivateMode );
1342+ }
1343+
1344+ private void closeTabs (List <Session > aTabs , boolean privateMode ) {
13201345 WindowWidget targetWindow = mFocusedWindow ;
13211346 // Prepare available tabs to choose from
1322- ArrayList <Session > available = SessionStore .get ().getSortedSessions (mPrivateMode );
1347+ ArrayList <Session > available = SessionStore .get ().getSortedSessions (privateMode );
13231348 available .removeAll (aTabs );
1324- available .removeIf (session -> getWindowWithSession (session ) != null );
1349+ available .removeIf (session -> getWindowWithSession (session , privateMode ) != null );
13251350
13261351 // Sort windows by priority to take an available tab
1327- WindowWidget front = getFrontWindow ();
1328- ArrayList <WindowWidget > windows = new ArrayList <>( getCurrentWindows () );
1352+ WindowWidget front = getFrontWindow (privateMode );
1353+ ArrayList <WindowWidget > windows = getCurrentWindows (privateMode );
13291354 windows .sort ((w1 , w2 ) -> {
13301355 // Max priority for the target window
13311356 if (w1 == targetWindow ) {
0 commit comments