keeweb/app/scripts/comp/browser/popup-notifier.js

161 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import Backbone from 'backbone';
import { AuthReceiver } from 'comp/browser/auth-receiver';
import { Launcher } from 'comp/launcher';
import { Alerts } from 'comp/ui/alerts';
import { Links } from 'const/links';
import { Timeouts } from 'const/timeouts';
import { Locale } from 'util/locale';
import { Logger } from 'util/logger';
2016-03-26 21:12:56 +01:00
2017-01-31 07:50:28 +01:00
const PopupNotifier = {
logger: null,
2019-08-18 10:17:09 +02:00
init() {
2018-02-16 19:32:44 +01:00
this.logger = new Logger('popup-notifier');
2016-03-27 16:47:29 +02:00
if (Launcher) {
window.open = this._openLauncherWindow.bind(this);
2016-03-27 16:47:29 +02:00
} else {
2017-01-31 07:50:28 +01:00
const windowOpen = window.open;
2019-08-18 10:17:09 +02:00
window.open = function(...args) {
const win = windowOpen.apply(window, args);
2016-03-26 21:12:56 +01:00
if (win) {
PopupNotifier.deferCheckClosed(win);
Backbone.trigger('popup-opened', win);
2016-03-27 15:36:07 +02:00
} else {
if (!Alerts.alertDisplayed) {
Alerts.error({
header: Locale.authPopupRequired,
body: Locale.authPopupRequiredBody
});
}
2016-03-26 21:12:56 +01:00
}
return win;
};
}
},
2019-08-18 10:17:09 +02:00
_openLauncherWindow(url, title, settings) {
2017-01-31 07:50:28 +01:00
const opts = {
2016-03-27 16:47:29 +02:00
show: false,
2016-07-12 07:05:07 +02:00
webPreferences: {
nodeIntegration: false,
webSecurity: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true
2016-03-27 16:47:29 +02:00
}
};
if (settings) {
2017-01-31 07:50:28 +01:00
const settingsObj = {};
2016-07-17 13:30:38 +02:00
settings.split(',').forEach(part => {
2017-01-31 07:50:28 +01:00
const parts = part.split('=');
2016-03-27 16:47:29 +02:00
settingsObj[parts[0].trim()] = parts[1].trim();
});
2019-08-16 23:05:39 +02:00
if (settingsObj.width) {
opts.width = +settingsObj.width;
}
if (settingsObj.height) {
opts.height = +settingsObj.height;
}
if (settingsObj.top) {
opts.y = +settingsObj.top;
}
if (settingsObj.left) {
opts.x = +settingsObj.left;
}
2016-03-27 16:47:29 +02:00
}
2017-01-31 07:50:28 +01:00
let win = Launcher.openWindow(opts);
win.webContents.on('will-redirect', (e, url) => {
if (PopupNotifier.isOwnUrl(url)) {
2016-03-27 16:47:29 +02:00
win.webContents.stop();
win.close();
PopupNotifier.processReturnToApp(url);
2016-03-27 16:47:29 +02:00
}
});
win.webContents.on('will-navigate', (e, url) => {
if (PopupNotifier.isOwnUrl(url)) {
2016-03-27 16:47:29 +02:00
e.preventDefault();
win.close();
PopupNotifier.processReturnToApp(url);
2016-03-27 16:47:29 +02:00
}
});
win.webContents.on('crashed', (e, killed) => {
this.logger.debug('crashed', e, killed);
2018-02-16 19:32:44 +01:00
this.deferCheckClosed(win);
win.close();
win = null;
});
2019-08-18 08:05:38 +02:00
win.webContents.on(
'did-fail-load',
(e, errorCode, errorDescription, validatedUrl, isMainFrame) => {
this.logger.debug(
'did-fail-load',
e,
errorCode,
errorDescription,
validatedUrl,
isMainFrame
);
this.deferCheckClosed(win);
win.close();
win = null;
}
);
win.once('page-title-updated', () => {
setTimeout(() => {
if (win) {
win.show();
win.focus();
}
}, Timeouts.PopupWaitTime);
});
2016-07-17 13:30:38 +02:00
win.on('closed', () => {
2019-08-18 08:05:38 +02:00
setTimeout(
PopupNotifier.triggerClosed.bind(PopupNotifier, win),
Timeouts.CheckWindowClosed
);
win = null;
2016-03-27 16:47:29 +02:00
});
win.loadURL(url);
2016-03-27 16:47:29 +02:00
Backbone.trigger('popup-opened', win);
return win;
},
isOwnUrl(url) {
2019-08-18 08:05:38 +02:00
return (
url.lastIndexOf(Links.WebApp, 0) === 0 ||
url.lastIndexOf(location.origin + location.pathname, 0) === 0
);
},
2019-08-18 10:17:09 +02:00
processReturnToApp(url) {
2017-01-31 07:50:28 +01:00
const returnMessage = AuthReceiver.urlArgsToMessage(url);
2016-03-27 16:47:29 +02:00
if (Object.keys(returnMessage).length > 0) {
2017-01-31 07:50:28 +01:00
const evt = new Event('message');
2016-03-27 16:47:29 +02:00
evt.data = returnMessage;
window.dispatchEvent(evt);
}
},
2019-08-18 10:17:09 +02:00
deferCheckClosed(win) {
2016-03-26 21:12:56 +01:00
setTimeout(PopupNotifier.checkClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
},
2019-08-18 10:17:09 +02:00
checkClosed(win) {
2016-03-26 21:12:56 +01:00
if (win.closed) {
2019-08-18 08:05:38 +02:00
setTimeout(
PopupNotifier.triggerClosed.bind(PopupNotifier, win),
Timeouts.CheckWindowClosed
);
2016-03-26 21:12:56 +01:00
} else {
PopupNotifier.deferCheckClosed(win);
}
2016-03-27 13:54:35 +02:00
},
2019-08-18 10:17:09 +02:00
triggerClosed(win) {
2016-03-27 13:54:35 +02:00
Backbone.trigger('popup-closed', win);
2016-03-26 21:12:56 +01:00
}
};
2019-09-15 14:16:32 +02:00
export { PopupNotifier };