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. 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. getMessageManager(context).sendRpcMessage(this.cpMessageName, {
  125. openerURL: openerURL || 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. sandbox._sandboxId_ = sandboxId;
  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. /******************************************************************************/