Skip to content

Commit

Permalink
Proper initialization of BraveShieldsHandler.mContext
Browse files Browse the repository at this point in the history
for custom tabs created via WarmupManager.inflateViewHierarchy

Fixes brave/brave-browser#28805
  • Loading branch information
AlexeyBarabash committed Mar 15, 2023
1 parent a1a3216 commit c06f78b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 15 additions & 7 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils;
import org.chromium.chrome.browser.custom_layout.popup_window_tooltip.PopupWindowTooltip;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.dependency_injection.ChromeActivityComponent;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
Expand Down Expand Up @@ -1652,22 +1653,29 @@ public void showDormantUsersEngagementDialog(String notificationType) {
}
}

static public ChromeTabbedActivity getChromeTabbedActivity() {
private static Activity getActivityOfType(Class<?> classOfActivity) {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof ChromeTabbedActivity)) continue;
if (!classOfActivity.isInstance(ref)) continue;

return (ChromeTabbedActivity)ref;
return ref;
}

return null;
}

static public ChromeTabbedActivity getChromeTabbedActivity() {
return (ChromeTabbedActivity) getActivityOfType(ChromeTabbedActivity.class);
}

static public CustomTabActivity getCustomTabActivity() {
return (CustomTabActivity) getActivityOfType(CustomTabActivity.class);
}

@NonNull
static public BraveActivity getBraveActivity() throws ActivityNotFoundException {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof BraveActivity)) continue;

return (BraveActivity)ref;
BraveActivity activity = (BraveActivity) getActivityOfType(BraveActivity.class);
if (activity != null) {
return activity;
}

throw new ActivityNotFoundException("BraveActivity Not Found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public BlockersInfo() {
public ArrayList<String> mBlockerNames;
}

private final Context mContext;
private Context mContext;
private PopupWindow mPopupWindow;
private AnimatorSet mMenuItemEnterAnimator;
private BraveShieldsMenuObserver mMenuObserver;
Expand Down Expand Up @@ -284,8 +284,22 @@ public void addObserver(BraveShieldsMenuObserver menuObserver) {
mMenuObserver = menuObserver;
}

private void ensureInitializedForCustomTabs() {
if (mHardwareButtonMenuAnchor == null && mContext == null) {
mContext = BraveActivity.getCustomTabActivity();
mHardwareButtonMenuAnchor = ((Activity) mContext).findViewById(R.id.menu_anchor_stub);
}
}

public void show(View anchorView, Tab tab) {
if (mHardwareButtonMenuAnchor == null) return;
// Current class can be initialized by WarmupManager.inflateViewHierarchy
// prior to creation of proper activity. In such case activity is available later.
// Try to find it, and give up if somehow we won't be able.
ensureInitializedForCustomTabs();

if (mHardwareButtonMenuAnchor == null || mContext == null) {
return;
}

mHost = tab.getUrl().getSpec();
mTitle = tab.getUrl().getHost();
Expand All @@ -299,8 +313,9 @@ public void show(View anchorView, Tab tab) {
updateValues(mTabId);
}

public PopupWindow showPopupMenu(View anchorView) {
if (mContext == null) return null;
private PopupWindow showPopupMenu(View anchorView) {
assert (mContext != null);
assert (mHardwareButtonMenuAnchor != null);

int rotation = ((Activity)mContext).getWindowManager().getDefaultDisplay().getRotation();
// This fixes the bug where the bottom of the menu starts at the top of
Expand Down

0 comments on commit c06f78b

Please sign in to comment.