Simplify complicated condition and improve code style 27/213027/29
authorDongHyun Song <dh81.song@samsung.com>
Thu, 29 Aug 2019 09:19:43 +0000 (18:19 +0900)
committerSangYong Park <sy302.park@samsung.com>
Tue, 17 Sep 2019 09:26:57 +0000 (09:26 +0000)
1. Eliminating unnecessary condition check
2. Unfiy variables style
3. Only set cookie path one times

Change-Id: I7eb6eaa4ac6ccf8e1cb2b1d2aee865705e66f776
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/src/runtime.js
wrt_app/src/web_application.js [changed mode: 0644->0755]

index 5933836..f4c97e3 100755 (executable)
@@ -29,8 +29,7 @@ class Runtime {
         this.handleIpcMessages();
         this.addonManager = null;
         this.isLaunched = false;
-        this.debugMode = false;
-        this.needInspector = false;
+        this.inspectorEnabledByVconf = false;
         this.webContents = null;
         this.allowQuit = false;
 
@@ -119,27 +118,27 @@ class Runtime {
                 app.emit('ready');
             } else {
                 console.log('Tizen Web App launch');
+                let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode');
                 if (!_this.webApplication) {
                     console.log('Creating WebApplication');
                     options.isAddonAvailable = !options.noAddons &&
                             _this.addonManager.isAddonAvailable();
-                    options.launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode');
+                    options.launchMode = launchMode;
                     _this.webApplication = new WebApplication(options);
-                    if (!_this.debugMode && wrt.needUseInspector() &&
-                            options.launchMode != 'backgroundExecution') {
-                        _this.webApplication.defaultSrc = src;
+                    _this.inspectorEnabledByVconf = wrt.needUseInspector();
+                    if (_this.inspectorEnabledByVconf && launchMode != 'backgroundExecution') {
+                        _this.webApplication.inspectorSrc = src;
                         src = "about:blank";
                     }
                     _this.webApplication.mainWindow.loadURL(src);
                     _this.webApplication.prelaunch(_this.addonManager.prelaunch_path, src);
                 } else {
                     console.log('Handling app-control event');
-                    if (_this.webApplication.preloadState == 'readyToShow') {
+                    if (_this.webApplication.preloadStatus == 'readyToShow') {
                         _this.webApplication.show();
                     } else {
-                        let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode');
                         if (launchMode != 'backgroundAtStartup') {
-                            _this.webApplication.preloadState = 'none';
+                            _this.webApplication.preloadStatus = 'none';
                         }
                     }
 
@@ -168,14 +167,7 @@ class Runtime {
                     }
                 }
             }
-            // FIX ME : It must be supplemented to set a specific path
-            wrt.setCookiePath();
-
-            // AUL public key/Vconf - To support inspector
-            if (!_this.debugMode && _this.checkInspectorCondition(appControl)) {
-                _this.debugMode = true;
-                _this.launchInspector(appControl);
-            }
+            _this.configureRuntime(appControl);
         });
         wrt.on('suspend', function() {
             console.log('suspend');
@@ -212,20 +204,27 @@ class Runtime {
         });
     }
     checkInspectorCondition(appControl) {
-        var _this = this;
-        let bundle_debug = (appControl.getData('__AUL_DEBUG__') === "1");
-        _this.needInspector =  wrt.needUseInspector();
-        return (bundle_debug || _this.needInspector);
+        let bundleDebug = (appControl.getData('__AUL_DEBUG__') === "1");
+        return (bundleDebug || this.inspectorEnabledByVconf);
     }
     launchInspector(appControl) {
-        var _this   = this;
         var portnum = wrt.getDebuggingPort();
         var data    = { "port" :  [ portnum.toString() ] };
-        if(_this.needInspector) {
-            _this.webApplication.debugport = portnum;
-        }
+        if (this.webApplication)
+            this.webApplication.debugPort = portnum;
         appControl.reply(data);
     }
+    configureRuntime(appControl) {
+        this.configureRuntime = (param) => {}; // call once
+
+        // FIX ME : It must be supplemented to set a specific path
+        wrt.setCookiePath();
+
+        // AUL public key/Vconf - To support inspector
+        if (this.checkInspectorCondition(appControl)) {
+            this.launchInspector(appControl);
+        }
+    }
     handleKeyEvents(key) {
         let path = null;
         let _this = this;
old mode 100644 (file)
new mode 100755 (executable)
index 1f791ba..3606fe5
@@ -34,19 +34,17 @@ class WebApplication {
         this.firstRendered = false;
         this.backgroundSupport = wrt.getBackgroundSupport();
         this.multitaskingSupport = wrt.getMultitaskingSupport();
-        this.debugport = 0;
-        this.debugDialogShow = false;
-        this.defaultSrc = '';
+        this.debugPort = 0;
+        this.inspectorSrc = '';
         if (options.launchMode == 'backgroundAtStartup') {
             console.log('backgroundAtStartup');
-            this.preloadState = 'preload';
+            this.preloadStatus = 'preload';
         } else {
-            this.preloadState = 'none';
+            this.preloadStatus = 'none';
         }
         if (options.launchMode == 'backgroundExecution') {
             console.log('backgroundExecution');
             this.backgroundExecution = true;
-            this.backgroundSupport = true;
         } else {
             this.backgroundExecution = false;
         }
@@ -175,17 +173,22 @@ class WebApplication {
         });
         wrt.on('app-status-changed', function(event, status) {
             console.log(`runningStatus: ${status}, ${self.loadFinished}`);
+            if (!self.isTVProfile) {
+                return;
+            }
             self.runningStatus = status;
-            if (self.loadFinished && self.runningStatus == 'behind') {
+            if (self.runningStatus === 'DialogClose' && self.inspectorSrc) {
+                console.log(`runningStatus is DialogClose, src is ${self.inspectorSrc}`);
+                self.mainWindow.loadURL(self.inspectorSrc);
+                self.inspectorSrc = '';
+            } else if (self.runningStatus == 'behind' && self.loadFinished) {
                 self.suspend();
             }
-            if (self.debugDialogShow && self.runningStatus == 'DialogClose') {
-                console.log(`runningStatus is DialogClose, src is ${self.defaultSrc}`);
-                self.debugDialogShow = false;
-                self.mainWindow.loadURL(self.defaultSrc);
-            }
         });
     }
+    backgroundRunnable() {
+        return this.backgroundSupport || this.backgroundExecution;
+    }
     getWindowOption(options) {
         return {
             fullscreen: false,
@@ -228,8 +231,8 @@ class WebApplication {
                 clearTimeout(self.showTimer);
             wrt.hideSplashScreen(1);
             self.firstRendered = true;
-            if (self.preloadState == 'preload') {
-                self.preloadState = 'readyToShow';
+            if (self.preloadStatus == 'preload') {
+                self.preloadStatus = 'readyToShow';
                 console.log('preloading show is skipped!');
                 return;
             }
@@ -240,31 +243,42 @@ class WebApplication {
             self.loadFinished = false;
         });
         this.mainWindow.webContents.on('did-finish-load', function() {
-            console.log(`webContents did-finish-load preloadState: ${self.preloadState}, status: ${self.runningStatus}`);
+            console.log('webContents did-finish-load');
             self.loadFinished = true;
             wrt.hideSplashScreen(2);
-            if (wrt.isIMEWebApp())
+            if (wrt.isIMEWebApp()) {
                 self.activateIMEWebHelperClient();
-            if (self.preloadState === 'readyToShow' || self.preloadState === 'preload' || self.runningStatus === 'behind') {
-                self.suspend();
-                if (self.runningStatus !== 'behind')
-                    wrt.notifyAppStatus('preload');
-            }
-            if(self.debugport && self.isTVProfile && !self.backgroundExecution) {
-                let message = self.debugport.toString() +
-                    "\r\nFast RWI is used, [about:blank] is loaded fist instead of \r\n[" +
-                    self.defaultSrc +
-                    "]\r\nClick OK button will start the real loading.\r\nNotes:\r\nPlease " +
-                    "connect to RWI in PC before click OK button.\r\nThen you can get " +
-                    "network log from the initial loading.\r\nPlease click Record button " +
-                    "in Timeline panel in PC before click OK button,\r\nThen you can get " +
-                    "profile log from the initial loading."
-                self.debugDialogShow = true;
-                self.debugport = 0;
-                wrt.modalDialog(self.mainWindow.webContents, message);
+            } else if (self.isTVProfile) {
+                if (self.inspectorSrc) {
+                    self.showInspectorGuide();
+                } else {
+                    self.suspendByStatus();
+                }
             }
         });
     }
+    suspendByStatus() {
+        if (this.preloadStatus === 'readyToShow' ||
+            this.preloadStatus === 'preload' ||
+            this.runningStatus === 'behind') {
+            console.log(`preloadStatus: ${this.preloadStatus}, runningStatus: ${this.runningStatus}`);
+            this.suspend();
+            if (this.runningStatus !== 'behind')
+                wrt.notifyAppStatus('preload');
+        }
+    }
+    showInspectorGuide() {
+        this.showInspectorGuide = () => {}; // call once
+        let message = this.debugPort.toString() +
+            "\r\nFast RWI is used, [about:blank] is loaded fist instead of \r\n[" +
+            this.inspectorSrc +
+            "]\r\nClick OK button will start the real loading.\r\nNotes:\r\nPlease " +
+            "connect to RWI in PC before click OK button.\r\nThen you can get " +
+            "network log from the initial loading.\r\nPlease click Record button " +
+            "in Timeline panel in PC before click OK button,\r\nThen you can get " +
+            "profile log from the initial loading."
+        wrt.modalDialog(this.mainWindow.webContents, message);
+    }
     suspend(path) {
         console.log('WebApplication : suspend');
         if (path != null) {
@@ -288,7 +302,7 @@ class WebApplication {
                 console.log('multitasking is not supported; quitting app')
                 app.quit();
             }, 1000);
-        } else if (!this.backgroundSupport) {
+        } else if (!this.backgroundRunnable()) {
             this.windowList.forEach((window) => window.setEnabled(false));
         }
         this.windowList[this.windowList.length - 1].hide();
@@ -310,12 +324,13 @@ class WebApplication {
             }
             return;
         }
-        if (!this.backgroundSupport) {
+        if (!this.backgroundRunnable()) {
             this.windowList.forEach((window) => window.setEnabled(true));
         }
         this.windowList[this.windowList.length - 1].show();
     }
     finalize() {
+        this.inspectorSrc = '';
         this.windowList.forEach((window) => {
             window.removeAllListeners();
         });
@@ -354,8 +369,10 @@ class WebApplication {
     }
     show() {
         console.log('WebApplication : show');
-        this.preloadState = 'none';
-        if (!this.backgroundExecution && !this.mainWindow.isVisible()) {
+        this.preloadStatus = 'none';
+        if (this.backgroundExecution) {
+            console.log('skip showing while backgroundExecution mode');
+        } else if (!this.mainWindow.isVisible()) {
             console.log('show window');
             this.mainWindow.show();
         }