[Addon] Refactor event emitter object handling & operations through it 47/216047/3
authorsurya.kumar7 <surya.kumar7@samsung.com>
Fri, 18 Oct 2019 11:57:21 +0000 (17:27 +0530)
committersurya.kumar7 <surya.kumar7@samsung.com>
Fri, 18 Oct 2019 13:12:26 +0000 (18:42 +0530)
1. While sending event emitter object to add-ons, visibility
is limited to only two of its functions - on & off
2. Invalid events are discarded even before it's passed to
EventEmitter object
3. Removed an excess dependancy between Runtime & WebApplication
while handling event emitter object
4. Removed a bit unused code

Change-Id: Ide4b8772eb6881384fd4b2bbd32562826ca4ea85
Signed-off-by: surya.kumar7 <surya.kumar7@samsung.com>
wrt_app/src/addon_manager.js
wrt_app/src/runtime.js
wrt_app/src/web_application.js

index aedebf469fdf899de5c985db8ab77bfd40be0e10..31c537ac4e1203423ee6afe9da625f0afbd9b1e3 100644 (file)
@@ -25,7 +25,7 @@ const ADDONS_DB_FILE = 'addons_db.json';
 const ADDONS_PATH = path.join(__dirname, '..', 'addon', 'browser', 'addonapi.js');
 
 // A set of predefined events for addons
