[Service] Support bi-direction message-port 05/283805/4 submit/tizen/20221108.160031
authorDongHyun Song <dh81.song@samsung.com>
Thu, 3 Nov 2022 05:45:47 +0000 (14:45 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Fri, 4 Nov 2022 07:19:09 +0000 (16:19 +0900)
From the bundle data, if there is the information of actual remote port,
then it make RemoteMessagePort object for the 2nd parameter of message-
port callback.

Related chromium-efl patch:
https://review.tizen.org/gerrit/#/c/platform/framework/web/chromium-efl/+/283803/

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

index c00692e..16cfa9e 100644 (file)
@@ -441,13 +441,45 @@ export class DeviceAPIRouter {
         });
     }
 
+    const makeMessagePort = (portName: string, isTrusted: boolean) => {
+      const messagePort = this.funcRequestLocalMessagePort(portName);
+      return {
+        messagePortName: portName,
+        isTrusted,
+        addMessagePortListener: (listener: any) => {
+          const listenerWrapper = (message: any) => {
+            let remoteAppId = '';
+            let remotePort = '';
+            message.forEach((bundle: any) => {
+              if (bundle.key === 'org_remote_app_id') {
+                remoteAppId = bundle.value;
+              } else if (bundle.key === 'org_remote_port') {
+                remotePort = bundle.value;
+              }
+            });
+            message = message.filter((bundle: any) => {
+              return !bundle.key.includes('org_remote');
+            });
+            let remoteMessagePort = null;
+            if (remoteAppId && remotePort)
+              remoteMessagePort = global.tizen.messageport.requestRemoteMessagePort(remoteAppId, remotePort);
+            return listener(message, remoteMessagePort);
+          }
+          return messagePort.addMessagePortListener(listenerWrapper);
+        },
+        removeMessagePortListener: (watchId: any) => {
+          return messagePort.removeMessagePortListener(watchId);
+        }
+      }
+    }
+
     global.tizen.messageport.requestLocalMessagePort = (portName: string) => {
       registerMessagePort('requestLocalMessagePort', portName);
-      return this.funcRequestLocalMessagePort(portName);
+      return makeMessagePort(portName, false);
     }
     global.tizen.messageport.requestTrustedLocalMessagePort = (portName: string) => {
       registerMessagePort('requestTrustedLocalMessagePort', portName);
-      return this.funcRequestLocalMessagePort(portName);
+      return makeMessagePort(portName, true);
     }
     global.tizen.messageport.requestTrustedRemoteMessagePort = (appId: string, portName: string) => {
       return {