[Service] Support requestTrustedRemoteMessagePort 72/267572/6
authorDongHyun Song <dh81.song@samsung.com>
Tue, 7 Dec 2021 07:12:21 +0000 (16:12 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 9 Dec 2021 03:41:09 +0000 (03:41 +0000)
MessagePort API requestTrustedRemoteMessagePort is only able to
communicate apps, which has same certificate.
Thus, wrt-service cannot send the message by this API.
 - 'receive' was already handled by wrt-service-launcher

To support this messaging (service --> trusted app), wrt-service-launcher
will relay the message by creating the local port for wrt-service.

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

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

index 98cbe038c975262e21d5b1444589003fed5326d3..b0a2b6d10e7a5dc0293b22d4636f3882c7db448b 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);
+            });
+        }
+      }
+    }
   }
 }