From 3607286a3e7d60efeecb5f04e6d7baaf701996a3 Mon Sep 17 00:00:00 2001 From: AlexVallat Date: Tue, 31 Mar 2015 12:03:35 +0100 Subject: [PATCH] Changes following review --- platform/firefox/vapi-background.js | 115 ++++++++++------------------ 1 file changed, 39 insertions(+), 76 deletions(-) diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 7272b8e..faaf1a5 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -295,11 +295,9 @@ var windowWatcher = { if ( tabBrowser.deck ) { // Fennec tabContainer = tabBrowser.deck; - tabContainer.addEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange); } else if ( tabBrowser.tabContainer ) { // desktop Firefox tabContainer = tabBrowser.tabContainer; - tabBrowser.addTabsProgressListener(tabWatcher); vAPI.contextMenu.register(this.document); } else { return; @@ -321,8 +319,6 @@ var windowWatcher = { /******************************************************************************/ var tabWatcher = { - SAME_DOCUMENT: Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT, - onTabClose: function({target}) { // target is tab in Firefox, browser in Fennec var tabId = vAPI.tabs.getTabId(target); @@ -331,69 +327,9 @@ var tabWatcher = { }, onTabSelect: function({target}) { - // target is tab in Firefox, browser in Fennec - var browser = (target.linkedBrowser || target); - var URI = browser.currentURI; - var aboutPath = URI.schemeIs('about') && URI.path; - var tabId = vAPI.tabs.getTabId(target); - - if ( !aboutPath || (aboutPath !== 'blank' && aboutPath !== 'newtab') ) { - vAPI.setIcon(tabId, getOwnerWindow(target)); - return; - } - - if ( browser.webNavigation.busyFlags === 0 || /*BUSY_FLAGS_NONE*/ - browser.webNavigation.busyFlags === undefined) { - vAPI.tabs.onNavigation({ - frameId: 0, - tabId: tabId, - url: URI.asciiSpec - }); - } - }, - - onLocationChange: function(browser, webProgress, request, location, flags) { - if ( !webProgress.isTopLevel ) { - return; - } - - var tabId = vAPI.tabs.getTabId(browser); - - // LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document" - if ( flags & this.SAME_DOCUMENT ) { - vAPI.tabs.onUpdated(tabId, {url: location.asciiSpec}, { - frameId: 0, - tabId: tabId, - url: browser.currentURI.asciiSpec - }); - return; - } - - // https://github.com/gorhill/uBlock/issues/105 - // Allow any kind of pages - vAPI.tabs.onNavigation({ - frameId: 0, - tabId: tabId, - url: location.asciiSpec - }); + vAPI.setIcon(vAPI.tabs.getTabId(target), getOwnerWindow(target)); + return; }, - - onFennecLocationChange: function({target: doc}) { - // Fennec "equivalent" to onLocationChange - // note that DOMTitleChanged is selected as it fires very early - // (before DOMContentLoaded), and it does fire even if there is no title - var win = doc.defaultView; - if ( win !== win.top ) { - return; - } - - vAPI.tabs.onNavigation({ - frameId: 0, - tabId: vAPI.tabs.getTabId(getOwnerWindow(win).BrowserApp.getTabForWindow(win)), - url: Services.io.newURI(win.location.href, null, null).asciiSpec - }); - } - }; /******************************************************************************/ @@ -445,7 +381,6 @@ vAPI.tabs = {}; /******************************************************************************/ vAPI.tabs.registerListeners = function() { - // onNavigation and onUpdated handled with tabWatcher.onLocationChange // onClosed - handled in tabWatcher.onTabClose // onPopup - handled in httpObserver.handlePopup @@ -471,7 +406,6 @@ vAPI.tabs.registerListeners = function() { if ( tabBrowser.deck ) { // Fennec tabContainer = tabBrowser.deck; - tabContainer.removeEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange); } else if ( tabBrowser.tabContainer ) { tabContainer = tabBrowser.tabContainer; tabBrowser.removeTabsProgressListener(tabWatcher); @@ -1265,14 +1199,6 @@ var httpObserver = { return; } - if ( vAPI.fennec && lastRequest.type === this.MAIN_FRAME ) { - vAPI.tabs.onNavigation({ - frameId: 0, - tabId: lastRequest.tabId, - url: URI.asciiSpec - }); - } - // If request is not handled we may use the data in on-modify-request if ( channel instanceof Ci.nsIWritablePropertyBag ) { channel.setProperty(this.REQDATAKEY, [ @@ -1397,6 +1323,38 @@ vAPI.net.registerListeners = function() { shouldLoadListener ); + var locationChangedListenerMessageName = location.host + ':locationChanged'; + var locationChangedListener = function(e) { + var details = e.data; + var browser = e.target; + var tabId = vAPI.tabs.getTabId(browser); + + //console.debug("nsIWebProgressListener: onLocationChange: " + details.url + " (" + details.flags + ")"); + + // LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document" + if ( details.flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT ) { + vAPI.tabs.onUpdated(tabId, {url: details.url}, { + frameId: 0, + tabId: tabId, + url: browser.currentURI.asciiSpec + }); + return; + } + + // https://github.com/gorhill/uBlock/issues/105 + // Allow any kind of pages + vAPI.tabs.onNavigation({ + frameId: 0, + tabId: tabId, + url: details.url, + }); + } + + vAPI.messaging.globalMessageManager.addMessageListener( + locationChangedListenerMessageName, + locationChangedListener + ); + httpObserver.register(); cleanupTasks.push(function() { @@ -1405,6 +1363,11 @@ vAPI.net.registerListeners = function() { shouldLoadListener ); + vAPI.messaging.globalMessageManager.removeMessageListener( + locationChangedListenerMessageName, + locationChangedListener + ); + httpObserver.unregister(); }); };