const wrt = require('../browser/wrt');
const vm = require('vm');
const TizenExtension = require('./tizen_extension');
+
var sandbox = [];
+var sandbox_count = 0;
wrt.on('start-service', (event, app_id) => {
console.log('start service app : ' + app_id);
new TizenExtension();
if (sandbox[app_id] === undefined) {
+ if (sandbox_count === 0) {
+ new TizenExtension();
+ }
+ sandbox_count++;
const Module = require('module');
sandbox[app_id] = {
console: console,
}
if (sandbox[app_id]['started'] === undefined) {
sandbox[app_id]['started'] = true;
+ sandbox[app_id]['stopped'] = undefined;
const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
vm.runInContext(start_callback_string, sandbox[app_id]);
}
const request_callback_string = 'if (module.exports.onRequest !== undefined) { module.exports.onRequest(); }';
vm.runInContext(request_callback_string, sandbox[app_id]);
-})
+});
+
+wrt.on('stop-service', (event, app_id) => {
+ if (sandbox[app_id]['stopped'] === undefined) {
+ sandbox_count--;
+ sandbox[app_id]['stopped'] = true;
+ sandbox[app_id]['started'] = undefined;
+ const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
+ vm.runInContext(stop_callback_string, sandbox[app_id]);
+ for(let key in sandbox[app_id]) {
+ delete sandbox[app_id][key];
+ }
+ delete sandbox[app_id];
+ if (sandbox_count === 0) {
+ tizen = null;
+ }
+ } else {
+ console.log('The global service has been already stopped.');
+ }
+});
process.on('exit', (code) => {
console.log('Exit with code : ' + code);
-})
+});