From 943c25bda3bd3c48c94efd61d85182018292f54d Mon Sep 17 00:00:00 2001 From: DongHyun Song Date: Wed, 2 Dec 2020 17:14:35 +0900 Subject: [PATCH] Refactor app-control event to improve readability 1) Split into small functions by their roles. 2) Move functions to proper class - most of them are located in WebApplication Change-Id: Ibd03eb6b6ef29c197266b7b9d51393134c06c375 Signed-off-by: DongHyun Song --- wrt_app/src/runtime.ts | 127 ++++++++++++++--------------------------- wrt_app/src/web_application.ts | 86 +++++++++++++++++++++++++--- 2 files changed, 120 insertions(+), 93 deletions(-) diff --git a/wrt_app/src/runtime.ts b/wrt_app/src/runtime.ts index 8857abd..8687822 100644 --- a/wrt_app/src/runtime.ts +++ b/wrt_app/src/runtime.ts @@ -80,92 +80,16 @@ class Runtime { wrt.on('app-control', (event: any, appControl: any) => { console.log('app-control'); - let loadInfo = appControl.getLoadInfo(); - let src = loadInfo.getSrc(); - if (wrt.isElectronApp()) { - console.log('Electron App launch'); - const Module = require('module'); - Module.globalPaths.push(wrt.getAppPath()); - let filePath = src[7] === '/' ? src.substr(8) : src.substr(7); // strip "file://" - let pkgJson = require(filePath); - let pos = filePath.lastIndexOf('/'); - - let mainJsPath = (pos !== -1 ? filePath.substr(0, pos + 1) : '') + - (pkgJson.main || 'index.js'); - console.log('loading path:', mainJsPath); - Module._load(mainJsPath, Module, true); - app.emit('ready'); + this.handleAppControlForElectronApp(appControl); + return; + } + console.log('Tizen Web App launch'); + if (!this.webApplication) { + this.createWebApplicationAndLoadUrl(appControl); } else { - console.log('Tizen Web App launch'); - let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode'); - if (!this.webApplication) { - console.log('Creating WebApplication'); - let options: RuntimeOption = { - isAddonAvailable: addonManager.isAddonAvailable(), - launchMode: launchMode - } - this.webApplication = new WebApplication(options); - if (wrt.tv) { - this.inspectorEnabledByVconf = wrt.tv.needUseInspector(); - if (this.inspectorEnabledByVconf && launchMode != 'backgroundExecution') { - this.webApplication.inspectorSrc = src; - src = "about:blank"; - } - } - this.webApplication.mainWindow.loadURL(src); - this.webApplication.prelaunch(src); - if (wrt.da) { - this.webApplication.mainWindow.emit('ready-to-show'); - } - } else { - console.log('Handling app-control event'); - if (this.webApplication.preloadStatus == 'readyToShow') { - this.webApplication.show(); - } else { - if (launchMode != 'backgroundAtStartup') - this.webApplication.preloadStatus = 'none'; - } - - let skipReload = appControl.getData('SkipReload'); - if (skipReload == 'Yes') { - console.log('skipping reload'); - // TODO : Need to care this situation and decide to pass the addon event emitter to resume() - this.webApplication.resume(); - return; - } - - let reload = loadInfo.getReload() || this.webApplication.isAlwaysReload; - if (!reload) { - let originalUrl = this.webApplication.mainWindow.webContents.getURL(); - if (wrt.tv) { - console.log(`appcontrol src = ${src}, original url = ${originalUrl}`); - if (src && originalUrl) { - let appcontrolUrl = (new URL(src)).href; - let oldUrl = (new URL(originalUrl)).href; - console.log(`appcontrolUrl = ${appcontrolUrl}, oldUrl = ${oldUrl}`); - // FIXME(dh81.song) - // Below case it must be distinguishable for known cases - // from 'file:///index.htmlx' to 'file:///index.html' - if (appcontrolUrl !== oldUrl.substr(0, appcontrolUrl.length)) - reload = true; - } else { - reload = true; - } - } else if (src !== originalUrl) { - reload = true; - } - } - // handle http://tizen.org/appcontrol/operation/main operation specially. - // only menu-screen app can send launch request with main operation. - // in this case, web app should have to resume web app not reset. - if (reload && appControl.getOperation() == 'http://tizen.org/appcontrol/operation/main') - reload = false; - if (reload) - this.webApplication.handleAppControlReload(src); - else - this.webApplication.sendAppControlEvent(); - } + console.log('Handling app-control event'); + this.webApplication.handleAppControlEvent(appControl); } this.launchInspector(appControl); }); @@ -225,6 +149,41 @@ class Runtime { wrt.getInstalledPkg(); } + private handleAppControlForElectronApp(appControl: any) { + console.log('Electron App launch'); + let src = appControl.getLoadInfo().getSrc(); + const Module = require('module'); + Module.globalPaths.push(wrt.getAppPath()); + let filePath = src[7] === '/' ? src.substr(8) : src.substr(7); // strip "file://" + let pkgJson = require(filePath); + let pos = filePath.lastIndexOf('/'); + + let mainJsPath = (pos !== -1 ? filePath.substr(0, pos + 1) : '') + + (pkgJson.main || 'index.js'); + console.log('loading path:', mainJsPath); + Module._load(mainJsPath, Module, true); + app.emit('ready'); + } + + private createWebApplicationAndLoadUrl(appControl: any) { + console.log('Creating WebApplication'); + let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode'); + let src = appControl.getLoadInfo().getSrc(); + let options: RuntimeOption = { + isAddonAvailable: addonManager.isAddonAvailable(), + launchMode: launchMode + } + this.webApplication = new WebApplication(options); + if (wrt.tv) { + this.inspectorEnabledByVconf = wrt.tv.needUseInspector(); + if (this.inspectorEnabledByVconf && launchMode != 'backgroundExecution') { + this.webApplication.inspectorSrc = src; + src = "about:blank"; + } + } + this.webApplication.loadUrl(src); + } + private launchInspector(appControl: NativeWRTjs.AppControl) { this.launchInspector = (param) => {}; // call once console.log('launchInspector'); diff --git a/wrt_app/src/web_application.ts b/wrt_app/src/web_application.ts index ce20833..147ff8e 100755 --- a/wrt_app/src/web_application.ts +++ b/wrt_app/src/web_application.ts @@ -27,7 +27,6 @@ export class WebApplication { backgroundExecution: boolean; defaultBackgroundColor: string; defaultTransparent: boolean; - isAlwaysReload: boolean; mainWindow: Electron.BrowserWindow; multitaskingSupport: boolean; notificationPermissionMap?: Map; @@ -60,7 +59,6 @@ export class WebApplication { this.backgroundExecution = false; } this.accessiblePath = wrt.tv?.getAccessiblePath(); - this.isAlwaysReload = (wrt.tv ? wrt.tv.isAlwaysReload() : false); this.multitaskingSupport = (wrt.tv ? wrt.tv.getMultitaskingSupport() : true); this.defaultBackgroundColor = (wrt.tv ? '#0000' : ((wrt.getPlatformType() === "product_wearable") ? '#000' : '#FFF')); @@ -312,13 +310,6 @@ export class WebApplication { return this.backgroundSupport || this.backgroundExecution; } - handleAppControlReload(url: string) { - console.log('WebApplication : handleAppControlReload'); - this.closeWindows(); - this.initDisplayDelay(false); - this.mainWindow.loadURL(url); - } - private suspendByStatus() { if (this.preloadStatus === 'readyToShow' || this.preloadStatus === 'preload' || @@ -354,6 +345,40 @@ Then you can get profile log from the initial loading.`; } } + handleAppControlEvent(appControl: any) { + let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode'); + this.handlePreloadState(launchMode); + + let skipReload = appControl.getData('SkipReload'); + if (skipReload == 'Yes') { + console.log('skipping reload'); + // TODO : Need to care this situation and decide to pass the addon event emitter to resume() + this.resume(); + return; + } + + let loadInfo = appControl.getLoadInfo(); + let src = loadInfo.getSrc(); + let reload = loadInfo.getReload() || this.needReload(src); + // handle http://tizen.org/appcontrol/operation/main operation specially. + // only menu-screen app can send launch request with main operation. + // in this case, web app should have to resume web app not reset. + if (reload && appControl.getOperation() == 'http://tizen.org/appcontrol/operation/main') + reload = false; + if (reload) + this.handleAppControlReload(src); + else + this.sendAppControlEvent(); + } + + loadUrl(src: string) { + this.mainWindow.loadURL(src); + this.prelaunch(src); + if (wrt.da) { + this.mainWindow.emit('ready-to-show'); + } + } + suspend() { if (this.suspended || this.inQuit) return; @@ -413,6 +438,49 @@ Then you can get profile log from the initial loading.`; this.inQuit = true; } + private needReload(src: string) { + let isAlwaysReload = (wrt.tv ? wrt.tv.isAlwaysReload() : false); + if (isAlwaysReload) { + return true; + } + let reload = false; + let originalUrl = this.mainWindow.webContents.getURL(); + if (wrt.tv) { + console.log(`appcontrol src = ${src}, original url = ${originalUrl}`); + if (src && originalUrl) { + let appcontrolUrl = (new URL(src)).href; + let oldUrl = (new URL(originalUrl)).href; + console.log(`appcontrolUrl = ${appcontrolUrl}, oldUrl = ${oldUrl}`); + // FIXME(dh81.song) + // Below case it must be distinguishable for known cases + // from 'file:///index.htmlx' to 'file:///index.html' + if (appcontrolUrl !== oldUrl.substr(0, appcontrolUrl.length)) + reload = true; + } else { + reload = true; + } + } else if (src !== originalUrl) { + reload = true; + } + return reload; + } + + private handleAppControlReload(url: string) { + console.log('WebApplication : handleAppControlReload'); + this.closeWindows(); + this.initDisplayDelay(false); + this.mainWindow.loadURL(url); + } + + private handlePreloadState(launchMode: string) { + if (this.preloadStatus == 'readyToShow') { + this.show(); + } else { + if (launchMode != 'backgroundAtStartup') + this.preloadStatus = 'none'; + } + } + private flushData() { console.log('WebApplication : FlushData'); this.windowList.forEach((window) => window.webContents.session.flushStorageData()); -- 2.7.4