[Service][VD] Stop service app if service app has exception 51/238551/7
authorDongHyun Song <dh81.song@samsung.com>
Wed, 15 Jul 2020 02:34:20 +0000 (11:34 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Fri, 17 Jul 2020 00:21:58 +0000 (09:21 +0900)
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 <dh81.song@samsung.com>
wrt_app/common/service_manager.ts

index 7552e81..d414bbd 100644 (file)
@@ -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])