2014-12-17 21:33:53 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µBlock - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014 The µBlock authors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
/* global Services, Components, XPCOMUtils, __URI__ */
|
2014-12-09 21:56:17 +01:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
this.EXPORTED_SYMBOLS = ['contentPolicy', 'docObserver'];
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
const {interfaces: Ci, utils: Cu} = Components;
|
2014-12-20 17:43:10 +01:00
|
|
|
const appName = __URI__.match(/:\/\/([^\/]+)/)[1];
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
Cu['import']('resource://gre/modules/Services.jsm');
|
|
|
|
Cu['import']('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
// Cu['import']('resource://gre/modules/devtools/Console.jsm');
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
const getMessageManager = function(context) {
|
|
|
|
return context
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDocShell)
|
|
|
|
.sameTypeRootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
2014-12-20 17:43:10 +01:00
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIContentFrameMessageManager);
|
2014-12-24 23:11:36 +01:00
|
|
|
};
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-20 20:20:23 +01:00
|
|
|
const contentPolicy = {
|
2014-12-17 10:33:02 +01:00
|
|
|
classDescription: 'content-policy implementation for ' + appName,
|
2014-12-09 21:56:17 +01:00
|
|
|
classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
|
2014-12-16 13:44:34 +01:00
|
|
|
contractID: '@' + appName + '/content-policy;1',
|
2014-12-09 21:56:17 +01:00
|
|
|
ACCEPT: Ci.nsIContentPolicy.ACCEPT,
|
2014-12-24 23:11:36 +01:00
|
|
|
messageName: appName + ':shouldLoad',
|
2014-12-09 21:56:17 +01:00
|
|
|
get componentRegistrar() {
|
|
|
|
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
},
|
|
|
|
get categoryManager() {
|
|
|
|
return Components.classes['@mozilla.org/categorymanager;1']
|
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
},
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIFactory,
|
|
|
|
Ci.nsIContentPolicy,
|
|
|
|
Ci.nsISupportsWeakReference
|
|
|
|
]),
|
|
|
|
createInstance: function(outer, iid) {
|
|
|
|
if (outer) {
|
|
|
|
throw Components.results.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
register: function() {
|
|
|
|
this.componentRegistrar.registerFactory(
|
|
|
|
this.classID,
|
|
|
|
this.classDescription,
|
|
|
|
this.contractID,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.categoryManager.addCategoryEntry(
|
|
|
|
'content-policy',
|
|
|
|
this.contractID,
|
|
|
|
this.contractID,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
},
|
|
|
|
unregister: function() {
|
|
|
|
this.componentRegistrar.unregisterFactory(this.classID, this);
|
|
|
|
this.categoryManager.deleteCategoryEntry(
|
|
|
|
'content-policy',
|
|
|
|
this.contractID,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
2014-12-24 23:11:36 +01:00
|
|
|
// https://bugzil.la/612921
|
2014-12-09 21:56:17 +01:00
|
|
|
shouldLoad: function(type, location, origin, context) {
|
2014-12-28 10:56:09 +01:00
|
|
|
// If we don't know what initiated the request, probably it's not a tab
|
|
|
|
if ( !context ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
|
2014-12-09 21:56:17 +01:00
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:35:14 +01:00
|
|
|
let win = type === 6
|
2014-12-16 18:55:41 +01:00
|
|
|
? context.contentWindow || context
|
2014-12-16 16:35:14 +01:00
|
|
|
: (context.ownerDocument || context).defaultView;
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
if ( win ) {
|
2014-12-24 23:11:36 +01:00
|
|
|
getMessageManager(win).sendSyncMessage(this.messageName, {
|
|
|
|
url: location.spec,
|
|
|
|
type: type,
|
|
|
|
frameId: type === 6 ? -1 : (win === win.top ? 0 : 1),
|
|
|
|
parentFrameId: win === win.top ? -1 : 0
|
|
|
|
});
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.ACCEPT;
|
2014-12-24 23:11:36 +01:00
|
|
|
}
|
2014-12-09 21:56:17 +01:00
|
|
|
};
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-20 20:20:23 +01:00
|
|
|
const docObserver = {
|
2014-12-28 10:56:09 +01:00
|
|
|
contentBaseURI: 'chrome://' + appName + '/content/js/',
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
|
|
|
]),
|
2014-12-09 21:56:17 +01:00
|
|
|
initContext: function(win, sandbox) {
|
2014-12-24 23:11:36 +01:00
|
|
|
let messager = getMessageManager(win);
|
2014-12-09 21:56:17 +01:00
|
|
|
|
|
|
|
if (sandbox) {
|
2014-12-17 21:33:53 +01:00
|
|
|
win = Cu.Sandbox([win], {
|
2014-12-09 21:56:17 +01:00
|
|
|
sandboxPrototype: win,
|
|
|
|
wantComponents: false,
|
|
|
|
wantXHRConstructor: false
|
|
|
|
});
|
2014-12-16 13:44:34 +01:00
|
|
|
|
|
|
|
win.self = win;
|
2014-12-16 22:31:03 +01:00
|
|
|
|
2014-12-17 08:46:18 +01:00
|
|
|
// anonymous function needs to be used here
|
2014-12-17 21:33:53 +01:00
|
|
|
win.injectScript = Cu.exportFunction(
|
2014-12-17 08:46:18 +01:00
|
|
|
function(script, evalCode) {
|
|
|
|
if (evalCode) {
|
2014-12-17 21:33:53 +01:00
|
|
|
Cu.evalInSandbox(script, win);
|
2014-12-17 08:46:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
Services.scriptloader.loadSubScript(script, win);
|
2014-12-17 08:46:18 +01:00
|
|
|
},
|
|
|
|
win
|
2014-12-16 22:31:03 +01:00
|
|
|
);
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
win.sendAsyncMessage = messager.sendAsyncMessage;
|
|
|
|
win.addMessageListener = messager.ublock_addMessageListener;
|
|
|
|
win.removeMessageListener = messager.ublock_removeMessageListener;
|
|
|
|
|
|
|
|
return win;
|
|
|
|
},
|
|
|
|
register: function() {
|
2014-12-28 10:56:09 +01:00
|
|
|
Services.obs.addObserver(this, 'document-element-inserted', true);
|
2014-12-09 21:56:17 +01:00
|
|
|
},
|
|
|
|
unregister: function() {
|
|
|
|
Services.obs.removeObserver(this, 'document-element-inserted');
|
|
|
|
},
|
|
|
|
observe: function(doc) {
|
|
|
|
let win = doc.defaultView;
|
|
|
|
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
let loc = win.location;
|
|
|
|
|
|
|
|
if (loc.protocol !== 'http:' && loc.protocol !== 'https:') {
|
|
|
|
if (loc.protocol === 'chrome:' && loc.host === appName) {
|
2014-12-09 21:56:17 +01:00
|
|
|
this.initContext(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lss = Services.scriptloader.loadSubScript;
|
|
|
|
win = this.initContext(win, true);
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
lss(this.contentBaseURI + 'vapi-client.js', win);
|
|
|
|
lss(this.contentBaseURI + 'contentscript-start.js', win);
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
let docReady = function(e) {
|
|
|
|
this.removeEventListener(e.type, docReady, true);
|
2014-12-28 10:56:09 +01:00
|
|
|
lss(docObserver.contentBaseURI + 'contentscript-end.js', win);
|
2014-12-16 13:44:34 +01:00
|
|
|
};
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
win.document.addEventListener('DOMContentLoaded', docReady, true);
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
contentPolicy.register();
|
|
|
|
docObserver.register();
|
2014-12-17 21:33:53 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|