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.

281 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. // let {console} = Cu.import('resource://gre/modules/devtools/Console.jsm', null);
  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. get componentRegistrar() {
  44. return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  45. },
  46. get categoryManager() {
  47. return Components.classes['@mozilla.org/categorymanager;1']
  48. .getService(Ci.nsICategoryManager);
  49. },
  50. QueryInterface: (function() {
  51. let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
  52. return XPCOMUtils.generateQI([
  53. Ci.nsIFactory,
  54. Ci.nsIObserver,
  55. Ci.nsIContentPolicy,
  56. Ci.nsISupportsWeakReference
  57. ]);
  58. })(),
  59. createInstance: function(outer, iid) {
  60. if ( outer ) {
  61. throw Components.results.NS_ERROR_NO_AGGREGATION;
  62. }
  63. return this.QueryInterface(iid);
  64. },
  65. register: function() {
  66. Services.obs.addObserver(this, 'document-element-inserted', true);
  67. this.componentRegistrar.registerFactory(
  68. this.classID,
  69. this.classDescription,
  70. this.contractID,
  71. this
  72. );
  73. this.categoryManager.addCategoryEntry(
  74. 'content-policy',
  75. this.contractID,
  76. this.contractID,
  77. false,
  78. true
  79. );
  80. },
  81. unregister: function() {
  82. Services.obs.removeObserver(this, 'document-element-inserted');
  83. this.componentRegistrar.unregisterFactory(this.classID, this);
  84. this.categoryManager.deleteCategoryEntry(
  85. 'content-policy',
  86. this.contractID,
  87. false
  88. );
  89. },
  90. // https://bugzil.la/612921
  91. shouldLoad: function(type, location, origin, context) {
  92. if ( !context ) {
  93. return this.ACCEPT;
  94. }
  95. if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
  96. return this.ACCEPT;
  97. }
  98. let openerURL, frameId;
  99. if ( type === this.MAIN_FRAME ) {
  100. frameId = -1;
  101. context = context.contentWindow || context;
  102. try {
  103. if ( context !== context.opener ) {
  104. openerURL = context.opener.location.href;
  105. }
  106. } catch (ex) {}
  107. } else {
  108. // TODO: frameId from outerWindowID?
  109. // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
  110. frameId = context === context.top ? 0 : 1;
  111. context = (context.ownerDocument || context).defaultView;
  112. }
  113. // The context for the toolbar popup is an iframe element here,
  114. // so check context.top instead of context
  115. if ( !context.top || !context.location ) {
  116. return this.ACCEPT;
  117. }
  118. let messageManager = getMessageManager(context);
  119. let details = {
  120. frameId: frameId,
  121. openerURL: openerURL || null,
  122. parentFrameId: context === context.top ? -1 : 0,
  123. type: type,
  124. url: location.spec
  125. };
  126. if ( typeof messageManager.sendRpcMessage === 'function' ) {
  127. // https://bugzil.la/1092216
  128. messageManager.sendRpcMessage(this.cpMessageName, details);
  129. } else {
  130. // Compatibility for older versions
  131. messageManager.sendSyncMessage(this.cpMessageName, details);
  132. }
  133. return this.ACCEPT;
  134. },
  135. initContentScripts: function(win, sandbox) {
  136. let messager = getMessageManager(win);
  137. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  138. if ( sandbox ) {
  139. let sandboxName = [
  140. win.location.href.slice(0, 100),
  141. win.document.title.slice(0, 100)
  142. ].join(' | ');
  143. sandbox = Cu.Sandbox([win], {
  144. sandboxName: sandboxId + '[' + sandboxName + ']',
  145. sandboxPrototype: win,
  146. wantComponents: false,
  147. wantXHRConstructor: false
  148. });
  149. sandbox.injectScript = function(script, evalCode) {
  150. if ( evalCode ) {
  151. Cu.evalInSandbox(script, this);
  152. return;
  153. }
  154. Services.scriptloader.loadSubScript(script, this);
  155. }.bind(sandbox);
  156. }
  157. else {
  158. sandbox = win;
  159. }
  160. sandbox._sandboxId_ = sandboxId;
  161. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  162. sandbox.addMessageListener = function(callback) {
  163. if ( this._messageListener_ ) {
  164. this.removeMessageListener(
  165. this._sandboxId_,
  166. this._messageListener_
  167. );
  168. }
  169. this._messageListener_ = function(message) {
  170. callback(message.data);
  171. };
  172. messager.addMessageListener(
  173. this._sandboxId_,
  174. this._messageListener_
  175. );
  176. messager.addMessageListener(
  177. hostName + ':broadcast',
  178. this._messageListener_
  179. );
  180. }.bind(sandbox);
  181. sandbox.removeMessageListener = function() {
  182. try {
  183. messager.removeMessageListener(
  184. this._sandboxId_,
  185. this._messageListener_
  186. );
  187. messager.removeMessageListener(
  188. hostName + ':broadcast',
  189. this._messageListener_
  190. );
  191. } catch (ex) {
  192. // It throws sometimes, mostly when the popup closes
  193. }
  194. this._messageListener_ = null;
  195. }.bind(sandbox);
  196. return sandbox;
  197. },
  198. observe: function(subject) {
  199. let win = subject.defaultView;
  200. if ( !win ) {
  201. return;
  202. }
  203. let loc = win.location;
  204. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  205. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  206. this.initContentScripts(win);
  207. }
  208. // What about data: and about:blank?
  209. return;
  210. }
  211. let lss = Services.scriptloader.loadSubScript;
  212. let sandbox = this.initContentScripts(win, true);
  213. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  214. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  215. let docReady = function(e) {
  216. this.removeEventListener(e.type, docReady, true);
  217. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  218. };
  219. subject.addEventListener('DOMContentLoaded', docReady, true);
  220. }
  221. };
  222. /******************************************************************************/
  223. contentObserver.register();
  224. /******************************************************************************/