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;
let uniqueSandboxId = 1;
// let {console} = Cu.import('resource://gre/modules/devtools/Console.jsm', null);
/******************************************************************************/
const getMessageManager = function(win) {
@ -112,32 +114,18 @@ const contentObserver = {
// https://bugzil.la/612921
shouldLoad: function(type, location, origin, context) {
// If we don't know what initiated the request, probably it's not a tab
if ( !context ) {
return;
}
if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
return this.ACCEPT;
}
let openerURL;
let openerURL, frameId;
if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
if ( type !== this.MAIN_FRAME ) {
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 ) {
if ( type === this.MAIN_FRAME ) {
frameId = -1;
context = context.contentWindow || context;
try {
@ -146,31 +134,33 @@ const contentObserver = {
}
} catch (ex) {}
} 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;
}
// The context for the toolbar popup is an iframe element here,
// 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
let messageManager = getMessageManager(context);
let details = {
openerURL: openerURL || null,
url: location.spec,
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);
}
messageManager.sendRpcMessage(this.cpMessageName, details);
} else {
// Compatibility for older versions
messageManager.sendSyncMessage(this.cpMessageName, details);
}
return this.ACCEPT;
@ -208,6 +198,7 @@ const contentObserver = {
sandbox._sandboxId_ = sandboxId;
sandbox.sendAsyncMessage = messager.sendAsyncMessage;
sandbox.addMessageListener = function(callback) {
if ( this._messageListener_ ) {
this.removeMessageListener(
@ -229,6 +220,7 @@ const contentObserver = {
this._messageListener_
);
}.bind(sandbox);
sandbox.removeMessageListener = function() {
try {
messager.removeMessageListener(

View File

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

View File

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