Implement window list management 25/204625/2
authorSangYong Park <sy302.park@samsung.com>
Thu, 25 Apr 2019 06:06:11 +0000 (15:06 +0900)
committerSangYong Park <sy302.park@samsung.com>
Thu, 25 Apr 2019 06:15:46 +0000 (15:15 +0900)
Window is not showed properly now when child window is created.
Main window is showed because all window did showed by unordered list.
so, implement window list management.

Change-Id: I35f47d4681f65b7f96e4a970b75282ccbc9269af
Signed-off-by: SangYong Park <sy302.park@samsung.com>
wrt_app/src/web_application.js

index 48bb950..3a0c530 100755 (executable)
@@ -22,18 +22,13 @@ const wrt = require('../browser/wrt');
 
 class WebApplication {
     constructor(options) {
-        let winopt = this.getBrowserWindowOption(options);
-        console.log('opt: ' + JSON.stringify(winopt));
-        this.mainWindow = new BrowserWindow(winopt);
-        if (options.devMode) {
-            this.mainWindow.webContents.openDevTools({
-                detached: true
-            });
-        }
-        console.log('mainWindow id : ' + this.mainWindow.id);
+        this.initialize(options);
+        this.createMainWindow(options);
+    }
+    initialize(options) {
         this.pendingID = 0;
         this.pendingCallbacks = new Map();
-        this.handleEvents(winopt);
+        this.windowList = [];
         this.firstRendered = false;
         this.backgroundSupport = wrt.getBackgroundSupport();
         this.multitaskingSupport = wrt.getMultitaskingSupport();
@@ -44,16 +39,78 @@ class WebApplication {
             this.preloadState = 'none';
         }
         this.accessiblePath = wrt.getAccessiblePath();
-        console.log(this.accessiblePath);
+
+        let self = this;
+        app.on('browser-window-created', function(event, window) {
+            if (self.windowList.length > 0)
+                self.windowList[self.windowList.length - 1].hide();
+            self.windowList.push(window);
+            console.log(`window created : #${self.windowList.length}`);
+
+            window.on('closed', function() {
+                console.log(`window closed : #${self.windowList.length}`);
+                let index = self.windowList.indexOf(window);
+                self.windowList.splice(index, 1);
+                if (index === self.windowList.length && self.windowList.length > 0)
+                    self.windowList[self.windowList.length - 1].show();
+            });
+        });
+        app.on('web-contents-created', function(event, webContents) {
+            webContents.on('crashed', function() {
+                console.error('webContents crashed');
+                app.exit(100);
+            });
+            webContents.session.setPermissionRequestHandler(function(webContents, permission, callback) {
+                console.log(`handlePermissionRequests for ${permission}`);
+                if (permission === 'notifications') {
+                    const id = ++self.pendingID;
+                    console.log(`Raising a notification permission request with id: ${id}`);
+                    self.pendingCallbacks.set(id, callback);
+                    wrt.handleNotificationPermissionRequest(id, webContents);
+                } else if (permission === 'media') {
+                    const id = ++self.pendingID;
+                    console.log(`Raising a media permission request with id: ${id}`);
+                    self.pendingCallbacks.set(id, callback);
+                    wrt.handleMediaPermissionRequest(id, webContents);
+                } else {
+                    /* electron by default allows permission for all if no request handler
+                       is there; so granting permission only temporarily to not have any
+                       side effects */
+                    callback(true);
+                }
+            });
+        });
+        app.on('certificate-error', function(event, webContents, url, error, certificate, callback) {
+            console.log('A certificate error has occurred');
+            event.preventDefault();
+            if (certificate.data) {
+                const id = ++self.pendingID;
+                console.log(`Raising a certificate error response with id: ${id}`);
+                self.pendingCallbacks.set(id, callback);
+                wrt.handleCertificateError(id, webContents, certificate.data, url);
+            } else {
+                console.log('Certificate could not be opened');
+                callback(false);
+            }
+        });
+        app.on('login', function(event, webContents, request, authInfo, callback) {
+            console.log('Login info is required');
+            event.preventDefault();
+            const id = ++self.pendingID;
+            console.log(`Raising a login info request with id: ${id}`);
+            self.pendingCallbacks.set(id, callback);
+            wrt.handleAuthRequest(id, webContents);
+        });
         if (this.accessiblePath) {
+            console.log('accessiblePath : ' + this.accessiblePath);
             protocol.interceptFileProtocol('file', (request, callback) => {
                 const url = require('url');
                 let access_path, parsed_info = url.parse(request.url);
                 access_path = parsed_info.host + parsed_info.pathname;
                 console.log("check path: " + access_path);
 
-                for (let p in this.accessiblePath) {
-                    if (access_path.startsWith(this.accessiblePath[p])) {
+                for (let p in self.accessiblePath) {
+                    if (access_path.startsWith(self.accessiblePath[p])) {
                         callback(access_path);
                         return;
                     }
@@ -70,6 +127,27 @@ class WebApplication {
                 console.log(error);
             });
         }
+        wrt.on('permission-response', function(event, id, result) {
+            console.log(`permission-response for ${id} is ${result}`);
+            let callback = self.pendingCallbacks.get(id);
+            if (typeof callback === 'function') {
+                console.log('calling permission response callback');
+                callback(result);
+                self.pendingCallbacks.delete(id);
+            }
+        });
+        wrt.on('auth-response', function(event, id, submit, user, passwd) {
+            let callback = self.pendingCallbacks.get(id);
+            if (typeof callback === 'function') {
+                console.log('calling auth response callback');
+                if (submit) {
+                    callback(user, passwd);
+                } else {
+                    callback();
+                }
+                self.pendingCallbacks.delete(id);
+            }
+        });
     }
     getBrowserWindowOption(options) {
         return {
@@ -86,11 +164,16 @@ class WebApplication {
             }
         };
     }
-    handleEvents(options) {
+    createMainWindow(options) {
+        let winopt = this.getBrowserWindowOption(options);
+        this.mainWindow = new BrowserWindow(winopt);
+        if (options.devMode) {
+            this.mainWindow.webContents.openDevTools({
+                detached: true
+            });
+        }
         let self = this;
-        this.mainWindow.on('ready-to-show', function() {
-            if (self.firstRendered)
-                return;
+        this.mainWindow.once('ready-to-show', function() {
             console.log('mainWindow ready-to-show');
             wrt.hideSplashScreen(1);
             self.firstRendered = true;
@@ -101,14 +184,6 @@ class WebApplication {
             }
             self.show();
         });
-        this.mainWindow.on('closed', function() {
-            console.log('mainWindow closed!');
-            self.mainWindow = null;
-        });
-        this.mainWindow.webContents.on('crashed', function() {
-            console.error('webContents crashed');
-            app.exit(100);
-        });
         this.mainWindow.webContents.on('did-start-loading', function() {
             console.log('webContents did-start-loading');
         });
@@ -120,83 +195,17 @@ class WebApplication {
                 self.suspend();
             }
         });
-        wrt.on('permission-response', function(event, id, result) {
-            console.log(`permission-response for ${id} is ${result}`);
-            let callback = self.pendingCallbacks.get(id);
-            if (typeof callback === 'function') {
-                console.log('calling permission response callback');
-                callback(result);
-                self.pendingCallbacks.delete(id);
-            }
-        });
-        wrt.on('auth-response', function(event, id, submit, user, passwd) {
-            let callback = self.pendingCallbacks.get(id);
-            if (typeof callback === 'function') {
-                console.log('calling auth response callback');
-                if (submit) {
-                    callback(user, passwd);
-                } else {
-                    callback();
-                }
-                self.pendingCallbacks.delete(id);
-            }
-        });
-        app.on('certificate-error', function(event, webContents, url, error, certificate, callback) {
-            console.log('A certificate error has occurred');
-            event.preventDefault();
-            if (certificate.data) {
-                const id = ++self.pendingID;
-                console.log(`Raising a certificate error response with id: ${id}`);
-                self.pendingCallbacks.set(id, callback);
-                wrt.handleCertificateError(id, webContents, certificate.data, url);
-            } else {
-                console.log('Certificate could not be opened');
-                callback(false);
-            }
-        });
-        function handlePermissionRequests(webContents, permission, callback) {
-            console.log(`handlePermissionRequests for ${permission}`);
-            if (permission === 'notifications') {
-                const id = ++self.pendingID;
-                console.log(`Raising a notification permission request with id: ${id}`);
-                self.pendingCallbacks.set(id, callback);
-                wrt.handleNotificationPermissionRequest(id, webContents);
-            } else if (permission === 'media') {
-                const id = ++self.pendingID;
-                console.log(`Raising a media permission request with id: ${id}`);
-                self.pendingCallbacks.set(id, callback);
-                wrt.handleMediaPermissionRequest(id, webContents);
-            } else {
-                /* electron by default allows permission for all if no request handler
-                   is there; so granting permission only temporarily to not have any
-                   side effects */
-                callback(true);
-            }
-        }
-        this.mainWindow.webContents.session.setPermissionRequestHandler(handlePermissionRequests);
-        app.on('web-contents-created', function(event, webContents) {
-            const ses = webContents.session;
-            ses.setPermissionRequestHandler(handlePermissionRequests);
-        });
-        app.on('login', function(event, webContents, request, authInfo, callback) {
-            console.log('Login info is required');
-            event.preventDefault();
-            const id = ++self.pendingID;
-            console.log(`Raising a login info request with id: ${id}`);
-            self.pendingCallbacks.set(id, callback);
-            wrt.handleAuthRequest(id, webContents);
-        });
     }
     suspend() {
         console.log('WebApplication : suspend');
         let windows = BrowserWindow.getAllWindows();
-        windows.forEach((window) => window.hide());
         if (!this.multitaskingSupport) {
             console.log('multitasking is not supported; quitting app')
             app.quit();
         } else if (!this.backgroundSupport) {
             windows.forEach((window) => window.setEnabled(false));
         }
+        this.windowList[this.windowList.length - 1].hide();
     }
     resume() {
         console.log('WebApplication : resume');
@@ -205,10 +214,10 @@ class WebApplication {
             return;
         }
         BrowserWindow.getAllWindows().forEach((window) => {
-            window.show();
             if (!this.backgroundSupport)
                 window.setEnabled(true);
         });
+        this.windowList[this.windowList.length - 1].show();
     }
     sendAppControlEvent() {
         const kAppControlEventScript =