Skip to content

Commit

Permalink
Merge branch 'webProgressListener'
Browse files Browse the repository at this point in the history
Conflicts:
	platform/firefox/vapi-background.js
  • Loading branch information
AlexVallat committed Mar 31, 2015
2 parents 22c60bd + 726325e commit ae310df
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 65 deletions.
42 changes: 35 additions & 7 deletions platform/firefox/frameModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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();

/******************************************************************************/
6 changes: 5 additions & 1 deletion platform/firefox/frameScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

/******************************************************************************/

var locationChangeListener; // Keep alive while frameScript is alive

(function() {

'use strict';

/******************************************************************************/

let {contentObserver} = Components.utils.import(
let {contentObserver, LocationChangeListener} = Components.utils.import(
Components.stack.filename.replace('Script', 'Module'),
null
);
Expand All @@ -54,6 +56,8 @@ let onLoadCompleted = function() {

addMessageListener('ublock-load-completed', onLoadCompleted);

locationChangeListener = new LocationChangeListener(docShell);

/******************************************************************************/

})();
Expand Down
94 changes: 37 additions & 57 deletions platform/firefox/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -334,49 +330,6 @@ var tabWatcher = {
vAPI.setIcon(vAPI.tabs.getTabId(target), getOwnerWindow(target));
return;
},

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
});
}

};

/******************************************************************************/
Expand Down Expand Up @@ -428,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

Expand All @@ -454,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);
Expand Down Expand Up @@ -1248,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, [
Expand Down Expand Up @@ -1380,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() {
Expand All @@ -1388,6 +1363,11 @@ vAPI.net.registerListeners = function() {
shouldLoadListener
);

vAPI.messaging.globalMessageManager.removeMessageListener(
locationChangedListenerMessageName,
locationChangedListener
);

httpObserver.unregister();
});
};
Expand Down

0 comments on commit ae310df

Please sign in to comment.