[Service] Refactor start/stop service codes 21/228621/2
authorSangYong Park <sy302.park@samsung.com>
Tue, 24 Mar 2020 05:46:09 +0000 (14:46 +0900)
committerSangYong Park <sy302.park@samsung.com>
Fri, 3 Apr 2020 06:06:51 +0000 (15:06 +0900)
UI and global service implementation of start/stop service have
been merged into one. and, remove unnecessary codes.

Change-Id: Ic27ca887316b48e8e2bb23202ee238ecd62b001d
Signed-off-by: SangYong Park <sy302.park@samsung.com>
wrt_app/common/service_manager.js [new file with mode: 0644]
wrt_app/common/wrt_xwalk_extension.js
wrt_app/service/access_control_manager.js
wrt_app/service/main.js
wrt_app/src/runtime.js
wrt_app/src/was_event.js [deleted file]
wrt_app/src/web_application.js

diff --git a/wrt_app/common/service_manager.js b/wrt_app/common/service_manager.js
new file mode 100644 (file)
index 0000000..254900f
--- /dev/null
@@ -0,0 +1,110 @@
+const Module = require('module');
+const TimerManager = require('../service/timer_manager');
+const XWalkExtension = require('./wrt_xwalk_extension');
+const vm = require('vm');
+const wrt = require('../browser/wrt');
+
+let sandbox = {};
+let service_source = {};
+let is_global_service = !!wrt.readService;
+
+function callFunctionInContext(name, sandbox) {
+  const script = `if (typeof ${name} === 'function') { ${name}(); }`;
+  vm.runInContext(script, sandbox);
+}
+
+function startService(id, filename) {
+  if (sandbox[id] === undefined) {
+    XWalkExtension.initialize();
+
+    sandbox[id] = {
+      console: console,
+      module: new Module,
+      require: require,
+      tizen: tizen,
+    };
+    sandbox[id].module.exports.onStop = () => {
+      callFunctionInContext('module.exports.onExit', sandbox[id]);
+    };
+    if (wrt.tv)
+      sandbox[id].webapis = webapis;
+
+    if (is_global_service) {
+      const permissions = wrt.getPrivileges(id);
+      console.log('permissions : ' + permissions);
+      const AccessControlManager = require('../service/access_control_manager');
+      AccessControlManager.initialize(permissions, sandbox[id]);
+    }
+    for (let key in global)
+      sandbox[id][key] = global[key];
+
+    sandbox[id].timer_manager = new TimerManager();
+    const timer_api = sandbox[id].timer_manager.getTimerAPI();
+    for (let key in timer_api)
+      sandbox[id][key] = timer_api[key];
+
+    let standard_object_list = [ Error, EvalError, RangeError, ReferenceError,
+        SyntaxError, TypeError, URIError, Number, BigInt, Math, Date,
+        String, RegExp, Array, Int8Array, Uint8Array, Uint8ClampedArray,
+        Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array,
+        Float64Array, BigInt64Array, BigUint64Array, Map, Set, WeakMap,
+        WeakSet, ArrayBuffer, DataView, JSON, Promise, Reflect, Proxy,
+        Intl, Intl.Collator, Intl.DateTimeFormat, Intl.NumberFormat, Intl.PluralRules,
+        WebAssembly, WebAssembly.Module, WebAssembly.Instance, WebAssembly.Memory,
+        WebAssembly.Table, WebAssembly.CompileError, WebAssembly.LinkError,
+        WebAssembly.RuntimeError, Boolean, Function, Object, Symbol ];
+    for (let idx in standard_object_list)
+      sandbox[id][standard_object_list[idx].name] = standard_object_list[idx];
+
+    let options = {};
+    let code;
+    if (is_global_service) {
+      options.filename = id;
+      let service_id = id.substr(0, id.indexOf(":"));
+      if (service_source[service_id] === undefined)
+        service_source[service_id] = wrt.readService(id);
+      code = service_source[service_id];
+    } else {
+      const fs = require('fs');
+      code = fs.readFileSync(filename);
+    }
+
+    vm.runInNewContext(code, sandbox[id], options);
+  }
+
+  if (sandbox[id]['started'] === undefined) {
+    sandbox[id]['started'] = true;
+    sandbox[id]['stopped'] = undefined;
+    callFunctionInContext('module.exports.onStart', sandbox[id]);
+    if (is_global_service)
+      wrt.finishStartingService(id);
+  } else {
+    console.log(id + ' service has been started.');
+  }
+
+  callFunctionInContext('module.exports.onRequest', sandbox[id]);
+}
+
+function stopService(id) {
+  if (sandbox[id]['stopped']) {
+    console.log(id + ' service has been already stopped.');
+    return;
+  }
+
+  sandbox[id]['stopped'] = true;
+  sandbox[id]['started'] = undefined;
+  callFunctionInContext('module.exports.onStop', sandbox[id]);
+
+  sandbox[id].timer_manager.releaseRemainingTimers();
+  for (let key in sandbox[id])
+    delete sandbox[id][key];
+  delete sandbox[id];
+
+  if (Object.keys(sandbox).length === 0)
+    XWalkExtension.cleanup();
+}
+
+module.exports = {
+  startService,
+  stopService
+};
index d04b65f..ec566a1 100644 (file)
@@ -16,8 +16,9 @@
 
 require('./exception_handling');
 
