Commit 57a60743 authored by Mei Liang's avatar Mei Liang Committed by Commit Bot

[TabGroupModelFilter] Fix crash on undo last closed tab

The cause is TabModel#setIndex() event is fired when undoing the first
tab, before the tab actually undo in TabGroupModelFilter.

This CL is a band-aid fix for M74. We cache the selected tab that is not
exist in the TabGroupFilterModel. And try to select that tab on the
next addTab()/undo call. This will fix the current crash and will not
causing any side effects on TabGroupModelFilter normal usage.

Bug: 948518
Change-Id: Ic65e521baebe65c54b1033f6be35dd2dbdd1a676
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1552281Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Commit-Queue: Mei Liang <meiliang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#648363}
parent 5b6dfafb
......@@ -110,10 +110,20 @@ public abstract class TabModelFilter extends EmptyTabModelObserver implements Ta
*/
protected abstract void reorder();
// TODO(crbug.com/948518): This is a band-aid fix for not crashing when undo the last closed
// tab, should remove later.
/**
* @return Whether filter should notify observers about the SetIndex call.
*/
protected boolean shouldNotifyObserversOnSetIndex() {
return true;
}
// TabModelObserver implementation.
@Override
public void didSelectTab(Tab tab, int type, int lastId) {
selectTab(tab);
if (!shouldNotifyObserversOnSetIndex()) return;
for (TabModelObserver observer : mFilteredObservers) {
observer.didSelectTab(tab, type, lastId);
}
......
......@@ -103,6 +103,7 @@ public class TabGroupModelFilter extends TabModelFilter {
private int mCurrentGroupIndex = TabList.INVALID_TAB_INDEX;
// The number of groups with at least 2 tabs.
private int mActualGroupCount;
private Tab mAbsentSelectedTab;
public TabGroupModelFilter(TabModel tabModel) {
super(tabModel);
......@@ -165,6 +166,12 @@ public class TabGroupModelFilter extends TabModelFilter {
mGroupIdToGroupMap.put(groupId, tabGroup);
mGroupIdToGroupIndexMap.put(groupId, mGroupIdToGroupIndexMap.size());
}
if (mAbsentSelectedTab != null) {
Tab absentSelectedTab = mAbsentSelectedTab;
mAbsentSelectedTab = null;
selectTab(absentSelectedTab);
}
}
@Override
......@@ -198,9 +205,14 @@ public class TabGroupModelFilter extends TabModelFilter {
@Override
protected void selectTab(Tab tab) {
assert mAbsentSelectedTab == null;
int groupId = tab.getRootId();
mGroupIdToGroupMap.get(groupId).setLastShownTabId(tab.getId());
mCurrentGroupIndex = mGroupIdToGroupIndexMap.get(groupId);
if (mGroupIdToGroupMap.get(groupId) == null) {
mAbsentSelectedTab = tab;
} else {
mGroupIdToGroupMap.get(groupId).setLastShownTabId(tab.getId());
mCurrentGroupIndex = mGroupIdToGroupIndexMap.get(groupId);
}
}
@Override
......@@ -232,6 +244,11 @@ public class TabGroupModelFilter extends TabModelFilter {
}
}
@Override
protected boolean shouldNotifyObserversOnSetIndex() {
return mAbsentSelectedTab == null;
}
// TabList implementation.
@Override
public boolean isIncognito() {
......
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