Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
ENVIRONMENT_TYPE_SIDEPANEL,
PLATFORM_FIREFOX,
MESSAGE_TYPE,
POPUP_FILE,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactoring

POPUP_INIT_FILE,
SIDEPANEL_FILE,
} from '../../shared/constants/app';
import { EXTENSION_MESSAGES } from '../../shared/constants/messages';
import { BACKGROUND_LIVENESS_METHOD } from '../../shared/constants/ui-initialization';
Expand Down Expand Up @@ -154,6 +157,7 @@ log.setLevel(process.env.METAMASK_DEBUG ? 'debug' : 'info', false);
const platform = new ExtensionPlatform();
const notificationManager = new NotificationManager();
const isFirefox = getPlatform() === PLATFORM_FIREFOX;
const POPUP_LAUNCH_FILE = isFirefox ? POPUP_FILE : POPUP_INIT_FILE;

/**
* Parses port connection info for routing decisions.
Expand Down Expand Up @@ -1837,36 +1841,39 @@ export function setupController(
onTransactionFailed,
);

function onTransactionFailed() {
failedTxCount += 1;
const popupFile = isFirefox ? 'popup.html' : 'popup-init.html';
function setClientOpenOptions(tab) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Extracted from call sites: onTransactionFailed and clearFailedTxBadge

const popup = tab ? `${POPUP_LAUNCH_FILE}?tab=${tab}` : POPUP_LAUNCH_FILE;
const sidepanelPath = tab ? `${SIDEPANEL_FILE}?tab=${tab}` : SIDEPANEL_FILE;

try {
if (isManifestV3) {
browser.action.setPopup({ popup: `${popupFile}?tab=activity` });
browser.action.setPopup({ popup });
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the next popup action

browser.sidePanel?.setOptions?.({ path: sidepanelPath });
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set sidepanel option on next open

} else {
browser.browserAction.setPopup({ popup: `${popupFile}?tab=activity` });
browser.browserAction.setPopup({ popup });
}
} catch (e) {
console.error('Error setting failed tx badge popup:', e);
console.error('Error setting extension action URLs:', e);
}
}

function onTransactionFailed() {
if (isClientOpenStatus()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Don't run this unless the extension is closed

return;
}

failedTxCount += 1;
setClientOpenOptions('activity');
updateBadge();
}

function clearFailedTxBadge() {
if (!failedTxCount) {
return;
}

failedTxCount = 0;
const popupFile = isFirefox ? 'popup.html' : 'popup-init.html';
try {
if (isManifestV3) {
browser.action.setPopup({ popup: popupFile });
} else {
browser.browserAction.setPopup({ popup: popupFile });
}
} catch (e) {
console.error('Error clearing failed tx badge popup:', e);
}
setClientOpenOptions();
updateBadge();
}

Expand Down
4 changes: 4 additions & 0 deletions shared/constants/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const ENVIRONMENT_TYPE_FULLSCREEN = 'fullscreen';
export const ENVIRONMENT_TYPE_SIDEPANEL = 'sidepanel';
export const ENVIRONMENT_TYPE_BACKGROUND = 'background';

export const POPUP_FILE = 'popup.html';
export const POPUP_INIT_FILE = 'popup-init.html';
export const SIDEPANEL_FILE = 'sidepanel.html';

export const PLATFORM_BRAVE = 'Brave';
export const PLATFORM_CHROME = 'Chrome';
export const PLATFORM_CHROMIUM = 'Chromium';
Expand Down
6 changes: 4 additions & 2 deletions shared/lib/environment-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import {
ENVIRONMENT_TYPE_NOTIFICATION,
ENVIRONMENT_TYPE_POPUP,
ENVIRONMENT_TYPE_SIDEPANEL,
POPUP_FILE,
SIDEPANEL_FILE,
} from '../constants/app';

/**
* @see {@link getEnvironmentType}
*/
const getEnvironmentTypeMemo = memoize((url: string) => {
const parsedUrl = new URL(url);
if (parsedUrl.pathname === '/popup.html') {
if (parsedUrl.pathname === `/${POPUP_FILE}`) {
return ENVIRONMENT_TYPE_POPUP;
} else if (['/home.html'].includes(parsedUrl.pathname)) {
return ENVIRONMENT_TYPE_FULLSCREEN;
} else if (parsedUrl.pathname === '/notification.html') {
return ENVIRONMENT_TYPE_NOTIFICATION;
} else if (parsedUrl.pathname === '/sidepanel.html') {
} else if (parsedUrl.pathname === `/${SIDEPANEL_FILE}`) {
return ENVIRONMENT_TYPE_SIDEPANEL;
}
return ENVIRONMENT_TYPE_BACKGROUND;
Expand Down
Loading