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.

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