[DeviceHome][VD] Provides webapis.sso APIs 98/282598/12 submit/tizen/20221028.160022
authorDongHyun Song <dh81.song@samsung.com>
Thu, 6 Oct 2022 09:24:55 +0000 (18:24 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 27 Oct 2022 05:02:06 +0000 (05:02 +0000)
This patch introduces 2 sso API.
 - webapis.sso.getGuid(): string;
 - webapis.sso.getOspAccessToken(clientId: string, secret: string,
       resolve: Function, reject?: Function): void;
   . reject is optional

When the 'OspAccessToken' event is emitted from WRT side to the main
thread, then, it propagates the event to the all workers. But the worker
has registered 'OspAccessToken' event can only take it.
Like as this manner, message listener can be expanded for another
purpose later.

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

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

index 7754a63..c00692e 100644 (file)
@@ -1,5 +1,6 @@
 import { wrt } from '../browser/wrt';
 import * as ServiceMessage from '../common/service_message';
+import { parentPort } from 'worker_threads';
 
 export class DeviceAPIRouter {
   currentApplication: any;
@@ -20,6 +21,7 @@ export class DeviceAPIRouter {
   permissions: string[];
   pkgApiVersion: string;
   messagePortInfo: any;
+  messageListeners: any;
 
   constructor(id: string, isGlobal: boolean) {
     this.id = id;
@@ -30,6 +32,7 @@ export class DeviceAPIRouter {
     this.permissions = [];
     this.pkgApiVersion = '';
     this.messagePortInfo = {};
+    this.messageListeners = {};
 
     this.initWebapis();
     if (isGlobal) {
@@ -114,9 +117,11 @@ export class DeviceAPIRouter {
       isRunningApp: { writable: false, enumerable: true },
       postPlainNotification: { writable: false, enumerable: true },
     });
+
     this.initMDEWebapis();
     this.initEdgeWebapis();
     this.initProductWebapis();
+    this.initSSOWebapis();
   }
 
   initMDEWebapis() {
@@ -184,6 +189,34 @@ export class DeviceAPIRouter {
     }
   }
 
+  initSSOWebapis() {
+    if (wrt.tv) {
+      global.webapis.sso = {};
+      global.webapis.sso.getGuid = () => {
+        return wrt.tv?.getSSOGuid() || '';
+      }
+      global.webapis.sso.getOspAccessToken = (clientId: string, secret: string, resolve: Function, reject?: Function) => {
+        parentPort?.postMessage({ type: 'register-message', listener: 'OspAccessToken' });
+        this.messageListeners['OspAccessToken'] = { resolve, reject };
+        return wrt.tv?.getOspAccessToken(clientId, secret) || '';
+      }
+      Object.defineProperties(global.webapis.sso, {
+        getGuid: { writable: false, enumerable: true },
+        getOspAccessToken: { writable: false, enumerable: true },
+      });
+    }
+  }
+
+  notifyMessage(notification: any) {
+    if (!this.messageListeners[notification.listener])
+      return;
+    console.debug(`notification.listener : ${notification.listener}`);
+    if (notification.data && notification.data.indexOf('error_code') > -1)
+      this.messageListeners[notification.listener].reject?.(notification.data);
+    else
+      this.messageListeners[notification.listener].resolve(notification.data);
+  }
+
   initProductWebapis() {
     // for TV profile
     if (wrt.tv && !global.webapis.productinfo) {
index 7552ea9..e1ff243 100755 (executable)
@@ -20,19 +20,24 @@ import '../common/init';
 import { wrt } from '../browser/wrt';
 import * as ServiceManager from './service_manager';
 
-wrt.on('start-service', (event: any, internal_id: string) => {
-  console.debug(`start service app : ${internal_id}`);
-  ServiceManager.startService(internal_id, '');
+wrt.on('start-service', (event: any, internalId: string) => {
+  console.debug(`start service app : ${internalId}`);
+  ServiceManager.startService(internalId, '');
 });
 
-wrt.on('stop-service', (event: any, internal_id: string) => {
-  console.debug(`stop service app : ${internal_id}`);
-  ServiceManager.stopService(internal_id);
+wrt.on('stop-service', (event: any, internalId: string) => {
+  console.debug(`stop service app : ${internalId}`);
+  ServiceManager.stopService(internalId);
 });
 
-wrt.on('builtin-service', (event: any, internal_id: string, service_name: string) => {
-  console.debug(`id: ${internal_id}, service_name: ${service_name}`);
-  ServiceManager.handleBuiltinService(internal_id, service_name);
+wrt.on('builtin-service', (event: any, internalId: string, serviceName: string) => {
+  console.debug(`id: ${internalId}, serviceName: ${serviceName}`);
+  ServiceManager.handleBuiltinService(internalId, serviceName);
+});
+
+wrt.on('message', (event: any, type: string, data: string) => {
+  console.debug(`onMessage : ${type} - ${data}`);
+  ServiceManager.notifyMessage(type, data);
 });
 
 wrt.on('quit', (event: any) => {
index de807eb..a91715b 100644 (file)
@@ -5,8 +5,14 @@ import * as XWalkExtension from '../common/wrt_xwalk_extension';
 interface WorkerMap {
   [id: string]: any;
 }
+
+interface MessageObserver {
+  [id: string]: Set<string>;
+}
+
 let workers: WorkerMap = {};
 let dyingWorkerQueue: WorkerMap = {};
+let messageObservers: MessageObserver = {};
 
 Object.defineProperty(global, 'serviceType', {
   value: wrt.getServiceModel(),
@@ -46,10 +52,21 @@ function createWorker(id: string, startService: string, filename: string) {
       filename
     }
   });
-  workers[id].on('message', (message: string) => {
-    if (message === 'will-terminate') {
-      dyingWorkerQueue[id] = message;
-      checkDyingWorker();
+  workers[id].on('message', (message: any) => {
+    switch (message.type) {
+      case 'will-terminate':
+        delete messageObservers[id];
+        dyingWorkerQueue[id] = 'will-terminate';
+        checkDyingWorker();
+        break;
+      case 'register-message':
+        if (!messageObservers[id]) {
+          messageObservers[id] = new Set();
+        }
+        messageObservers[id].add(message.listener);
+        break;
+      default:
+        break;
     }
   });
   workers[id].on('exit', (code: number) => {
@@ -116,4 +133,16 @@ export function handleBuiltinService(serviceId: string, serviceName: string) {
     let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
     createWorker(serviceId, startService, '');
   }
+}
+
+export function notifyMessage(listener: string, data: string) {
+  for (const id in messageObservers) {
+    if (messageObservers[id].has(listener)) {
+      console.debug(`notify message - ${listener}`);
+      if (workers[id]) {
+        const notification = { listener, data };
+        workers[id].postMessage({ type: 'notify-message', notification });
+      }
+    }
+  }
 }
\ No newline at end of file
index 5f524c6..de15a91 100644 (file)
@@ -54,6 +54,7 @@ let requestStopService = (id: string) => {
 let app: any = null;
 let dummyTimer: any;
 let periodLauncherAlive = 40; // 4s
+let deviceApiRouter: any = null;
 
 let checkLauncherAlive = (id: string) => {
   periodLauncherAlive--;
@@ -88,7 +89,7 @@ export function start(id: string, filename: string) {
   });
 
   console.debug(`serviceType : ${global['serviceType']}`)
-  new DeviceAPIRouter(id, isGlobalService());
+  deviceApiRouter = new DeviceAPIRouter(id, isGlobalService());
   printAppControlData(id);
   ServiceMessage.initConsoleMessageNotification(id);
 
@@ -157,15 +158,23 @@ function run() {
 
   parentPort?.on('message', (message) => {
     console.debug(`Received message type : ${message.type}`);
-    if (message.type === 'wake') {
-      app?.onRequest();
-    } else if (message.type === 'stop') {
-      stop(id);
-      setTimeout(() => {
-        XWalkExtension.cleanup();
-        parentPort?.postMessage("will-terminate");
-        parentPort?.close();
-      }, message.delay);
+    switch (message.type) {
+      case 'wake':
+        app?.onRequest();
+        break;
+      case 'stop':
+        stop(id);
+        setTimeout(() => {
+          XWalkExtension.cleanup();
+          parentPort?.postMessage({ type : 'will-terminate' });
+          parentPort?.close();
+        }, message.delay);
+        break;
+      case 'notify-message':
+        deviceApiRouter?.notifyMessage(message.notification);
+        break;
+      default:
+        break;
     }
   });
 }