1 import { wrt } from '../browser/wrt';
3 export class DeviceAPIRouter {
4 currentApplication: any;
5 funcCurrentApplication: any;
6 funcRequestedAppcontrol: any;
10 funcGetSharedUri: any;
12 funcGetPackageInfo: any;
18 permissions: string[];
19 pkgApiVersion: string;
21 constructor(id: string, isGlobal: boolean) {
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 = '';
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();
43 global.webapis = global.webapis ?? {};
45 global.webapis.getCallerAppId = () => {
46 return this.callerAppId;
48 global.webapis.getServiceId = () => {
49 return this.serviceId;
51 let app_info = global.tizen.application.getAppInfo(this.serviceId);
53 this.packageId = app_info.packageId;
55 global.webapis.getPackageId = () => {
56 return this.packageId;
58 this.initProductWebapis();
61 initProductWebapis() {
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];
69 let tv = wrt.tv as NativeWRTjs.TVExtension;
70 return (global.webapis.cachedProperty[name] = tv.queryProductValue(name));
72 global.webapis.productinfo = {
74 return getCachedValue('getDuid');
77 return getCachedValue('getFirmware');
80 return getCachedValue('getLocalSet');
83 return getCachedValue('getModel');
86 return getCachedValue('getModelCode');
89 return getCachedValue('getRealModel');
91 getSmartTVServerVersion: () => {
92 return getCachedValue('getSmartTVServerVersion');
94 getSmartTVServerType: () => {
95 return Number(getCachedValue('getSmartTVServerType'));
98 return (getCachedValue('isWallModel') === 'true');
101 return (getCachedValue('isUHDAModel') === 'true');
103 is8KPanelSupported: () => {
104 return (getCachedValue('is8KPanelSupported') === 'true');
108 global.webapis.productinfo.ProductInfoSiServerType = {
109 SI_TYPE_OPERATIING_SERVER: 0,
110 SI_TYPE_DEVELOPMENT_SERVER: 1,
111 SI_TYPE_DEVELOPING_SERVER: 2
116 initAccessControlManager() {
117 console.log(`permissions : ${this.permissions}`);
118 const AccessControlManager = require('./access_control_manager');
119 AccessControlManager.initialize(this.permissions);
123 return global.webapis.getServiceId();
127 return global.webapis.getPackageId();
130 hasNoneOrNull(args: any[]) {
131 return !args.length || null === args[0];
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) {
148 this.currentApplication.appInfo[key] = this.getServiceId();
150 this.currentApplication.appInfo[key] = originCurrentApplication.appInfo[key];
154 if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
155 this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
157 this.currentApplication[key] = originCurrentApplication[key];
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 }
172 Object.defineProperties(this.currentApplication, {
173 appInfo: { enumerable: true },
174 contextId: { writable: false, enumerable: true }
176 // tizen.application.getCurrentApplication().getRequestedAppControl()
177 this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
178 this.currentApplication.getRequestedAppControl = () => {
179 console.log(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
181 wrt.tv.setCurrentApplication(this.getServiceId());
182 return this.funcRequestedAppcontrol();
184 return this.currentApplication;
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);
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);
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 }
216 return this.funcGetContext(args[0]);
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);
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);
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);
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]);
257 return func.apply(global.tizen.filesystem, args);
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);
276 refineXwalkUtilApis() {
277 global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
278 if (!this.permissions.includes(privilege)) {
279 throw 'Permission denied';
282 global.xwalk.utils.getPkgApiVersion = () => {
283 return this.pkgApiVersion;