1 import '../common/init';
2 import * as XWalkExtension from '../common/wrt_xwalk_extension';
3 import { DeviceAPIRouter } from './device_api_router';
4 import { isMainThread, parentPort, workerData } from 'worker_threads';
5 import { wrt } from '../browser/wrt';
7 Object.defineProperty(global, 'serviceType', {
8 value: wrt.getServiceModel(),
12 function isServiceApplication() {
13 return global['serviceType'] !== 'UI';
16 function isGlobalService() {
17 return global['serviceType'] === 'GLOBAL';
20 function printAppControlData(id: string) {
21 let reqAppControl = global.tizen.application.getCurrentApplication().getRequestedAppControl();
23 console.debug(`id: ${id}, appControlData operation: ${reqAppControl.appControl.operation}`);
24 let appControlData = reqAppControl.appControl.data;
25 for (let dataIndex in appControlData) {
26 for (let valueIndex in appControlData[dataIndex].value)
27 console.debug(`data[${dataIndex}][${valueIndex}]: ${appControlData[dataIndex].value[valueIndex]}`);
32 function registerExtensionResolver(id: string) {
34 let extensionResolver = (module: any, file_path: string) => {
35 console.debug(`resolved path: ${file_path}`);
36 let content = (wrt.tv as NativeWRTjs.TVExtension).decryptFile(id, file_path);
39 if (content.charCodeAt(0) === 0xFEFF)
40 content = content.slice(1);
41 module._compile(content, file_path);
44 require.extensions['.js.spm'] = extensionResolver;
45 require.extensions['.spm'] = extensionResolver;
49 let requestStopService = (id: string) => {
50 requestStopService = (id: string) => {};
51 setTimeout(() => wrt.stopService(id), 500);
56 let periodLauncherAlive = 20; // 2s
58 let checkLauncherAlive = (id: string) => {
59 periodLauncherAlive--;
60 if (!periodLauncherAlive) {
61 periodLauncherAlive = 20;
62 if (!wrt.checkLauncherAlive(id)) {
63 console.debug(`${id} launcher was killed.`)
64 requestStopService(id);
65 checkLauncherAlive = () => {};
70 export function start(id: string, filename: string) {
71 let ids = id.split(':');
72 let serviceId = ids[0];
73 let packageId = wrt.getPackageId(id);
75 console.debug(`${id}'s pkgid is empty, so stop service`);
76 requestStopService(id);
79 wrt.setServiceAppId(id);
80 wrt.security?.dropThreadPrivilege(packageId, serviceId);
82 XWalkExtension.initialize();
83 XWalkExtension.setRuntimeMessageHandler((type, data) => {
84 if (type === 'tizen://exit') {
85 console.debug(`${id} will be closed by ${type}`);
86 requestStopService(id);
90 console.debug(`serviceType : ${global['serviceType']}`)
91 new DeviceAPIRouter(id, isGlobalService());
92 printAppControlData(id);
94 // This is for awaking up uv loop.
95 if (isGlobalService()) {
96 dummyTimer = setInterval(() => {
97 checkLauncherAlive(id);
101 if (isServiceApplication()) {
102 registerExtensionResolver(id);
103 filename = wrt.getStartServiceFile(id);
104 console.debug(`start global service file: ${filename}`);
108 app = require(filename);
109 if (app.onStart !== undefined) {
112 if (app.onRequest !== undefined) {
116 console.debug(`exception on start: ${e}`);
117 requestStopService(id);
119 if (isGlobalService()) {
120 wrt.finishStartingService(id);
125 export function stop(id: string) {
127 clearInterval(dummyTimer);
129 if (app.onStop !== undefined) {
131 } else if (app.onExit !== undefined) {
135 console.debug(`exception on stop: ${e}`);
137 if (isGlobalService())
138 wrt.finalizeService(id);
143 let id = workerData.id;
145 console.debug('workerData.id is empty!');
149 Object.defineProperty(global, 'internalId', {
154 wrt.tv?.serviceMount(id);
155 let filename = workerData.filename;
158 parentPort?.on('message', (message) => {
159 console.debug(`Received message type : ${message.type}`);
160 if (message.type === 'wake') {
162 } else if (message.type === 'stop') {
165 XWalkExtension.cleanup();
166 parentPort?.postMessage("will-terminate");
168 wrt.tv?.serviceUmount(id);