[Service] StopService when appid is empty
[platform/framework/web/wrtjs.git] / wrt_app / service / service_runner.ts
index 0dc0ae6..1955c53 100644 (file)
@@ -19,11 +19,11 @@ function isGlobalService() {
 function printAppControlData(id: string)  {
   let reqAppControl = global.tizen.application.getCurrentApplication().getRequestedAppControl();
   if (reqAppControl) {
-    console.log(`id: ${id}, appControlData operation: ${reqAppControl.appControl.operation}`);
+    console.debug(`id: ${id}, appControlData operation: ${reqAppControl.appControl.operation}`);
     let appControlData = reqAppControl.appControl.data;
     for (let dataIndex in appControlData) {
       for (let valueIndex in appControlData[dataIndex].value)
-        console.log(`data[${dataIndex}][${valueIndex}]: ${appControlData[dataIndex].value[valueIndex]}`);
+        console.debug(`data[${dataIndex}][${valueIndex}]: ${appControlData[dataIndex].value[valueIndex]}`);
     }
   }
 }
@@ -31,7 +31,7 @@ function printAppControlData(id: string)  {
 function registerExtensionResolver(id: string) {
   if (wrt.tv) {
     let extensionResolver = (module: any, file_path: string) => {
-      console.log(`resolved path: ${file_path}`);
+      console.debug(`resolved path: ${file_path}`);
       let content = (wrt.tv as NativeWRTjs.TVExtension).decryptFile(id, file_path);
       if (content) {
         // Remove BOM
@@ -59,7 +59,7 @@ let checkLauncherAlive = (id: string) => {
   if (!periodLauncherAlive) {
     periodLauncherAlive = 20;
     if (!wrt.checkLauncherAlive(id)) {
-      console.log(`${id} launcher was killed.`)
+      console.debug(`${id} launcher was killed.`)
       requestStopService(id);
       checkLauncherAlive = () => {};
     }
@@ -67,21 +67,28 @@ let checkLauncherAlive = (id: string) => {
 }
 
 export function start(id: string, filename: string) {
+  let ids = id.split(':');
+  let serviceId = ids[0];
+  let packageId = wrt.getPackageId(id);
+  if (!packageId) {
+    console.debug(`${id}'s pkgid is empty, so stop service`);
+    requestStopService(id);
+    return;
+  }
   wrt.setServiceAppId(id);
+  wrt.security?.dropThreadPrivilege(packageId, serviceId);
+
   XWalkExtension.initialize();
   XWalkExtension.setRuntimeMessageHandler((type, data) => {
     if (type === 'tizen://exit') {
-      console.log(`${id} will be closed by ${type}`);
+      console.debug(`${id} will be closed by ${type}`);
       requestStopService(id);
     }
   });
 
-  console.log(`serviceType : ${global['serviceType']}`)
+  console.debug(`serviceType : ${global['serviceType']}`)
   new DeviceAPIRouter(id, isGlobalService());
-
-  // this is workaround solution to make webapis singleton worker
-  // ahead of dropThreadPrivilege()
-  global.tizen.systeminfo.getPropertyValue("CPU", () => { }, () => { });
+  printAppControlData(id);
 
   // This is for awaking up uv loop.
   if (isGlobalService()) {
@@ -93,16 +100,10 @@ export function start(id: string, filename: string) {
   if (isServiceApplication()) {
     registerExtensionResolver(id);
     filename = wrt.getStartServiceFile(id);
-    console.log(`start global service file: ${filename}`);
+    console.debug(`start global service file: ${filename}`);
   }
 
   try {
-    let ids = id.split(':');
-    let serviceId = ids[0];
-    let packageId = global.webapis.getPackageId();
-    wrt.security?.dropThreadPrivilege(packageId, serviceId);
-    printAppControlData(id);
-
     app = require(filename);
     if (app.onStart !== undefined) {
       app.onStart();
@@ -110,12 +111,13 @@ export function start(id: string, filename: string) {
     if (app.onRequest !== undefined) {
       app.onRequest();
     }
+  } catch (e) {
+    console.debug(`exception on start: ${e}`);
+    requestStopService(id);
+  } finally {
     if (isGlobalService()) {
       wrt.finishStartingService(id);
     }
-  } catch (e) {
-    console.log(`exception on start: ${e}`);
-    requestStopService(id);
   }
 }
 
@@ -129,22 +131,28 @@ export function stop(id: string) {
       app.onExit();
     }
   } catch (e) {
-    console.log(`exception on stop: ${e}`);
+    console.debug(`exception on stop: ${e}`);
   }
 }
 
 function run() {
   let id = workerData.id;
-  // FIXME: this should be 'wrt.tv?.serviceMount(id)' after Tizen 6.5 release
-  (wrt.tv as any)?.serviceMount(id);
+  if (!id) {
+    console.debug('workerData.id is empty!');
+    process.exit();
+  }
 
+  Object.defineProperty(global, 'internalId', {
+    value: id,
+    writable: false
+  });
+
+  wrt.tv?.serviceMount(id);
   let filename = workerData.filename;
   start(id, filename);
 
-  if (!parentPort)
-    return;
-  parentPort.on('message', (message) => {
-    console.log(`Received message type : ${message.type}`);
+  parentPort?.on('message', (message) => {
+    console.debug(`Received message type : ${message.type}`);
     if (message.type === 'wake') {
       app?.onRequest();
     } else if (message.type === 'stop') {
@@ -152,7 +160,8 @@ function run() {
       setTimeout(() => {
         XWalkExtension.cleanup();
         parentPort?.postMessage("will-terminate");
-        (wrt.tv as any)?.serviceUmount(id);
+        parentPort?.close();
+        wrt.tv?.serviceUmount(id);
       }, message.delay);
     }
   });