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.

278 lines
8.8 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. openerURL = context.opener.location.href;
  103. } catch (ex) {}
  104. let isPopup = location.spec === 'about:blank' && openerURL;
  105. if ( !location.schemeIs('data') && !isPopup ) {
  106. return this.ACCEPT;
  107. }
  108. } else if ( type === this.MAIN_FRAME ) {
  109. context = context.contentWindow || context;
  110. try {
  111. openerURL = context.opener.location.href;
  112. } catch (ex) {}
  113. } else {
  114. context = (context.ownerDocument || context).defaultView;
  115. }
  116. // The context for the toolbar popup is an iframe element here,
  117. // so check context.top instead of context
  118. if ( context.top && context.location ) {
  119. // https://bugzil.la/1092216
  120. getMessageManager(context).sendRpcMessage(this.cpMessageName, {
  121. openerURL: openerURL || 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._urlNormalizer_ = function(url, baseURI) {
  157. baseURI = Services.io.newURI(baseURI, null, null);
  158. return Services.io.newURI(url, null, baseURI).asciiSpec;
  159. };
  160. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  161. sandbox.addMessageListener = function(callback) {
  162. if ( this._messageListener_ ) {
  163. this.removeMessageListener(
  164. this._sandboxId_,
  165. this._messageListener_
  166. );
  167. }
  168. this._messageListener_ = function(message) {
  169. callback(message.data);
  170. };
  171. messager.addMessageListener(
  172. this._sandboxId_,
  173. this._messageListener_
  174. );
  175. messager.addMessageListener(
  176. hostName + ':broadcast',
  177. this._messageListener_
  178. );
  179. }.bind(sandbox);
  180. sandbox.removeMessageListener = function() {
  181. try {
  182. messager.removeMessageListener(
  183. this._sandboxId_,
  184. this._messageListener_
  185. );
  186. messager.removeMessageListener(
  187. hostName + ':broadcast',
  188. this._messageListener_
  189. );
  190. } catch (ex) {
  191. // It throws sometimes, mostly when the popup closes
  192. }
  193. this._messageListener_ = null;
  194. }.bind(sandbox);
  195. return sandbox;
  196. },
  197. observe: function(subject) {
  198. let win = subject.defaultView;
  199. if ( !win ) {
  200. return;
  201. }
  202. let loc = win.location;
  203. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  204. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  205. this.initContentScripts(win);
  206. }
  207. // What about data: and about:blank?
  208. return;
  209. }
  210. let lss = Services.scriptloader.loadSubScript;
  211. let sandbox = this.initContentScripts(win, true);
  212. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  213. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  214. let docReady = function(e) {
  215. this.removeEventListener(e.type, docReady, true);
  216. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  217. };
  218. subject.addEventListener('DOMContentLoaded', docReady, true);
  219. }
  220. };
  221. /******************************************************************************/
  222. contentObserver.register();
  223. /******************************************************************************/