Merge pull request #1135 from AlexVallat/master

Use webProgressListener instead of tabsProgressListener
This commit is contained in:
Deathamns 2015-03-31 13:33:53 +02:00
commit b053f1eccf
3 changed files with 78 additions and 84 deletions

View File

@ -23,10 +23,12 @@
/******************************************************************************/ /******************************************************************************/
this.EXPORTED_SYMBOLS = ['contentObserver']; this.EXPORTED_SYMBOLS = ['contentObserver', 'LocationChangeListener'];
const {interfaces: Ci, utils: Cu} = Components; const {interfaces: Ci, utils: Cu} = Components;
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
const {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
const hostName = Services.io.newURI(Components.stack.filename, null, null).host; const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
// Cu.import('resource://gre/modules/devtools/Console.jsm'); // Cu.import('resource://gre/modules/devtools/Console.jsm');
@ -65,16 +67,12 @@ const contentObserver = {
.getService(Ci.nsICategoryManager); .getService(Ci.nsICategoryManager);
}, },
QueryInterface: (function() { QueryInterface: XPCOMUtils.generateQI([
let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
return XPCOMUtils.generateQI([
Ci.nsIFactory, Ci.nsIFactory,
Ci.nsIObserver, Ci.nsIObserver,
Ci.nsIContentPolicy, Ci.nsIContentPolicy,
Ci.nsISupportsWeakReference Ci.nsISupportsWeakReference
]); ]),
})(),
createInstance: function(outer, iid) { createInstance: function(outer, iid) {
if ( outer ) { if ( outer ) {
@ -312,6 +310,36 @@ const contentObserver = {
/******************************************************************************/ /******************************************************************************/
const locationChangedMessageName = hostName + ':locationChanged';
const LocationChangeListener = function(docShell) {
if (docShell) {
docShell.QueryInterface(Ci.nsIInterfaceRequestor);
this.docShell = docShell.getInterface(Ci.nsIWebProgress);
this.messageManager = docShell.getInterface(Ci.nsIContentFrameMessageManager);
if (this.messageManager && typeof this.messageManager.sendAsyncMessage === 'function') {
this.docShell.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
}
}
};
LocationChangeListener.prototype.QueryInterface = XPCOMUtils.generateQI(["nsIWebProgressListener", "nsISupportsWeakReference"]);
LocationChangeListener.prototype.onLocationChange = function(webProgress, request, location, flags) {
if ( !webProgress.isTopLevel ) {
return;
}
this.messageManager.sendAsyncMessage(locationChangedMessageName, {
url: location.asciiSpec,
flags: flags,
});
};
/******************************************************************************/
contentObserver.register(); contentObserver.register();
/******************************************************************************/ /******************************************************************************/

View File

@ -21,13 +21,15 @@
/******************************************************************************/ /******************************************************************************/
var locationChangeListener; // Keep alive while frameScript is alive
(function() { (function() {
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
let {contentObserver} = Components.utils.import( let {contentObserver, LocationChangeListener} = Components.utils.import(
Components.stack.filename.replace('Script', 'Module'), Components.stack.filename.replace('Script', 'Module'),
null null
); );
@ -54,6 +56,8 @@ let onLoadCompleted = function() {
addMessageListener('ublock-load-completed', onLoadCompleted); addMessageListener('ublock-load-completed', onLoadCompleted);
locationChangeListener = new LocationChangeListener(docShell);
/******************************************************************************/ /******************************************************************************/
})(); })();

View File

@ -295,11 +295,9 @@ var windowWatcher = {
if ( tabBrowser.deck ) { if ( tabBrowser.deck ) {
// Fennec // Fennec
tabContainer = tabBrowser.deck; tabContainer = tabBrowser.deck;
tabContainer.addEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange);
} else if ( tabBrowser.tabContainer ) { } else if ( tabBrowser.tabContainer ) {
// desktop Firefox // desktop Firefox
tabContainer = tabBrowser.tabContainer; tabContainer = tabBrowser.tabContainer;
tabBrowser.addTabsProgressListener(tabWatcher);
vAPI.contextMenu.register(this.document); vAPI.contextMenu.register(this.document);
} else { } else {
return; return;
@ -321,8 +319,6 @@ var windowWatcher = {
/******************************************************************************/ /******************************************************************************/
var tabWatcher = { var tabWatcher = {
SAME_DOCUMENT: Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT,
onTabClose: function({target}) { onTabClose: function({target}) {
// target is tab in Firefox, browser in Fennec // target is tab in Firefox, browser in Fennec
var tabId = vAPI.tabs.getTabId(target); var tabId = vAPI.tabs.getTabId(target);
@ -331,68 +327,8 @@ var tabWatcher = {
}, },
onTabSelect: function({target}) { onTabSelect: function({target}) {
// target is tab in Firefox, browser in Fennec vAPI.setIcon(vAPI.tabs.getTabId(target), getOwnerWindow(target));
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*/ ) {
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
});
},
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
});
}
}; };
/******************************************************************************/ /******************************************************************************/
@ -444,7 +380,6 @@ vAPI.tabs = {};
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.registerListeners = function() { vAPI.tabs.registerListeners = function() {
// onNavigation and onUpdated handled with tabWatcher.onLocationChange
// onClosed - handled in tabWatcher.onTabClose // onClosed - handled in tabWatcher.onTabClose
// onPopup - handled in httpObserver.handlePopup // onPopup - handled in httpObserver.handlePopup
@ -470,10 +405,8 @@ vAPI.tabs.registerListeners = function() {
if ( tabBrowser.deck ) { if ( tabBrowser.deck ) {
// Fennec // Fennec
tabContainer = tabBrowser.deck; tabContainer = tabBrowser.deck;
tabContainer.removeEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange);
} else if ( tabBrowser.tabContainer ) { } else if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer; tabContainer = tabBrowser.tabContainer;
tabBrowser.removeTabsProgressListener(tabWatcher);
} }
tabContainer.removeEventListener('TabClose', tabWatcher.onTabClose); tabContainer.removeEventListener('TabClose', tabWatcher.onTabClose);
@ -1264,14 +1197,6 @@ var httpObserver = {
return; 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 request is not handled we may use the data in on-modify-request
if ( channel instanceof Ci.nsIWritablePropertyBag ) { if ( channel instanceof Ci.nsIWritablePropertyBag ) {
channel.setProperty(this.REQDATAKEY, [ channel.setProperty(this.REQDATAKEY, [
@ -1396,6 +1321,38 @@ vAPI.net.registerListeners = function() {
shouldLoadListener 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(); httpObserver.register();
cleanupTasks.push(function() { cleanupTasks.push(function() {
@ -1404,6 +1361,11 @@ vAPI.net.registerListeners = function() {
shouldLoadListener shouldLoadListener
); );
vAPI.messaging.globalMessageManager.removeMessageListener(
locationChangedListenerMessageName,
locationChangedListener
);
httpObserver.unregister(); httpObserver.unregister();
}); });
}; };