Commit 90dde94f authored by Sinan Sahin's avatar Sinan Sahin Committed by Commit Bot

[Context menu] Attempt to fix NPE in mOnMenuClosed

When ContextMenuHelper#setPopulatorFactory is called, the current
ContextMenuPopulator is destroyed. If a context menu was showing when
dismissed after the populator is destroyed. This is because the
populator is accessed when the context menu is dismissed.

This CL holds a reference to the current context menu and dismisses it
in #setPopulatorFactory.

Bug: 1133172
Change-Id: I879a4278c045c3a4c9028b9b4825b5cd497a5e9e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2446775Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Sinan Sahin <sinansahin@google.com>
Cr-Commit-Position: refs/heads/master@{#813407}
parent aa6f85b5
......@@ -40,6 +40,7 @@ public class ContextMenuHelper {
private ContextMenuPopulator mCurrentPopulator;
private ContextMenuPopulatorFactory mPopulatorFactory;
private ContextMenuParams mCurrentContextMenuParams;
private ContextMenuUi mCurrentContextMenu;
private WindowAndroid mWindow;
private Callback<Integer> mCallback;
private Runnable mOnMenuShown;
......@@ -59,6 +60,10 @@ public class ContextMenuHelper {
@CalledByNative
private void destroy() {
if (mCurrentContextMenu != null) {
mCurrentContextMenu.dismiss();
mCurrentContextMenu = null;
}
if (mCurrentPopulator != null) mCurrentPopulator.onDestroy();
if (mPopulatorFactory != null) mPopulatorFactory.onDestroy();
mNativeContextMenuHelper = 0;
......@@ -66,6 +71,10 @@ public class ContextMenuHelper {
@CalledByNative
private void setPopulatorFactory(ContextMenuPopulatorFactory populatorFactory) {
if (mCurrentContextMenu != null) {
mCurrentContextMenu.dismiss();
mCurrentContextMenu = null;
}
if (mCurrentPopulator != null) mCurrentPopulator.onDestroy();
mCurrentPopulator = null;
if (mPopulatorFactory != null) mPopulatorFactory.onDestroy();
......@@ -112,6 +121,7 @@ public class ContextMenuHelper {
};
mOnMenuClosed = (notAbandoned) -> {
recordTimeToTakeActionHistogram(mSelectedItemBeforeDismiss || notAbandoned);
mCurrentContextMenu = null;
mCurrentPopulator.onMenuClosed();
if (LensUtils.enableShoppyImageMenuItem()
|| LensUtils.enableImageChip(mCurrentPopulator.isIncognito())) {
......@@ -151,6 +161,7 @@ public class ContextMenuHelper {
final RevampedContextMenuCoordinator menuCoordinator = new RevampedContextMenuCoordinator(
topContentOffsetPx, () -> shareImageWithLastShareComponent());
mCurrentContextMenu = menuCoordinator;
if (LensUtils.enableImageChip(mCurrentPopulator.isIncognito())) {
LensAsyncManager lensAsyncManager =
......
......@@ -39,4 +39,9 @@ public interface ContextMenuUi {
void displayMenu(WindowAndroid window, WebContents webContents, ContextMenuParams params,
List<Pair<Integer, ModelList>> items, Callback<Integer> onItemClicked,
Runnable onMenuShown, Callback<Boolean> onMenuClosed);
/**
* Dismiss the context menu.
*/
void dismiss();
}
......@@ -89,6 +89,11 @@ public class RevampedContextMenuCoordinator implements ContextMenuUi {
onMenuClosed, /* lensAsyncManager=*/null);
}
@Override
public void dismiss() {
dismissDialog();
}
// Shows the Context Menu in Chrome with the lens chip (if supported).
void displayMenuWithLensChip(final WindowAndroid window, WebContents webContents,
ContextMenuParams params, List<Pair<Integer, ModelList>> items,
......
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