From 7771ea5b412b4fefc0af113b5f31cc61b388d5b0 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Thu, 6 Dec 2018 10:33:23 +0530 Subject: [PATCH] Typescript - Add login to close screen snippet --- src/browser/main-api-handler.ts | 13 ++++++++++--- src/browser/screen-snippet.ts | 13 +++++++++++-- src/browser/window-utils.ts | 24 ++++++++++++++++++++++++ src/renderer/ssf-api.ts | 16 +++++++++++++++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/browser/main-api-handler.ts b/src/browser/main-api-handler.ts index f9dee45c..1568ff40 100644 --- a/src/browser/main-api-handler.ts +++ b/src/browser/main-api-handler.ts @@ -5,7 +5,14 @@ import { LocaleType } from '../common/i18n'; import { logger } from '../common/logger'; import { activityDetection } from './activity-detection'; import { screenSnippet } from './screen-snippet'; -import { isValidWindow, setDataUrl, showBadgeCount, showPopupMenu, updateLocale } from './window-utils'; +import { + isValidWindow, + sanitize, + setDataUrl, + showBadgeCount, + showPopupMenu, + updateLocale, +} from './window-utils'; /** * Handle API related ipc messages from renderers. Only messages from windows @@ -64,12 +71,12 @@ ipcMain.on(apiName.symphonyApi, (event: Electron.Event, arg: IApiArgs) => { configureNotification.openConfigurationWindow(arg.windowName); } break; - case ApiCmds.sanitize: + */case apiCmds.sanitize: if (typeof arg.windowName === 'string') { sanitize(arg.windowName); } break; - case ApiCmds.bringToFront: + /*case ApiCmds.bringToFront: // validates the user bring to front config and activates the wrapper if (typeof arg.reason === 'string' && arg.reason === 'notification') { bringToFront(arg.windowName, arg.reason); diff --git a/src/browser/screen-snippet.ts b/src/browser/screen-snippet.ts index 69e71fcb..4afcd1fe 100644 --- a/src/browser/screen-snippet.ts +++ b/src/browser/screen-snippet.ts @@ -55,7 +55,7 @@ class ScreenSnippet { // only allow one screen capture at a time. if (this.child) this.child.kill(); try { - this.child = await this.execCmd(this.captureUtil, this.captureUtilArgs); + await this.execCmd(this.captureUtil, this.captureUtilArgs); const { message, data, type } = await this.convertFileToData(); webContents.send('screen-snippet-data', { message, data, type }); } catch (error) { @@ -63,6 +63,15 @@ class ScreenSnippet { } } + /** + * Kills the child process when the application is reloaded + */ + public killChildProcess(): void { + if (this.child && typeof this.child.kill === 'function') { + this.child.kill(); + } + } + /** * Executes the given command via a child process * @@ -76,7 +85,7 @@ class ScreenSnippet { */ private execCmd(captureUtil: string, captureUtilArgs: ReadonlyArray): Promise { return new Promise((resolve, reject) => { - return execFile(captureUtil, captureUtilArgs, (error: ExecException | null) => { + return this.child = execFile(captureUtil, captureUtilArgs, (error: ExecException | null) => { if (this.isAlwaysOnTop) updateAlwaysOnTop(true, false); if (error && error.killed) { // processs was killed, just resolve with no data. diff --git a/src/browser/window-utils.ts b/src/browser/window-utils.ts index fbbbff96..b6e77746 100644 --- a/src/browser/window-utils.ts +++ b/src/browser/window-utils.ts @@ -5,6 +5,7 @@ import * as url from 'url'; import { isMac, isWindowsOS } from '../common/env'; import { i18n, LocaleType } from '../common/i18n'; import { logger } from '../common/logger'; +import { screenSnippet } from './screen-snippet'; import { ICustomBrowserWindow, windowHandler } from './window-handler'; const checkValidWindow = true; @@ -217,4 +218,27 @@ export const showPopupMenu = (opts: Electron.PopupOptions): void => { const appMenu = windowHandler.appMenu; if (appMenu) appMenu.popupMenu({ ...popupOpts, ...opts }); } +}; + +/** + * Method that is invoked when the application is reloaded/navigated + * window.addEventListener('beforeunload') + * + * @param windowName {string} + */ +export const sanitize = (windowName: string): void => { + // To make sure the reload event is from the main window + const mainWindow = windowHandler.getMainWindow(); + if (mainWindow && windowName === mainWindow.winName) { + // reset the badge count whenever an user refreshes the electron client + showBadgeCount(0); + + // Terminates the screen snippet process on reload + if (!isMac) { + screenSnippet.killChildProcess(); + } + // TODO: write method to clean up child window + // Closes all the child windows + // windowMgr.cleanUpChildWindows(); + } }; \ No newline at end of file diff --git a/src/renderer/ssf-api.ts b/src/renderer/ssf-api.ts index c6716f6a..5edb3cc9 100644 --- a/src/renderer/ssf-api.ts +++ b/src/renderer/ssf-api.ts @@ -150,4 +150,18 @@ ipcRenderer.on('activity', (_event: Event, arg: IActivityDetection) => { if (typeof arg === 'object' && typeof local.activityDetection === 'function') { local.activityDetection(arg); } -}); \ No newline at end of file +}); + +// Invoked whenever the app is reloaded/navigated +const sanitize = (): void => { + local.ipcRenderer.send(apiName, { + cmd: apiCmds.sanitize, + windowName: window.name || 'main', + }); +}; + +/** + * Window Events + */ + +window.addEventListener('beforeunload', sanitize, false); \ No newline at end of file