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.

269 lines
8.5 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', {});
  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. var opener;
  96. if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
  97. if ( type !== this.MAIN_FRAME ) {
  98. return this.ACCEPT;
  99. }
  100. context = context.contentWindow || context;
  101. try {
  102. opener = context.opener.location.href;
  103. } catch (ex) {}
  104. let isPopup = location.spec === 'about:blank' && opener;
  105. if ( location.scheme !== 'data' && !isPopup ) {
  106. return this.ACCEPT;
  107. }
  108. } else if ( type === this.MAIN_FRAME ) {
  109. context = context.contentWindow || context;
  110. try {
  111. opener = context.opener.location.href;
  112. } catch (ex) {}
  113. } else {
  114. context = (context.ownerDocument || context).defaultView;
  115. }
  116. // The context for the popups is an iframe element here,
  117. // so check context.top instead
  118. if ( context.top && context.location ) {
  119. // https://bugzil.la/1092216
  120. getMessageManager(context).sendRpcMessage(this.cpMessageName, {
  121. opener: opener || null,
  122. url: location.spec,
  123. type: type,
  124. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  125. parentFrameId: context === context.top ? -1 : 0
  126. });
  127. }
  128. return this.ACCEPT;
  129. },
  130. initContentScripts: function(win, sandbox) {
  131. let messager = getMessageManager(win);
  132. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  133. if ( sandbox ) {
  134. let sandboxName = [
  135. win.location.href.slice(0, 100),
  136. win.document.title.slice(0, 100)
  137. ].join(' | ');
  138. sandbox = Cu.Sandbox([win], {
  139. sandboxName: sandboxId + '[' + sandboxName + ']',
  140. sandboxPrototype: win,
  141. wantComponents: false,
  142. wantXHRConstructor: false
  143. });
  144. sandbox.injectScript = function(script, evalCode) {
  145. if ( evalCode ) {
  146. Cu.evalInSandbox(script, this);
  147. return;
  148. }
  149. Services.scriptloader.loadSubScript(script, this);
  150. }.bind(sandbox);
  151. }
  152. else {
  153. sandbox = win;
  154. }
  155. sandbox._sandboxId_ = sandboxId;
  156. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  157. sandbox.addMessageListener = function(callback) {
  158. if ( this._messageListener_ ) {
  159. this.removeMessageListener(
  160. this._sandboxId_,
  161. this._messageListener_
  162. );
  163. }
  164. this._messageListener_ = function(message) {
  165. callback(message.data);
  166. };
  167. messager.addMessageListener(
  168. this._sandboxId_,
  169. this._messageListener_
  170. );
  171. messager.addMessageListener(
  172. hostName + ':broadcast',
  173. this._messageListener_
  174. );
  175. }.bind(sandbox);
  176. sandbox.removeMessageListener = function() {
  177. messager.removeMessageListener(
  178. this._sandboxId_,
  179. this._messageListener_
  180. );
  181. messager.removeMessageListener(
  182. hostName + ':broadcast',
  183. this._messageListener_
  184. );
  185. this._messageListener_ = null;
  186. }.bind(sandbox);
  187. return sandbox;
  188. },
  189. observe: function(subject) {
  190. let win = subject.defaultView;
  191. if ( !win ) {
  192. return;
  193. }
  194. let loc = win.location;
  195. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  196. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  197. this.initContentScripts(win);
  198. }
  199. // What about data: and about:blank?
  200. return;
  201. }
  202. let lss = Services.scriptloader.loadSubScript;
  203. let sandbox = this.initContentScripts(win, true);
  204. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  205. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  206. let docReady = function(e) {
  207. this.removeEventListener(e.type, docReady, true);
  208. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  209. };
  210. subject.addEventListener('DOMContentLoaded', docReady, true);
  211. }
  212. };
  213. /******************************************************************************/
  214. contentObserver.register();
  215. /******************************************************************************/