1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/metro/base/content/WebProgress.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,244 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// Progress heartbeat timer duration (ms) 1.9 +const kHeartbeatDuration = 1000; 1.10 +// Start and end progress screen css margins as percentages 1.11 +const kProgressMarginStart = 30; 1.12 +const kProgressMarginEnd = 70; 1.13 + 1.14 +const WebProgress = { 1.15 + get _identityBox() { return document.getElementById("identity-box"); }, 1.16 + 1.17 + init: function init() { 1.18 + messageManager.addMessageListener("Content:StateChange", this); 1.19 + messageManager.addMessageListener("Content:LocationChange", this); 1.20 + messageManager.addMessageListener("Content:SecurityChange", this); 1.21 + 1.22 + Elements.progress.addEventListener("transitionend", this, true); 1.23 + Elements.tabList.addEventListener("TabSelect", this, true); 1.24 + 1.25 + let urlBar = document.getElementById("urlbar-edit"); 1.26 + urlBar.addEventListener("input", this, false); 1.27 + 1.28 + return this; 1.29 + }, 1.30 + 1.31 + receiveMessage: function receiveMessage(aMessage) { 1.32 + let json = aMessage.json; 1.33 + let tab = Browser.getTabForBrowser(aMessage.target); 1.34 + 1.35 + switch (aMessage.name) { 1.36 + case "Content:StateChange": { 1.37 + if (json.stateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW) { 1.38 + if (json.stateFlags & Ci.nsIWebProgressListener.STATE_START) 1.39 + this._windowStart(json, tab); 1.40 + else if (json.stateFlags & Ci.nsIWebProgressListener.STATE_STOP) 1.41 + this._windowStop(json, tab); 1.42 + } 1.43 + 1.44 + if (json.stateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) { 1.45 + if (json.stateFlags & Ci.nsIWebProgressListener.STATE_START) 1.46 + this._networkStart(json, tab); 1.47 + else if (json.stateFlags & Ci.nsIWebProgressListener.STATE_STOP) 1.48 + this._networkStop(json, tab); 1.49 + } 1.50 + 1.51 + this._progressStep(tab); 1.52 + break; 1.53 + } 1.54 + 1.55 + case "Content:LocationChange": { 1.56 + this._locationChange(json, tab); 1.57 + this._progressStep(tab); 1.58 + break; 1.59 + } 1.60 + 1.61 + case "Content:SecurityChange": { 1.62 + this._securityChange(json, tab); 1.63 + this._progressStep(tab); 1.64 + break; 1.65 + } 1.66 + } 1.67 + }, 1.68 + 1.69 + handleEvent: function handleEvent(aEvent) { 1.70 + switch (aEvent.type) { 1.71 + case "transitionend": 1.72 + this._progressTransEnd(aEvent); 1.73 + break; 1.74 + case "TabSelect": 1.75 + this._onTabSelect(aEvent); 1.76 + break; 1.77 + case "input": 1.78 + this._onUrlBarInput(aEvent); 1.79 + break; 1.80 + } 1.81 + }, 1.82 + 1.83 + _securityChange: function _securityChange(aJson, aTab) { 1.84 + let state = aJson.state; 1.85 + let nsIWebProgressListener = Ci.nsIWebProgressListener; 1.86 + 1.87 + if (state & nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL) { 1.88 + aTab._identityState = "verifiedIdentity"; 1.89 + } else if (state & nsIWebProgressListener.STATE_IS_SECURE) { 1.90 + aTab._identityState = "verifiedDomain"; 1.91 + } else { 1.92 + aTab._identityState = ""; 1.93 + } 1.94 + 1.95 + if (aTab == Browser.selectedTab) { 1.96 + this._identityBox.className = aTab._identityState; 1.97 + } 1.98 + }, 1.99 + 1.100 + _locationChange: function _locationChange(aJson, aTab) { 1.101 + let spec = aJson.location; 1.102 + let location = spec.split("#")[0]; // Ignore fragment identifier changes. 1.103 + 1.104 + if (aTab == Browser.selectedTab) { 1.105 + BrowserUI.updateURI(); 1.106 + BrowserUI.update(); 1.107 + BrowserUI.updateStartURIAttributes(aJson.location); 1.108 + } 1.109 + 1.110 + let locationHasChanged = (location != aTab.browser.lastLocation); 1.111 + if (locationHasChanged) { 1.112 + Browser.getNotificationBox(aTab.browser).removeTransientNotifications(); 1.113 + aTab.browser.lastLocation = location; 1.114 + aTab.browser.userTypedValue = ""; 1.115 + aTab.browser.appIcon = { href: null, size:-1 }; 1.116 + 1.117 +#ifdef MOZ_CRASHREPORTER 1.118 + if (CrashReporter.enabled) 1.119 + CrashReporter.annotateCrashReport("URL", spec); 1.120 +#endif 1.121 + } 1.122 + 1.123 + let event = document.createEvent("UIEvents"); 1.124 + event.initUIEvent("URLChanged", true, false, window, locationHasChanged); 1.125 + aTab.browser.dispatchEvent(event); 1.126 + }, 1.127 + 1.128 + _networkStart: function _networkStart(aJson, aTab) { 1.129 + aTab.startLoading(); 1.130 + 1.131 + if (aTab == Browser.selectedTab) { 1.132 + // NO_STARTUI_VISIBILITY since the current uri for the tab has not 1.133 + // been updated yet. If we're coming off of the start page, this 1.134 + // would briefly show StartUI until _locationChange is called. 1.135 + BrowserUI.update(BrowserUI.NO_STARTUI_VISIBILITY); 1.136 + } 1.137 + }, 1.138 + 1.139 + _networkStop: function _networkStop(aJson, aTab) { 1.140 + aTab.endLoading(); 1.141 + 1.142 + if (aTab == Browser.selectedTab) { 1.143 + BrowserUI.update(); 1.144 + } 1.145 + }, 1.146 + 1.147 + _windowStart: function _windowStart(aJson, aTab) { 1.148 + this._progressStart(aJson, aTab); 1.149 + }, 1.150 + 1.151 + _windowStop: function _windowStop(aJson, aTab) { 1.152 + this._progressStop(aJson, aTab); 1.153 + }, 1.154 + 1.155 + _progressStart: function _progressStart(aJson, aTab) { 1.156 + // We will get multiple calls from _windowStart, so 1.157 + // only process once. 1.158 + if (aTab._progressActive) 1.159 + return; 1.160 + 1.161 + aTab._progressActive = true; 1.162 + 1.163 + // 'Whoosh' in 1.164 + aTab._progressCount = kProgressMarginStart; 1.165 + this._showProgressBar(aTab); 1.166 + }, 1.167 + 1.168 + _showProgressBar: function (aTab) { 1.169 + // display the track 1.170 + if (aTab == Browser.selectedTab) { 1.171 + Elements.progressContainer.removeAttribute("collapsed"); 1.172 + Elements.progress.style.width = aTab._progressCount + "%"; 1.173 + Elements.progress.removeAttribute("fade"); 1.174 + } 1.175 + 1.176 + // Create a pulse timer to keep things moving even if we don't 1.177 + // collect any state changes. 1.178 + setTimeout(function() { 1.179 + WebProgress._progressStepTimer(aTab); 1.180 + }, kHeartbeatDuration, this); 1.181 + }, 1.182 + 1.183 + _stepProgressCount: function _stepProgressCount(aTab) { 1.184 + // Step toward the end margin in smaller slices as we get closer 1.185 + let left = kProgressMarginEnd - aTab._progressCount; 1.186 + let step = left * .05; 1.187 + aTab._progressCount += Math.ceil(step); 1.188 + 1.189 + // Don't go past the 'whoosh out' margin. 1.190 + if (aTab._progressCount > kProgressMarginEnd) { 1.191 + aTab._progressCount = kProgressMarginEnd; 1.192 + } 1.193 + }, 1.194 + 1.195 + _progressStep: function _progressStep(aTab) { 1.196 + if (!aTab._progressActive) 1.197 + return; 1.198 + this._stepProgressCount(aTab); 1.199 + if (aTab == Browser.selectedTab) { 1.200 + Elements.progress.style.width = aTab._progressCount + "%"; 1.201 + } 1.202 + }, 1.203 + 1.204 + _progressStepTimer: function _progressStepTimer(aTab) { 1.205 + if (!aTab._progressActive) 1.206 + return; 1.207 + this._progressStep(aTab); 1.208 + 1.209 + setTimeout(function() { 1.210 + WebProgress._progressStepTimer(aTab); 1.211 + }, kHeartbeatDuration, this); 1.212 + }, 1.213 + 1.214 + _progressStop: function _progressStop(aJson, aTab) { 1.215 + aTab._progressActive = false; 1.216 + // 'Whoosh out' and fade 1.217 + if (aTab == Browser.selectedTab) { 1.218 + Elements.progress.style.width = "100%"; 1.219 + Elements.progress.setAttribute("fade", true); 1.220 + } 1.221 + }, 1.222 + 1.223 + _progressTransEnd: function _progressTransEnd(aEvent) { 1.224 + if (!Elements.progress.hasAttribute("fade")) 1.225 + return; 1.226 + // Close out fade finished, reset 1.227 + if (aEvent.propertyName == "opacity") { 1.228 + Elements.progress.style.width = "0px"; 1.229 + Elements.progressContainer.setAttribute("collapsed", true); 1.230 + } 1.231 + }, 1.232 + 1.233 + _onTabSelect: function(aEvent) { 1.234 + let tab = Browser.getTabFromChrome(aEvent.originalTarget); 1.235 + this._identityBox.className = tab._identityState || ""; 1.236 + if (tab._progressActive) { 1.237 + this._showProgressBar(tab); 1.238 + } else { 1.239 + Elements.progress.setAttribute("fade", true); 1.240 + Elements.progressContainer.setAttribute("collapsed", true); 1.241 + } 1.242 + }, 1.243 + 1.244 + _onUrlBarInput: function(aEvent) { 1.245 + Browser.selectedTab._identityState = this._identityBox.className = ""; 1.246 + }, 1.247 +};