1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-07-01 12:30:23 +12:00

Firefox: improvements for content scripts

This commit is contained in:
Deathamns 2015-01-08 21:18:05 +01:00 committed by gorhill
parent f570bc7f48
commit de1f643510

View file

@ -25,16 +25,17 @@
this.EXPORTED_SYMBOLS = ['contentObserver']; this.EXPORTED_SYMBOLS = ['contentObserver'];
const {interfaces: Ci, utils: Cu} = this.Components; const {interfaces: Ci, utils: Cu} = Components;
const appName = this.__URI__.match(/:\/\/([^\/]+)/)[1]; const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
let uniqueSandboxId = 1;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm'); Cu.import('resource://gre/modules/devtools/Console.jsm');
/******************************************************************************/ /******************************************************************************/
const getMessageManager = function(context) { const getMessageManager = function(win) {
return context return win
.QueryInterface(Ci.nsIInterfaceRequestor) .QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDocShell) .getInterface(Ci.nsIDocShell)
.sameTypeRootTreeItem .sameTypeRootTreeItem
@ -46,13 +47,14 @@ const getMessageManager = function(context) {
/******************************************************************************/ /******************************************************************************/
const contentObserver = { const contentObserver = {
classDescription: 'content-policy for ' + appName, classDescription: 'content-policy for ' + hostName,
classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'), classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
contractID: '@' + appName + '/content-policy;1', contractID: '@' + hostName + '/content-policy;1',
ACCEPT: Ci.nsIContentPolicy.ACCEPT, ACCEPT: Ci.nsIContentPolicy.ACCEPT,
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT, MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
contentBaseURI: 'chrome://' + appName + '/content/js/', contentBaseURI: 'chrome://' + hostName + '/content/js/',
messageName: appName + ':shouldLoad', cpMessageName: hostName + ':shouldLoad',
frameSandboxes: new WeakMap(),
get componentRegistrar() { get componentRegistrar() {
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar); return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
@ -64,7 +66,7 @@ const contentObserver = {
}, },
QueryInterface: (function() { QueryInterface: (function() {
let {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {}); let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', {});
return XPCOMUtils.generateQI([ return XPCOMUtils.generateQI([
Ci.nsIFactory, Ci.nsIFactory,
@ -84,6 +86,7 @@ const contentObserver = {
register: function() { register: function() {
Services.obs.addObserver(this, 'document-element-inserted', true); Services.obs.addObserver(this, 'document-element-inserted', true);
Services.obs.addObserver(this, 'dom-window-destroyed', true);
this.componentRegistrar.registerFactory( this.componentRegistrar.registerFactory(
this.classID, this.classID,
@ -102,6 +105,7 @@ const contentObserver = {
unregister: function() { unregister: function() {
Services.obs.removeObserver(this, 'document-element-inserted'); Services.obs.removeObserver(this, 'document-element-inserted');
Services.obs.removeObserver(this, 'dom-window-destroyed');
this.componentRegistrar.unregisterFactory(this.classID, this); this.componentRegistrar.unregisterFactory(this.classID, this);
this.categoryManager.deleteCategoryEntry( this.categoryManager.deleteCategoryEntry(
@ -150,7 +154,7 @@ const contentObserver = {
// so check context.top instead // so check context.top instead
if ( context.top && context.location ) { if ( context.top && context.location ) {
// https://bugzil.la/1092216 // https://bugzil.la/1092216
getMessageManager(context).sendRpcMessage(this.messageName, { getMessageManager(context).sendRpcMessage(this.cpMessageName, {
opener: opener || null, opener: opener || null,
url: location.spec, url: location.spec,
type: type, type: type,
@ -164,39 +168,89 @@ const contentObserver = {
initContentScripts: function(win, sandbox) { initContentScripts: function(win, sandbox) {
let messager = getMessageManager(win); let messager = getMessageManager(win);
let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
if ( sandbox ) { if ( sandbox ) {
win = Cu.Sandbox([win], { let sandboxName = [
win.location.href.slice(0, 100),
win.document.title.slice(0, 100)
].join(' | ');
sandbox = Cu.Sandbox([win], {
sandboxName: sandboxId + '[' + sandboxName + ']',
sandboxPrototype: win, sandboxPrototype: win,
wantComponents: false, wantComponents: false,
wantXHRConstructor: false wantXHRConstructor: false
}); });
win.self = win; sandbox.injectScript = function(script, evalCode) {
// anonymous function needs to be used here
win.injectScript = Cu.exportFunction(
function(script, evalCode) {
if ( evalCode ) { if ( evalCode ) {
Cu.evalInSandbox(script, win); Cu.evalInSandbox(script, this);
return; return;
} }
Services.scriptloader.loadSubScript(script, win); Services.scriptloader.loadSubScript(script, this);
}, }.bind(sandbox);
win }
else {
sandbox = win;
}
if ( win !== win.top ) {
this.frameSandboxes.set(win, sandbox);
}
sandbox._sandboxId_ = sandboxId;
sandbox.sendAsyncMessage = messager.sendAsyncMessage;
sandbox.addMessageListener = function(callback) {
if ( this._messageListener_ ) {
this.removeMessageListener(
this._sandboxId_,
this._messageListener_
); );
} }
win.sendAsyncMessage = messager.sendAsyncMessage; this._messageListener_ = function(message) {
win.addMessageListener = messager.ublock_addMessageListener; callback(message.data);
win.removeMessageListener = messager.ublock_removeMessageListener; };
return win; messager.addMessageListener(
this._sandboxId_,
this._messageListener_
);
messager.addMessageListener(
hostName + ':broadcast',
this._messageListener_
);
}.bind(sandbox);
sandbox.removeMessageListener = function() {
messager.removeMessageListener(
this._sandboxId_,
this._messageListener_
);
messager.removeMessageListener(
hostName + ':broadcast',
this._messageListener_
);
this._messageListener_ = null;
}.bind(sandbox);
return sandbox;
}, },
observe: function(doc) { observe: function(subject, topic) {
let win = doc.defaultView; if ( topic === 'dom-window-destroyed' ) {
let sandbox = this.frameSandboxes.get(subject);
if ( sandbox ) {
sandbox.removeMessageListener();
this.frameSandboxes.delete(subject);
}
return;
}
let win = subject.defaultView;
if ( !win ) { if ( !win ) {
return; return;
@ -205,7 +259,7 @@ const contentObserver = {
let loc = win.location; let loc = win.location;
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) { if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
if ( loc.protocol === 'chrome:' && loc.host === appName ) { if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
this.initContentScripts(win); this.initContentScripts(win);
} }
@ -214,17 +268,17 @@ const contentObserver = {
} }
let lss = Services.scriptloader.loadSubScript; let lss = Services.scriptloader.loadSubScript;
win = this.initContentScripts(win, true); let sandbox = this.initContentScripts(win, true);
lss(this.contentBaseURI + 'vapi-client.js', win); lss(this.contentBaseURI + 'vapi-client.js', sandbox);
lss(this.contentBaseURI + 'contentscript-start.js', win); lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
let docReady = function(e) { let docReady = function(e) {
this.removeEventListener(e.type, docReady, true); this.removeEventListener(e.type, docReady, true);
lss(contentObserver.contentBaseURI + 'contentscript-end.js', win); lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
}; };
win.document.addEventListener('DOMContentLoaded', docReady, true); subject.addEventListener('DOMContentLoaded', docReady, true);
} }
}; };