import * as XWalkExtension from './wrt_xwalk_extension';
import * as vm from 'vm';
import { wrt } from '../browser/wrt';
+import { DeviceAPIRouter } from '../service/device_api_router';
interface ContextMap {
[id: string]: vm.Context;
}
let sandbox: ContextMap = {};
-let is_global_service: boolean = !!wrt.getStartServiceFile;
+let internal_handler: ContextOption = {};
+let service_type: string = wrt.getServiceModel?.() ?? 'UI';
function callFunctionInContext(name: string, sandbox: vm.Context) {
const script = `if (typeof ${name} === 'function') { ${name}(); }`;
}
}
- if (is_global_service) {
+ if (service_type !== 'UI') {
const permissions = wrt.getPrivileges(id);
console.log(`permissions : ${permissions}`);
const AccessControlManager = require('../service/access_control_manager');
for (let key in global)
sandbox[id][key] = global[key];
- sandbox[id].timer_manager = new TimerManager();
- const timer_api = sandbox[id].timer_manager.getTimerAPI();
+ internal_handler[id] = {};
+ internal_handler[id].timer_manager = new TimerManager();
+ const timer_api = internal_handler[id].timer_manager.getTimerAPI();
for (let key in timer_api)
sandbox[id][key] = timer_api[key];
let options: ContextOption = {};
let code;
- if (is_global_service) {
+ if (service_type !== 'UI') {
options.filename = id;
if (wrt.tv) {
let extension_resolver = function (module: any, file_path: string) {
sandbox[id].require.extensions['.js.spm'] = extension_resolver;
sandbox[id].require.extensions['.spm'] = extension_resolver;
}
- let start_service_file = wrt.getStartServiceFile(id);
- console.log(`start service file: ${start_service_file}`);
- code = `const app = require('${start_service_file}')`;
- } else {
- code = `const app = require('${filename}')`;
+ filename = wrt.getStartServiceFile(id);
+ console.log(`start global service file: ${filename}`);
+ }
+ code = `const app = require('${filename}')`;
+ if (service_type === 'DAEMON') {
+ internal_handler[id].deivce_api_router = new DeviceAPIRouter(id, sandbox);
}
-
vm.runInNewContext(code, sandbox[id], options);
}
sandbox[id]['started'] = true;
sandbox[id]['stopped'] = undefined;
callFunctionInContext('app.onStart', sandbox[id]);
- if (is_global_service)
+ if (service_type !== 'UI')
wrt.finishStartingService(id);
} else {
console.log(id + ' service has been started.');
sandbox[id]['started'] = undefined;
callFunctionInContext('app.onStop', sandbox[id]);
- sandbox[id].timer_manager.releaseRemainingTimers();
+ internal_handler[id].timer_manager.releaseRemainingTimers();
for (let key in sandbox[id])
delete sandbox[id][key];
delete sandbox[id];
+ for (let key in internal_handler[id])
+ delete internal_handler[id][key];
+ delete internal_handler[id];
if (Object.keys(sandbox).length === 0)
XWalkExtension.cleanup();