From: DongHyun Song Date: Wed, 15 Jul 2020 02:34:20 +0000 (+0900) Subject: [Service][VD] Stop service app if service app has exception X-Git-Tag: submit/tizen/20200723.045512~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=61bd5ec1220f00484d664f543339f9ce195badda;p=platform%2Fframework%2Fweb%2Fwrtjs.git [Service][VD] Stop service app if service app has exception If onRequest has Javascript exception, it cannot execute reamining code. It is not highly able to reach tizen.application...exit(). Thus, with onRequest exception, it is proper that the app is closed automatically. Change-Id: I8e4d2cdc09013f674fb7ddcf40dfbb18ea6a86e7 Signed-off-by: DongHyun Song --- diff --git a/wrt_app/common/service_manager.ts b/wrt_app/common/service_manager.ts index 7552e819..d414bbde 100644 --- a/wrt_app/common/service_manager.ts +++ b/wrt_app/common/service_manager.ts @@ -17,17 +17,30 @@ let sandbox: ContextMap = {}; let internal_handler: ContextOption = {}; let service_type: string = wrt.getServiceModel?.() ?? 'UI'; -function callFunctionInContext(name: string, sandbox: vm.Context) { - const script = `if (typeof ${name} === 'function') { ${name}(); }`; - vm.runInContext(script, sandbox); +function requestStopService(id: string) { + console.log(`${id} will be closed`); + setTimeout(() => wrt.stopService(id), 500); +} + +function callFunctionInContext(name: string, id: string) { + try { + const script = `if (typeof ${name} === 'function') { ${name}(); }`; + vm.runInContext(script, sandbox[id]); + } catch (e) { + console.log(`${name} has exception: ${e}`); + if (wrt.tv) { + requestStopService(id); + } + } } export function startService(id: string, filename?: string) { if (sandbox[id] === undefined) { XWalkExtension.initialize(); XWalkExtension.setRuntimeMessageHandler((type, data) => { - if (type === 'tizen://exit') - setTimeout(() => {wrt.stopService(id)}, 500); + if (type === 'tizen://exit') { + requestStopService(id); + } }); sandbox[id] = { console: console, @@ -36,7 +49,7 @@ export function startService(id: string, filename?: string) { tizen: global.tizen, }; sandbox[id].module.exports.onStop = () => { - callFunctionInContext('module.exports.onExit', sandbox[id]); + callFunctionInContext('module.exports.onExit', id); }; let service_id = id.split(':')[0]; sandbox[id].webapis = wrt.tv ? global.webapis : {}; @@ -106,14 +119,13 @@ export function startService(id: string, filename?: string) { if (sandbox[id]['started'] === undefined) { sandbox[id]['started'] = true; sandbox[id]['stopped'] = undefined; - callFunctionInContext('app.onStart', sandbox[id]); + callFunctionInContext('app.onStart', id); if (service_type !== 'UI') wrt.finishStartingService(id); } else { console.log(id + ' service has been started.'); } - - callFunctionInContext('app.onRequest', sandbox[id]); + callFunctionInContext('app.onRequest', id); } export function stopService(id: string) { @@ -125,7 +137,7 @@ export function stopService(id: string) { sandbox[id]['stopped'] = true; sandbox[id]['started'] = undefined; - callFunctionInContext('app.onStop', sandbox[id]); + callFunctionInContext('app.onStop', id); internal_handler[id].timer_manager.releaseRemainingTimers(); for (let key in sandbox[id])