[Service] Implement routing layer for web deivce API 72/229772/16 submit/tizen/20200709.080654
authorYoungsoo Choi <kenshin@samsung.com>
Tue, 7 Jul 2020 04:55:48 +0000 (13:55 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Wed, 8 Jul 2020 02:58:13 +0000 (11:58 +0900)
This patch introduce an wrapper class to support Tizen/Product
webapis compatibility of global model.

Change-Id: Ibb7171937afd332c00902f819555a0015441159d
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/common/service_manager.ts
wrt_app/service/device_api_router.ts [new file with mode: 0644]

index 65c2e97..9ef1d29 100644 (file)
@@ -3,6 +3,7 @@ import { TimerManager } from '../service/timer_manager';
 import * as XWalkExtension from './wrt_xwalk_extension';
 import * as vm from 'vm';
 import { wrt } from '../browser/wrt';
+import { DeviceAPIRouter } from '../service/device_api_router';
 
 interface ContextMap {
   [id: string]: vm.Context;
@@ -13,7 +14,8 @@ interface ContextOption {
 }
 
 let sandbox: ContextMap = {};
-let is_global_service: boolean = !!wrt.getStartServiceFile;
+let internal_handler: ContextOption = {};
+let service_type: string = wrt.getServiceModel?.() ?? 'UI';
 
 function callFunctionInContext(name: string, sandbox: vm.Context) {
   const script = `if (typeof ${name} === 'function') { ${name}(); }`;
@@ -51,7 +53,7 @@ export function startService(id: string, filename?: string) {
       }
     }
 
-    if (is_global_service) {
+    if (service_type !== 'UI') {
       const permissions = wrt.getPrivileges(id);
       console.log(`permissions : ${permissions}`);
       const AccessControlManager = require('../service/access_control_manager');
@@ -60,8 +62,9 @@ export function startService(id: string, filename?: string) {
     for (let key in global)
       sandbox[id][key] = global[key];
 
-    sandbox[id].timer_manager = new TimerManager();
-    const timer_api = sandbox[id].timer_manager.getTimerAPI();
+    internal_handler[id] = {};
+    internal_handler[id].timer_manager = new TimerManager();
+    const timer_api = internal_handler[id].timer_manager.getTimerAPI();
     for (let key in timer_api)
       sandbox[id][key] = timer_api[key];
 
@@ -77,7 +80,7 @@ export function startService(id: string, filename?: string) {
 
     let options: ContextOption = {};
     let code;
-    if (is_global_service) {
+    if (service_type !== 'UI') {
       options.filename = id;
       if (wrt.tv) {
         let extension_resolver = function (module: any, file_path: string) {
@@ -93,13 +96,13 @@ export function startService(id: string, filename?: string) {
         sandbox[id].require.extensions['.js.spm'] = extension_resolver;
         sandbox[id].require.extensions['.spm'] = extension_resolver;
       }
-      let start_service_file = wrt.getStartServiceFile(id);
-      console.log(`start service file: ${start_service_file}`);
-      code = `const app = require('${start_service_file}')`;
-    } else {
-      code = `const app = require('${filename}')`;
+      filename = wrt.getStartServiceFile(id);
+      console.log(`start global service file: ${filename}`);
+    }
+    code = `const app = require('${filename}')`;
+    if (service_type === 'DAEMON') {
+      internal_handler[id].deivce_api_router = new DeviceAPIRouter(id, sandbox);
     }
-
     vm.runInNewContext(code, sandbox[id], options);
   }
 
@@ -107,7 +110,7 @@ export function startService(id: string, filename?: string) {
     sandbox[id]['started'] = true;
     sandbox[id]['stopped'] = undefined;
     callFunctionInContext('app.onStart', sandbox[id]);
-    if (is_global_service)
+    if (service_type !== 'UI')
       wrt.finishStartingService(id);
   } else {
     console.log(id + ' service has been started.');
@@ -127,10 +130,13 @@ export function stopService(id: string) {
   sandbox[id]['started'] = undefined;
   callFunctionInContext('app.onStop', sandbox[id]);
 
-  sandbox[id].timer_manager.releaseRemainingTimers();
+  internal_handler[id].timer_manager.releaseRemainingTimers();
   for (let key in sandbox[id])
     delete sandbox[id][key];
   delete sandbox[id];
+  for (let key in internal_handler[id])
+    delete internal_handler[id][key];
+  delete internal_handler[id];
 
   if (Object.keys(sandbox).length === 0)
     XWalkExtension.cleanup();
diff --git a/wrt_app/service/device_api_router.ts b/wrt_app/service/device_api_router.ts
new file mode 100644 (file)
index 0000000..cbdb948
--- /dev/null
@@ -0,0 +1,17 @@
+import { wrt } from '../browser/wrt';
+
+export class DeviceAPIRouter {
+  service_id?: string;
+  package_id?: string;
+  current_application?: any;
+
+  constructor(id: string, sandbox: any) {
+    let tizen = global.tizen;
+    this.service_id = id.split(':')[0];
+    let app_info = tizen.application.getAppInfo(this.service_id);
+    if (app_info)
+      this.package_id = app_info.packageId;
+    console.log(`DeviceAPIRouter created for ${this.service_id} ${this.package_id}`);
+    // TODO
+  }
+}
\ No newline at end of file