1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Firefox: behind-the-scene requests / non-http tabs

This commit is contained in:
Deathamns 2015-01-26 20:26:45 +01:00
parent 44fa4d72d0
commit 263b756764
3 changed files with 67 additions and 81 deletions

View File

@ -30,6 +30,8 @@ const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
const hostName = Services.io.newURI(Components.stack.filename, null, null).host; const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
let uniqueSandboxId = 1; let uniqueSandboxId = 1;
// let {console} = Cu.import('resource://gre/modules/devtools/Console.jsm', null);
/******************************************************************************/ /******************************************************************************/
const getMessageManager = function(win) { const getMessageManager = function(win) {
@ -112,32 +114,18 @@ const contentObserver = {
// https://bugzil.la/612921 // https://bugzil.la/612921
shouldLoad: function(type, location, origin, context) { shouldLoad: function(type, location, origin, context) {
// If we don't know what initiated the request, probably it's not a tab
if ( !context ) { if ( !context ) {
return;
}
if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
return this.ACCEPT; return this.ACCEPT;
} }
let openerURL; let openerURL, frameId;
if ( !location.schemeIs('http') && !location.schemeIs('https') ) { if ( type === this.MAIN_FRAME ) {
if ( type !== this.MAIN_FRAME ) { frameId = -1;
return this.ACCEPT;
}
context = context.contentWindow || context;
try {
if ( context !== context.opener ) {
openerURL = context.opener.location.href;
}
} catch (ex) {}
let isPopup = location.spec === 'about:blank' && openerURL;
if ( !location.schemeIs('data') && !isPopup ) {
return this.ACCEPT;
}
} else if ( type === this.MAIN_FRAME ) {
context = context.contentWindow || context; context = context.contentWindow || context;
try { try {
@ -146,31 +134,33 @@ const contentObserver = {
} }
} catch (ex) {} } catch (ex) {}
} else { } else {
// TODO: frameId from outerWindowID?
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
frameId = context === context.top ? 0 : 1;
context = (context.ownerDocument || context).defaultView; context = (context.ownerDocument || context).defaultView;
} }
// The context for the toolbar popup is an iframe element here, // The context for the toolbar popup is an iframe element here,
// so check context.top instead of context // so check context.top instead of context
if ( context.top && context.location ) { if ( !context.top || !context.location ) {
return this.ACCEPT;
}
let messageManager = getMessageManager(context);
let details = {
frameId: frameId,
openerURL: openerURL || null,
parentFrameId: context === context.top ? -1 : 0,
type: type,
url: location.spec
};
if ( typeof messageManager.sendRpcMessage === 'function' ) {
// https://bugzil.la/1092216 // https://bugzil.la/1092216
let messageManager = getMessageManager(context); messageManager.sendRpcMessage(this.cpMessageName, details);
let details = { } else {
openerURL: openerURL || null, // Compatibility for older versions
url: location.spec, messageManager.sendSyncMessage(this.cpMessageName, details);
type: type,
frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
parentFrameId: context === context.top ? -1 : 0
};
// TODO: frameId from outerWindowID?
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
if ( typeof messageManager.sendRpcMessage === 'function' ) {
messageManager.sendRpcMessage(this.cpMessageName, details);
} else {
// Compatibility for older versions
messageManager.sendSyncMessage(this.cpMessageName, details);
}
} }
return this.ACCEPT; return this.ACCEPT;
@ -208,6 +198,7 @@ const contentObserver = {
sandbox._sandboxId_ = sandboxId; sandbox._sandboxId_ = sandboxId;
sandbox.sendAsyncMessage = messager.sendAsyncMessage; sandbox.sendAsyncMessage = messager.sendAsyncMessage;
sandbox.addMessageListener = function(callback) { sandbox.addMessageListener = function(callback) {
if ( this._messageListener_ ) { if ( this._messageListener_ ) {
this.removeMessageListener( this.removeMessageListener(
@ -229,6 +220,7 @@ const contentObserver = {
this._messageListener_ this._messageListener_
); );
}.bind(sandbox); }.bind(sandbox);
sandbox.removeMessageListener = function() { sandbox.removeMessageListener = function() {
try { try {
messager.removeMessageListener( messager.removeMessageListener(

View File

@ -335,10 +335,10 @@ vAPI.tabs = {};
/******************************************************************************/ /******************************************************************************/
vAPI.isNoTabId = function(tabId) { vAPI.isNoTabId = function(tabId) {
return tabId.toString() === '_'; return tabId.toString() === '-1';
}; };
vAPI.noTabId = '_'; vAPI.noTabId = '-1';
/******************************************************************************/ /******************************************************************************/
@ -387,12 +387,20 @@ vAPI.tabs.getTabId = function(target) {
var i, gBrowser = target.ownerDocument.defaultView.gBrowser; var i, gBrowser = target.ownerDocument.defaultView.gBrowser;
if ( !gBrowser ) {
return -1;
}
// This should be more efficient from version 35 // This should be more efficient from version 35
if ( gBrowser.getTabForBrowser ) { if ( gBrowser.getTabForBrowser ) {
i = gBrowser.getTabForBrowser(target); i = gBrowser.getTabForBrowser(target);
return i ? i.linkedPanel : -1; return i ? i.linkedPanel : -1;
} }
if ( !gBrowser.browsers ) {
return -1;
}
i = gBrowser.browsers.indexOf(target); i = gBrowser.browsers.indexOf(target);
if ( i !== -1 ) { if ( i !== -1 ) {
@ -795,6 +803,7 @@ var httpObserver = {
VALID_CSP_TARGETS: 1 << Ci.nsIContentPolicy.TYPE_DOCUMENT | VALID_CSP_TARGETS: 1 << Ci.nsIContentPolicy.TYPE_DOCUMENT |
1 << Ci.nsIContentPolicy.TYPE_SUBDOCUMENT, 1 << Ci.nsIContentPolicy.TYPE_SUBDOCUMENT,
typeMap: { typeMap: {
1: 'other',
2: 'script', 2: 'script',
3: 'image', 3: 'image',
4: 'stylesheet', 4: 'stylesheet',
@ -891,7 +900,7 @@ var httpObserver = {
return result === true; return result === true;
}, },
handleRequest: function(channel, details) { handleRequest: function(channel, URI, details) {
var onBeforeRequest = vAPI.net.onBeforeRequest; var onBeforeRequest = vAPI.net.onBeforeRequest;
var type = this.typeMap[details.type] || 'other'; var type = this.typeMap[details.type] || 'other';
@ -901,11 +910,11 @@ var httpObserver = {
var result = onBeforeRequest.callback({ var result = onBeforeRequest.callback({
frameId: details.frameId, frameId: details.frameId,
hostname: channel.URI.asciiHost, hostname: URI.asciiHost,
parentFrameId: details.parentFrameId, parentFrameId: details.parentFrameId,
tabId: details.tabId, tabId: details.tabId,
type: type, type: type,
url: channel.URI.asciiSpec url: URI.asciiSpec
}); });
if ( !result || typeof result !== 'object' ) { if ( !result || typeof result !== 'object' ) {
@ -987,8 +996,8 @@ var httpObserver = {
var lastRequest = this.lastRequest; var lastRequest = this.lastRequest;
if ( !lastRequest.url || lastRequest.url !== URI.spec ) { if ( lastRequest.url === null ) {
lastRequest.url = null; this.handleRequest(channel, URI, {tabId: vAPI.noTabId, type: 1});
return; return;
} }
@ -1023,7 +1032,7 @@ var httpObserver = {
} }
} }
if ( this.handleRequest(channel, lastRequest) ) { if ( this.handleRequest(channel, URI, lastRequest) ) {
return; return;
} }
@ -1074,7 +1083,7 @@ var httpObserver = {
parentFrameId: -1 parentFrameId: -1
}; };
if ( this.handleRequest(newChannel, details) ) { if ( this.handleRequest(newChannel, URI, details) ) {
result = this.ABORT; result = this.ABORT;
return; return;
} }
@ -1106,19 +1115,6 @@ vAPI.net.registerListeners = function() {
var shouldLoadListenerMessageName = location.host + ':shouldLoad'; var shouldLoadListenerMessageName = location.host + ':shouldLoad';
var shouldLoadListener = function(e) { var shouldLoadListener = function(e) {
var details = e.data; var details = e.data;
// data: and about:blank
if ( details.url.charAt(0) !== 'h' ) {
vAPI.net.onBeforeRequest.callback({
frameId: details.frameId,
parentFrameId: details.parentFrameId,
tabId: vAPI.tabs.getTabId(e.target),
type: 'main_frame',
url: 'http://' + details.url.slice(0, details.url.indexOf(':'))
});
return;
}
var lastRequest = httpObserver.lastRequest; var lastRequest = httpObserver.lastRequest;
lastRequest.url = details.url; lastRequest.url = details.url;
lastRequest.type = details.type; lastRequest.type = details.type;

View File

@ -143,28 +143,26 @@ vAPI.messaging = {
}; };
return this.channels[channelName]; return this.channels[channelName];
},
toggleListener: function({type, persisted}) {
if ( !vAPI.messaging.connector ) {
return;
}
if ( type === 'pagehide' ) {
removeMessageListener();
return;
}
if ( persisted ) {
addMessageListener(vAPI.messaging.connector);
}
} }
}; };
/******************************************************************************/ window.addEventListener('pagehide', vAPI.messaging.toggleListener, true);
window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
var toggleListener = function({type, persisted}) {
if ( !vAPI.messaging.connector ) {
return;
}
if ( type === 'pagehide' ) {
removeMessageListener();
return;
}
if ( persisted ) {
addMessageListener(vAPI.messaging.connector);
}
};
window.addEventListener('pagehide', toggleListener, true);
window.addEventListener('pageshow', toggleListener, true);
/******************************************************************************/ /******************************************************************************/