[Service] Add edge orchestration interface
[platform/framework/web/wrtjs.git] / wrt_app / service / device_api_router.ts
1 import { wrt } from '../browser/wrt';
2
3 export class DeviceAPIRouter {
4   currentApplication: any;
5   funcCurrentApplication: any;
6   funcRequestedAppcontrol: any;
7   funcGetAppInfo: any;
8   funcGetAppcerts: any;
9   funcGetContext: any;
10   funcGetSharedUri: any;
11   funcGetMetadata: any;
12   funcGetPackageInfo: any;
13
14   id: string;
15   serviceId: string;
16   packageId: string;
17   callerAppId: string;
18   permissions: string[];
19
20   constructor(id: string, isGlobal: boolean) {
21     this.id = id;
22     let ids = id.split(':');
23     this.serviceId = ids[0];
24     this.callerAppId = ids[1] ?? '';
25     this.packageId = this.serviceId.split('.')[0];
26     this.permissions = [];
27
28     this.initWebapis();
29     if (isGlobal) {
30       this.permissions = wrt.getPrivileges(this.id);
31       this.refineApplicationApis();
32       this.refinePackageApis();
33       this.refineFilesystemApis()
34       this.initAccessControlManager();
35       this.refineXwalkUtilApis();
36     }
37   }
38
39   initWebapis() {
40     global.webapis = global.webapis ?? {};
41
42     global.webapis.getCallerAppId = () => {
43       return this.callerAppId;
44     }
45     global.webapis.getServiceId = () => {
46       return this.serviceId;
47     }
48     let app_info = global.tizen.application.getAppInfo(this.serviceId);
49     if (app_info) {
50       this.packageId = app_info.packageId;
51     }
52     global.webapis.getPackageId = () => {
53       return this.packageId;
54     }
55     this.initEdgeWebapis();
56     this.initProductWebapis();
57   }
58
59   initEdgeWebapis() {
60     if (wrt['edge'] && !global.webapis.edge) {
61       let edge = wrt.edge as NativeWRTjs.EdgeExtension;
62       global.webapis.edge = {
63         orchestrationGetDevicelist: (service_name: string, exec_type: string) => {
64           return edge.orchestrationGetDevicelist(service_name, exec_type);
65         },
66         orchestrationReadCapability: (ip: string) => {
67           return edge.orchestrationReadCapability(ip);
68         },
69         orchestrationRequestService: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string) => {
70           return edge.orchestrationRequestService(app_name, self_select, exec_type, exec_parameter);
71         },
72         orchestrationRequestServiceOnDevice: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string, ip: string) => {
73           return edge.orchestrationRequestServiceOnDevice(app_name, self_select, exec_type, exec_parameter, ip);
74         },
75         orchestrationWriteCapability: (json: string) => {
76           return edge.orchestrationWriteCapability(json);
77         },
78       }
79       Object.defineProperty(global.webapis, 'edge', { writable: false, enumerable: true });
80     }
81   }
82
83   initProductWebapis() {
84     // for TV profile
85     if (wrt.tv && !global.webapis.productinfo) {
86       global.webapis.cachedProperty = global.webapis.cachedProperty ?? {};
87       let getCachedValue = (name: string) => {
88         if (global.webapis.cachedProperty[name]) {
89           return global.webapis.cachedProperty[name];
90         }
91         let tv = wrt.tv as NativeWRTjs.TVExtension;
92         return (global.webapis.cachedProperty[name] = tv.queryProductValue(name));
93       }
94       global.webapis.productinfo = {
95         getDuid: () => {
96           return getCachedValue('getDuid');
97         },
98         getFirmware: () => {
99           return getCachedValue('getFirmware');
100         },
101         getLocalSet: () => {
102           return getCachedValue('getLocalSet');
103         },
104         getModel: () => {
105           return getCachedValue('getModel');
106         },
107         getModelCode: () => {
108           return getCachedValue('getModelCode');
109         },
110         getRealModel: () => {
111           return getCachedValue('getRealModel');
112         },
113         getSmartTVServerVersion: () => {
114           return getCachedValue('getSmartTVServerVersion');
115         },
116         getSmartTVServerType: () => {
117           return Number(getCachedValue('getSmartTVServerType'));
118         },
119         isWallModel: () => {
120           return (getCachedValue('isWallModel') === 'true');
121         },
122         isUHDAModel: () => {
123           return (getCachedValue('isUHDAModel') === 'true');
124         },
125         is8KPanelSupported: () => {
126           return (getCachedValue('is8KPanelSupported') === 'true');
127         }
128       };
129
130       global.webapis.productinfo.ProductInfoSiServerType = {
131           SI_TYPE_OPERATIING_SERVER: 0,
132           SI_TYPE_DEVELOPMENT_SERVER: 1,
133           SI_TYPE_DEVELOPING_SERVER: 2
134       };
135     }
136   }
137
138   initAccessControlManager() {
139     console.log(`permissions : ${this.permissions}`);
140     const AccessControlManager = require('./access_control_manager');
141     AccessControlManager.initialize(this.permissions);
142   }
143
144   getServiceId() {
145     return global.webapis.getServiceId();
146   }
147
148   getPackageId() {
149     return global.webapis.getPackageId();
150   }
151
152   hasNoneOrNull(args: any[]) {
153     return !args.length || null === args[0];
154   }
155
156   refineApplicationApis() {
157     // tizen.application.getCurrentApplication()
158     this.funcCurrentApplication = global.tizen.application.getCurrentApplication;
159     global.tizen.application.getCurrentApplication = () => {
160       console.log(`Routing - getCurrentApplication() : ${this.getServiceId()}`);
161       if (this.currentApplication)
162         return this.currentApplication;
163       this.currentApplication = {};
164       const originCurrentApplication = this.funcCurrentApplication();
165       for (let key in originCurrentApplication) {
166         if (key === 'appInfo') {
167           this.currentApplication.appInfo = {};
168           for (let key in originCurrentApplication.appInfo) {
169             if (key === 'id') {
170               this.currentApplication.appInfo[key] = this.getServiceId();
171             } else {
172               this.currentApplication.appInfo[key] = originCurrentApplication.appInfo[key];
173             }
174           }
175         } else {
176           if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
177             this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
178           } else {
179             this.currentApplication[key] = originCurrentApplication[key];
180           }
181         }
182       }
183       Object.defineProperties(this.currentApplication.appInfo, {
184         categories: { writable: false, enumerable: true },
185         iconPath: { writable: false, enumerable: true },
186         id: { writable: false, enumerable: true },
187         installDate: { writable: false, enumerable: true },
188         name: { writable: false, enumerable: true },
189         packageId: { writable: false, enumerable: true },
190         show: { writable: false, enumerable: true },
191         size: { enumerable: true },
192         version: { writable: false, enumerable: true }
193       });
194       Object.defineProperties(this.currentApplication, {
195         appInfo: { enumerable: true },
196         contextId: { writable: false, enumerable: true }
197       });
198       // tizen.application.getCurrentApplication().getRequestedAppControl()
199       this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
200       this.currentApplication.getRequestedAppControl = () => {
201         console.log(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
202         if (wrt.tv)
203           wrt.tv.setCurrentApplication(this.getServiceId());
204         return this.funcRequestedAppcontrol();
205       }
206       return this.currentApplication;
207     }
208     // tizen.application.getAppInfo()
209     this.funcGetAppInfo = global.tizen.application.getAppInfo;
210     global.tizen.application.getAppInfo = (...args: any[]) => {
211       let app_id = args[0];
212       if (this.hasNoneOrNull(args))
213         app_id = this.getServiceId();
214       console.log(`Routing - getAppInfo(${app_id})`);
215       return this.funcGetAppInfo(app_id);
216     }
217     // tizen.application.getAppCerts()
218     this.funcGetAppcerts = global.tizen.application.getAppCerts;
219     global.tizen.application.getAppCerts = (...args: any[]) => {
220       let app_id = args[0];
221       if (this.hasNoneOrNull(args))
222         app_id = this.getServiceId();
223       console.log(`Routing - getAppCerts() ` + app_id);
224       return this.funcGetAppcerts(app_id);
225     }
226     // tizen.application.getAppContext()
227     this.funcGetContext = global.tizen.application.getAppContext;
228     global.tizen.application.getAppContext = (...args: any[]) => {
229       console.log(`Routing - getAppContext()`);
230       if (this.hasNoneOrNull(args)) {
231         const context = {"id": this.funcGetContext().id, "appId": this.getServiceId()};
232         Object.defineProperties(context, {
233           appId: { writable: false, enumerable: true },
234           id: { writable: false, enumerable: true }
235         });
236         return context;
237       }
238       return this.funcGetContext(args[0]);
239     }
240     // tizen.application.getAppSharedURI()
241     this.funcGetSharedUri = global.tizen.application.getAppSharedURI;
242     global.tizen.application.getAppSharedURI = (...args: any[]) => {
243       let app_id = args[0];
244       if (this.hasNoneOrNull(args))
245         app_id = this.getServiceId();
246       console.log(`Routing - getAppSharedURI()`);
247       return this.funcGetSharedUri(app_id);
248     }
249     // tizen.application.getAppMetaData()
250     this.funcGetMetadata = global.tizen.application.getAppMetaData;
251     global.tizen.application.getAppMetaData = (...args: any[]) => {
252       let app_id = args[0];
253       if (this.hasNoneOrNull(args))
254         app_id = this.getServiceId();
255       console.log(`Routing - getAppMetaData()`);
256       return this.funcGetMetadata(app_id);
257     }
258   }
259
260   refinePackageApis() {
261     // tizen.package.getPackageInfo()
262     this.funcGetPackageInfo = global.tizen.package.getPackageInfo;
263     global.tizen.package.getPackageInfo = (...args: any[]) => {
264       let package_id = args[0];
265       if (this.hasNoneOrNull(args))
266         package_id = this.getPackageId();
267       console.log(`Routing - getPackageInfo() : ${package_id}`);
268       return this.funcGetPackageInfo(package_id);
269     }
270   }
271
272   injectVirtualRootResolver(func: Function) {
273     return (...args: any[]) => {
274       console.log('arguments : ' + args);
275       if (args.length && !(args[0] === null || args[0] === undefined)) {
276         args[0] = wrt.resolveVirtualRoot(this.getServiceId(), args[0]);
277         console.log('updated argument[0] : ' + args[0]);
278       }
279       return func.apply(global.tizen.filesystem, args);
280     }
281   }
282
283   refineFilesystemApis() {
284     global.tizen.filesystem.resolve = this.injectVirtualRootResolver(global.tizen.filesystem.resolve);
285     global.tizen.filesystem.listDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.listDirectory);
286     global.tizen.filesystem.createDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.createDirectory);
287     global.tizen.filesystem.deleteDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.deleteDirectory);
288     global.tizen.filesystem.openFile = this.injectVirtualRootResolver(global.tizen.filesystem.openFile);
289     global.tizen.filesystem.deleteFile = this.injectVirtualRootResolver(global.tizen.filesystem.deleteFile);
290     global.tizen.filesystem.moveFile = this.injectVirtualRootResolver(global.tizen.filesystem.moveFile);
291     global.tizen.filesystem.copyFile = this.injectVirtualRootResolver(global.tizen.filesystem.copyFile);
292     global.tizen.filesystem.isFile = this.injectVirtualRootResolver(global.tizen.filesystem.isFile);
293     global.tizen.filesystem.toURI = this.injectVirtualRootResolver(global.tizen.filesystem.toURI);
294     global.tizen.filesystem.isDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.isDirectory);
295     global.tizen.filesystem.pathExists = this.injectVirtualRootResolver(global.tizen.filesystem.pathExists);
296   }
297
298   refineXwalkUtilApis() {
299     global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
300       if (!this.permissions.includes(privilege)) {
301         throw 'Permission denied';
302       }
303     }
304   }
305 }