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 = this.funcGetAppInfo(this.getServiceId());
216 if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
217 this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
219 this.currentApplication[key] = originCurrentApplication[key];
223 Object.defineProperties(this.currentApplication.appInfo, {
224 categories: { writable: false, enumerable: true },
225 iconPath: { writable: false, enumerable: true },
226 id: { writable: false, enumerable: true },
227 installDate: { writable: false, enumerable: true },
228 name: { writable: false, enumerable: true },
229 packageId: { writable: false, enumerable: true },
230 show: { writable: false, enumerable: true },
231 size: { enumerable: true },
232 version: { writable: false, enumerable: true }
234 Object.defineProperties(this.currentApplication, {
235 appInfo: { enumerable: true },
236 contextId: { writable: false, enumerable: true }
238 // tizen.application.getCurrentApplication().getRequestedAppControl()
239 this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
240 this.currentApplication.getRequestedAppControl = () => {
241 console.log(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
243 wrt.tv.setCurrentApplication(this.getServiceId());
244 return this.funcRequestedAppcontrol();
246 return this.currentApplication;
248 // tizen.application.getAppInfo()
249 this.funcGetAppInfo = global.tizen.application.getAppInfo;
250 global.tizen.application.getAppInfo = (...args: any[]) => {
251 let app_id = args[0];
252 if (this.hasNoneOrNull(args))
253 app_id = this.getServiceId();
254 console.log(`Routing - getAppInfo(${app_id})`);
255 return this.funcGetAppInfo(app_id);
257 // tizen.application.getAppCerts()
258 this.funcGetAppcerts = global.tizen.application.getAppCerts;
259 global.tizen.application.getAppCerts = (...args: any[]) => {
260 let app_id = args[0];
261 if (this.hasNoneOrNull(args))
262 app_id = this.getServiceId();
263 console.log(`Routing - getAppCerts() ` + app_id);
264 return this.funcGetAppcerts(app_id);
266 // tizen.application.getAppContext()
267 this.funcGetContext = global.tizen.application.getAppContext;
268 global.tizen.application.getAppContext = (...args: any[]) => {
269 console.log(`Routing - getAppContext()`);
270 if (this.hasNoneOrNull(args)) {
271 const context = {"id": this.funcGetContext().id, "appId": this.getServiceId()};
272 Object.defineProperties(context, {
273 appId: { writable: false, enumerable: true },
274 id: { writable: false, enumerable: true }
278 return this.funcGetContext(args[0]);
280 // tizen.application.getAppSharedURI()
281 this.funcGetSharedUri = global.tizen.application.getAppSharedURI;
282 global.tizen.application.getAppSharedURI = (...args: any[]) => {
283 let app_id = args[0];
284 if (this.hasNoneOrNull(args))
285 app_id = this.getServiceId();
286 console.log(`Routing - getAppSharedURI()`);
287 return this.funcGetSharedUri(app_id);
289 // tizen.application.getAppMetaData()
290 this.funcGetMetadata = global.tizen.application.getAppMetaData;
291 global.tizen.application.getAppMetaData = (...args: any[]) => {
292 let app_id = args[0];
293 if (this.hasNoneOrNull(args))
294 app_id = this.getServiceId();
295 console.log(`Routing - getAppMetaData()`);
296 return this.funcGetMetadata(app_id);
300 refinePackageApis() {
301 // tizen.package.getPackageInfo()
302 this.funcGetPackageInfo = global.tizen.package.getPackageInfo;
303 global.tizen.package.getPackageInfo = (...args: any[]) => {
304 let package_id = args[0];
305 if (this.hasNoneOrNull(args))
306 package_id = this.getPackageId();
307 console.log(`Routing - getPackageInfo() : ${package_id}`);
308 return this.funcGetPackageInfo(package_id);
312 injectVirtualRootResolver(func: Function) {
313 return (...args: any[]) => {
314 if (args.length && !(args[0] === null || args[0] === undefined)) {
315 args[0] = wrt.resolveVirtualRoot(this.getServiceId(), args[0]);
316 console.log('updated argument[0] : ' + args[0]);
318 return func.apply(global.tizen.filesystem, args);
322 refineFilesystemApis() {
323 global.tizen.filesystem.resolve = this.injectVirtualRootResolver(global.tizen.filesystem.resolve);
324 global.tizen.filesystem.listDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.listDirectory);
325 global.tizen.filesystem.createDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.createDirectory);
326 global.tizen.filesystem.deleteDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.deleteDirectory);
327 global.tizen.filesystem.openFile = this.injectVirtualRootResolver(global.tizen.filesystem.openFile);
328 global.tizen.filesystem.deleteFile = this.injectVirtualRootResolver(global.tizen.filesystem.deleteFile);
329 global.tizen.filesystem.moveFile = this.injectVirtualRootResolver(global.tizen.filesystem.moveFile);
330 global.tizen.filesystem.copyFile = this.injectVirtualRootResolver(global.tizen.filesystem.copyFile);
331 global.tizen.filesystem.isFile = this.injectVirtualRootResolver(global.tizen.filesystem.isFile);
332 global.tizen.filesystem.toURI = this.injectVirtualRootResolver(global.tizen.filesystem.toURI);
333 global.tizen.filesystem.isDirectory = this.injectVirtualRootResolver(global.tizen.filesystem.isDirectory);
334 global.tizen.filesystem.pathExists = this.injectVirtualRootResolver(global.tizen.filesystem.pathExists);
337 refineXwalkUtilApis() {
338 global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
339 if (!this.permissions.includes(privilege)) {
340 throw 'Permission denied';
343 global.xwalk.utils.getPkgApiVersion = () => {
344 return this.pkgApiVersion;