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.

258 lines
8.1 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/uMatrix
  15. */
  16. 'use strict';
  17. /******************************************************************************/
  18. this.EXPORTED_SYMBOLS = ['contentObserver', 'LocationChangeListener'];
  19. const {interfaces: Ci, utils: Cu} = Components;
  20. const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
  21. const {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
  22. const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
  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('{c84283d4-9975-41b7-b1a4-f106af56b51d}'),
  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. uniqueSandboxId: 1,
  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: XPCOMUtils.generateQI([
  51. Ci.nsIFactory,
  52. Ci.nsIObserver,
  53. Ci.nsIContentPolicy,
  54. Ci.nsISupportsWeakReference
  55. ]),
  56. createInstance: function(outer, iid) {
  57. if ( outer ) {
  58. throw Components.results.NS_ERROR_NO_AGGREGATION;
  59. }
  60. return this.QueryInterface(iid);
  61. },
  62. register: function() {
  63. Services.obs.addObserver(this, 'document-element-inserted', true);
  64. this.componentRegistrar.registerFactory(
  65. this.classID,
  66. this.classDescription,
  67. this.contractID,
  68. this
  69. );
  70. this.categoryManager.addCategoryEntry(
  71. 'content-policy',
  72. this.contractID,
  73. this.contractID,
  74. false,
  75. true
  76. );
  77. },
  78. unregister: function() {
  79. Services.obs.removeObserver(this, 'document-element-inserted');
  80. this.componentRegistrar.unregisterFactory(this.classID, this);
  81. this.categoryManager.deleteCategoryEntry(
  82. 'content-policy',
  83. this.contractID,
  84. false
  85. );
  86. },
  87. // https://bugzil.la/612921
  88. shouldLoad: function(type, location, origin, context) {
  89. return this.ACCEPT;
  90. },
  91. initContentScripts: function(win, sandbox) {
  92. let messager = getMessageManager(win);
  93. let sandboxId = hostName + ':sb:' + this.uniqueSandboxId++;
  94. if ( sandbox ) {
  95. let sandboxName = [
  96. win.location.href.slice(0, 100),
  97. win.document.title.slice(0, 100)
  98. ].join(' | ');
  99. sandbox = Cu.Sandbox([win], {
  100. sandboxName: sandboxId + '[' + sandboxName + ']',
  101. sandboxPrototype: win,
  102. wantComponents: false,
  103. wantXHRConstructor: false
  104. });
  105. sandbox.injectScript = function(script) {
  106. Services.scriptloader.loadSubScript(script, sandbox);
  107. };
  108. }
  109. else {
  110. sandbox = win;
  111. }
  112. sandbox._sandboxId_ = sandboxId;
  113. sandbox.sendAsyncMessage = messager.sendAsyncMessage;
  114. sandbox.addMessageListener = function(callback) {
  115. if ( sandbox._messageListener_ ) {
  116. sandbox.removeMessageListener();
  117. }
  118. sandbox._messageListener_ = function(message) {
  119. callback(message.data);
  120. };
  121. messager.addMessageListener(
  122. sandbox._sandboxId_,
  123. sandbox._messageListener_
  124. );
  125. messager.addMessageListener(
  126. hostName + ':broadcast',
  127. sandbox._messageListener_
  128. );
  129. };
  130. sandbox.removeMessageListener = function() {
  131. try {
  132. messager.removeMessageListener(
  133. sandbox._sandboxId_,
  134. sandbox._messageListener_
  135. );
  136. messager.removeMessageListener(
  137. hostName + ':broadcast',
  138. sandbox._messageListener_
  139. );
  140. } catch (ex) {
  141. // It throws sometimes, mostly when the popup closes
  142. }
  143. sandbox._messageListener_ = null;
  144. };
  145. return sandbox;
  146. },
  147. observe: function(doc) {
  148. let win = doc.defaultView;
  149. if ( !win ) {
  150. return;
  151. }
  152. let loc = win.location;
  153. if ( !loc ) {
  154. return;
  155. }
  156. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' && loc.protocol !== 'file:' ) {
  157. if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
  158. this.initContentScripts(win);
  159. }
  160. // What about data: and about:blank?
  161. return;
  162. }
  163. let lss = Services.scriptloader.loadSubScript;
  164. let sandbox = this.initContentScripts(win, true);
  165. lss(this.contentBaseURI + 'vapi-client.js', sandbox);
  166. lss(this.contentBaseURI + 'contentscript-start.js', sandbox);
  167. let docReady = (e) => {
  168. let doc = e.target;
  169. doc.removeEventListener(e.type, docReady, true);
  170. lss(this.contentBaseURI + 'contentscript-end.js', sandbox);
  171. };
  172. if ( doc.readyState === 'loading') {
  173. doc.addEventListener('DOMContentLoaded', docReady, true);
  174. } else {
  175. docReady({ target: doc, type: 'DOMContentLoaded' });
  176. }
  177. }
  178. };
  179. /******************************************************************************/
  180. const locationChangedMessageName = hostName + ':locationChanged';
  181. const LocationChangeListener = function(docShell) {
  182. if (docShell) {
  183. docShell.QueryInterface(Ci.nsIInterfaceRequestor);
  184. this.docShell = docShell.getInterface(Ci.nsIWebProgress);
  185. this.messageManager = docShell.getInterface(Ci.nsIContentFrameMessageManager);
  186. if (this.messageManager && typeof this.messageManager.sendAsyncMessage === 'function') {
  187. this.docShell.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
  188. }
  189. }
  190. };
  191. LocationChangeListener.prototype.QueryInterface = XPCOMUtils.generateQI(["nsIWebProgressListener", "nsISupportsWeakReference"]);
  192. LocationChangeListener.prototype.onLocationChange = function(webProgress, request, location, flags) {
  193. if ( !webProgress.isTopLevel ) {
  194. return;
  195. }
  196. this.messageManager.sendAsyncMessage(locationChangedMessageName, {
  197. url: location.asciiSpec,
  198. flags: flags,
  199. });
  200. };
  201. /******************************************************************************/
  202. contentObserver.register();
  203. /******************************************************************************/