funcGetMetadata: any;
funcGetPackageInfo: any;
funcRequestLocalMessagePort: any;
- funcRequestTrustedLocalMessagePort: any;
id: string;
serviceId: string;
callerAppId: string;
permissions: string[];
pkgApiVersion: string;
+ messagePortInfo: any;
constructor(id: string, isGlobal: boolean) {
this.id = id;
this.packageId = wrt.getPackageId(id);
this.permissions = [];
this.pkgApiVersion = '';
+ this.messagePortInfo = {};
this.initWebapis();
if (isGlobal) {
refineMessagePortApis() {
this.funcRequestLocalMessagePort =
global.tizen.messageport.requestLocalMessagePort;
- this.funcRequestTrustedLocalMessagePort =
- global.tizen.messageport.requestTrustedLocalMessagePort;
- const registerMessagePort = (name: string, portName: string) => {
+ const registerMessagePort = (name: string, portName: string, appId?: string, callback?: any) => {
+ let portInfo = `${name}::${this.serviceId}::${portName}`;
+ if (this.messagePortInfo[portInfo] === 'created') {
+ callback?.();
+ return;
+ }
let data_payload = [
- new global.tizen.ApplicationControlData(name, [ portName ]),
-
+ new global.tizen.ApplicationControlData(name, [portName]),
];
+ if (appId) {
+ data_payload.push(new global.tizen.ApplicationControlData('appId', [appId]));
+ }
let appControl = new global.tizen.ApplicationControl(
- "http://tizen.org/appcontrol/operation/default", null, null, null,
- data_payload, null);
+ "http://tizen.org/appcontrol/operation/default", null, null, null,
+ data_payload, null);
global.tizen.application.launchAppControl(appControl, this.serviceId,
- () => console.debug(`'${this.serviceId}::${name}' is requsted`));
+ () => {
+ console.debug(`'${portInfo}' is requsted`)
+ callback?.();
+ this.messagePortInfo[portInfo] = 'created';
+ });
}
global.tizen.messageport.requestLocalMessagePort = (portName: string) => {
registerMessagePort('requestTrustedLocalMessagePort', portName);
return this.funcRequestLocalMessagePort(portName);
}
+ global.tizen.messageport.requestTrustedRemoteMessagePort = (appId: string, portName: string) => {
+ return {
+ messagePortName: portName,
+ appId,
+ isTrusted: true,
+ sendMessage: (data: any, localMessagePort?: any) => {
+ registerMessagePort('requestTrustedRemoteMessagePort', portName, appId,
+ () => {
+ let remotePort = global.tizen.messageport.requestRemoteMessagePort(this.serviceId, portName);
+ remotePort.sendMessage(data, localMessagePort);
+ });
+ }
+ }
+ }
}
}
[id: string]: any;
}
let workers: WorkerMap = {};
+let dyingWorkerQueue: WorkerMap = {};
Object.defineProperty(global, 'serviceType', {
value: wrt.getServiceModel(),
writable: false
});
+function checkDyingWorker() {
+ let dyingWorkers = Object.keys(dyingWorkerQueue);
+ if (dyingWorkers.length) {
+ let workerId = dyingWorkers[0];
+ if (dyingWorkerQueue[workerId] === 'will-terminate') {
+ dyingWorkerQueue[workerId] = 'terminated';
+ workers[workerId].terminate();
+ }
+ }
+}
+
function createWorker(id: string, startService: string, filename: string) {
if (workers[id]) {
workers[id].postMessage({ type: 'wake' });
});
workers[id].on('message', (message: string) => {
if (message === 'will-terminate') {
- workers[id].terminate();
+ dyingWorkerQueue[id] = message;
+ checkDyingWorker();
}
});
workers[id].on('exit', (code: number) => {
wrt.tv?.serviceUmount(id);
+ delete dyingWorkerQueue[id];
delete workers[id];
let runningServices = Object.keys(workers);
- console.debug(`exit code(${code}), remain services(${runningServices})`);
+ console.debug(`${id} terminated, remain services(${runningServices})`);
+ checkDyingWorker();
});
}