From c5546e322f427615790e91ba476a24ae11163e7c Mon Sep 17 00:00:00 2001 From: AlexVallat Date: Thu, 26 Mar 2015 21:00:56 +0000 Subject: [PATCH] Use a nsIWebProgressListener instead of a tabsProgressListener for location change monitoring --- platform/firefox/frameModule.js | 42 +++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/platform/firefox/frameModule.js b/platform/firefox/frameModule.js index f33154c..78915ca 100644 --- a/platform/firefox/frameModule.js +++ b/platform/firefox/frameModule.js @@ -23,10 +23,12 @@ /******************************************************************************/ -this.EXPORTED_SYMBOLS = ['contentObserver']; +this.EXPORTED_SYMBOLS = ['contentObserver', 'LocationChangeListener']; const {interfaces: Ci, utils: Cu} = Components; 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; // Cu.import('resource://gre/modules/devtools/Console.jsm'); @@ -65,16 +67,12 @@ const contentObserver = { .getService(Ci.nsICategoryManager); }, - QueryInterface: (function() { - let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null); - - return XPCOMUtils.generateQI([ + QueryInterface: XPCOMUtils.generateQI([ Ci.nsIFactory, Ci.nsIObserver, Ci.nsIContentPolicy, Ci.nsISupportsWeakReference - ]); - })(), + ]), createInstance: function(outer, iid) { if ( outer ) { @@ -309,6 +307,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_ALL); + } + } +} + +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(); /******************************************************************************/