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.

299 lines
9.4 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. // Cu.import('resource://gre/modules/devtools/Console.jsm');
  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. getFrameId: function(win) {
  91. return win
  92. .QueryInterface(Ci.nsIInterfaceRequestor)
  93. .getInterface(Ci.nsIDOMWindowUtils)
  94. .outerWindowID;
  95. },
  96. // https://bugzil.la/612921
  97. shouldLoad: function(type, location, origin, context) {
  98. if ( !context ) {
  99. return this.ACCEPT;
  100. }
  101. if ( !location.schemeIs('http') && !location.schemeIs('https') ) {
  102. return this.ACCEPT;
  103. }
  104. let openerURL = null;
  105. if ( type === this.MAIN_FRAME ) {
  106. // When an iframe is loaded, it will be reported first as type = 6,
  107. // then immediately after that type = 7, so ignore the first report.
  108. // Origin should be "chrome://browser/content/browser.xul" here.
  109. // The lack of side-effects are not guaranteed though.
  110. if ( origin === null || origin.schemeIs('chrome') === false ) {
  111. return this.ACCEPT;
  112. }
  113. context = context.contentWindow || context;
  114. try {
  115. if ( context !== context.opener ) {
  116. openerURL = context.opener.location.href;
  117. }
  118. } catch (ex) {}
  119. } else {
  120. context = (context.ownerDocument || context).defaultView;
  121. }
  122. // The context for the toolbar popup is an iframe element here,
  123. // so check context.top instead of context
  124. if ( !context.top || !context.location ) {
  125. return this.ACCEPT;
  126. }
  127. let isTopLevel = context === context.top;
  128. let frameId = isTopLevel ? 0 : this.getFrameId(context);
  129. let parentFrameId;
  130. if ( isTopLevel ) {
  131. parentFrameId = -1;
  132. } else if ( context.parent === context.top ) {
  133. parentFrameId = 0;
  134. } else {
  135. parentFrameId = this.getFrameId(context.parent);
  136. }
  137. let messageManager = getMessageManager(context);
  138. let details = {
  139. frameId: frameId,
  140. openerURL: openerURL,
  141. parentFrameId: parentFrameId,
  142. type: type,
  143. url: location.spec
  144. };
  145. if ( typeof messageManager.sendRpcMessage === 'function' ) {
  146. // https://bugzil.la/1092216
  147. messageManager.sendRpcMessage(this.cpMessageName, details);
  148. } else {
  149. // Compatibility for older versions
  150. messageManager.sendSyncMessage(this.cpMessageName, details);
  151. }
  152. return this.ACCEPT;
  153. },
  154. initContentScripts: function(win, sandbox) {
  155. let messager = getMessageManager(win);
  156. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  157. if ( sandbox ) {
  158. let sandboxName = [
  159. win.location.href.slice(0, 100),
  160. win.document.title.slice(0, 100)
  161. ].join(' | ');
  162. sandbox = Cu.Sandbox([win], {
  163. sandboxName: sandboxId + '[' + sandboxName + ']',
  164. sandboxPrototype: win,
  165. wantComponents: false,
  166. wantXHRConstructor: false
  167. });
  168. sandbox.injectScript = function(script) {
  169. Services.scriptloader.loadSubScript(script, sandbox);
  170. };
  171. }
  172. else {
  173. sandbox = win;
  174. }
  175. sandbox._sandboxId_ = sandboxId;
  176. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  177. sandbox.addMessageListener = function(callback) {
  178. if ( sandbox._messageListener_ ) {
  179. sandbox.removeMessageListener(
  180. sandbox._sandboxId_,
  181. sandbox._messageListener_
  182. );
  183. }
  184. sandbox._messageListener_ = function(message) {
  185. callback(message.data);
  186. };
  187. messager.addMessageListener(
  188. sandbox._sandboxId_,
  189. sandbox._messageListener_
  190. );
  191. messager.addMessageListener(
  192. hostName + ':broadcast',
  193. sandbox._messageListener_
  194. );
  195. };
  196. sandbox.removeMessageListener = function() {
  197. try {
  198. messager.removeMessageListener(
  199. sandbox._sandboxId_,
  200. sandbox._messageListener_
  201. );
  202. messager.removeMessageListener(
  203. hostName + ':broadcast',
  204. sandbox._messageListener_
  205. );
  206. } catch (ex) {
  207. // It throws sometimes, mostly when the popup closes
  208. }
  209. sandbox._messageListener_ = null;
  210. };
  211. return sandbox;
  212. },
  213. observe: function(subject) {
  214. let win = subject.defaultView;
  215. if ( !win ) {
  216. return;
  217. }
  218. let loc = win.location;
  219. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  220. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  221. this.initContentScripts(win);
  222. }
  223. // What about data: and about:blank?
  224. return;
  225. }
  226. let lss = Services.scriptloader.loadSubScript;
  227. let sandbox = this.initContentScripts(win, true);
  228. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  229. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  230. let docReady = function(e) {
  231. this.removeEventListener(e.type, docReady, true);
  232. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  233. };
  234. subject.addEventListener('DOMContentLoaded', docReady, true);
  235. }
  236. };
  237. /******************************************************************************/
  238. contentObserver.register();
  239. /******************************************************************************/