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;
13 funcRequestLocalMessagePort: any;
14 funcRequestTrustedLocalMessagePort: any;
20 permissions: string[];
21 pkgApiVersion: string;
23 constructor(id: string, isGlobal: boolean) {
25 let ids = id.split(':');
26 this.serviceId = ids[0];
27 this.callerAppId = ids[1] ?? '';
28 this.packageId = wrt.getPackageId(id);
29 this.permissions = [];
30 this.pkgApiVersion = '';
34 this.permissions = wrt.getPrivileges(this.id);
35 this.pkgApiVersion = wrt.getPkgApiVersion(this.id);
36 this.refineApplicationApis();
37 this.refinePackageApis();
38 this.refineFilesystemApis()
39 this.refineMessagePortApis()
40 this.refineXwalkUtilApis();
45 global.webapis = global.webapis ?? {};
46 global.webapis.getCallerAppId = () => {
47 return this.callerAppId;
49 global.webapis.getServiceId = () => {
50 return this.serviceId;
52 global.webapis.getPackageId = () => {
53 return this.packageId;
55 global.webapis.postPlainNotification = (title: string, message: string, timeout?: number) => {
56 return wrt.postPlainNotification(title, message, timeout ?? 10);
58 Object.defineProperties(global.webapis, {
59 getCallerAppId: { writable: false, enumerable: true },
60 getPackageId: { writable: false, enumerable: true },
61 getServiceId: { writable: false, enumerable: true },
62 postPlainNotification: { writable: false, enumerable: true },
64 this.initMDEWebapis();
65 this.initEdgeWebapis();
66 this.initProductWebapis();
70 if (wrt['mde'] && !global.webapis.mde) {
71 let mde = wrt.mde as NativeWRTjs.MDEExtension;
72 global.webapis.mde = {};
74 global.webapis.mde.deInitVirtualEventGenerator = (type: number) => {
75 return mde.deInitVirtualEventGenerator(type);
77 global.webapis.mde.generateVirtualKeyEvent = (keycode: number, state: number) => {
78 return mde.generateVirtualKeyEvent(keycode, state);
80 global.webapis.mde.generateVirtualMouseButtonEvent = (button: number, status: number) => {
81 return mde.generateVirtualMouseButtonEvent(button, status);
83 global.webapis.mde.generateVirtualMouseMoveEvent = (x: number, y: number, move_count: number) => {
84 return mde.generateVirtualMouseMoveEvent(x, y, move_count);
86 global.webapis.mde.initVirtualEventGenerator = (type: number) => {
87 return mde.initVirtualEventGenerator(type);
90 global.webapis.mde.getCurrentLoginId = () => {
91 return mde.getCurrentLoginId();
93 global.webapis.mde.getDeviceName = () => {
94 return mde.getDeviceName();
96 global.webapis.mde.launchBrowserFromUrl = (url: string) => {
97 return mde.launchBrowserFromUrl(url);
99 Object.defineProperty(global.webapis, 'mde', { writable: false, enumerable: true });
104 if ((wrt as any)['edge'] && !global.webapis.edge) {
105 let edge = (wrt as any).edge as any;
106 global.webapis.edge = {
107 orchestrationGetDevicelist: (service_name: string, exec_type: string) => {
108 return edge?.orchestrationGetDevicelist(service_name, exec_type);
110 orchestrationReadCapability: (ip: string) => {
111 return edge?.orchestrationReadCapability(ip);
113 orchestrationRequestService: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string) => {
114 return edge?.orchestrationRequestService(app_name, self_select, exec_type, exec_parameter);
116 orchestrationRequestServiceOnDevice: (app_name: string, self_select: boolean, exec_type: string, exec_parameter: string, ip: string) => {
117 return edge?.orchestrationRequestServiceOnDevice(app_name, self_select, exec_type, exec_parameter, ip);
119 orchestrationWriteCapability: (json: string) => {
120 return edge?.orchestrationWriteCapability(json);
123 Object.defineProperty(global.webapis, 'edge', { writable: false, enumerable: true });
127 initProductWebapis() {
129 if (wrt.tv && !global.webapis.productinfo) {
130 global.webapis.cachedProperty = global.webapis.cachedProperty ?? {};
131 let getCachedValue = (name: string) => {
132 if (global.webapis.cachedProperty[name]) {
133 return global.webapis.cachedProperty[name];
135 let tv = wrt.tv as NativeWRTjs.TVExtension;
136 return (global.webapis.cachedProperty[name] = tv.queryProductValue(name));
138 global.webapis.productinfo = {
140 return getCachedValue('getDuid');
143 return getCachedValue('getFirmware');
146 return getCachedValue('getLocalSet');
149 return getCachedValue('getModel');
151 getModelCode: () => {
152 return getCachedValue('getModelCode');
154 getRealModel: () => {
155 return getCachedValue('getRealModel');
157 getSmartTVServerVersion: () => {
158 return getCachedValue('getSmartTVServerVersion');
160 getSmartTVServerType: () => {
161 return Number(getCachedValue('getSmartTVServerType'));
164 return (getCachedValue('isWallModel') === 'true');
167 return (getCachedValue('isUHDAModel') === 'true');
169 is8KPanelSupported: () => {
170 return (getCachedValue('is8KPanelSupported') === 'true');
174 global.webapis.productinfo.ProductInfoSiServerType = {
175 SI_TYPE_OPERATIING_SERVER: 0,
176 SI_TYPE_DEVELOPMENT_SERVER: 1,
177 SI_TYPE_DEVELOPING_SERVER: 2
183 return global.webapis.getServiceId();
187 return global.webapis.getPackageId();
190 hasNoneOrNull(args: any[]) {
191 return !args.length || null === args[0];
194 refineApplicationApis() {
195 // tizen.application.getCurrentApplication()
196 this.funcCurrentApplication = global.tizen.application.getCurrentApplication;
197 global.tizen.application.getCurrentApplication = () => {
198 console.debug(`Routing - getCurrentApplication() : ${this.getServiceId()}`);
199 if (this.currentApplication)
200 return this.currentApplication;
201 this.currentApplication = {};
202 const originCurrentApplication = this.funcCurrentApplication();
203 for (let key in originCurrentApplication) {
204 if (key === 'appInfo') {
205 this.currentApplication.appInfo = this.funcGetAppInfo(this.getServiceId());
207 if (key === 'broadcastEvent' || key === 'broadcastTrustedEvent') {
208 this.currentApplication[key] = originCurrentApplication[key].bind(originCurrentApplication);
210 this.currentApplication[key] = originCurrentApplication[key];
214 Object.defineProperties(this.currentApplication.appInfo, {
215 categories: { writable: false, enumerable: true },
216 iconPath: { writable: false, enumerable: true },
217 id: { writable: false, enumerable: true },
218 installDate: { writable: false, enumerable: true },
219 name: { writable: false, enumerable: true },
220 packageId: { writable: false, enumerable: true },
221 show: { writable: false, enumerable: true },
222 size: { enumerable: true },
223 version: { writable: false, enumerable: true }
225 Object.defineProperties(this.currentApplication, {
226 appInfo: { enumerable: true },
227 contextId: { writable: false, enumerable: true }
229 // tizen.application.getCurrentApplication().getRequestedAppControl()
230 this.funcRequestedAppcontrol = this.currentApplication.getRequestedAppControl;
231 this.currentApplication.getRequestedAppControl = () => {
232 console.debug(`Routing - getRequestedAppControl() : ${this.getServiceId()}`);
233 return this.funcRequestedAppcontrol();
235 return this.currentApplication;
237 // tizen.application.getAppInfo()
238 this.funcGetAppInfo = global.tizen.application.getAppInfo;
239 global.tizen.application.getAppInfo = (...args: any[]) => {
240 let app_id = args[0];
241 if (this.hasNoneOrNull(args))
242 app_id = this.getServiceId();
243 console.debug(`Routing - getAppInfo(${app_id})`);
244 return this.funcGetAppInfo(app_id);
246 // tizen.application.getAppCerts()
247 this.funcGetAppcerts = global.tizen.application.getAppCerts;
248 global.tizen.application.getAppCerts = (...args: any[]) => {
249 let app_id = args[0];
250 if (this.hasNoneOrNull(args))
251 app_id = this.getServiceId();
252 console.debug(`Routing - getAppCerts() ` + app_id);
253 return this.funcGetAppcerts(app_id);
255 // tizen.application.getAppContext()
256 this.funcGetContext = global.tizen.application.getAppContext;
257 global.tizen.application.getAppContext = (...args: any[]) => {
258 console.debug(`Routing - getAppContext()`);
259 if (this.hasNoneOrNull(args)) {
260 const context = {"id": this.funcGetContext().id, "appId": this.getServiceId()};
261 Object.defineProperties(context, {
262 appId: { writable: false, enumerable: true },
263 id: { writable: false, enumerable: true }
267 return this.funcGetContext(args[0]);
269 // tizen.application.getAppSharedURI()
270 this.funcGetSharedUri = global.tizen.application.getAppSharedURI;
271 global.tizen.application.getAppSharedURI = (...args: any[]) => {
272 let app_id = args[0];
273 if (this.hasNoneOrNull(args))
274 app_id = this.getServiceId();
275 console.debug(`Routing - getAppSharedURI()`);
276 return this.funcGetSharedUri(app_id);
278 // tizen.application.getAppMetaData()
279 this.funcGetMetadata = global.tizen.application.getAppMetaData;
280 global.tizen.application.getAppMetaData = (...args: any[]) => {
281 let app_id = args[0];
282 if (this.hasNoneOrNull(args))
283 app_id = this.getServiceId();
284 console.debug(`Routing - getAppMetaData()`);
285 return this.funcGetMetadata(app_id);
289 refinePackageApis() {
290 // tizen.package.getPackageInfo()
291 this.funcGetPackageInfo = global.tizen.package.getPackageInfo;
292 global.tizen.package.getPackageInfo = (...args: any[]) => {
293 let package_id = args[0];
294 if (this.hasNoneOrNull(args))
295 package_id = this.getPackageId();
296 console.debug(`Routing - getPackageInfo() : ${package_id}`);
297 return this.funcGetPackageInfo(package_id);
301 refineFilesystemApis() {
303 let virtualPath = ['wgt-private', 'wgt-private-tmp', 'wgt-package'];
304 virtualPath.forEach(name => {
305 let realPath = wrt.resolveVirtualRoot(this.id, name);
306 global.tizen.filesystem.setVirtualPath(name, realPath, 'INTERVAL', 'MOUNTED');
309 console.debug(`refineFilesystemApis has an error ${e}`);
313 refineXwalkUtilApis() {
314 global.xwalk.utils.checkPrivilegeAccess = (privilege: string) => {
315 if (!this.permissions.includes(privilege)) {
316 throw 'Permission denied';
319 global.xwalk.utils.getPkgApiVersion = () => {
320 return this.pkgApiVersion;
324 refineMessagePortApis() {
325 this.funcRequestLocalMessagePort =
326 global.tizen.messageport.requestLocalMessagePort;
327 this.funcRequestTrustedLocalMessagePort =
328 global.tizen.messageport.requestTrustedLocalMessagePort;
330 const registerMessagePort = (name: string, portName: string) => {
332 new global.tizen.ApplicationControlData(name, [ portName ]),
335 let appControl = new global.tizen.ApplicationControl(
336 "http://tizen.org/appcontrol/operation/default", null, null, null,
338 global.tizen.application.launchAppControl(appControl, this.serviceId,
339 () => console.debug(`'${this.serviceId}::${name}' is requsted`));
342 global.tizen.messageport.requestLocalMessagePort = (portName: string) => {
343 registerMessagePort('requestLocalMessagePort', portName);
344 return this.funcRequestLocalMessagePort(portName);
346 global.tizen.messageport.requestTrustedLocalMessagePort = (portName: string) => {
347 registerMessagePort('requestTrustedLocalMessagePort', portName);
348 return this.funcRequestLocalMessagePort(portName);
353 let instance: DeviceAPIRouter | undefined;
354 export const initialize = (id: string, isGlobal: boolean) => {
355 instance = new DeviceAPIRouter(id, isGlobal);
358 export const cleanup = () => {
359 delete global.webapis;
360 instance = undefined;