[Service][Global] Add handler of stop-service 94/218694/7
authorYoungsoo Choi <kenshin.choi@samsung.com>
Wed, 27 Nov 2019 05:29:54 +0000 (21:29 -0800)
committerYoungsoo Choi <kenshin.choi@samsung.com>
Mon, 30 Dec 2019 07:33:13 +0000 (23:33 -0800)
This adds handler of stop-service.

Change-Id: I7c73f9a204844216141edc47ab26e142368ec3d1
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
wrt_app/service/main.js

index 9713ffb..19594ce 100755 (executable)
 const wrt = require('../browser/wrt');
 const vm = require('vm');
 const TizenExtension = require('./tizen_extension');
+
 var sandbox = [];
+var sandbox_count = 0;
 
 wrt.on('start-service', (event, app_id) => {
   console.log('start service app : ' + app_id);
   new TizenExtension();
   if (sandbox[app_id] === undefined) {
+    if (sandbox_count === 0) {
+      new TizenExtension();
+    }
+    sandbox_count++;
     const Module = require('module');
     sandbox[app_id] = {
       console: console,
@@ -57,13 +63,33 @@ wrt.on('start-service', (event, app_id) => {
   }
   if (sandbox[app_id]['started'] === undefined) {
     sandbox[app_id]['started'] = true;
+    sandbox[app_id]['stopped'] = undefined;
     const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
     vm.runInContext(start_callback_string, sandbox[app_id]);
   }
   const request_callback_string = 'if (module.exports.onRequest !== undefined) { module.exports.onRequest(); }';
   vm.runInContext(request_callback_string, sandbox[app_id]);
-})
+});
+
+wrt.on('stop-service', (event, app_id) => {
+  if (sandbox[app_id]['stopped'] === undefined) {
+    sandbox_count--;
+    sandbox[app_id]['stopped'] = true;
+    sandbox[app_id]['started'] = undefined;
+    const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
+    vm.runInContext(stop_callback_string, sandbox[app_id]);
+    for(let key in sandbox[app_id]) {
+      delete sandbox[app_id][key];
+    }
+    delete sandbox[app_id];
+    if (sandbox_count === 0) {
+      tizen = null;
+    }
+  } else {
+    console.log('The global service has been already stopped.');
+  }
+});
 
 process.on('exit', (code) => {
   console.log('Exit with code : ' + code);
-})
+});