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.

235 lines
7.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
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} = this.Components;
  20. const appName = this.__URI__.match(/:\/\/([^\/]+)/)[1];
  21. Cu.import('resource://gre/modules/Services.jsm');
  22. Cu.import('resource://gre/modules/devtools/Console.jsm');
  23. /******************************************************************************/
  24. const getMessageManager = function(context) {
  25. return context
  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 ' + appName,
  36. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  37. contractID: '@' + appName + '/content-policy;1',
  38. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  39. MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  40. contentBaseURI: 'chrome://' + appName + '/content/js/',
  41. messageName: appName + ':shouldLoad',
  42. get componentRegistrar() {
  43. return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  44. },
  45. get categoryManager() {
  46. return Components.classes['@mozilla.org/categorymanager;1']
  47. .getService(Ci.nsICategoryManager);
  48. },
  49. QueryInterface: (function() {
  50. let {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {});
  51. return XPCOMUtils.generateQI([
  52. Ci.nsIFactory,
  53. Ci.nsIObserver,
  54. Ci.nsIContentPolicy,
  55. Ci.nsISupportsWeakReference
  56. ]);
  57. })(),
  58. createInstance: function(outer, iid) {
  59. if ( outer ) {
  60. throw Components.results.NS_ERROR_NO_AGGREGATION;
  61. }
  62. return this.QueryInterface(iid);
  63. },
  64. register: function() {
  65. Services.obs.addObserver(this, 'document-element-inserted', true);
  66. this.componentRegistrar.registerFactory(
  67. this.classID,
  68. this.classDescription,
  69. this.contractID,
  70. this
  71. );
  72. this.categoryManager.addCategoryEntry(
  73. 'content-policy',
  74. this.contractID,
  75. this.contractID,
  76. false,
  77. true
  78. );
  79. },
  80. unregister: function() {
  81. Services.obs.removeObserver(this, 'document-element-inserted');
  82. this.componentRegistrar.unregisterFactory(this.classID, this);
  83. this.categoryManager.deleteCategoryEntry(
  84. 'content-policy',
  85. this.contractID,
  86. false
  87. );
  88. },
  89. // https://bugzil.la/612921
  90. shouldLoad: function(type, location, origin, context) {
  91. // If we don't know what initiated the request, probably it's not a tab
  92. if ( !context ) {
  93. return this.ACCEPT;
  94. }
  95. var opener;
  96. if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
  97. if ( type !== this.MAIN_FRAME ) {
  98. return this.ACCEPT;
  99. }
  100. context = context.contentWindow || context;
  101. try {
  102. opener = context.opener.location.href;
  103. } catch (ex) {}
  104. let isPopup = location.spec === 'about:blank' && opener;
  105. if ( location.scheme !== 'data' && !isPopup ) {
  106. return this.ACCEPT;
  107. }
  108. } else if ( type === this.MAIN_FRAME ) {
  109. context = context.contentWindow || context;
  110. try {
  111. opener = context.opener.location.href;
  112. } catch (ex) {}
  113. } else {
  114. context = (context.ownerDocument || context).defaultView;
  115. }
  116. // The context for the popups is an iframe element here,
  117. // so check context.top instead
  118. if ( context.top && context.location ) {
  119. // https://bugzil.la/1092216
  120. getMessageManager(context).sendRpcMessage(this.messageName, {
  121. opener: opener || null,
  122. url: location.spec,
  123. type: type,
  124. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  125. parentFrameId: context === context.top ? -1 : 0
  126. });
  127. }
  128. return this.ACCEPT;
  129. },
  130. initContentScripts: function(win, sandbox) {
  131. let messager = getMessageManager(win);
  132. if ( sandbox ) {
  133. win = Cu.Sandbox([win], {
  134. sandboxPrototype: win,
  135. wantComponents: false,
  136. wantXHRConstructor: false
  137. });
  138. win.self = win;
  139. // anonymous function needs to be used here
  140. win.injectScript = Cu.exportFunction(
  141. function(script, evalCode) {
  142. if ( evalCode ) {
  143. Cu.evalInSandbox(script, win);
  144. return;
  145. }
  146. Services.scriptloader.loadSubScript(script, win);
  147. },
  148. win
  149. );
  150. }
  151. win.sendAsyncMessage = messager.sendAsyncMessage;
  152. win.addMessageListener = messager.ublock_addMessageListener;
  153. win.removeMessageListener = messager.ublock_removeMessageListener;
  154. return win;
  155. },
  156. observe: function(doc) {
  157. let win = doc.defaultView;
  158. if ( !win ) {
  159. return;
  160. }
  161. let loc = win.location;
  162. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  163. if ( loc.protocol === 'chrome:' && loc.host === appName ) {
  164. this.initContentScripts(win);
  165. }
  166. // What about data: and about:blank?
  167. return;
  168. }
  169. let lss = Services.scriptloader.loadSubScript;
  170. win = this.initContentScripts(win, true);
  171. lss(this.contentBaseURI + 'vapi-client.js', win);
  172. lss(this.contentBaseURI + 'contentscript-start.js', win);
  173. let docReady = function(e) {
  174. this.removeEventListener(e.type, docReady, true);
  175. lss(contentObserver.contentBaseURI + 'contentscript-end.js', win);
  176. };
  177. win.document.addEventListener('DOMContentLoaded', docReady, true);
  178. }
  179. };
  180. /******************************************************************************/
  181. contentObserver.register();
  182. /******************************************************************************/