summaryrefslogtreecommitdiffhomepage
path: root/app/shutdown-handler.js
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2018-07-18 15:07:37 +0200
committerAndrej Mihajlov <and@mullvad.net>2018-08-15 17:39:38 +0200
commit71592249b2dd669b6f24f37bfb7b0f4e43b74998 (patch)
treea6097dc7e5d94d06e99c65fdfe160e824395f50c /app/shutdown-handler.js
parente84e87f4ce5a8c242f756566cdc6fb59a45f7bea (diff)
downloadmullvadvpn-71592249b2dd669b6f24f37bfb7b0f4e43b74998.tar.xz
mullvadvpn-71592249b2dd669b6f24f37bfb7b0f4e43b74998.zip
Add workspaces
Diffstat (limited to 'app/shutdown-handler.js')
-rw-r--r--app/shutdown-handler.js93
1 files changed, 0 insertions, 93 deletions
diff --git a/app/shutdown-handler.js b/app/shutdown-handler.js
deleted file mode 100644
index 6bc2d90f46..0000000000
--- a/app/shutdown-handler.js
+++ /dev/null
@@ -1,93 +0,0 @@
-// @flow
-
-import { app, ipcMain, ipcRenderer } from 'electron';
-import { log } from './lib/platform';
-import type { WebContents } from 'electron';
-
-// The timeout before the shutdown is enforced
-const SHUTDOWN_TIMEOUT = 3000;
-
-/**
- * Shutdown coordinator postpones the application shutdown until ShutdownHandler finished the
- * shutdown sequence.
- *
- * ShutdownCoordinator can only be created from main process.
- */
-class ShutdownCoordinator {
- _canQuit = false;
- _isQuitting = false;
- _webContents: WebContents;
- _shutdownTimeout: ?TimeoutID;
-
- constructor(webContents: WebContents) {
- this._webContents = webContents;
-
- app.on('before-quit', this._onBeforeQuit);
- ipcMain.on('app-shutdown-reply', this._onShutdownReply);
- }
-
- dispose() {
- app
- .removeListener('before-quit', this._onBeforeQuit)
- .removeListener('app-shutdown-reply', this._onShutdownReply);
- }
-
- _onBeforeQuit = (event) => {
- if (!this._canQuit) {
- // make sure we don't call the shutdown handler twice
- if (!this._isQuitting) {
- this._isQuitting = true;
-
- // start timer to force shutdown if the renderer process is not able to handle shutdown in
- // a timely manner.
- this._shutdownTimeout = setTimeout(this._onShutdownTimeout, SHUTDOWN_TIMEOUT);
-
- this._webContents.send('app-shutdown');
- }
-
- event.preventDefault();
- }
- };
-
- _onShutdownReply = () => {
- const shutdownTimeout = this._shutdownTimeout;
- if (shutdownTimeout) {
- clearTimeout(shutdownTimeout);
- }
- this._grantShutdown();
- };
-
- _onShutdownTimeout = () => {
- log.warn('It took longer than expected to shutdown. Forcing shutdown now.');
- this._grantShutdown();
- };
-
- _grantShutdown() {
- this._canQuit = true;
- app.quit();
- }
-}
-
-/**
- * Executes the provided closure when application is about to quit.
- * It works in tandem with ShutdownCoordinator and can only be used in renderer process.
- * The shutdown will be postponed until the Promise returned from closure is resolved.
- * ShutdownCoordinator will force the shutdown if the returned Promise is not resolved within
- * the allocated time (see SHUTDOWN_TIMEOUT).
- */
-function setShutdownHandler(handler: () => Promise<void>) {
- ipcRenderer.once('app-shutdown', async (event) => {
- try {
- await handler();
- } catch (error) {
- log.warn(`An error occurred in shutdown handler: ${error.message}`);
- }
- event.sender.send('app-shutdown-reply');
- });
-}
-
-if (ipcMain) {
- module.exports = { ShutdownCoordinator };
-} else {
- module.exports = { setShutdownHandler };
-}