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 let app_info = global.tizen.application.getAppInfo(this.serviceId);
45 this.packageId = app_info.packageId;
48 global.webapis = global.webapis ?? {};
49 global.webapis.getCallerAppId = () => {
50 return this.callerAppId;
52 global.webapis.getServiceId = () => {
53 return this.serviceId;
55 global.webapis.getPackageId = () => {
56 return this.packageId;
58 global.webapis.postPlainNotification = (title: string, message: string, timeout?: number) => {
59 return wrt.postPlainNotification(title, message, timeout ?? 10);
61 Object.defineProperties(global.webapis, {
62 getCallerAppId: { writable: false, enumerable: true },
63 getPackageId: { writable: false, enumerable: true },
64 getServiceId: { writable: false, enumerable: true },
65 postPlainNotification: { writable: false, enumerable: true },
67 this.initMDEWebapis();
68 this.initEdgeWebapis();
69 this.initProductWebapis();
73 if (wrt['mde'] && !global.webapis.mde) {
74 let mde = wrt.mde as NativeWRTjs.MDEExtension;
75 global.webapis.mde = {};
77 global.webapis.mde.deInitVirtualEventGenerator = (type: number) => {
78 return mde.deInitVirtualEventGenerator(type);
80 global.webapis.mde.generateVirtualKeyEvent = (keycode: number, state: number) => {
81 return mde.generateVirtualKeyEvent(keycode, state);
83 global.webapis.mde.generateVirtualMouseButtonEvent = (button: number, status: number) => {
84 return mde.generateVirtualMouseButtonEvent(button, status);
86 global.webapis.mde.generateVirtualMouseMoveEvent = (x: number, y: number, move_count: number) => {
87 return mde.generateVirtualMouseMoveEvent(x, y, move_count);
89 global.webapis.mde.initVirtualEventGenerator = (type: number) => {
90 return mde.initVirtualEventGenerator(type);
93 global.webapis.mde.getCurrentLoginId = () => {
94 return mde.getCurrentLoginId();
96 global.webapis.mde.getDeviceName = () => {
97 return mde.getDeviceName();
99 global.webapis.mde.launchBrowserFromUrl = (url: string) => {
100 return mde.launchBrowserFromUrl(url);
102 Object.defineProperty(global.webapis, 'mde', { writable: false, enumerable: true });
107 if ((wrt as any)['edge'] && !global.webapis.edge) {
108 let edge = (wrt as any).edge as any;
109 global.webapis.edge = {
110 orchestrationGetDevicelist: (service_name: string, exec_type: string) => {
111 return edge?.orchestrationGetDevicelist(service_name, exec_type);
113 orchestrationReadCapability: (ip: string) => {
114 return edge?.orchestrationReadCapability(ip);
116 orchestrationRequestService: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string) => {
117 return edge?.orchestrationRequestService(app_name, self_select, exec_type, exec_parameter);
119 orchestrationRequestServiceOnDevice: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string, ip: string) => {
120 return edge?.orchestrationRequestServiceOnDevice(app_name, self_select, exec_type, exec_parameter, ip);
122 orchestrationWriteCapability: (json: string) => {
123 return edge?.orchestrationWriteCapability(json);
126 Object.defineProperty(global.webapis, 'edge', { writable: false, enumerable: true });
130 initProductWebapis() {
132 if (wrt.tv && !global.webapis.productinfo) {
133 global.webapis.cachedProperty = global.webapis.cachedProperty ?? {};
134 let getCachedValue = (name: string) => {
135 if (global.webapis.cachedProperty[name]) {
136 return global.webapis.cachedProperty[name];
138 let tv = wrt.tv as NativeWRTjs.TVExtension;
139 return (global.webapis.cachedProperty[name] = tv.queryProductValue(name));
141 global.webapis.productinfo = {
143 return getCachedValue('getDuid');
146 return getCachedValue('getFirmware');
149 return getCachedValue('getLocalSet');
152 return getCachedValue('getModel');
154 getModelCode: () => {
155 return getCachedValue('getModelCode');
157 getRealModel: () => {
158 return getCachedValue('getRealModel');
160 getSmartTVServerVersion: () => {
161 return getCachedValue('getSmartTVServerVersion');
163 getSmartTVServerType: () => {
164 return Number(getCachedValue('getSmartTVServerType'));
167 return (getCachedValue('isWallModel') === 'true');
170 return (getCachedValue('isUHDAModel') === 'true');
172 is8KPanelSupported: () => {
173 return (getCachedValue('is8KPanelSupported') === 'true');
177 global.webapis.productinfo.ProductInfoSiServerType = {
178 SI_TYPE_OPERATIING_SERVER: 0,
179 SI_TYPE_DEVELOPMENT_SERVER: 1,
180 SI_TYPE_DEVELOPING_SERVER: 2
185 initAccessControlManager() {
186 console.log(`permissions : ${this.permissions}`);
187 const AccessControlManager = require('./access_control_manager');
188 AccessControlManager.initialize(this.packageId, this.serviceId, this.permissions);
192 return global.webapis.getServiceId();
196 return global.webapis.getPackageId();
199 hasNoneOrNull(args: any[]) {
200 return !args.length || null === args[0];
203 refineApplicationApis() {
204 // tizen.application.getCurrentApplication()
205 this.funcCurrentApplication = global.tizen.application.getCurrentApplication;
206 global.tizen.application.getCurrentApplication = () => {
207 console.log(`Routing - getCurrentApplication() : ${this.getServiceId()}`);
208 if (this.currentApplication)
209 return this.currentApplication;
210 this.currentApplication = {};
211 const originCurrentApplication = this.funcCurrentApplication();
212 for (let key in originCurrentApplication) {
213 if (key === 'appInfo') {
214 this.currentApplication.appInfo = {};
215 for (let key in originCurrentApplication.appInfo) {
217 this.currentApplication.appInfo[key] = this.getServiceId();
219 this.currentApplication.appInfo[key] = originCurrentApplication.appInfo[key];
223 if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
224 this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
226 this.currentApplication[key] = originCurrentApplication[key];
230 Object.defineProperties(this.currentApplication.appInfo, {
231 categories: { writable: false, enumerable: true },
232 iconPath: { writable: false, enumerable: true },
233 id: { writable: false, enumerable: true },
234 installDate: { writable: false, enumerable: true },
235 name: { writable: false, enumerable: true },
236 packageId: { writable: false, enumerable: true },
237 show: { writable: false, enumerable: true },
238 size: { enumerable: true },
239 version: { writable: false, enumerable: true }
241 Object.defineProperties(this.currentApplication, {
242 appInfo: { enumerable: true },
243 contextId: { writable: false, enumerable: true }
245 // tizen.application.getCurrentApplication().getRequestedAppControl()
246 this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
247 this.currentApplication.getRequestedAppControl = () => {
248 console.log(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
250 wrt.tv.setCurrentApplication(this.getServiceId());
251 return this.funcRequestedAppcontrol();
253 return this.currentApplication;
255 // tizen.application.getAppInfo()
256 this.funcGetAppInfo = global.tizen.application.getAppInfo;
257 global.tizen.application.getAppInfo = (...args: any[]) => {
258 let app_id = args[0];
259 if (this.hasNoneOrNull(args))
260 app_id = this.getServiceId();
261 console.log(`Routing - getAppInfo(${app_id})`);
262 return this.funcGetAppInfo(app_id);
264 // tizen.application.getAppCerts()
265 this.funcGetAppcerts = global.tizen.application.getAppCerts;
266 global.tizen.application.getAppCerts = (...args: any[]) => {
267 let app_id = args[0];
268 if (this.hasNoneOrNull(args))
269 app_id = this.getServiceId();
270 console.log(`Routing - getAppCerts() ` + app_id);
271 return this.funcGetAppcerts(app_id);
273 // tizen.application.getAppContext()
274 this.funcGetContext = global.tizen.application.getAppContext;
275 global.tizen.application.getAppContext = (...args: any[]) => {
276 console.log(`Routing - getAppContext()`);
277 if (this.hasNoneOrNull(args)) {
278 const context = {"id": this.funcGetContext().id, "appId": this.getServiceId()};
279 Object.defineProperties(context, {
280 appId: { writable: false, enumerable: true },
281 id: { writable: false, enumerable: true }
285 return this.funcGetContext(args[0]);
287 // tizen.application.getAppSharedURI()
288 this.funcGetSharedUri = global.tizen.application.getAppSharedURI;
289 global.tizen.application.getAppSharedURI = (...args: any[]) => {
290 let app_id = args[0];
291 if (this.hasNoneOrNull(args))
292 app_id = this.getServiceId();
293 console.log(`Routing - getAppSharedURI()`);
294 return this.funcGetSharedUri(app_id);
296 // tizen.application.getAppMetaData()
297 this.funcGetMetadata = global.tizen.application.getAppMetaData;
298 global.tizen.application.getAppMetaData = (...args: any[]) => {
299 let app_id = args[0];
300 if (this.hasNoneOrNull(args))
301 app_id = this.getServiceId();
302 console.log(`Routing - getAppMetaData()`);
303 return this.funcGetMetadata(app_id);
307 refinePackageApis() {
308 // tizen.package.getPackageInfo()
309 this.funcGetPackageInfo = global.tizen.package.getPackageInfo;
310 global.tizen.package.getPackageInfo = (...args: any[]) => {
311 let package_id = args[0];
312 if (this.hasNoneOrNull(args))
313 package_id = this.getPackageId();
314 console.log(`Routing - getPackageInfo() : ${package_id}`);
315 return this.funcGetPackageInfo(package_id);
319 injectVirtualRootResolver(func: Function) {
320 return (...args: any[]) => {
321 console.log('arguments : ' + args);
322 if (args.length && !(args[0] === null || args[0] === undefined)) {
323 args[0] = wrt.resolveVirtualRoot(this.getServiceId(), args[0]);
324 console.log('updated argument[0] : ' + args[0]);
326 return func.apply(global.tizen.filesystem, args);
330 refineFilesystemApis() {
331 global.tizen.filesystem.resolve = this.injectVirtualRootResolver(global.tizen.filesystem.resolve);
332 global.tizen.filesystem.listDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.listDirectory);
333 global.tizen.filesystem.createDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.createDirectory);
334 global.tizen.filesystem.deleteDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.deleteDirectory);
335 global.tizen.filesystem.openFile = this.injectVirtualRootResolver(global.tizen.filesystem.openFile);
336 global.tizen.filesystem.deleteFile = this.injectVirtualRootResolver(global.tizen.filesystem.deleteFile);
337 global.tizen.filesystem.moveFile = this.injectVirtualRootResolver(global.tizen.filesystem.moveFile);
338 global.tizen.filesystem.copyFile = this.injectVirtualRootResolver(global.tizen.filesystem.copyFile);
339 global.tizen.filesystem.isFile = this.injectVirtualRootResolver(global.tizen.filesystem.isFile);
340 global.tizen.filesystem.toURI = this.injectVirtualRootResolver(global.tizen.filesystem.toURI);
341 global.tizen.filesystem.isDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.isDirectory);
342 global.tizen.filesystem.pathExists = this.injectVirtualRootResolver(global.tizen.filesystem.pathExists);
345 refineXwalkUtilApis() {
346 global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
347 if (!this.permissions.includes(privilege)) {
348 throw 'Permission denied';
351 global.xwalk.utils.getPkgApiVersion = () => {
352 return this.pkgApiVersion;