[Tizen 7.0] Sync up branches from tizen_6.5 to tizen 93/270893/1
authorDongHyun Song <dh81.song@samsung.com>
Fri, 11 Feb 2022 00:47:08 +0000 (09:47 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Fri, 11 Feb 2022 00:48:37 +0000 (09:48 +0900)
[Service] Terminate service apps one by one
https://review.tizen.org/gerrit/#/c/platform/framework/web/wrtjs/+/267413/

[Service] Support requestTrustedRemoteMessagePort
https://review.tizen.org/gerrit/#/c/platform/framework/web/wrtjs/+/267572/

Change-Id: I0ac239a8f731be00f0eabc6b01413c8b0c52362b
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/service/device_api_router.ts
wrt_app/service/service_manager.ts

index 98cbe03..b0a2b6d 100644 (file)
@@ -11,7 +11,6 @@ export class DeviceAPIRouter {
   funcGetMetadata: any;
   funcGetPackageInfo: any;
   funcRequestLocalMessagePort: any;
-  funcRequestTrustedLocalMessagePort: any;
 
   id: string;
   serviceId: string;
@@ -19,6 +18,7 @@ export class DeviceAPIRouter {
   callerAppId: string;
   permissions: string[];
   pkgApiVersion: string;
+  messagePortInfo: any;
 
   constructor(id: string, isGlobal: boolean) {
     this.id = id;
@@ -28,6 +28,7 @@ export class DeviceAPIRouter {
     this.packageId = wrt.getPackageId(id);
     this.permissions = [];
     this.pkgApiVersion = '';
+    this.messagePortInfo = {};
 
     this.initWebapis();
     if (isGlobal) {
@@ -324,19 +325,28 @@ export class DeviceAPIRouter {
   refineMessagePortApis() {
     this.funcRequestLocalMessagePort =
       global.tizen.messageport.requestLocalMessagePort;
-    this.funcRequestTrustedLocalMessagePort =
-      global.tizen.messageport.requestTrustedLocalMessagePort;
 
-    const registerMessagePort = (name: string, portName: string) => {
+    const registerMessagePort = (name: string, portName: string, appId?: string, callback?: any) => {
+      let portInfo = `${name}::${this.serviceId}::${portName}`;
+      if (this.messagePortInfo[portInfo] === 'created') {
+        callback?.();
+        return;
+      }
       let data_payload = [
-        new global.tizen.ApplicationControlData(name, [ portName ]),
-
+        new global.tizen.ApplicationControlData(name, [portName]),
       ];
+      if (appId) {
+        data_payload.push(new global.tizen.ApplicationControlData('appId', [appId]));
+      }
       let appControl = new global.tizen.ApplicationControl(
-          "http://tizen.org/appcontrol/operation/default", null, null, null,
-          data_payload, null);
+        "http://tizen.org/appcontrol/operation/default", null, null, null,
+        data_payload, null);
       global.tizen.application.launchAppControl(appControl, this.serviceId,
-        () => console.debug(`'${this.serviceId}::${name}' is requsted`));
+        () => {
+          console.debug(`'${portInfo}' is requsted`)
+          callback?.();
+          this.messagePortInfo[portInfo] = 'created';
+        });
     }
 
     global.tizen.messageport.requestLocalMessagePort = (portName: string) => {
@@ -347,5 +357,19 @@ export class DeviceAPIRouter {
       registerMessagePort('requestTrustedLocalMessagePort', portName);
       return this.funcRequestLocalMessagePort(portName);
     }
+    global.tizen.messageport.requestTrustedRemoteMessagePort = (appId: string, portName: string) => {
+      return {
+        messagePortName: portName,
+        appId,
+        isTrusted: true,
+        sendMessage: (data: any, localMessagePort?: any) => {
+          registerMessagePort('requestTrustedRemoteMessagePort', portName, appId,
+            () => {
+              let remotePort = global.tizen.messageport.requestRemoteMessagePort(this.serviceId, portName);
+              remotePort.sendMessage(data, localMessagePort);
+            });
+        }
+      }
+    }
   }
 }
index fe3fd77..c639eba 100644 (file)
@@ -6,12 +6,24 @@ interface WorkerMap {
   [id: string]: any;
 }
 let workers: WorkerMap = {};
+let dyingWorkerQueue: WorkerMap = {};
 
 Object.defineProperty(global, 'serviceType', {
   value: wrt.getServiceModel(),
   writable: false
 });
 
+function checkDyingWorker() {
+  let dyingWorkers = Object.keys(dyingWorkerQueue);
+  if (dyingWorkers.length) {
+    let workerId = dyingWorkers[0];
+    if (dyingWorkerQueue[workerId] === 'will-terminate') {
+      dyingWorkerQueue[workerId] = 'terminated';
+      workers[workerId].terminate();
+    }
+  }
+}
+
 function createWorker(id: string, startService: string, filename: string) {
   if (workers[id]) {
     workers[id].postMessage({ type: 'wake' });
@@ -27,14 +39,17 @@ function createWorker(id: string, startService: string, filename: string) {
   });
   workers[id].on('message', (message: string) => {
     if (message === 'will-terminate') {
-      workers[id].terminate();
+      dyingWorkerQueue[id] = message;
+      checkDyingWorker();
     }
   });
   workers[id].on('exit', (code: number) => {
     wrt.tv?.serviceUmount(id);
+    delete dyingWorkerQueue[id];
     delete workers[id];
     let runningServices = Object.keys(workers);
-    console.debug(`exit code(${code}), remain services(${runningServices})`);
+    console.debug(`${id} terminated, remain services(${runningServices})`);
+    checkDyingWorker();
   });
 }