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.

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