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-09 21:56:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
this.EXPORTED_SYMBOLS = ['contentObserver'];
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
const {interfaces: Ci, utils: Cu} = Components;
|
|
|
|
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
|
|
|
|
const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
|
|
|
|
let uniqueSandboxId = 1;
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
// let {console} = Cu.import('resource://gre/modules/devtools/Console.jsm', null);
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
const getMessageManager = function(win) {
|
|
|
|
return win
|
2014-12-24 23:11:36 +01:00
|
|
|
.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
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
const contentObserver = {
|
2015-01-08 21:18:05 +01:00
|
|
|
classDescription: 'content-policy for ' + hostName,
|
2014-12-09 21:56:17 +01:00
|
|
|
classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
|
2015-01-08 21:18:05 +01:00
|
|
|
contractID: '@' + hostName + '/content-policy;1',
|
2014-12-09 21:56:17 +01:00
|
|
|
ACCEPT: Ci.nsIContentPolicy.ACCEPT,
|
2015-01-02 18:41:41 +01:00
|
|
|
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
|
2015-01-08 21:18:05 +01:00
|
|
|
contentBaseURI: 'chrome://' + hostName + '/content/js/',
|
|
|
|
cpMessageName: hostName + ':shouldLoad',
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
get componentRegistrar() {
|
|
|
|
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
get categoryManager() {
|
|
|
|
return Components.classes['@mozilla.org/categorymanager;1']
|
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-01-04 13:58:17 +01:00
|
|
|
QueryInterface: (function() {
|
2015-01-11 18:41:52 +01:00
|
|
|
let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
|
2015-01-04 13:58:17 +01:00
|
|
|
|
|
|
|
return XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIFactory,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIContentPolicy,
|
|
|
|
Ci.nsISupportsWeakReference
|
|
|
|
]);
|
|
|
|
})(),
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
createInstance: function(outer, iid) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( outer ) {
|
2014-12-09 21:56:17 +01:00
|
|
|
throw Components.results.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
register: function() {
|
2015-01-02 18:41:41 +01:00
|
|
|
Services.obs.addObserver(this, 'document-element-inserted', true);
|
|
|
|
|
2014-12-09 21:56:17 +01: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 21:26:06 +01:00
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
unregister: function() {
|
2015-01-02 18:41:41 +01:00
|
|
|
Services.obs.removeObserver(this, 'document-element-inserted');
|
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
this.componentRegistrar.unregisterFactory(this.classID, this);
|
|
|
|
this.categoryManager.deleteCategoryEntry(
|
|
|
|
'content-policy',
|
|
|
|
this.contractID,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
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 ( !context ) {
|
2015-01-26 20:38:22 +01:00
|
|
|
return this.ACCEPT;
|
2014-12-28 10:56:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 13:24:35 +01:00
|
|
|
if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
|
2015-01-26 20:26:45 +01:00
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
let openerURL, frameId;
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( type === this.MAIN_FRAME ) {
|
|
|
|
frameId = -1;
|
2015-01-02 18:41:41 +01:00
|
|
|
context = context.contentWindow || context;
|
|
|
|
|
|
|
|
try {
|
2015-01-15 15:58:14 +01:00
|
|
|
if ( context !== context.opener ) {
|
|
|
|
openerURL = context.opener.location.href;
|
|
|
|
}
|
2015-01-02 18:41:41 +01:00
|
|
|
} catch (ex) {}
|
|
|
|
} else {
|
2015-01-26 20:26:45 +01:00
|
|
|
// TODO: frameId from outerWindowID?
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
|
|
|
|
frameId = context === context.top ? 0 : 1;
|
2015-01-02 18:41:41 +01:00
|
|
|
context = (context.ownerDocument || context).defaultView;
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 13:24:35 +01:00
|
|
|
// The context for the toolbar popup is an iframe element here,
|
|
|
|
// so check context.top instead of context
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( !context.top || !context.location ) {
|
|
|
|
return this.ACCEPT;
|
|
|
|
}
|
2015-01-16 11:42:34 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
let messageManager = getMessageManager(context);
|
|
|
|
let details = {
|
|
|
|
frameId: frameId,
|
|
|
|
openerURL: openerURL || null,
|
|
|
|
parentFrameId: context === context.top ? -1 : 0,
|
|
|
|
type: type,
|
|
|
|
url: location.spec
|
|
|
|
};
|
2015-01-16 11:42:34 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( typeof messageManager.sendRpcMessage === 'function' ) {
|
|
|
|
// https://bugzil.la/1092216
|
|
|
|
messageManager.sendRpcMessage(this.cpMessageName, details);
|
|
|
|
} else {
|
|
|
|
// Compatibility for older versions
|
|
|
|
messageManager.sendSyncMessage(this.cpMessageName, details);
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.ACCEPT;
|
2015-01-02 18:41:41 +01:00
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
initContentScripts: function(win, sandbox) {
|
2014-12-24 23:11:36 +01:00
|
|
|
let messager = getMessageManager(win);
|
2015-01-08 21:18:05 +01:00
|
|
|
let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( sandbox ) {
|
2015-01-08 21:18:05 +01:00
|
|
|
let sandboxName = [
|
|
|
|
win.location.href.slice(0, 100),
|
|
|
|
win.document.title.slice(0, 100)
|
|
|
|
].join(' | ');
|
|
|
|
|
|
|
|
sandbox = Cu.Sandbox([win], {
|
|
|
|
sandboxName: sandboxId + '[' + sandboxName + ']',
|
2014-12-09 21:56:17 +01:00
|
|
|
sandboxPrototype: win,
|
|
|
|
wantComponents: false,
|
|
|
|
wantXHRConstructor: false
|
|
|
|
});
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox.injectScript = function(script) {
|
|
|
|
Services.scriptloader.loadSubScript(script, sandbox);
|
|
|
|
};
|
2015-01-08 21:18:05 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sandbox = win;
|
|
|
|
}
|
2014-12-17 08:46:18 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
sandbox._sandboxId_ = sandboxId;
|
|
|
|
sandbox.sendAsyncMessage = messager.sendAsyncMessage;
|
2015-01-26 20:26:45 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
sandbox.addMessageListener = function(callback) {
|
2015-01-27 13:31:17 +01:00
|
|
|
if ( sandbox._messageListener_ ) {
|
|
|
|
sandbox.removeMessageListener(
|
|
|
|
sandbox._sandboxId_,
|
|
|
|
sandbox._messageListener_
|
2015-01-08 21:18:05 +01:00
|
|
|
);
|
|
|
|
}
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._messageListener_ = function(message) {
|
2015-01-08 21:18:05 +01:00
|
|
|
callback(message.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
messager.addMessageListener(
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._sandboxId_,
|
|
|
|
sandbox._messageListener_
|
2015-01-08 21:18:05 +01:00
|
|
|
);
|
|
|
|
messager.addMessageListener(
|
|
|
|
hostName + ':broadcast',
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._messageListener_
|
2015-01-08 21:18:05 +01:00
|
|
|
);
|
2015-01-27 13:31:17 +01:00
|
|
|
};
|
2015-01-26 20:26:45 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
sandbox.removeMessageListener = function() {
|
2015-01-11 18:41:52 +01:00
|
|
|
try {
|
|
|
|
messager.removeMessageListener(
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._sandboxId_,
|
|
|
|
sandbox._messageListener_
|
2015-01-11 18:41:52 +01:00
|
|
|
);
|
|
|
|
messager.removeMessageListener(
|
|
|
|
hostName + ':broadcast',
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._messageListener_
|
2015-01-11 18:41:52 +01:00
|
|
|
);
|
|
|
|
} catch (ex) {
|
|
|
|
// It throws sometimes, mostly when the popup closes
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
sandbox._messageListener_ = null;
|
|
|
|
};
|
2015-01-08 21:18:05 +01:00
|
|
|
|
|
|
|
return sandbox;
|
2014-12-09 21:56:17 +01:00
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-01-09 07:58:07 +01:00
|
|
|
observe: function(subject) {
|
2015-01-08 21:18:05 +01:00
|
|
|
let win = subject.defaultView;
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !win ) {
|
2014-12-09 21:56:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
let loc = win.location;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
|
2015-01-08 21:18:05 +01:00
|
|
|
if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
this.initContentScripts(win);
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 13:58:17 +01:00
|
|
|
// What about data: and about:blank?
|
2014-12-09 21:56:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lss = Services.scriptloader.loadSubScript;
|
2015-01-08 21:18:05 +01:00
|
|
|
let sandbox = this.initContentScripts(win, true);
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
lss(this.contentBaseURI + 'vapi-client.js', sandbox);
|
|
|
|
lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
|
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);
|
2015-01-08 21:18:05 +01:00
|
|
|
lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
|
2014-12-16 13:44:34 +01:00
|
|
|
};
|
2014-12-09 21:56:17 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
subject.addEventListener('DOMContentLoaded', docReady, true);
|
2014-12-09 21:56:17 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
contentObserver.register();
|
2014-12-17 21:33:53 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|