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.

298 lines
9.3 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 parentFrameId;
  129. if ( isTopLevel ) {
  130. parentFrameId = -1;
  131. } else if ( context.parent === context.top ) {
  132. parentFrameId = 0;
  133. } else {
  134. parentFrameId = this.getFrameId(context.parent);
  135. }
  136. let messageManager = getMessageManager(context);
  137. let details = {
  138. frameId: isTopLevel ? 0 : this.getFrameId(context),
  139. openerURL: openerURL,
  140. parentFrameId: parentFrameId,
  141. type: type,
  142. url: location.spec
  143. };
  144. if ( typeof messageManager.sendRpcMessage === 'function' ) {
  145. // https://bugzil.la/1092216
  146. messageManager.sendRpcMessage(this.cpMessageName, details);
  147. } else {
  148. // Compatibility for older versions
  149. messageManager.sendSyncMessage(this.cpMessageName, details);
  150. }
  151. return this.ACCEPT;
  152. },
  153. initContentScripts: function(win, sandbox) {
  154. let messager = getMessageManager(win);
  155. let sandboxId = hostName + ':sb:' + uniqueSandboxId++;
  156. if ( sandbox ) {
  157. let sandboxName = [
  158. win.location.href.slice(0, 100),
  159. win.document.title.slice(0, 100)
  160. ].join(' | ');
  161. sandbox = Cu.Sandbox([win], {
  162. sandboxName: sandboxId + '[' + sandboxName + ']',
  163. sandboxPrototype: win,
  164. wantComponents: false,
  165. wantXHRConstructor: false
  166. });
  167. sandbox.injectScript = function(script) {
  168. Services.scriptloader.loadSubScript(script, sandbox);
  169. };
  170. }
  171. else {
  172. sandbox = win;
  173. }
  174. sandbox._sandboxId_ = sandboxId;
  175. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  176. sandbox.addMessageListener = function(callback) {
  177. if ( sandbox._messageListener_ ) {
  178. sandbox.removeMessageListener(
  179. sandbox._sandboxId_,
  180. sandbox._messageListener_
  181. );
  182. }
  183. sandbox._messageListener_ = function(message) {
  184. callback(message.data);
  185. };
  186. messager.addMessageListener(
  187. sandbox._sandboxId_,
  188. sandbox._messageListener_
  189. );
  190. messager.addMessageListener(
  191. hostName + ':broadcast',
  192. sandbox._messageListener_
  193. );
  194. };
  195. sandbox.removeMessageListener = function() {
  196. try {
  197. messager.removeMessageListener(
  198. sandbox._sandboxId_,
  199. sandbox._messageListener_
  200. );
  201. messager.removeMessageListener(
  202. hostName + ':broadcast',
  203. sandbox._messageListener_
  204. );
  205. } catch (ex) {
  206. // It throws sometimes, mostly when the popup closes
  207. }
  208. sandbox._messageListener_ = null;
  209. };
  210. return sandbox;
  211. },
  212. observe: function(subject) {
  213. let win = subject.defaultView;
  214. if ( !win ) {
  215. return;
  216. }
  217. let loc = win.location;
  218. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  219. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  220. this.initContentScripts(win);
  221. }
  222. // What about data: and about:blank?
  223. return;
  224. }
  225. let lss = Services.scriptloader.loadSubScript;
  226. let sandbox = this.initContentScripts(win, true);
  227. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  228. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  229. let docReady = function(e) {
  230. this.removeEventListener(e.type, docReady, true);
  231. lss(contentObserver.contentBaseURI + 'contentscript-end.js', sandbox);
  232. };
  233. subject.addEventListener('DOMContentLoaded', docReady, true);
  234. }
  235. };
  236. /******************************************************************************/
  237. contentObserver.register();
  238. /******************************************************************************/