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[];
20 constructor(id: string, isGlobal: boolean) {
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 = [];
30 this.permissions = wrt.getPrivileges(this.id);
31 this.refineApplicationApis();
32 this.refinePackageApis();
33 this.refineFilesystemApis()
34 this.initAccessControlManager();
35 this.refineXwalkUtilApis();
40 global.webapis = global.webapis ?? {};
42 global.webapis.getCallerAppId = () => {
43 return this.callerAppId;
45 global.webapis.getServiceId = () => {
46 return this.serviceId;
48 let app_info = global.tizen.application.getAppInfo(this.serviceId);
50 this.packageId = app_info.packageId;
52 global.webapis.getPackageId = () => {
53 return this.packageId;
55 this.initEdgeWebapis();
56 this.initProductWebapis();
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);
66 orchestrationReadCapability: (ip: string) => {
67 return edge.orchestrationReadCapability(ip);
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);
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);
75 orchestrationWriteCapability: (json: string) => {
76 return edge.orchestrationWriteCapability(json);
79 Object.defineProperty(global.webapis, 'edge', { writable: false, enumerable: true });
83 initProductWebapis() {
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];
91 let tv = wrt.tv as NativeWRTjs.TVExtension;
92 return (global.webapis.cachedProperty[name] = tv.queryProductValue(name));
94 global.webapis.productinfo = {
96 return getCachedValue('getDuid');
99 return getCachedValue('getFirmware');
102 return getCachedValue('getLocalSet');
105 return getCachedValue('getModel');
107 getModelCode: () => {
108 return getCachedValue('getModelCode');
110 getRealModel: () => {
111 return getCachedValue('getRealModel');
113 getSmartTVServerVersion: () => {
114 return getCachedValue('getSmartTVServerVersion');
116 getSmartTVServerType: () => {
117 return Number(getCachedValue('getSmartTVServerType'));
120 return (getCachedValue('isWallModel') === 'true');
123 return (getCachedValue('isUHDAModel') === 'true');
125 is8KPanelSupported: () => {
126 return (getCachedValue('is8KPanelSupported') === 'true');
130 global.webapis.productinfo.ProductInfoSiServerType = {
131 SI_TYPE_OPERATIING_SERVER: 0,
132 SI_TYPE_DEVELOPMENT_SERVER: 1,
133 SI_TYPE_DEVELOPING_SERVER: 2
138 initAccessControlManager() {
139 console.log(`permissions : ${this.permissions}`);
140 const AccessControlManager = require('./access_control_manager');
141 AccessControlManager.initialize(this.permissions);
145 return global.webapis.getServiceId();
149 return global.webapis.getPackageId();
152 hasNoneOrNull(args: any[]) {
153 return !args.length || null === args[0];
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) {
170 this.currentApplication.appInfo[key] = this.getServiceId();
172 this.currentApplication.appInfo[key] = originCurrentApplication.appInfo[key];
176 if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
177 this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
179 this.currentApplication[key] = originCurrentApplication[key];
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 }
194 Object.defineProperties(this.currentApplication, {
195 appInfo: { enumerable: true },
196 contextId: { writable: false, enumerable: true }
198 // tizen.application.getCurrentApplication().getRequestedAppControl()
199 this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
200 this.currentApplication.getRequestedAppControl = () => {
201 console.log(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
203 wrt.tv.setCurrentApplication(this.getServiceId());
204 return this.funcRequestedAppcontrol();
206 return this.currentApplication;
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);
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);
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 }
238 return this.funcGetContext(args[0]);
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);
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);
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);
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]);
279 return func.apply(global.tizen.filesystem, args);
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);
298 refineXwalkUtilApis() {
299 global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
300 if (!this.permissions.includes(privilege)) {
301 throw 'Permission denied';