[Service][UI] Provide sandbox context for each ui servcie app 73/214773/9 accepted/tizen/unified/20191002.013355 submit/tizen/20191001.060134
authorYoungsoo Choi <kenshin.choi@samsung.com>
Thu, 26 Sep 2019 06:46:45 +0000 (23:46 -0700)
committerYoungsoo Choi <kenshin.choi@samsung.com>
Tue, 1 Oct 2019 03:47:59 +0000 (20:47 -0700)
This provides sandbox context for each ui service application
and supports startService handler, onStart, and onStop APIs.

Change-Id: I72291fe162ab6966deabc20ae7ded77f0d8f6f08
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
wrt_app/src/runtime.js

index 2fb132b..f39e244 100755 (executable)
@@ -30,6 +30,7 @@ class Runtime {
         this.addonManager = null;
         this.isLaunched = false;
         this.inspectorEnabledByVconf = false;
+        this.sandbox = [];
         this.webContents = null;
 
         var _this = this;
@@ -178,17 +179,39 @@ class Runtime {
         });
         wrt.on('message', function(event, type, params) {
             console.log('message type(' + type + ') params : ' + params);
+            const app_id = params[0];
+            const vm = require('vm');
             if (type === 'startService') {
-                const vm = require('vm');
-                const fs = require('fs');
-                let sandbox = {
-                    console: console,
-                    module: module,
-                    require: require,
-                };
-                let options = {};
-                let code = fs.readFileSync(params[1]);
-                vm.runInNewContext(code, sandbox, options);
+                if (_this.sandbox[app_id] === undefined) {
+                    const fs = require('fs');
+                    _this.sandbox[app_id] = {
+                        console: console,
+                        module: module,
+                        require: require,
+                    };
+                    let options = {};
+                    let code = fs.readFileSync(params[1]);
+                    vm.runInNewContext(code, _this.sandbox[app_id], options);
+                }
+                if (_this.sandbox[app_id]['started'] === undefined) {
+                    _this.sandbox[app_id]['started'] = true;
+                    _this.sandbox[app_id]['stopped'] = undefined;
+                    const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
+                    vm.runInContext(start_callback_string, _this.sandbox[app_id]);
+                } else {
+                    console.log('UI service has been started.');
+                }
+                event.preventDefault();
+            } else if (type === 'stopService') {
+                if (_this.sandbox[app_id]['stopped'] === undefined) {
+                    _this.sandbox[app_id]['stopped'] = true;
+                    _this.sandbox[app_id]['started'] = undefined;
+                    const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
+                    vm.runInContext(stop_callback_string, _this.sandbox[app_id]);
+                    _this.sandbox[app_id] = undefined;
+                } else {
+                    console.log('UI service has been stopped.');
+                }
                 event.preventDefault();
             }
         });