1 import { Worker, isMainThread } from 'worker_threads';
2 import { wrt } from '../browser/wrt';
3 import * as XWalkExtension from '../common/wrt_xwalk_extension';
8 let workers: WorkerMap = {};
9 let dyingWorkerQueue: WorkerMap = {};
11 Object.defineProperty(global, 'serviceType', {
12 value: wrt.getServiceModel(),
16 function checkDyingWorker() {
17 let dyingWorkers = Object.keys(dyingWorkerQueue);
18 if (dyingWorkers.length) {
19 let workerId = dyingWorkers[0];
20 if (dyingWorkerQueue[workerId] === 'will-terminate') {
21 dyingWorkerQueue[workerId] = 'terminated';
22 workers[workerId].terminate();
27 function createWorker(id: string, startService: string, filename: string) {
29 workers[id].postMessage({ type: 'wake' });
33 wrt.tv?.serviceMount(id);
34 workers[id] = new Worker(startService, {
40 workers[id].on('message', (message: string) => {
41 if (message === 'will-terminate') {
42 dyingWorkerQueue[id] = message;
46 workers[id].on('exit', (code: number) => {
47 wrt.tv?.serviceUmount(id);
48 delete dyingWorkerQueue[id];
50 let runningServices = Object.keys(workers);
51 console.debug(`${id} terminated, remain services(${runningServices})`);
56 function terminateWorker(id: string, delay: number) {
58 console.debug(`This worker is already terminated. ${id}`);
61 console.debug(`${id} will shutdown after ${delay}ms`);
62 workers[id].postMessage({ type: 'stop', delay });
65 let initializeExtensionOnMain = () => {
66 initializeExtensionOnMain = () => {};
67 XWalkExtension.initialize();
68 // This is workaround solution to make webapis's singleton worker, which has
69 // same smack label with pid's.
70 // It must be handled ahead of dropThreadPrivilege()
71 // Otherwise, smack violation might hanppen from 'libdbuspolicy'.
72 global.tizen.systeminfo.getPropertyValue("CPU", () => { }, () => { });
75 export function startService(id: string, filename: string) {
76 console.debug(`startService - ${id}`);
77 initializeExtensionOnMain();
78 if (global['serviceType'] === 'STANDALONE') {
79 let ids = id.split(':');
80 let serviceId = ids[0];
81 let packageId = serviceId.split('.')[0];
82 wrt.security?.dropThreadPrivilege(packageId, serviceId);
84 let startService = `${__dirname}/service_runner.js`;
85 createWorker(id, startService, filename);
88 export function stopService(id: string) {
89 console.debug(`stopService - ${id}`);
90 terminateWorker(id, 500);
93 export function handleBuiltinService(serviceId: string, serviceName: string) {
97 let need_stop = (serviceName.substr(0, 5) === 'stop_');
99 console.debug(`${serviceName} will be terminated.`);
100 workers[serviceId].terminate();
102 console.debug(`Builtin service is ${serviceName}`);
103 let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
104 createWorker(serviceId, startService, '');