-var api_ = {};
-var extensions_ = {};
+let instance;
+let api_ = {};
+let extensions_ = {};
 
 class XWalkExtension {
   constructor() {
@@ -42,8 +43,8 @@ class XWalkExtension {
 
   /**
    * Creates namespace for 'name' in given object.
-   * Eg. this.createNamespace(GLOBAL, 'tizen.contact') will create:
-   * GLOBAL.tizen.contact = {}
+   * Eg. this.createNamespace(global, 'tizen.contact') will create:
+   * global.tizen.contact = {}
    *
    * @param {Object} object
    * @param {String} name
@@ -60,7 +61,7 @@ class XWalkExtension {
   exposeApi(ext) {
     var i, entry_points, entry_point, tmp, parent_name, base_name;
 
-    // additional entry points are installed in GLOBAL context by eval()
+    // additional entry points are installed in global context by eval()
     // so we need to move it to protected api_ object first
     entry_points = [...new Set(ext.entry_points)];
     for (i = 0; i < entry_points.length; i++) {
@@ -69,8 +70,8 @@ class XWalkExtension {
       parent_name = tmp[0];
       base_name = tmp[tmp.length - 1];
 
-      api_[parent_name][base_name] = GLOBAL[parent_name][base_name];
-      delete GLOBAL[parent_name][base_name];
+      api_[parent_name][base_name] = global[parent_name][base_name];
+      delete global[parent_name][base_name];
     }
 
     entry_points.push(ext.name);
@@ -81,7 +82,7 @@ class XWalkExtension {
       parent_name = tmp[0];
       base_name = tmp[tmp.length - 1];
 
-      Object.defineProperty(GLOBAL[parent_name], base_name, {
+      Object.defineProperty(global[parent_name], base_name, {
         get: function (parent_name, base_name) {
           return function () {
               return api_[parent_name][base_name];
@@ -97,19 +98,18 @@ class XWalkExtension {
    * @param {Object} ext
    */
   load(ext) {
-    if (ext.loaded) {
+    if (ext.loaded)
       return;
-    }
-    ext.loadInstance();
 
+    ext.loadInstance();
     ext.loaded = true;
 
     this.createNamespace(api_, ext.name);
-    this.createNamespace(GLOBAL, ext.name);
+    this.createNamespace(global, ext.name);
 
-    var api = (ext.use_trampoline) ? api_ : GLOBAL;
+    var api = (ext.use_trampoline) ? api_ : global;
 
-    var jscode =
+    const jscode =
       '(function(extension) {' +
       '  extension.internal = {};' +
       '  extension.internal.sendSyncMessage = extension.sendSyncMessage;' +
@@ -176,9 +176,9 @@ class XWalkExtension {
       var parent_name = tmp[0];
       var base_name = tmp[tmp.length - 1];
 
-      this.createNamespace(GLOBAL, entry_points[i]);
+      this.createNamespace(global, entry_points[i]);
 
-      Object.defineProperty(GLOBAL[parent_name], base_name, {
+      Object.defineProperty(global[parent_name], base_name, {
         get: function (parent_name, base_name) {
           return function() {
             try {
@@ -203,9 +203,23 @@ class XWalkExtension {
       var tmp = entry_points[i].split('.');
       var parent_name = tmp[0];
       var base_name = tmp[tmp.length - 1];
-      delete GLOBAL[parent_name][base_name];
+      delete global[parent_name][base_name];
     }
   }
 }
 
-module.exports = XWalkExtension;
+const extension_api = {
+  initialize: () => {
+    if (!instance)
+      instance = new XWalkExtension;
+  },
+  cleanup: () => {
+    delete global.tizen;
+    instance = undefined;
+  },
+  preventCleanup: () => {
+    extension_api.cleanup = () => {};
+  }
+}
+
+module.exports = extension_api;
index bda869d..6dd0530 100644 (file)
-class AccessControlManager {
-  constructor(permissions, sandbox) {
-    this.permissions = permissions;
-    this.sandbox = sandbox;
-    this.systeminfo = {};
-    this.systeminfo.getPropertyValue = sandbox.tizen.systeminfo.getPropertyValue;
-  }
-  initialize() {
-    const permissions = this.permissions;
-    let tizen = this.sandbox.tizen;
-    if (!permissions.includes("http://tizen.org/privilege/alarm")) {
-      tizen.alarm.add =
-      tizen.alarm.remove =
-      tizen.alarm.removeAll =
-      tizen.alarm.get =
-      tizen.alarm.getAll =
-      tizen.alarm.getAlarmNotification =
-      tizen.alarm.addAlarmNotification = function() {
-        console.log('The alarm permission is missing.');
-      }
+function initialize(permissions, sandbox) {
+  let tizen = sandbox.tizen;
+  if (!permissions.includes("http://tizen.org/privilege/alarm")) {
+    tizen.alarm.add =
+    tizen.alarm.remove =
+    tizen.alarm.removeAll =
+    tizen.alarm.get =
+    tizen.alarm.getAll =
+    tizen.alarm.getAlarmNotification =
+    tizen.alarm.addAlarmNotification = function() {
+      console.log('The alarm permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/apphistory.read")) {
-      tizen.application.getAppsUsageInfo =
-      tizen.application.getBatteryUsageInfo = function() {
-        console.log('The application.read permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/apphistory.read")) {
+    tizen.application.getAppsUsageInfo =
+    tizen.application.getBatteryUsageInfo = function() {
+      console.log('The application.read permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/application.launch")) {
-      tizen.application.launch =
-      tizen.application.launchAppControl = function() {
-        console.log('The application.launch permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/application.launch")) {
+    tizen.application.launch =
+    tizen.application.launchAppControl = function() {
+      console.log('The application.launch permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/application.info")) {
-      tizen.application.getAppMetaData = function() {
-        console.log('The application.info permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/application.info")) {
+    tizen.application.getAppMetaData = function() {
+      console.log('The application.info permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/appmanager.certificate")) {
-      tizen.application.getAppCerts = function() {
-        console.log('The application.certificate permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/appmanager.certificate")) {
+    tizen.application.getAppCerts = function() {
+      console.log('The application.certificate permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/appmanager.kill")) {
-      tizen.application.kill = function() {
-        console.log('The application.kill permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/appmanager.kill")) {
+    tizen.application.kill = function() {
+      console.log('The application.kill permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/appmanager.launch") ||
-        !permissions.includes("http://tizen.org/privilege/datasharing")) {
-      tizen.datacontrol.addChangeListener =
-      tizen.datacontrol.removeChangeListener = function() {
-        console.log('The appmanager.launch or datasharing permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/appmanager.launch") ||
+      !permissions.includes("http://tizen.org/privilege/datasharing")) {
+    tizen.datacontrol.addChangeListener =
+    tizen.datacontrol.removeChangeListener = function() {
+      console.log('The appmanager.launch or datasharing permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/datacontrol.consumer")) {
-      tizen.datacontrol.getValue =
-      tizen.datacontrol.updateValue =
-      tizen.datacontrol.insert =
-      tizen.datacontrol.update =
-      tizen.datacontrol.remove =
-      tizen.datacontrol.select =
-      tizen.datacontrol.addValue =
-      tizen.datacontrol.removeValue =
-      tizen.datacontrol.getDataControlConsumer = function() {
-        console.log('The datacontrol.consumer permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/datacontrol.consumer")) {
+    tizen.datacontrol.getValue =
+    tizen.datacontrol.updateValue =
+    tizen.datacontrol.insert =
+    tizen.datacontrol.update =
+    tizen.datacontrol.remove =
+    tizen.datacontrol.select =
+    tizen.datacontrol.addValue =
+    tizen.datacontrol.removeValue =
+    tizen.datacontrol.getDataControlConsumer = function() {
+      console.log('The datacontrol.consumer permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/filesystem.read")) {
-      tizen.filesystem.listDirectory =
-      tizen.filesystem.isFile =
-      tizen.filesystem.isDirectory =
-      tizen.filesystem.pathExists =
-      tizen.filesystem.copyFile =
-      tizen.filesystem.copyDirectory =
-      tizen.filesystem.moveFile =
-      tizen.filesystem.moveDirectory =
-      tizen.filesystem.resolve = function() {
-        console.log('The filesystem.read permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/filesystem.read")) {
+    tizen.filesystem.listDirectory =
+    tizen.filesystem.isFile =
+    tizen.filesystem.isDirectory =
+    tizen.filesystem.pathExists =
+    tizen.filesystem.copyFile =
+    tizen.filesystem.copyDirectory =
+    tizen.filesystem.moveFile =
+    tizen.filesystem.moveDirectory =
+    tizen.filesystem.resolve = function() {
+      console.log('The filesystem.read permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/filesystem.write")) {
-      tizen.filesystem.createDirectory =
-      tizen.filesystem.deleteFile =
-      tizen.filesystem.deleteDirectory =
-      tizen.filesystem.copyFile =
-      tizen.filesystem.copyDirectory =
-      tizen.filesystem.moveFile =
-      tizen.filesystem.moveDirectory =
-      tizen.filesystem.rename = function() {
-        console.log('The filesystem.write permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/filesystem.write")) {
+    tizen.filesystem.createDirectory =
+    tizen.filesystem.deleteFile =
+    tizen.filesystem.deleteDirectory =
+    tizen.filesystem.copyFile =
+    tizen.filesystem.copyDirectory =
+    tizen.filesystem.moveFile =
+    tizen.filesystem.moveDirectory =
+    tizen.filesystem.rename = function() {
+      console.log('The filesystem.write permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/notification")) {
-      tizen.alarm.addAlarmNotification = function() {
-        console.log('The notification permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/notification")) {
+    tizen.alarm.addAlarmNotification = function() {
+      console.log('The notification permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/package.info")) {
-      tizen.package.setPackageInfoEventListener =
-      tizen.package.unsetPackageInfoEventListener =
-      tizen.package.getPackageInfo =
-      tizen.package.getPackagesInfo = function() {
-        console.log('The package.info permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/package.info")) {
+    tizen.package.setPackageInfoEventListener =
+    tizen.package.unsetPackageInfoEventListener =
+    tizen.package.getPackageInfo =
+    tizen.package.getPackagesInfo = function() {
+      console.log('The package.info permission is missing.');
     }
-    if (!permissions.includes("http://tizen.org/privilege/packagemanager.install")) {
-      tizen.package.install =
-      tizen.package.uninstall = function() {
-        console.log('The packagemanager.install permission is missing.');
-      }
+  }
+  if (!permissions.includes("http://tizen.org/privilege/packagemanager.install")) {
+    tizen.package.install =
+    tizen.package.uninstall = function() {
+      console.log('The packagemanager.install permission is missing.');
     }
-    // systeminfo : Runtime privilege validation is required, based on parameters
-    tizen.systeminfo.getPropertyValue = function(type, onSuccessCallback, onErrorCallback) {
-      if (type === "CELLULAR_NETWORK" && !permissions.includes("http://tizen.org/privilege/telephony")) {
-        console.log('The telephony permission is missing.');
-        return;
-      }
-      this.systeminfo.getPropertyValue.apply(tizen.systeminfo, arguments);
-    }.bind(this);
   }
+  // systeminfo : Runtime privilege validation is required, based on parameters
+  let getPropertyValue = tizen.systeminfo.getPropertyValue;
+  tizen.systeminfo.getPropertyValue = function(type, onSuccessCallback, onErrorCallback) {
+    if (type === "CELLULAR_NETWORK" && !permissions.includes("http://tizen.org/privilege/telephony")) {
+      console.log('The telephony permission is missing.');
+      return;
+    }
+    getPropertyValue.apply(tizen.systeminfo, arguments);
+  }.bind(this);
+}
+
+module.exports = {
+  initialize
 }
-module.exports = AccessControlManager;
index 7f8b055..22ea797 100755 (executable)
 
 require('../common/init')
 const wrt = require('../browser/wrt');
-const vm = require('vm');
-const AccessControlManager = require('./access_control_manager');
-const XWalkExtension = require('../common/wrt_xwalk_extension');
-
-var sandbox = [];
-var sandbox_count = 0;
-var service_source = [];
+const ServiceManager = require('../common/service_manager');
 
 wrt.on('start-service', (event, internal_id) => {
   console.log('start service app : ' + internal_id);
-  if (sandbox[internal_id] === undefined) {
-    if (sandbox_count === 0) {
-      new XWalkExtension();
-    }
-    sandbox_count++;
-    const Module = require('module');
-    sandbox[internal_id] = {
-      console: console,
-      module: new Module,
-      require: require,
-      tizen: tizen,
-    };
-    if (wrt.tv) {
-      sandbox[internal_id].webapis = webapis;
-    }
-    let permissions = wrt.getPrivileges(internal_id);
-    console.log('permissions : ' + permissions);
-    let access_control_manager = new AccessControlManager(permissions, sandbox[internal_id]);
-    access_control_manager.initialize();
-    for (let key in global) {
-      sandbox[internal_id][key] = global[key];
-    }
-    let standard_object_list = [ Error, EvalError, RangeError, ReferenceError,
-        SyntaxError, TypeError, URIError, Number, BigInt, Math, Date,
-        String, RegExp, Array, Int8Array, Uint8Array, Uint8ClampedArray,
-        Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array,
-        Float64Array, BigInt64Array, BigUint64Array, Map, Set, WeakMap,
-        WeakSet, ArrayBuffer, DataView, JSON, Promise, Reflect, Proxy,
-        Intl, Intl.Collator, Intl.DateTimeFormat, Intl.NumberFormat, Intl.PluralRules,
-        WebAssembly, WebAssembly.Module, WebAssembly.Instance, WebAssembly.Memory,
-        WebAssembly.Table, WebAssembly.CompileError, WebAssembly.LinkError,
-        WebAssembly.RuntimeError, Boolean, Function, Object, Symbol ];
-    for (let idx in standard_object_list) {
-      sandbox[internal_id][standard_object_list[idx].name] = standard_object_list[idx];
-    }
-    let options = { filename: internal_id };
-    let service_id = internal_id.substr(0, internal_id.indexOf(":"));
-    if (service_source[service_id] === undefined) {
-      service_source[service_id] = wrt.readService(internal_id);
-    }
-    vm.runInNewContext(service_source[service_id], sandbox[internal_id], options);
-  }
-  if (sandbox[internal_id]['started'] === undefined) {
-    sandbox[internal_id]['started'] = true;
-    sandbox[internal_id]['stopped'] = undefined;
-    const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
-    vm.runInContext(start_callback_string, sandbox[internal_id]);
-    wrt.finishStartingService(internal_id);
-  }
-  const request_callback_string = 'if (module.exports.onRequest !== undefined) { module.exports.onRequest(); }';
-  vm.runInContext(request_callback_string, sandbox[internal_id]);
+  ServiceManager.startService(internal_id);
 });
 
 wrt.on('stop-service', (event, internal_id) => {
-  if (sandbox[internal_id]['stopped'] === undefined) {
-    sandbox_count--;
-    sandbox[internal_id]['stopped'] = true;
-    sandbox[internal_id]['started'] = undefined;
-    const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
-    vm.runInContext(stop_callback_string, sandbox[internal_id]);
-    for(let key in sandbox[internal_id]) {
-      delete sandbox[internal_id][key];
-    }
-    delete sandbox[internal_id];
-    if (sandbox_count === 0) {
-      tizen = null;
-    }
-  } else {
-    console.log('The global service has been already stopped.');
-  }
+  ServiceManager.stopService(internal_id);
 });
 
 process.on('exit', (code) => {
index eca2a84..af747b2 100755 (executable)
@@ -20,10 +20,7 @@ const wrt = require('../browser/wrt');  // Load first for log
 const AddonManager = require('./addon_manager');
 const {app, ipcMain} = require('electron');
 const IPC_MESSAGE = require('./ipc_message');
-const TimerManager = require('../service/timer_manager');
-const WAS_EVENT = require('./was_event');
 const WebApplication = require('./web_application');
-const XWalkExtension = require('../common/wrt_xwalk_extension');
 
 class Runtime {
     constructor(options) {
@@ -32,11 +29,7 @@ class Runtime {
         this.addonManager = null;
         this.isLaunched = false;
         this.inspectorEnabledByVconf = false;
-        this.sandbox = [];
-        this.sandbox_count = 0;
-        this.webContents = null;
         this.addonPkgs = [];
-        this.extensionInit = false;
 
         var _this = this;
         app.on('before-quit', function(event) {
@@ -82,8 +75,7 @@ class Runtime {
             console.log('web-contents-created');
             if (wrt.tv)
                 _this.setCookiePath();
-            _this.webContents = webContents;
-            _this.webContents.on('before-input-event', function(event, input) {
+            webContents.on('before-input-event', function(event, input) {
                 if (_this.isLaunched && _this.webApplication) {
                     _this.handleKeyEvents(input.key);
                 }
@@ -95,9 +87,10 @@ class Runtime {
             if (!options.noAddons) {
                 let addonBuilt = _this.addonManager.build();
                 console.log("addonBuild : " + addonBuilt.length);
-                if (!_this.extensionInit && addonBuilt.length) {
-                  new XWalkExtension();
-                  _this.extensionInit = true;
+                if (addonBuilt.length) {
+                  const XWalkExtension = require('./wrt_xwalk_extension');
+                  XWalkExtension.initialize();
+                  XWalkExtension.preventCleanup();
                 }
             }
             if (wrt.tv) {
@@ -218,77 +211,11 @@ class Runtime {
         wrt.on('message', function(event, type, params) {
             console.log('message type(' + type + ') params : ' + params);
             const app_id = params[0];
-            const vm = require('vm');
             if (type === 'startService') {
-                if (_this.sandbox[app_id] === undefined) {
-                    if (_this.sandbox_count === 0 && !_this.extensionInit) {
-                        new XWalkExtension();
-                        _this.extensionInit = true;
-                    }
-                    _this.sandbox_count++;
-                    const fs = require('fs');
-                    const Module = require('module');
-                    _this.sandbox[app_id] = {
-                        console: console,
-                        module: new Module,
-                        require: require,
-                        tizen: tizen,
-                    };
-                    if (wrt.tv) {
-                        _this.sandbox[app_id].webapis = webapis;
-                    }
-                    for(let key in global) {
-                        _this.sandbox[app_id][key] = global[key];
-                    }
-                    _this.sandbox[app_id].timer_manager = new TimerManager();
-                    const timer_api = _this.sandbox[app_id].timer_manager.getTimerAPI();
-                    for(let key in timer_api) {
-                        _this.sandbox[app_id][key] = timer_api[key];
-                    }
-                    let standard_object_list = [ Error, EvalError, RangeError, ReferenceError,
-                            SyntaxError, TypeError, URIError, Number, BigInt, Math, Date,
-                            String, RegExp, Array, Int8Array, Uint8Array, Uint8ClampedArray,
-                            Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array,
-                            Float64Array, BigInt64Array, BigUint64Array, Map, Set, WeakMap,
-                            WeakSet, ArrayBuffer, DataView, JSON, Promise, Reflect, Proxy,
-                            Intl, Intl.Collator, Intl.DateTimeFormat, Intl.NumberFormat, Intl.PluralRules,
-                            WebAssembly, WebAssembly.Module, WebAssembly.Instance, WebAssembly.Memory,
-                            WebAssembly.Table, WebAssembly.CompileError, WebAssembly.LinkError,
-                            WebAssembly.RuntimeError, Boolean, Function, Object, Symbol ];
-                    for (let idx in standard_object_list) {
-                        _this.sandbox[app_id][standard_object_list[idx].name] = standard_object_list[idx];
-                    }
-                    let options = {};
-                    let code = fs.readFileSync(params[1]);
-                    vm.runInNewContext(code, _this.sandbox[app_id], options);
-                }
-                if (_this.sandbox[app_id]['started'] === undefined) {
-                    _this.sandbox[app_id]['started'] = true;
-                    _this.sandbox[app_id]['stopped'] = undefined;
-                    const start_callback_string = 'if (module.exports.onStart !== undefined) { module.exports.onStart(); }';
-                    vm.runInContext(start_callback_string, _this.sandbox[app_id]);
-                } else {
-                    console.log('UI service has been started.');
-                }
+                require('../common/service_manager').startService(app_id, params[1]);
                 event.preventDefault();
             } else if (type === 'stopService') {
-                if (_this.sandbox[app_id]['stopped'] === undefined) {
-                    _this.sandbox_count--;
-                    _this.sandbox[app_id]['stopped'] = true;
-                    _this.sandbox[app_id]['started'] = undefined;
-                    const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
-                    vm.runInContext(stop_callback_string, _this.sandbox[app_id]);
-                    _this.sandbox[app_id]['timer_manager'].releaseRemainingTimers();
-                    for(let key in _this.sandbox[app_id]) {
-                        delete _this.sandbox[app_id][key];
-                    }
-                    delete _this.sandbox[app_id];
-                    if (_this.sandbox_count === 0) {
-                        tizen = null;
-                    }
-                } else {
-                    console.log('UI service has been stopped.');
-                }
+                require('../common/service_manager').stopService(app_id);
                 event.preventDefault();
             }
         });
diff --git a/wrt_app/src/was_event.js b/wrt_app/src/was_event.js
deleted file mode 100755 (executable)
index bf843d5..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-
-'use strict';
-
-module.exports = Object.freeze({
-    RUNTIME: {
-        MINIMIZE: 'was:runtime:minimize',
-        UNMINIMIZE: 'was:runtime:unminimize',
-        MAXIMIZE: 'was:runtime:maximize',
-        UNMAXIMIZE: 'was:runtime:unmaximize',
-        FOCUS: 'was:runtime:focus',
-        UNFOCUS: 'was:runtime:unfocus'
-    },
-    WEBAPPLICATION: {
-        SUSPEND: 'was:webapplication:suspend',
-        RESUME: 'was:webapplication:resume'
-    }
-});
index 396c654..270a1ef 100644 (file)
@@ -17,7 +17,6 @@
 'use strict';
 
 const { app, protocol } = require('electron');
-const WAS_EVENT = require('./was_event');
 const wrt = require('../browser/wrt');
 const WRTWebContents = require('../browser/wrt_web_contents');
 const WRTWindow = require('../browser/wrt_window');