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