[WRT Service] Provide sandbox context per servcie app and support legacy APIs 09/211909/11
authorYoungsoo Choi <kenshin.choi@samsung.com>
Mon, 12 Aug 2019 02:03:05 +0000 (19:03 -0700)
committerYoungsoo Choi <kenshin.choi@samsung.com>
Fri, 23 Aug 2019 07:12:24 +0000 (07:12 +0000)
This provides sandbox context per service application
and supports onStart and onRequest legacy APIs on single nodejs instance.

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

index 6a99f55..9b1edad 100755 (executable)
 
 const wrt = require('../browser/wrt');
 const vm = require('vm');
+var sandbox = [];
 
-wrt.on('start-service', (event, appID) => {
-  console.log('start service app : ' + appID);
-  let sandbox = { console: console };
-  let options = { filename: appID };
-  vm.runInNewContext(wrt.readService(appID), sandbox, options);
+wrt.on('start-service', (event, app_id) => {
+  console.log('start service app : ' + app_id);
+  if (sandbox[app_id] === undefined) {
+    sandbox[app_id] = {
+      console: console,
+      module: module,
+      require: require,
+    };
+    let options = { filename: app_id };
+    vm.runInNewContext(wrt.readService(app_id), sandbox[app_id], options);
+  }
+  if (sandbox[app_id]['started'] === undefined) {
+    sandbox[app_id]['started'] = true;
+    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]);
   if (!wrt.terminateClient(app_id)) {
     console.log('Failed to terminate client process');
   }