From 57bf583fb6c30ff4428e728bfe47e8e10edcd413 Mon Sep 17 00:00:00 2001 From: jliddev Date: Tue, 22 Nov 2022 01:04:13 -0600 Subject: [PATCH] more wago cleanup --- wowup-electron/app/main.ts | 2 +- wowup-electron/app/wago-handler.ts | 114 +++++++++++++++++++++++++++++ wowup-electron/app/wago.ts | 79 -------------------- 3 files changed, 115 insertions(+), 80 deletions(-) create mode 100644 wowup-electron/app/wago-handler.ts delete mode 100644 wowup-electron/app/wago.ts diff --git a/wowup-electron/app/main.ts b/wowup-electron/app/main.ts index d0c52825..ad556a19 100644 --- a/wowup-electron/app/main.ts +++ b/wowup-electron/app/main.ts @@ -42,7 +42,7 @@ import * as platform from "./platform"; import { initializeDefaultPreferences } from "./preferences"; import { PUSH_NOTIFICATION_EVENT, pushEvents } from "./push"; import { initializeStoreIpcHandlers, preferenceStore } from "./stores"; -import { wagoHandler } from "./wago"; +import { wagoHandler } from "./wago-handler"; import * as windowState from "./window-state"; // LOGGING SETUP diff --git a/wowup-electron/app/wago-handler.ts b/wowup-electron/app/wago-handler.ts new file mode 100644 index 00000000..c32e41ba --- /dev/null +++ b/wowup-electron/app/wago-handler.ts @@ -0,0 +1,114 @@ +import { BrowserWindow, ipcMain, WebContents } from "electron"; +import * as log from "electron-log"; + +class WagoHandler { + private _initialized = false; + private _window: BrowserWindow | undefined = undefined; + private _tokenTimer: ReturnType | undefined = undefined; + private _webContents: WebContents | undefined = undefined; + private _tokenMap = new Map(); + + initialize(window: BrowserWindow): void { + if (this._initialized) { + return; + } + + this._window = window; + + // Just forward the token event out to the window + // this is not a handler, just a passive listener + ipcMain.on("wago-token-received", (evt, token: string) => { + if (token.length < 20) { + log.warn(`[wago-handler] malformed token detected: ${token.length}`); + return; + } + + log.warn("[wago-handler] clearing reload timer"); + this._tokenMap.set(this._webContents?.id ?? 0, true); + this.stopTimeout(); + this._window?.webContents?.send("wago-token-received", token); + }); + } + + initializeWebContents(webContents: WebContents) { + if (this._webContents !== undefined) { + this.removeListeners(this._webContents); + // log.warn("[wago-handler] unable to set webContents, already exists", this._webContents.id, webContents.id); + // return; + } + + this._webContents = webContents; + + webContents.on("did-fail-provisional-load", this.onDidFailProvisionalLoad); + webContents.on("did-fail-load", this.onDidFail); + webContents.on("will-navigate", this.onWillNavigate); + webContents.on("did-finish-load", () => { + log.debug("[wago-handler] did-finish-load", this._tokenMap, webContents.id); + if (this._tokenMap.has(webContents.id)) { + this.stopTimeout(); + } + }); + + // webview allowpopups must be enabled for any link to work + // https://www.electronjs.org/docs/latest/api/webview-tag#allowpopups + webContents.setWindowOpenHandler(this.onWindowOpenHandler); + } + + private stopTimeout() { + clearTimeout(this._tokenTimer); + this._tokenTimer = undefined; + } + + private readonly onDidFailProvisionalLoad = (evt: Electron.Event, code: number, description: string) => { + log.error("[webview] did-fail-provisional-load", code, description); + if (this._webContents !== undefined) { + this.setReloadTime(this._webContents); + } + }; + + private readonly onDidFail = (evt: Electron.Event, code: number, description: string, url: string) => { + log.error("[wago-handler] did-fail-load", code, description, url); + if (this._webContents !== undefined) { + this.setReloadTime(this._webContents); + } + }; + + private readonly onWillNavigate = (evt: Electron.Event, url: string) => { + log.debug("[wago-handler] will-navigate", url); + if (this._webContents !== undefined && this._webContents.getURL() === url) { + log.debug(`[wago-handler] reload detected`); + } else { + evt.preventDefault(); // block the webview from navigating at all + } + }; + + private readonly onWindowOpenHandler = (details: Electron.HandlerDetails): { action: "deny" } => { + log.debug("[webview] setWindowOpenHandler"); + this._window?.webContents.send("webview-new-window", details); // forward this new window to the app for processing + return { action: "deny" }; + }; + + private removeListeners(webContents: WebContents) { + this.stopTimeout(); + webContents.off("did-fail-provisional-load", this.onDidFailProvisionalLoad); + webContents.off("did-fail-load", this.onDidFail); + webContents.off("will-navigate", this.onWillNavigate); + webContents.setWindowOpenHandler((details: Electron.HandlerDetails) => ({ action: "allow" })); + } + + private setReloadTime(webContents: WebContents) { + if (this._tokenMap.has(webContents.id)) { + return; + } + + if (this._tokenTimer === undefined) { + log.warn("[wago-handler] setting reload timer"); + this._tokenTimer = setTimeout(() => { + log.error("[wago-handler] reload"); + webContents.reload(); + }, 5000); + } + } +} + +export const wagoHandler = new WagoHandler(); diff --git a/wowup-electron/app/wago.ts b/wowup-electron/app/wago.ts deleted file mode 100644 index d8790ba5..00000000 --- a/wowup-electron/app/wago.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { BrowserWindow, ipcMain, WebContents } from "electron"; -import * as log from "electron-log"; - -class WagoHandler { - private _initialized = false; - private _window: BrowserWindow | undefined = undefined; - private _tokenTimer: ReturnType | undefined = undefined; - private _webContents: WebContents | undefined = undefined; - - initialize(window: BrowserWindow): void { - if (this._initialized) { - return; - } - - this._window = window; - - // Just forward the token event out to the window - // this is not a handler, just a passive listener - ipcMain.on("wago-token-received", (evt, token: string) => { - if (token.length < 20) { - log.warn(`[wago-handler] malformed token detected: ${token.length}`); - return; - } - - log.warn("[wago-handler] clearing reload timer"); - clearTimeout(this._tokenTimer); - this._tokenTimer = undefined; - this._window?.webContents?.send("wago-token-received", token); - }); - } - - initializeWebContents(webContents: WebContents) { - if (this._webContents !== undefined) { - log.warn("[wago-handler] unable to set webcontents, already exists", this._webContents.id, webContents.id); - return; - } - - this._webContents = webContents; - - webContents.on("did-fail-provisional-load", (evt, errCode, errDesc) => { - log.error("[webview] did-fail-provisional-load", errCode, errDesc); - this.setReloadTime(webContents); - }); - - webContents.on("did-fail-load", (evt, code, desc, url) => { - log.error("[wago-handler] did-fail-load", code, desc, url); - this.setReloadTime(webContents); - }); - - webContents.on("will-navigate", (evt, url) => { - log.debug("[wago-handler] will-navigate", url); - if (webContents.getURL() === url) { - log.debug(`[wago-handler] reload detected`); - } else { - evt.preventDefault(); // block the webview from navigating at all - } - }); - - // webview allowpopups must be enabled for any link to work - // https://www.electronjs.org/docs/latest/api/webview-tag#allowpopups - webContents.setWindowOpenHandler((details) => { - log.debug("[webview] setWindowOpenHandler"); - this._window?.webContents.send("webview-new-window", details); // forward this new window to the app for processing - return { action: "deny" }; - }); - } - - private setReloadTime(webContents: WebContents) { - if (this._tokenTimer === undefined) { - log.warn("[wago-handler] setting reload timer"); - this._tokenTimer = setTimeout(() => { - log.error("[wago-handler] reload"); - webContents.reload(); - }, 5000); - } - } -} - -export const wagoHandler = new WagoHandler();