From 679e34719546f3c7d9a32c74ce64d039e2ce2d3f Mon Sep 17 00:00:00 2001 From: Youngsoo Choi Date: Wed, 25 Sep 2019 23:46:45 -0700 Subject: [PATCH] [Service][UI] Provide sandbox context for each ui servcie app 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 --- wrt_app/src/runtime.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/wrt_app/src/runtime.js b/wrt_app/src/runtime.js index 2fb132b..f39e244 100755 --- a/wrt_app/src/runtime.js +++ b/wrt_app/src/runtime.js @@ -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(); } }); -- 2.7.4