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.

276 lines
8.7 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) {
  150. Services.scriptloader.loadSubScript(script, sandbox);
  151. };
  152. }
  153. else {
  154. sandbox = win;
  155. }
  156. sandbox._sandboxId_ = sandboxId;
  157. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  158. sandbox.addMessageListener = function(callback) {
  159. if ( sandbox._messageListener_ ) {
  160. sandbox.removeMessageListener(
  161. sandbox._sandboxId_,
  162. sandbox._messageListener_
  163. );
  164. }
  165. sandbox._messageListener_ = function(message) {
  166. callback(message.data);
  167. };
  168. messager.addMessageListener(
  169. sandbox._sandboxId_,
  170. sandbox._messageListener_
  171. );
  172. messager.addMessageListener(
  173. hostName + ':broadcast',
  174. sandbox._messageListener_
  175. );
  176. };
  177. sandbox.removeMessageListener = function() {
  178. try {
  179. messager.removeMessageListener(
  180. sandbox._sandboxId_,
  181. sandbox._messageListener_
  182. );
  183. messager.removeMessageListener(
  184. hostName + ':broadcast',
  185. sandbox._messageListener_
  186. );
  187. } catch (ex) {
  188. // It throws sometimes, mostly when the popup closes
  189. }
  190. sandbox._messageListener_ = null;
  191. };
  192. return sandbox;
  193. },
  194. observe: function(subject) {
  195. let win = subject.defaultView;
  196. if ( !win ) {
  197. return;
  198. }
  199. let loc = win.location;
  200. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  201. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  202. this.initContentScripts(win);
  203. }
  204. // What about data: and about:blank?
  205. return;
  206. }
  207. let lss = Services.scriptloader.loadSubScript;
  208. let sandbox = this.initContentScripts(win, true);
  209. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  210. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  211. let docReady = function(e) {
  212. this.removeEventListener(e.type, docReady, true);
  213. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  214. };
  215. subject.addEventListener('DOMContentLoaded', docReady, true);
  216. }
  217. };
  218. /******************************************************************************/
  219. contentObserver.register();
  220. /******************************************************************************/