You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. µBlock - a browser extension to block requests.
  3. Copyright (C) 2014 The µBlock authors
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uBlock
  15. */
  16. 'use strict';
  17. /******************************************************************************/
  18. this.EXPORTED_SYMBOLS = ['contentObserver'];
  19. const {interfaces: Ci, utils: Cu} = Components;
  20. const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
  21. const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
  22. let uniqueSandboxId = 1;
  23. /******************************************************************************/
  24. const getMessageManager = function(win) {
  25. return win
  26. .QueryInterface(Ci.nsIInterfaceRequestor)
  27. .getInterface(Ci.nsIDocShell)
  28. .sameTypeRootTreeItem
  29. .QueryInterface(Ci.nsIDocShell)
  30. .QueryInterface(Ci.nsIInterfaceRequestor)
  31. .getInterface(Ci.nsIContentFrameMessageManager);
  32. };
  33. /******************************************************************************/
  34. const contentObserver = {
  35. classDescription: 'content-policy for ' + hostName,
  36. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  37. contractID: '@' + hostName + '/content-policy;1',
  38. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  39. MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  40. contentBaseURI: 'chrome://' + hostName + '/content/js/',
  41. cpMessageName: hostName + ':shouldLoad',
  42. get componentRegistrar() {
  43. return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  44. },
  45. get categoryManager() {
  46. return Components.classes['@mozilla.org/categorymanager;1']
  47. .getService(Ci.nsICategoryManager);
  48. },
  49. QueryInterface: (function() {
  50. let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
  51. return XPCOMUtils.generateQI([
  52. Ci.nsIFactory,
  53. Ci.nsIObserver,
  54. Ci.nsIContentPolicy,
  55. Ci.nsISupportsWeakReference
  56. ]);
  57. })(),
  58. createInstance: function(outer, iid) {
  59. if ( outer ) {
  60. throw Components.results.NS_ERROR_NO_AGGREGATION;
  61. }
  62. return this.QueryInterface(iid);
  63. },
  64. register: function() {
  65. Services.obs.addObserver(this, 'document-element-inserted', true);
  66. this.componentRegistrar.registerFactory(
  67. this.classID,
  68. this.classDescription,
  69. this.contractID,
  70. this
  71. );
  72. this.categoryManager.addCategoryEntry(
  73. 'content-policy',
  74. this.contractID,
  75. this.contractID,
  76. false,
  77. true
  78. );
  79. },
  80. unregister: function() {
  81. Services.obs.removeObserver(this, 'document-element-inserted');
  82. this.componentRegistrar.unregisterFactory(this.classID, this);
  83. this.categoryManager.deleteCategoryEntry(
  84. 'content-policy',
  85. this.contractID,
  86. false
  87. );
  88. },
  89. // https://bugzil.la/612921
  90. shouldLoad: function(type, location, origin, context) {
  91. // If we don't know what initiated the request, probably it's not a tab
  92. if ( !context ) {
  93. return this.ACCEPT;
  94. }
  95. let openerURL;
  96. if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
  97. if ( type !== this.MAIN_FRAME ) {
  98. return this.ACCEPT;
  99. }
  100. context = context.contentWindow || context;
  101. try {
  102. if ( context !== context.opener ) {
  103. openerURL = context.opener.location.href;
  104. }
  105. } catch (ex) {}
  106. let isPopup = location.spec === 'about:blank' && openerURL;
  107. if ( !location.schemeIs('data') && !isPopup ) {
  108. return this.ACCEPT;
  109. }
  110. } else if ( type === this.MAIN_FRAME ) {
  111. context = context.contentWindow || context;
  112. try {
  113. if ( context !== context.opener ) {
  114. openerURL = context.opener.location.href;
  115. }
  116. } catch (ex) {}
  117. } else {
  118. context = (context.ownerDocument || context).defaultView;
  119. }
  120. // The context for the toolbar popup is an iframe element here,
  121. // so check context.top instead of context
  122. if ( context.top && context.location ) {
  123. // https://bugzil.la/1092216
  124. let messageManager = getMessageManager(context);
  125. let details = {
  126. openerURL: openerURL || null,
  127. url: location.spec,
  128. type: type,
  129. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  130. parentFrameId: context === context.top ? -1 : 0
  131. };
  132. // TODO: frameId from outerWindowID?
  133. // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
  134. if ( typeof messageManager.sendRpcMessage === 'function' ) {
  135. messageManager.sendRpcMessage(this.cpMessageName, details);
  136. } else {
  137. // Compatibility for older versions
  138. messageManager.sendSyncMessage(this.cpMessageName, details);
  139. }
  140. }
  141. return this.ACCEPT;
  142. },
  143. initContentScripts: function(win, sandbox) {
  144. let messager = getMessageManager(win);
  145. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  146. if ( sandbox ) {
  147. let sandboxName = [
  148. win.location.href.slice(0, 100),
  149. win.document.title.slice(0, 100)
  150. ].join(' | ');
  151. sandbox = Cu.Sandbox([win], {
  152. sandboxName: sandboxId + '[' + sandboxName + ']',
  153. sandboxPrototype: win,
  154. wantComponents: false,
  155. wantXHRConstructor: false
  156. });
  157. sandbox.injectScript = function(script, evalCode) {
  158. if ( evalCode ) {
  159. Cu.evalInSandbox(script, this);
  160. return;
  161. }
  162. Services.scriptloader.loadSubScript(script, this);
  163. }.bind(sandbox);
  164. }
  165. else {
  166. sandbox = win;
  167. }
  168. sandbox._sandboxId_ = sandboxId;
  169. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  170. sandbox.addMessageListener = function(callback) {
  171. if ( this._messageListener_ ) {
  172. this.removeMessageListener(
  173. this._sandboxId_,
  174. this._messageListener_
  175. );
  176. }
  177. this._messageListener_ = function(message) {
  178. callback(message.data);
  179. };
  180. messager.addMessageListener(
  181. this._sandboxId_,
  182. this._messageListener_
  183. );
  184. messager.addMessageListener(
  185. hostName + ':broadcast',
  186. this._messageListener_
  187. );
  188. }.bind(sandbox);
  189. sandbox.removeMessageListener = function() {
  190. try {
  191. messager.removeMessageListener(
  192. this._sandboxId_,
  193. this._messageListener_
  194. );
  195. messager.removeMessageListener(
  196. hostName + ':broadcast',
  197. this._messageListener_
  198. );
  199. } catch (ex) {
  200. // It throws sometimes, mostly when the popup closes
  201. }
  202. this._messageListener_ = null;
  203. }.bind(sandbox);
  204. return sandbox;
  205. },
  206. observe: function(subject) {
  207. let win = subject.defaultView;
  208. if ( !win ) {
  209. return;
  210. }
  211. let loc = win.location;
  212. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  213. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  214. this.initContentScripts(win);
  215. }
  216. // What about data: and about:blank?
  217. return;
  218. }
  219. let lss = Services.scriptloader.loadSubScript;
  220. let sandbox = this.initContentScripts(win, true);
  221. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  222. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  223. let docReady = function(e) {
  224. this.removeEventListener(e.type, docReady, true);
  225. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  226. };
  227. subject.addEventListener('DOMContentLoaded', docReady, true);
  228. }
  229. };
  230. /******************************************************************************/
  231. contentObserver.register();
  232. /******************************************************************************/