2021-11-15 17:13:51 +01:00
|
|
|
import EventEmitter from 'safe-event-emitter';
|
2021-02-04 19:15:23 +01:00
|
|
|
import ExtensionPlatform from '../platforms/extension';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const NOTIFICATION_HEIGHT = 620;
|
|
|
|
const NOTIFICATION_WIDTH = 360;
|
2017-03-31 03:33:19 +02:00
|
|
|
|
2021-11-15 17:13:51 +01:00
|
|
|
export const NOTIFICATION_MANAGER_EVENTS = {
|
|
|
|
POPUP_CLOSED: 'onPopupClosed',
|
|
|
|
};
|
|
|
|
|
2022-01-07 16:57:33 +01:00
|
|
|
/**
|
|
|
|
* A collection of methods for controlling the showing and hiding of the notification popup.
|
|
|
|
*/
|
2021-11-15 17:13:51 +01:00
|
|
|
export default class NotificationManager extends EventEmitter {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor() {
|
2021-11-15 17:13:51 +01:00
|
|
|
super();
|
2021-02-04 19:15:23 +01:00
|
|
|
this.platform = new ExtensionPlatform();
|
2021-11-15 17:13:51 +01:00
|
|
|
this.platform.addOnRemovedListener(this._onWindowClosed.bind(this));
|
2020-04-11 17:12:45 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:09:19 +01:00
|
|
|
/**
|
|
|
|
* Mark the notification popup as having been automatically closed.
|
|
|
|
*
|
|
|
|
* This lets us differentiate between the cases where we close the
|
|
|
|
* notification popup v.s. when the user closes the popup window directly.
|
|
|
|
*/
|
|
|
|
markAsAutomaticallyClosed() {
|
|
|
|
this._popupAutomaticallyClosed = true;
|
|
|
|
}
|
|
|
|
|
2018-04-20 20:18:44 +02:00
|
|
|
/**
|
|
|
|
* Either brings an existing MetaMask notification window into focus, or creates a new notification window. New
|
|
|
|
* notification windows are given a 'popup' type.
|
|
|
|
*
|
2023-03-02 17:50:00 +01:00
|
|
|
* @param {Function} setCurrentPopupId - setter of current popup id from appStateController
|
|
|
|
* @param {number} currentPopupId - id of current opened metamask popup window
|
2018-04-20 20:18:44 +02:00
|
|
|
*/
|
2023-03-02 17:50:00 +01:00
|
|
|
async showPopup(setCurrentPopupId, currentPopupId) {
|
|
|
|
this._popupId = currentPopupId;
|
|
|
|
this._setCurrentPopupId = setCurrentPopupId;
|
|
|
|
const popup = await this._getPopup(currentPopupId);
|
2020-04-11 17:12:45 +02:00
|
|
|
// Bring focus to chrome popup
|
|
|
|
if (popup) {
|
|
|
|
// bring focus to existing chrome popup
|
2021-02-04 19:15:23 +01:00
|
|
|
await this.platform.focusWindow(popup.id);
|
2020-04-11 17:12:45 +02:00
|
|
|
} else {
|
2023-03-02 17:50:00 +01:00
|
|
|
// create new notification popup
|
2021-02-04 19:15:23 +01:00
|
|
|
let left = 0;
|
|
|
|
let top = 0;
|
2020-04-20 00:00:17 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const lastFocused = await this.platform.getLastFocusedWindow();
|
2020-04-20 00:00:17 +02:00
|
|
|
// Position window in top right corner of lastFocused window.
|
2021-02-04 19:15:23 +01:00
|
|
|
top = lastFocused.top;
|
|
|
|
left = lastFocused.left + (lastFocused.width - NOTIFICATION_WIDTH);
|
2020-04-20 00:00:17 +02:00
|
|
|
} catch (_) {
|
|
|
|
// The following properties are more than likely 0, due to being
|
|
|
|
// opened from the background chrome process for the extension that
|
|
|
|
// has no physical dimensions
|
2021-02-04 19:15:23 +01:00
|
|
|
const { screenX, screenY, outerWidth } = window;
|
|
|
|
top = Math.max(screenY, 0);
|
|
|
|
left = Math.max(screenX + (outerWidth - NOTIFICATION_WIDTH), 0);
|
2020-04-20 00:00:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 17:12:45 +02:00
|
|
|
const popupWindow = await this.platform.openWindow({
|
|
|
|
url: 'notification.html',
|
|
|
|
type: 'popup',
|
|
|
|
width: NOTIFICATION_WIDTH,
|
|
|
|
height: NOTIFICATION_HEIGHT,
|
2020-04-20 00:00:17 +02:00
|
|
|
left,
|
|
|
|
top,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-04-30 00:04:03 +02:00
|
|
|
|
|
|
|
// Firefox currently ignores left/top for create, but it works for update
|
2020-07-27 20:33:25 +02:00
|
|
|
if (popupWindow.left !== left && popupWindow.state !== 'fullscreen') {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this.platform.updateWindowPosition(popupWindow.id, left, top);
|
2020-04-30 00:04:03 +02:00
|
|
|
}
|
2023-03-02 17:50:00 +01:00
|
|
|
// pass new created popup window id to appController setter
|
|
|
|
// and store the id to private variable this._popupId for future access
|
|
|
|
this._setCurrentPopupId(popupWindow.id);
|
2021-02-04 19:15:23 +01:00
|
|
|
this._popupId = popupWindow.id;
|
2020-04-11 17:12:45 +02:00
|
|
|
}
|
2017-03-31 03:33:19 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:13:51 +01:00
|
|
|
_onWindowClosed(windowId) {
|
|
|
|
if (windowId === this._popupId) {
|
2023-03-02 17:50:00 +01:00
|
|
|
this._setCurrentPopupId(undefined);
|
2021-11-15 17:13:51 +01:00
|
|
|
this._popupId = undefined;
|
2022-01-05 18:09:19 +01:00
|
|
|
this.emit(NOTIFICATION_MANAGER_EVENTS.POPUP_CLOSED, {
|
|
|
|
automaticallyClosed: this._popupAutomaticallyClosed,
|
|
|
|
});
|
|
|
|
this._popupAutomaticallyClosed = undefined;
|
2021-11-15 17:13:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 20:18:44 +02:00
|
|
|
/**
|
|
|
|
* Checks all open MetaMask windows, and returns the first one it finds that is a notification window (i.e. has the
|
|
|
|
* type 'popup')
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async _getPopup() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const windows = await this.platform.getAllWindows();
|
|
|
|
return this._getPopupIn(windows);
|
2017-03-31 03:33:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 20:18:44 +02:00
|
|
|
/**
|
2018-06-17 19:16:30 +02:00
|
|
|
* Given an array of windows, returns the 'popup' that has been opened by MetaMask, or null if no such window exists.
|
2018-04-20 20:18:44 +02:00
|
|
|
*
|
|
|
|
* @private
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Array} windows - An array of objects containing data about the open MetaMask extension windows.
|
2018-04-20 20:18:44 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_getPopupIn(windows) {
|
|
|
|
return windows
|
|
|
|
? windows.find((win) => {
|
|
|
|
// Returns notification popup
|
2021-02-04 19:15:23 +01:00
|
|
|
return win && win.type === 'popup' && win.id === this._popupId;
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
: null;
|
2017-03-31 03:33:19 +02:00
|
|
|
}
|
|
|
|
}
|