-let EventList = [
+const EventList = [
     'newListener',      // A listener is added via on() or addListener()
     'removeListener',   // A listener is removed via off() or removeListener()
     'lcPrelaunch',      // An app is at just before launching
@@ -43,10 +43,10 @@ const {EventEmitter} = require('events');
 
 class AddonManager {
     constructor() {
+        this.registerAPIModule();
         this.addons_list_ = null;
         this.addons_ = null;
         this.evt_emitter_ = null;
-        this.registerAPIModule();
     }
 
     registerAPIModule() {
@@ -129,7 +129,7 @@ class AddonManager {
             console.log('activate: ' + addon_path + ' name:' + name);
             try {
                 let Addon = require(addon_path);
-                addon = new Addon(this.evt_emitter_);
+                addon = new Addon(this.wrappedEventEmitter);
             } catch (e) {
                 console.error('activate - error on require() : ' + e);
                 return;
@@ -157,7 +157,7 @@ class AddonManager {
             console.log('deactivate: path:' + addon_path);
             try {
                 let Addon = require(addon_path);
-                addon = new Addon(this.evt_emitter_);
+                addon = new Addon(this.wrappedEventEmitter);
             } catch (e) {
                 console.error('deactivate - error on require() : ' + e);
             }
@@ -177,7 +177,6 @@ class AddonManager {
         for (var name in this.addons_) {
             this.activate(app, name);
         }
-        this.removeInvalidListener();
     }
 
     deactivateAll(app) {
@@ -196,12 +195,21 @@ class AddonManager {
 
     initEventListener() {
         this.evt_emitter_ = new EventEmitter();
-        this.evt_emitter_.on('newListener', function(event, listener) {
-            if (EventList.indexOf(event) != -1)
-                console.log('Valid Event:' + event);
-            else {
-                console.log('Invalid Event:' + event);
+        this.wrappedEventEmitter = {
+            on: (eventName, listener) => {
+                if (EventList.indexOf(eventName) !== -1)
+                    this.evt_emitter_.on(eventName, listener);
+                else console.log('Invalid Event: ' + eventName);
+            },
+            off: (eventName, listener) => {
+                if (EventList.indexOf(eventName) !== -1)
+                    this.evt_emitter_.off(eventName, listener);
+                else console.log('Invalid Event: ' + eventName);
             }
+        }
+
+        this.evt_emitter_.on('newListener', function(event, listener) {
+            console.log('A listener for ' + event + ' has been added');
         });
 
         this.evt_emitter_.on('removeListener', function(event, listener) {
@@ -209,28 +217,6 @@ class AddonManager {
         });
     }
 
-    removeInvalidListener() {
-        let activeEvents = this.evt_emitter_.eventNames();
-        for (var i in activeEvents) {
-            let item = activeEvents[i];
-            if (EventList.indexOf(item) == -1) {
-                console.log('Found invalid event(' + item + ')');
-                if (this.evt_emitter_)
-                    this.evt_emitter_.removeAllListeners(item);
-                else
-                    console.log('event listener is NOT valid');
-            }
-        }
-        activeEvents = this.evt_emitter_.eventNames();
-        console.log('Remained active events : ' + activeEvents);
-    }
-    existEventListeners(event) {
-        let activeEvents = this.evt_emitter_.eventNames();
-        if (activeEvents.indexOf(event) == -1)
-            return false;
-        else return true;
-    }
-
     static getManifestFile() {
         return MANIFEST_FILE;
     }
index 6130fcd98f60eb3c08eecf81b945a4920d11867b..736e68e9750e0bd0d49b39c6315c0f46d693c1a3 100755 (executable)
@@ -39,7 +39,7 @@ class Runtime {
         app.on('before-quit', function(event) {
             console.log('before-quit');
             if (!wrt.isElectronApp()) {
-                _this.webApplication.quit(_this.addonManager.evt_emitter_);
+                _this.webApplication.quit();
                 _this.webApplication.finalize();
                 _this.webApplication = null;
             }
@@ -127,7 +127,7 @@ class Runtime {
                         src = "about:blank";
                     }
                     _this.webApplication.mainWindow.loadURL(src);
-                    _this.webApplication.prelaunch(_this.addonManager.evt_emitter_, src);
+                    _this.webApplication.prelaunch(src);
                 } else {
                     console.log('Handling app-control event');
                     if (_this.webApplication.preloadStatus == 'readyToShow') {
@@ -169,12 +169,12 @@ class Runtime {
         wrt.on('suspend', function() {
             console.log('suspend');
             if (_this.webApplication)
-                _this.webApplication.suspend(_this.addonManager.evt_emitter_);
+                _this.webApplication.suspend();
         });
         wrt.on('resume', function() {
             console.log('resume');
             if (_this.webApplication)
-                _this.webApplication.resume(_this.addonManager.evt_emitter_);
+                _this.webApplication.resume();
         });
         wrt.on('low-memory', function() {
             console.log('low-memory');
@@ -313,7 +313,7 @@ class Runtime {
         }
 
         if (valid) {
-            _this.webApplication.keyEvent(_this.addonManager.evt_emitter_, key);
+            _this.webApplication.keyEvent(key);
         }
     }
 }
index d06e0d7598e601c8cacd4eedf3d4faa7ffe88ce0..e6e75b550632e9ade9d52d0c920b05b4fd031607 100755 (executable)
@@ -287,11 +287,11 @@ class WebApplication {
             "profile log from the initial loading."
         wrt.showDialog(this.mainWindow.webContents, message);
     }
-    suspend(evtEmitter) {
+    suspend() {
         console.log('WebApplication : suspend');
-        if (evtEmitter) {
+        if (this.addonEmitter) {
             console.log('WebApplication : suspend - Found event emitter');
-            evtEmitter.emit('lcSuspend', this.mainWindow.id);
+            this.addonEmitter.emit('lcSuspend', this.mainWindow.id);
         } else {
             console.log('WebApplication : suspend - Invalid event emitter');
         }
@@ -313,14 +313,14 @@ class WebApplication {
         }
         this.windowList[this.windowList.length - 1].hide();
     }
-    resume(evtEmitter) {
+    resume() {
         console.log('WebApplication : resume');
 
         this.suspended = false;
 
-        if (evtEmitter) {
+        if (this.addonEmitter) {
             console.log('WebApplication : resume - Found event emitter');
-            evtEmitter.emit('lcResume', this.mainWindow.id);
+            this.addonEmitter.emit('lcResume', this.mainWindow.id);
         } else {
             console.log('WebApplication : resume - Invalid event emitter');
         }
@@ -347,11 +347,11 @@ class WebApplication {
             window.removeAllListeners();
         });
     }
-    quit(evtEmitter) {
+    quit() {
         console.log('WebApplication : quit');
-        if (evtEmitter) {
+        if (this.addonEmitter) {
             console.log('WebApplication : quit - Found event emitter');
-            evtEmitter.emit('lcQuit', this.mainWindow.id);
+            this.addonEmitter.emit('lcQuit', this.mainWindow.id);
         } else {
             console.log('WebApplication : quit - Invalid event emitter');
         }
@@ -389,9 +389,9 @@ class WebApplication {
                 window.destroy();
         });
     }
-    keyEvent(evtEmitter, key) {
+    keyEvent(key) {
         console.log('WebApplication : keyEvent');
-        if (!evtEmitter) {
+        if (!this.addonEmitter) {
             console.log('Invalid event emitter for key hook');
             return;
         }
@@ -399,22 +399,22 @@ class WebApplication {
         switch(key) {
             case "ArrowUp":
             case "Up":
-                evtEmitter.emit('hwUpkey', this.mainWindow.id);
+                this.addonEmitter.emit('hwUpkey', this.mainWindow.id);
                 break;
             case "ArrowDown":
             case "Down":
-                evtEmitter.emit('hwDownkey', this.mainWindow.id);
+                this.addonEmitter.emit('hwDownkey', this.mainWindow.id);
                 break;
             default:
                 console.log('No handler for ' + key);
                 break;
         }
     }
-    prelaunch(evtEmitter, origURL) {
+    prelaunch(origURL) {
         console.log('WebApplication : prelaunch');
-        if (evtEmitter) {
+        if (this.addonEmitter) {
             console.log('WebApplication : prelaunch - Found event emitter');
-            evtEmitter.emit('lcPrelaunch', this.mainWindow.id, origURL);
+            this.addonEmitter.emit('lcPrelaunch', this.mainWindow.id, origURL);
         } else {
             console.log('WebApplication : prelaunch - Invalid event emitter');
         }