2014-12-17 13:33:53 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-2016 The uBlock Origin authors
|
2014-12-17 13:33:53 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
/* exported processObserver */
|
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-10-08 09:15:58 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/800
|
2016-09-24 12:36:08 -06:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
'contentObserver',
|
|
|
|
'processObserver',
|
|
|
|
'LocationChangeListener'
|
|
|
|
];
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
const {interfaces: Ci, utils: Cu} = Components;
|
|
|
|
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
|
2015-03-26 15:00:56 -06:00
|
|
|
const {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
|
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
|
2015-09-30 07:33:38 -06:00
|
|
|
const rpcEmitterName = hostName + ':child-process-message';
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2016-03-05 12:04:36 -07:00
|
|
|
//Cu.import('resource://gre/modules/Console.jsm'); // Firefox >= 44
|
|
|
|
//Cu.import('resource://gre/modules/devtools/Console.jsm'); // Firefox < 44
|
2015-01-26 12:26:45 -07:00
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
const getMessageManager = function(win) {
|
2015-05-22 10:19:17 -06:00
|
|
|
let iface = win
|
2014-12-24 15:11:36 -07:00
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDocShell)
|
|
|
|
.sameTypeRootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
2015-05-22 10:19:17 -06:00
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return iface.getInterface(Ci.nsIContentFrameMessageManager);
|
|
|
|
} catch (ex) {
|
|
|
|
// This can throw. It appears `shouldLoad` can be called *after* a
|
|
|
|
// tab has been closed. For example, a case where this happens all
|
|
|
|
// the time (FF38):
|
|
|
|
// - Open twitter.com (assuming you have an account and are logged in)
|
|
|
|
// - Close twitter.com
|
|
|
|
// There will be an exception raised when `shouldLoad` is called
|
|
|
|
// to process a XMLHttpRequest with URL `https://twitter.com/i/jot`
|
|
|
|
// fired from `https://twitter.com/`, *after* the tab is closed.
|
|
|
|
// In such case, `win` is `about:blank`.
|
|
|
|
}
|
|
|
|
return null;
|
2014-12-24 15:11:36 -07:00
|
|
|
};
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2014
|
|
|
|
// Have a dictionary of hostnames for which there are script tag filters. This
|
|
|
|
// allow for coarse-testing before firing a synchronous message to the
|
|
|
|
// parent process. Script tag filters are not very common, so this allows
|
|
|
|
// to skip the blocking of the child process most of the time.
|
|
|
|
|
|
|
|
var scriptTagFilterer = (function() {
|
|
|
|
var scriptTagHostnames;
|
|
|
|
|
|
|
|
var getCpmm = function() {
|
|
|
|
var svc = Services;
|
|
|
|
if ( !svc ) { return; }
|
|
|
|
var cpmm = svc.cpmm;
|
|
|
|
if ( cpmm ) { return cpmm; }
|
|
|
|
cpmm = Components.classes['@mozilla.org/childprocessmessagemanager;1'];
|
|
|
|
if ( cpmm ) { return cpmm.getService(Ci.nsISyncMessageSender); }
|
|
|
|
};
|
|
|
|
|
|
|
|
var getScriptTagHostnames = function() {
|
|
|
|
if ( scriptTagHostnames ) {
|
|
|
|
return scriptTagHostnames;
|
|
|
|
}
|
|
|
|
var cpmm = getCpmm();
|
|
|
|
if ( !cpmm ) { return; }
|
|
|
|
var r = cpmm.sendSyncMessage(rpcEmitterName, { fnName: 'getScriptTagHostnames' });
|
|
|
|
if ( Array.isArray(r) && Array.isArray(r[0]) ) {
|
|
|
|
scriptTagHostnames = new Set(r[0]);
|
|
|
|
}
|
|
|
|
return scriptTagHostnames;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getScriptTagFilters = function(details) {
|
|
|
|
let cpmm = getCpmm();
|
|
|
|
if ( !cpmm ) { return; }
|
|
|
|
let r = cpmm.sendSyncMessage(rpcEmitterName, {
|
|
|
|
fnName: 'getScriptTagFilters',
|
|
|
|
rootURL: details.rootURL,
|
|
|
|
frameURL: details.frameURL,
|
|
|
|
frameHostname: details.frameHostname
|
|
|
|
});
|
|
|
|
if ( Array.isArray(r) ) {
|
|
|
|
return r[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var regexFromHostname = function(details) {
|
|
|
|
// If target hostname has no script tag filter, no point querying
|
|
|
|
// chrome process.
|
|
|
|
var hostnames = getScriptTagHostnames();
|
|
|
|
if ( !hostnames ) { return; }
|
|
|
|
var hn = details.frameHostname, pos, entity;
|
|
|
|
for (;;) {
|
|
|
|
if ( hostnames.has(hn) ) {
|
|
|
|
return getScriptTagFilters(details);
|
|
|
|
}
|
|
|
|
pos = hn.indexOf('.');
|
|
|
|
if ( pos === -1 ) { break; }
|
|
|
|
entity = hn.slice(0, pos) + '.*';
|
|
|
|
if ( hostnames.has(entity) ) {
|
|
|
|
return getScriptTagFilters(details);
|
|
|
|
}
|
|
|
|
hn = hn.slice(pos + 1);
|
|
|
|
if ( hn === '' ) { break; }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var reset = function() {
|
|
|
|
scriptTagHostnames = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
get: regexFromHostname,
|
|
|
|
reset: reset
|
|
|
|
};
|
|
|
|
})();
|
2015-11-16 13:12:26 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-10-08 09:15:58 -06:00
|
|
|
var contentObserver = {
|
2015-01-08 13:18:05 -07:00
|
|
|
classDescription: 'content-policy for ' + hostName,
|
2015-04-12 17:15:58 -06:00
|
|
|
classID: Components.ID('{7afbd130-cbaf-46c2-b944-f5d24305f484}'),
|
2015-01-08 13:18:05 -07:00
|
|
|
contractID: '@' + hostName + '/content-policy;1',
|
2014-12-09 13:56:17 -07:00
|
|
|
ACCEPT: Ci.nsIContentPolicy.ACCEPT,
|
2015-10-27 08:11:52 -06:00
|
|
|
REJECT: Ci.nsIContentPolicy.REJECT_REQUEST,
|
2015-01-02 10:41:41 -07:00
|
|
|
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
|
2015-05-17 08:32:40 -06:00
|
|
|
SUB_FRAME: Ci.nsIContentPolicy.TYPE_SUBDOCUMENT,
|
2015-01-08 13:18:05 -07:00
|
|
|
contentBaseURI: 'chrome://' + hostName + '/content/js/',
|
|
|
|
cpMessageName: hostName + ':shouldLoad',
|
2015-12-01 13:07:22 -07:00
|
|
|
popupMessageName: hostName + ':shouldLoadPopup',
|
2015-01-28 13:08:24 -07:00
|
|
|
ignoredPopups: new WeakMap(),
|
2015-02-05 10:05:41 -07:00
|
|
|
uniqueSandboxId: 1,
|
2016-08-06 13:27:33 -06:00
|
|
|
canE10S: Services.vc.compare(Services.appinfo.platformVersion, '44') > 0,
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
get componentRegistrar() {
|
|
|
|
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
get categoryManager() {
|
|
|
|
return Components.classes['@mozilla.org/categorymanager;1']
|
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2015-03-26 15:00:56 -06:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2015-01-04 05:58:17 -07:00
|
|
|
Ci.nsIFactory,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIContentPolicy,
|
|
|
|
Ci.nsISupportsWeakReference
|
2015-03-26 15:00:56 -06:00
|
|
|
]),
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
createInstance: function(outer, iid) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( outer ) {
|
2014-12-09 13:56:17 -07:00
|
|
|
throw Components.results.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
2015-01-02 10:41:41 -07:00
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
register: function() {
|
2015-01-02 10:41:41 -07:00
|
|
|
Services.obs.addObserver(this, 'document-element-inserted', true);
|
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
this.componentRegistrar.registerFactory(
|
|
|
|
this.classID,
|
|
|
|
this.classDescription,
|
|
|
|
this.contractID,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.categoryManager.addCategoryEntry(
|
|
|
|
'content-policy',
|
|
|
|
this.contractID,
|
|
|
|
this.contractID,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
unregister: function() {
|
2015-01-02 10:41:41 -07:00
|
|
|
Services.obs.removeObserver(this, 'document-element-inserted');
|
|
|
|
|
2014-12-09 13:56:17 -07:00
|
|
|
this.componentRegistrar.unregisterFactory(this.classID, this);
|
|
|
|
this.categoryManager.deleteCategoryEntry(
|
|
|
|
'content-policy',
|
|
|
|
this.contractID,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2015-01-27 08:37:02 -07:00
|
|
|
getFrameId: function(win) {
|
|
|
|
return win
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.outerWindowID;
|
|
|
|
},
|
|
|
|
|
2016-01-27 09:02:08 -07:00
|
|
|
handlePopup: function(location, origin, context) {
|
2015-12-01 13:07:22 -07:00
|
|
|
let openeeContext = context.contentWindow || context;
|
|
|
|
if (
|
|
|
|
typeof openeeContext.opener !== 'object' ||
|
|
|
|
openeeContext.opener === null ||
|
|
|
|
openeeContext.opener === context ||
|
|
|
|
this.ignoredPopups.has(openeeContext)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/452
|
|
|
|
// Use location of top window, not that of a frame, as this
|
|
|
|
// would cause tab id lookup (necessary for popup blocking) to
|
|
|
|
// always fail.
|
2016-01-27 09:03:30 -07:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1305
|
|
|
|
// Opener could be a dead object, using it would cause a throw.
|
|
|
|
// Repro case:
|
|
|
|
// - Open http://delishows.to/show/chicago-med/season/1/episode/6
|
|
|
|
// - Click anywhere in the background
|
2016-01-27 09:02:08 -07:00
|
|
|
let openerURL = null;
|
|
|
|
try {
|
|
|
|
let opener = openeeContext.opener.top || openeeContext.opener;
|
|
|
|
openerURL = opener.location && opener.location.href;
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
// If no valid opener URL found, use the origin URL.
|
2015-12-01 13:07:22 -07:00
|
|
|
if ( openerURL === null ) {
|
2016-01-27 09:02:08 -07:00
|
|
|
openerURL = origin.asciiSpec;
|
2015-12-01 13:07:22 -07:00
|
|
|
}
|
|
|
|
let messageManager = getMessageManager(openeeContext);
|
|
|
|
if ( messageManager === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( typeof messageManager.sendRpcMessage === 'function' ) {
|
|
|
|
// https://bugzil.la/1092216
|
|
|
|
messageManager.sendRpcMessage(this.popupMessageName, openerURL);
|
|
|
|
} else {
|
|
|
|
// Compatibility for older versions
|
|
|
|
messageManager.sendSyncMessage(this.popupMessageName, openerURL);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-24 15:11:36 -07:00
|
|
|
// https://bugzil.la/612921
|
2014-12-09 13:56:17 -07:00
|
|
|
shouldLoad: function(type, location, origin, context) {
|
2015-06-17 15:56:04 -06:00
|
|
|
// For whatever reason, sometimes the global scope is completely
|
|
|
|
// uninitialized at this point. Repro steps:
|
|
|
|
// - Launch FF with uBlock enabled
|
|
|
|
// - Disable uBlock
|
|
|
|
// - Enable uBlock
|
|
|
|
// - Services and all other global variables are undefined
|
|
|
|
// Hopefully will eventually understand why this happens.
|
2016-08-15 20:50:24 -06:00
|
|
|
if ( Services === undefined || !context ) {
|
2015-01-26 12:38:22 -07:00
|
|
|
return this.ACCEPT;
|
2014-12-28 02:56:09 -07:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:07:22 -07:00
|
|
|
if ( type === this.MAIN_FRAME ) {
|
2016-01-27 09:02:08 -07:00
|
|
|
this.handlePopup(location, origin, context);
|
2015-12-01 13:07:22 -07:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:50:24 -06:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1232354
|
|
|
|
// For modern versions of Firefox, the frameId/parentFrameId
|
|
|
|
// information can be found in channel.loadInfo of the HTTP observer.
|
|
|
|
if ( this.canE10S ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:24:35 -07:00
|
|
|
if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
|
2015-01-26 12:26:45 -07:00
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
2015-01-02 10:41:41 -07:00
|
|
|
|
2015-01-26 12:26:45 -07:00
|
|
|
if ( type === this.MAIN_FRAME ) {
|
2015-01-02 10:41:41 -07:00
|
|
|
context = context.contentWindow || context;
|
2015-05-17 08:32:40 -06:00
|
|
|
} else if ( type === this.SUB_FRAME ) {
|
2015-02-15 11:25:11 -07:00
|
|
|
context = context.contentWindow;
|
2015-01-02 10:41:41 -07:00
|
|
|
} else {
|
|
|
|
context = (context.ownerDocument || context).defaultView;
|
2014-12-09 13:56:17 -07:00
|
|
|
}
|
|
|
|
|
2016-08-13 19:45:01 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1893
|
|
|
|
// I don't know why this happens. I observed that when it occurred, the
|
|
|
|
// resource was not seen by the HTTP observer, as if it was a spurious
|
|
|
|
// call to shouldLoad().
|
|
|
|
if ( !context ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:24:35 -07:00
|
|
|
// The context for the toolbar popup is an iframe element here,
|
|
|
|
// so check context.top instead of context
|
2015-01-26 12:26:45 -07:00
|
|
|
if ( !context.top || !context.location ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
2015-01-16 03:42:34 -07:00
|
|
|
|
2015-01-26 12:26:45 -07:00
|
|
|
let messageManager = getMessageManager(context);
|
2015-05-22 10:19:17 -06:00
|
|
|
if ( messageManager === null ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
|
|
|
|
2016-08-15 20:50:24 -06:00
|
|
|
let isTopContext = context === context.top;
|
2016-08-06 13:27:33 -06:00
|
|
|
var parentFrameId;
|
|
|
|
if ( isTopContext ) {
|
|
|
|
parentFrameId = -1;
|
|
|
|
} else if ( context.parent === context.top ) {
|
|
|
|
parentFrameId = 0;
|
|
|
|
} else {
|
|
|
|
parentFrameId = this.getFrameId(context.parent);
|
|
|
|
}
|
|
|
|
|
2016-08-06 08:20:11 -06:00
|
|
|
let rpcData = this.rpcData;
|
2016-08-06 13:27:33 -06:00
|
|
|
rpcData.frameId = isTopContext ? 0 : this.getFrameId(context);
|
|
|
|
rpcData.pFrameId = parentFrameId;
|
2016-08-06 08:20:11 -06:00
|
|
|
rpcData.type = type;
|
|
|
|
rpcData.url = location.spec;
|
2015-01-16 03:42:34 -07:00
|
|
|
|
2015-11-06 12:18:40 -07:00
|
|
|
//console.log('shouldLoad: type=' + type + ' url=' + location.spec);
|
2015-01-26 12:26:45 -07:00
|
|
|
if ( typeof messageManager.sendRpcMessage === 'function' ) {
|
|
|
|
// https://bugzil.la/1092216
|
2016-08-06 08:20:11 -06:00
|
|
|
messageManager.sendRpcMessage(this.cpMessageName, rpcData);
|
2015-01-26 12:26:45 -07:00
|
|
|
} else {
|
|
|
|
// Compatibility for older versions
|
2016-08-06 08:20:11 -06:00
|
|
|
messageManager.sendSyncMessage(this.cpMessageName, rpcData);
|
2014-12-09 13:56:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.ACCEPT;
|
2015-01-02 10:41:41 -07:00
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2016-08-06 08:20:11 -06:00
|
|
|
// Reuse object to avoid repeated memory allocation.
|
|
|
|
rpcData: { frameId: 0, pFrameId: -1, type: 0, url: '' },
|
|
|
|
|
2015-07-03 11:32:00 -06:00
|
|
|
initContentScripts: function(win, create) {
|
2014-12-24 15:11:36 -07:00
|
|
|
let messager = getMessageManager(win);
|
2015-02-05 10:05:41 -07:00
|
|
|
let sandboxId = hostName + ':sb:' + this.uniqueSandboxId++;
|
2015-07-03 11:32:00 -06:00
|
|
|
let sandbox;
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2015-07-03 11:32:00 -06:00
|
|
|
if ( create ) {
|
2015-01-08 13:18:05 -07:00
|
|
|
let sandboxName = [
|
|
|
|
win.location.href.slice(0, 100),
|
|
|
|
win.document.title.slice(0, 100)
|
|
|
|
].join(' | ');
|
|
|
|
|
2015-08-15 12:17:50 -06:00
|
|
|
// https://github.com/gorhill/uMatrix/issues/325
|
|
|
|
// "Pass sameZoneAs to sandbox constructor to make GCs cheaper"
|
2015-01-08 13:18:05 -07:00
|
|
|
sandbox = Cu.Sandbox([win], {
|
2015-08-15 12:17:50 -06:00
|
|
|
sameZoneAs: win.top,
|
2015-01-08 13:18:05 -07:00
|
|
|
sandboxName: sandboxId + '[' + sandboxName + ']',
|
2014-12-09 13:56:17 -07:00
|
|
|
sandboxPrototype: win,
|
|
|
|
wantComponents: false,
|
|
|
|
wantXHRConstructor: false
|
|
|
|
});
|
2014-12-16 05:44:34 -07:00
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox.getScriptTagFilters = function(details) {
|
|
|
|
return scriptTagFilterer.get(details);
|
|
|
|
};
|
2015-09-30 07:33:38 -06:00
|
|
|
|
2015-01-27 05:31:17 -07:00
|
|
|
sandbox.injectScript = function(script) {
|
2016-08-31 15:50:49 -06:00
|
|
|
let svc = Services;
|
2015-10-28 07:35:13 -06:00
|
|
|
// Sandbox appears void.
|
|
|
|
// I've seen this happens, need to investigate why.
|
2016-08-31 15:50:49 -06:00
|
|
|
if ( svc === undefined ) { return; }
|
2015-10-28 07:35:13 -06:00
|
|
|
svc.scriptloader.loadSubScript(script, sandbox);
|
2015-01-27 05:31:17 -07:00
|
|
|
};
|
2015-07-03 11:32:00 -06:00
|
|
|
|
2016-08-31 15:50:49 -06:00
|
|
|
sandbox.injectCSS = function(sheetURI) {
|
|
|
|
try {
|
|
|
|
let wu = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
wu.loadSheetUsingURIString(sheetURI, wu.USER_SHEET);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sandbox.removeCSS = function(sheetURI) {
|
|
|
|
try {
|
|
|
|
let wu = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
wu.removeSheetUsingURIString(sheetURI, wu.USER_SHEET);
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox.topContentScript = win === win.top;
|
|
|
|
|
2016-09-25 14:02:11 -06:00
|
|
|
// https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Message_Manager/Frame_script_loading_and_lifetime#Unloading_frame_scripts
|
2015-07-03 11:32:00 -06:00
|
|
|
// The goal is to have content scripts removed from web pages. This
|
|
|
|
// helps remove traces of uBlock from memory when disabling/removing
|
|
|
|
// the addon.
|
|
|
|
// For example, this takes care of:
|
|
|
|
// https://github.com/gorhill/uBlock/commit/ea4faff383789053f423498c1f1165c403fde7c7#commitcomment-11964137
|
|
|
|
// > "gets the whole selected tab flashing"
|
|
|
|
sandbox.outerShutdown = function() {
|
|
|
|
sandbox.removeMessageListener();
|
|
|
|
sandbox.addMessageListener =
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox.getScriptTagFilters =
|
2016-08-31 15:50:49 -06:00
|
|
|
sandbox.injectCSS =
|
2015-07-03 11:32:00 -06:00
|
|
|
sandbox.injectScript =
|
2015-09-30 07:33:38 -06:00
|
|
|
sandbox.outerShutdown =
|
2016-08-31 15:50:49 -06:00
|
|
|
sandbox.removeCSS =
|
2015-07-03 11:32:00 -06:00
|
|
|
sandbox.removeMessageListener =
|
2015-09-30 07:33:38 -06:00
|
|
|
sandbox.sendAsyncMessage = function(){};
|
2015-07-03 11:32:00 -06:00
|
|
|
sandbox.vAPI = {};
|
2016-09-25 14:02:11 -06:00
|
|
|
messager = null;
|
2015-07-03 11:32:00 -06:00
|
|
|
};
|
2015-01-08 13:18:05 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sandbox = win;
|
|
|
|
}
|
2014-12-17 00:46:18 -07:00
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
sandbox._sandboxId_ = sandboxId;
|
|
|
|
sandbox.sendAsyncMessage = messager.sendAsyncMessage;
|
2015-01-26 12:26:45 -07:00
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
sandbox.addMessageListener = function(callback) {
|
2015-01-27 05:31:17 -07:00
|
|
|
if ( sandbox._messageListener_ ) {
|
2015-02-05 10:05:41 -07:00
|
|
|
sandbox.removeMessageListener();
|
2015-01-08 13:18:05 -07:00
|
|
|
}
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2015-01-27 05:31:17 -07:00
|
|
|
sandbox._messageListener_ = function(message) {
|
2015-01-08 13:18:05 -07:00
|
|
|
callback(message.data);
|
|
|
|
};
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox._broadcastListener_ = function(message) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2014
|
|
|
|
if ( sandbox.topContentScript ) {
|
2016-09-25 14:02:11 -06:00
|
|
|
let details;
|
|
|
|
try { details = JSON.parse(message.data); } catch (ex) {}
|
|
|
|
let msg = details && details.msg || {};
|
|
|
|
if ( msg.what === 'staticFilteringDataChanged' ) {
|
|
|
|
if ( scriptTagFilterer ) {
|
|
|
|
scriptTagFilterer.reset();
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 12:36:08 -06:00
|
|
|
}
|
|
|
|
callback(message.data);
|
|
|
|
};
|
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
messager.addMessageListener(
|
2015-01-27 05:31:17 -07:00
|
|
|
sandbox._sandboxId_,
|
|
|
|
sandbox._messageListener_
|
2015-01-08 13:18:05 -07:00
|
|
|
);
|
|
|
|
messager.addMessageListener(
|
|
|
|
hostName + ':broadcast',
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox._broadcastListener_
|
2015-01-08 13:18:05 -07:00
|
|
|
);
|
2015-01-27 05:31:17 -07:00
|
|
|
};
|
2015-01-26 12:26:45 -07:00
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
sandbox.removeMessageListener = function() {
|
2015-07-03 05:00:29 -06:00
|
|
|
if ( !sandbox._messageListener_ ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-25 14:02:11 -06:00
|
|
|
// It throws sometimes, mostly when the popup closes
|
2015-01-11 10:41:52 -07:00
|
|
|
try {
|
|
|
|
messager.removeMessageListener(
|
2015-01-27 05:31:17 -07:00
|
|
|
sandbox._sandboxId_,
|
|
|
|
sandbox._messageListener_
|
2015-01-11 10:41:52 -07:00
|
|
|
);
|
2016-09-25 14:02:11 -06:00
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
try {
|
2015-01-11 10:41:52 -07:00
|
|
|
messager.removeMessageListener(
|
|
|
|
hostName + ':broadcast',
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox._broadcastListener_
|
2015-01-11 10:41:52 -07:00
|
|
|
);
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
sandbox._messageListener_ = sandbox._broadcastListener_ = null;
|
2015-01-27 05:31:17 -07:00
|
|
|
};
|
2015-01-08 13:18:05 -07:00
|
|
|
|
|
|
|
return sandbox;
|
2014-12-09 13:56:17 -07:00
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2015-01-28 13:08:24 -07:00
|
|
|
ignorePopup: function(e) {
|
|
|
|
if ( e.isTrusted === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let contObs = contentObserver;
|
|
|
|
contObs.ignoredPopups.set(this, true);
|
|
|
|
this.removeEventListener('keydown', contObs.ignorePopup, true);
|
|
|
|
this.removeEventListener('mousedown', contObs.ignorePopup, true);
|
|
|
|
},
|
|
|
|
|
2015-03-10 06:06:59 -06:00
|
|
|
observe: function(doc) {
|
2015-06-17 15:56:04 -06:00
|
|
|
// For whatever reason, sometimes the global scope is completely
|
|
|
|
// uninitialized at this point. Repro steps:
|
|
|
|
// - Launch FF with uBlock enabled
|
|
|
|
// - Disable uBlock
|
|
|
|
// - Enable uBlock
|
|
|
|
// - Services and all other global variables are undefined
|
|
|
|
// Hopefully will eventually understand why this happens.
|
|
|
|
if ( Services === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-18 09:44:24 -06:00
|
|
|
let win = doc.defaultView || null;
|
|
|
|
if ( win === null ) {
|
2014-12-09 13:56:17 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:08:24 -07:00
|
|
|
if ( win.opener && this.ignoredPopups.has(win) === false ) {
|
|
|
|
win.addEventListener('keydown', this.ignorePopup, true);
|
|
|
|
win.addEventListener('mousedown', this.ignorePopup, true);
|
|
|
|
}
|
|
|
|
|
2015-05-29 14:40:59 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/260
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Document/contentType
|
|
|
|
// "Non-standard, only supported by Gecko. To be used in
|
|
|
|
// "chrome code (i.e. Extensions and XUL applications)."
|
|
|
|
// TODO: We may have to exclude more types, for now let's be
|
|
|
|
// conservative and focus only on the one issue reported, i.e. let's
|
|
|
|
// not test against 'text/html'.
|
|
|
|
if ( doc.contentType.startsWith('image/') ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-28 02:56:09 -07:00
|
|
|
let loc = win.location;
|
|
|
|
|
2015-04-02 06:54:06 -06:00
|
|
|
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' && loc.protocol !== 'file:' ) {
|
2015-01-08 13:18:05 -07:00
|
|
|
if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
|
2015-01-02 10:41:41 -07:00
|
|
|
this.initContentScripts(win);
|
2014-12-09 13:56:17 -07:00
|
|
|
}
|
|
|
|
|
2015-01-04 05:58:17 -07:00
|
|
|
// What about data: and about:blank?
|
2014-12-09 13:56:17 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lss = Services.scriptloader.loadSubScript;
|
2015-01-08 13:18:05 -07:00
|
|
|
let sandbox = this.initContentScripts(win, true);
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2015-05-16 14:31:44 -06:00
|
|
|
try {
|
|
|
|
lss(this.contentBaseURI + 'vapi-client.js', sandbox);
|
2016-06-27 17:09:04 -06:00
|
|
|
lss(this.contentBaseURI + 'contentscript.js', sandbox);
|
2015-05-16 14:31:44 -06:00
|
|
|
} catch (ex) {
|
2015-06-04 07:37:53 -06:00
|
|
|
//console.exception(ex.msg, ex.stack);
|
2015-05-16 14:31:44 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-12-09 13:56:17 -07:00
|
|
|
|
2015-03-10 06:06:59 -06:00
|
|
|
let docReady = (e) => {
|
|
|
|
let doc = e.target;
|
|
|
|
doc.removeEventListener(e.type, docReady, true);
|
2015-03-09 10:57:52 -06:00
|
|
|
|
2016-07-09 19:37:16 -06:00
|
|
|
if ( doc.querySelector('a[href^="abp:"],a[href^="https://subscribe.adblockplus.org/?"]') ) {
|
2015-11-20 06:47:29 -07:00
|
|
|
lss(this.contentBaseURI + 'scriptlets/subscriber.js', sandbox);
|
2015-03-10 06:06:59 -06:00
|
|
|
}
|
2015-03-09 10:57:52 -06:00
|
|
|
};
|
|
|
|
|
2015-03-12 11:20:48 -06:00
|
|
|
if ( doc.readyState === 'loading') {
|
|
|
|
doc.addEventListener('DOMContentLoaded', docReady, true);
|
|
|
|
} else {
|
|
|
|
docReady({ target: doc, type: 'DOMContentLoaded' });
|
|
|
|
}
|
2014-12-09 13:56:17 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-09-24 12:36:08 -06:00
|
|
|
var processObserver = {
|
|
|
|
start: function() {
|
|
|
|
scriptTagFilterer.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-03-06 17:49:27 -07:00
|
|
|
var LocationChangeListener = function(docShell, webProgress) {
|
|
|
|
var mm = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIContentFrameMessageManager);
|
|
|
|
if ( !mm || typeof mm.sendAsyncMessage !== 'function' ) {
|
2015-05-17 16:27:53 -06:00
|
|
|
return;
|
|
|
|
}
|
2015-09-30 07:33:38 -06:00
|
|
|
this.messageManager = mm;
|
2016-03-06 17:49:27 -07:00
|
|
|
webProgress.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
|
2015-03-31 05:03:35 -06:00
|
|
|
};
|
2015-03-26 15:00:56 -06:00
|
|
|
|
2016-03-06 17:49:27 -07:00
|
|
|
LocationChangeListener.prototype.messageName = hostName + ':locationChanged';
|
|
|
|
|
2015-05-17 16:27:53 -06:00
|
|
|
LocationChangeListener.prototype.QueryInterface = XPCOMUtils.generateQI([
|
|
|
|
'nsIWebProgressListener',
|
|
|
|
'nsISupportsWeakReference'
|
|
|
|
]);
|
2015-03-26 15:00:56 -06:00
|
|
|
|
|
|
|
LocationChangeListener.prototype.onLocationChange = function(webProgress, request, location, flags) {
|
|
|
|
if ( !webProgress.isTopLevel ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-06 17:49:27 -07:00
|
|
|
this.messageManager.sendAsyncMessage(this.messageName, {
|
2015-03-26 15:00:56 -06:00
|
|
|
url: location.asciiSpec,
|
2015-09-14 09:28:33 -06:00
|
|
|
flags: flags
|
2015-03-26 15:00:56 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 10:41:41 -07:00
|
|
|
contentObserver.register();
|
2014-12-17 13:33:53 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|