[Service][Global] Support standalone model 65/228865/13
authorDongHyun Song <dh81.song@samsung.com>
Fri, 10 Apr 2020 07:11:04 +0000 (16:11 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Fri, 10 Apr 2020 10:26:46 +0000 (19:26 +0900)
This patch will support wrt-service is working same as legacy
wrt-service model.

Related path:
  https://review.tizen.org/gerrit/228864/

Change-Id: I0858d177ceadab49ffd7bfcf33dfda69b2230424
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/common/service_manager.js
wrt_app/common/wrt_xwalk_extension.js
wrt_app/service/main.js

index 254900f..a3d1952 100644 (file)
@@ -5,8 +5,7 @@ const vm = require('vm');
 const wrt = require('../browser/wrt');
 
 let sandbox = {};
-let service_source = {};
-let is_global_service = !!wrt.readService;
+let is_global_service = !!wrt.getStartServiceFile;
 
 function callFunctionInContext(name, sandbox) {
   const script = `if (typeof ${name} === 'function') { ${name}(); }`;
@@ -16,7 +15,10 @@ function callFunctionInContext(name, sandbox) {
 function startService(id, filename) {
   if (sandbox[id] === undefined) {
     XWalkExtension.initialize();
-
+    XWalkExtension.setRuntimeMessageHandler((type, data) => {
+      if (type === 'tizen://exit')
+        setTimeout(() => {wrt.stopService(id)}, 500);
+    });
     sandbox[id] = {
       console: console,
       module: new Module,
@@ -31,7 +33,7 @@ function startService(id, filename) {
 
     if (is_global_service) {
       const permissions = wrt.getPrivileges(id);
-      console.log('permissions : ' + permissions);
+      console.log(`permissions : ${permissions}`);
       const AccessControlManager = require('../service/access_control_manager');
       AccessControlManager.initialize(permissions, sandbox[id]);
     }
@@ -60,13 +62,25 @@ function startService(id, filename) {
     let code;
     if (is_global_service) {
       options.filename = id;
-      let service_id = id.substr(0, id.indexOf(":"));
-      if (service_source[service_id] === undefined)
-        service_source[service_id] = wrt.readService(id);
-      code = service_source[service_id];
+      if (wrt.tv) {
+        let extension_resolver = function (module, file_path) {
+          console.log(`resolved path: ${file_path}`);
+          let content = wrt.tv.decryptFile(id, file_path);
+          if (content) {
+            // Remove BOM
+            if (content.charCodeAt(0)  === 0xFEFF)
+              content = content.slice(1);
+            module._compile(content, file_path);
+          }
+        };
+        sandbox[id].require.extensions['.js.spm'] = extension_resolver;
+        sandbox[id].require.extensions['.spm'] = extension_resolver;
+      }
+      let start_service_file = wrt.getStartServiceFile(id);
+      console.log(`start service file: ${start_service_file}`);
+      code = `const app = require('${start_service_file}')`;
     } else {
-      const fs = require('fs');
-      code = fs.readFileSync(filename);
+      code = `const app = require('${filename}')`;
     }
 
     vm.runInNewContext(code, sandbox[id], options);
@@ -75,17 +89,18 @@ function startService(id, filename) {
   if (sandbox[id]['started'] === undefined) {
     sandbox[id]['started'] = true;
     sandbox[id]['stopped'] = undefined;
-    callFunctionInContext('module.exports.onStart', sandbox[id]);
+    callFunctionInContext('app.onStart', sandbox[id]);
     if (is_global_service)
       wrt.finishStartingService(id);
   } else {
     console.log(id + ' service has been started.');
   }
 
-  callFunctionInContext('module.exports.onRequest', sandbox[id]);
+  callFunctionInContext('app.onRequest', sandbox[id]);
 }
 
 function stopService(id) {
+  console.log('stopService')
   if (sandbox[id]['stopped']) {
     console.log(id + ' service has been already stopped.');
     return;
@@ -93,7 +108,7 @@ function stopService(id) {
 
   sandbox[id]['stopped'] = true;
   sandbox[id]['started'] = undefined;
-  callFunctionInContext('module.exports.onStop', sandbox[id]);
+  callFunctionInContext('app.onStop', sandbox[id]);
 
   sandbox[id].timer_manager.releaseRemainingTimers();
   for (let key in sandbox[id])
index ec566a1..5e58a18 100644 (file)
@@ -94,6 +94,10 @@ class XWalkExtension {
     }
   }
 
+  runtimeMessageHandler(type, data, callback) {
+    console.log('This is prototype of runtimeMessageHandler');
+  }
+
   /**
    * @param {Object} ext
    */
@@ -132,13 +136,13 @@ class XWalkExtension {
           return ext.setMessageListener(fn);
         },
         sendRuntimeMessage: function(type, data) {
-          return runtimeMessageHandler(type, data);
+          return XWalkExtension.runtimeMessageHandler(type, data);
         },
         sendRuntimeSyncMessage: function(type, data) {
-          return runtimeMessageHandler(type, data);
+          return XWalkExtension.runtimeMessageHandler(type, data);
         },
         sendRuntimeAsyncMessage: function(type, data, callback) {
-          return runtimeMessageHandler(type, data, callback);
+          return XWalkExtension.runtimeMessageHandler(type, data, callback);
         },
         postData: function(msg, chunk) {
           return ext.postData(msg, chunk);
@@ -213,6 +217,9 @@ const extension_api = {
     if (!instance)
       instance = new XWalkExtension;
   },
+  setRuntimeMessageHandler: (handler) => {
+    XWalkExtension.runtimeMessageHandler = handler;
+  },
   cleanup: () => {
     delete global.tizen;
     instance = undefined;
index 22ea797..2cf534f 100755 (executable)
@@ -27,6 +27,8 @@ wrt.on('start-service', (event, internal_id) => {
 
 wrt.on('stop-service', (event, internal_id) => {
   ServiceManager.stopService(internal_id);
+  if (wrt.getServiceModel() == "STANDALONE")
+    process.exit();
 });
 
 process.on('exit', (code) => {