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.1 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. Cu.import('resource://gre/modules/devtools/Console.jsm');
  24. /******************************************************************************/
  25. const getMessageManager = function(win) {
  26. return win
  27. .QueryInterface(Ci.nsIInterfaceRequestor)
  28. .getInterface(Ci.nsIDocShell)
  29. .sameTypeRootTreeItem
  30. .QueryInterface(Ci.nsIDocShell)
  31. .QueryInterface(Ci.nsIInterfaceRequestor)
  32. .getInterface(Ci.nsIContentFrameMessageManager);
  33. };
  34. /******************************************************************************/
  35. const contentObserver = {
  36. classDescription: 'content-policy for ' + hostName,
  37. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  38. contractID: '@' + hostName + '/content-policy;1',
  39. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  40. MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  41. contentBaseURI: 'chrome://' + hostName + '/content/js/',
  42. cpMessageName: hostName + ':shouldLoad',
  43. frameSandboxes: new WeakMap(),
  44. get componentRegistrar() {
  45. return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  46. },
  47. get categoryManager() {
  48. return Components.classes['@mozilla.org/categorymanager;1']
  49. .getService(Ci.nsICategoryManager);
  50. },
  51. QueryInterface: (function() {
  52. let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', {});
  53. return XPCOMUtils.generateQI([
  54. Ci.nsIFactory,
  55. Ci.nsIObserver,
  56. Ci.nsIContentPolicy,
  57. Ci.nsISupportsWeakReference
  58. ]);
  59. })(),
  60. createInstance: function(outer, iid) {
  61. if ( outer ) {
  62. throw Components.results.NS_ERROR_NO_AGGREGATION;
  63. }
  64. return this.QueryInterface(iid);
  65. },
  66. register: function() {
  67. Services.obs.addObserver(this, 'document-element-inserted', true);
  68. Services.obs.addObserver(this, 'dom-window-destroyed', true);
  69. this.componentRegistrar.registerFactory(
  70. this.classID,
  71. this.classDescription,
  72. this.contractID,
  73. this
  74. );
  75. this.categoryManager.addCategoryEntry(
  76. 'content-policy',
  77. this.contractID,
  78. this.contractID,
  79. false,
  80. true
  81. );
  82. },
  83. unregister: function() {
  84. Services.obs.removeObserver(this, 'document-element-inserted');
  85. Services.obs.removeObserver(this, 'dom-window-destroyed');
  86. this.componentRegistrar.unregisterFactory(this.classID, this);
  87. this.categoryManager.deleteCategoryEntry(
  88. 'content-policy',
  89. this.contractID,
  90. false
  91. );
  92. },
  93. // https://bugzil.la/612921
  94. shouldLoad: function(type, location, origin, context) {
  95. // If we don't know what initiated the request, probably it's not a tab
  96. if ( !context ) {
  97. return this.ACCEPT;
  98. }
  99. var opener;
  100. if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
  101. if ( type !== this.MAIN_FRAME ) {
  102. return this.ACCEPT;
  103. }
  104. context = context.contentWindow || context;
  105. try {
  106. opener = context.opener.location.href;
  107. } catch (ex) {}
  108. let isPopup = location.spec === 'about:blank' && opener;
  109. if ( location.scheme !== 'data' && !isPopup ) {
  110. return this.ACCEPT;
  111. }
  112. } else if ( type === this.MAIN_FRAME ) {
  113. context = context.contentWindow || context;
  114. try {
  115. opener = context.opener.location.href;
  116. } catch (ex) {}
  117. } else {
  118. context = (context.ownerDocument || context).defaultView;
  119. }
  120. // The context for the popups is an iframe element here,
  121. // so check context.top instead
  122. if ( context.top && context.location ) {
  123. // https://bugzil.la/1092216
  124. getMessageManager(context).sendRpcMessage(this.cpMessageName, {
  125. opener: opener || null,
  126. url: location.spec,
  127. type: type,
  128. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  129. parentFrameId: context === context.top ? -1 : 0
  130. });
  131. }
  132. return this.ACCEPT;
  133. },
  134. initContentScripts: function(win, sandbox) {
  135. let messager = getMessageManager(win);
  136. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  137. if ( sandbox ) {
  138. let sandboxName = [
  139. win.location.href.slice(0, 100),
  140. win.document.title.slice(0, 100)
  141. ].join(' | ');
  142. sandbox = Cu.Sandbox([win], {
  143. sandboxName: sandboxId + '[' + sandboxName + ']',
  144. sandboxPrototype: win,
  145. wantComponents: false,
  146. wantXHRConstructor: false
  147. });
  148. sandbox.injectScript = function(script, evalCode) {
  149. if ( evalCode ) {
  150. Cu.evalInSandbox(script, this);
  151. return;
  152. }
  153. Services.scriptloader.loadSubScript(script, this);
  154. }.bind(sandbox);
  155. }
  156. else {
  157. sandbox = win;
  158. }
  159. if ( win !== win.top ) {
  160. this.frameSandboxes.set(win, sandbox);
  161. }
  162. sandbox._sandboxId_ = sandboxId;
  163. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  164. sandbox.addMessageListener = function(callback) {
  165. if ( this._messageListener_ ) {
  166. this.removeMessageListener(
  167. this._sandboxId_,
  168. this._messageListener_
  169. );
  170. }
  171. this._messageListener_ = function(message) {
  172. callback(message.data);
  173. };
  174. messager.addMessageListener(
  175. this._sandboxId_,
  176. this._messageListener_
  177. );
  178. messager.addMessageListener(
  179. hostName + ':broadcast',
  180. this._messageListener_
  181. );
  182. }.bind(sandbox);
  183. sandbox.removeMessageListener = function() {
  184. messager.removeMessageListener(
  185. this._sandboxId_,
  186. this._messageListener_
  187. );
  188. messager.removeMessageListener(
  189. hostName + ':broadcast',
  190. this._messageListener_
  191. );
  192. this._messageListener_ = null;
  193. }.bind(sandbox);
  194. return sandbox;
  195. },
  196. observe: function(subject, topic) {
  197. if ( topic === 'dom-window-destroyed' ) {
  198. let sandbox = this.frameSandboxes.get(subject);
  199. if ( sandbox ) {
  200. sandbox.removeMessageListener();
  201. this.frameSandboxes.delete(subject);
  202. }
  203. return;
  204. }
  205. let win = subject.defaultView;
  206. if ( !win ) {
  207. return;
  208. }
  209. let loc = win.location;
  210. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  211. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  212. this.initContentScripts(win);
  213. }
  214. // What about data: and about:blank?
  215. return;
  216. }
  217. let lss = Services.scriptloader.loadSubScript;
  218. let sandbox = this.initContentScripts(win, true);
  219. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  220. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  221. let docReady = function(e) {
  222. this.removeEventListener(e.type, docReady, true);
  223. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  224. };
  225. subject.addEventListener('DOMContentLoaded', docReady, true);
  226. }
  227. };
  228. /******************************************************************************/
  229. contentObserver.register();
  230. /******************************************************************************/