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.

233 lines
7.2 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. /* global Services, Components, XPCOMUtils, __URI__ */
  17. 'use strict';
  18. /******************************************************************************/
  19. this.EXPORTED_SYMBOLS = ['contentObserver'];
  20. const {interfaces: Ci, utils: Cu} = Components;
  21. const appName = __URI__.match(/:\/\/([^\/]+)/)[1];
  22. Cu['import']('resource://gre/modules/Services.jsm');
  23. Cu['import']('resource://gre/modules/XPCOMUtils.jsm');
  24. // Cu['import']('resource://gre/modules/devtools/Console.jsm');
  25. /******************************************************************************/
  26. const getMessageManager = function(context) {
  27. return context
  28. .QueryInterface(Ci.nsIInterfaceRequestor)
  29. .getInterface(Ci.nsIDocShell)
  30. .sameTypeRootTreeItem
  31. .QueryInterface(Ci.nsIDocShell)
  32. .QueryInterface(Ci.nsIInterfaceRequestor)
  33. .getInterface(Ci.nsIContentFrameMessageManager);
  34. };
  35. /******************************************************************************/
  36. const contentObserver = {
  37. classDescription: 'content-policy for ' + appName,
  38. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  39. contractID: '@' + appName + '/content-policy;1',
  40. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  41. MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  42. contentBaseURI: 'chrome://' + appName + '/content/js/',
  43. messageName: appName + ':shouldLoad',
  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. // https://bugzil.la/612921
  89. shouldLoad: function(type, location, origin, context) {
  90. // If we don't know what initiated the request, probably it's not a tab
  91. if ( !context ) {
  92. return this.ACCEPT;
  93. }
  94. let opener;
  95. if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
  96. if ( type !== this.MAIN_FRAME ) {
  97. return this.ACCEPT;
  98. }
  99. context = context.contentWindow || context;
  100. try {
  101. opener = context.opener.location.href;
  102. } catch (ex) {}
  103. let isPopup = location.spec === 'about:blank' && opener;
  104. if ( location.scheme !== 'data' && !isPopup ) {
  105. return this.ACCEPT;
  106. }
  107. } else if ( type === this.MAIN_FRAME ) {
  108. context = context.contentWindow || context;
  109. try {
  110. opener = context.opener.location.href;
  111. } catch (ex) {}
  112. } else {
  113. context = (context.ownerDocument || context).defaultView;
  114. }
  115. // The context for the popups is an iframe element here,
  116. // so check context.top instead
  117. if ( context.top && context.location ) {
  118. // https://bugzil.la/1092216
  119. getMessageManager(context).sendRpcMessage(this.messageName, {
  120. opener: opener || null,
  121. url: location.spec,
  122. type: type,
  123. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  124. parentFrameId: context === context.top ? -1 : 0
  125. });
  126. }
  127. return this.ACCEPT;
  128. },
  129. initContentScripts: function(win, sandbox) {
  130. let messager = getMessageManager(win);
  131. if ( sandbox ) {
  132. win = Cu.Sandbox([win], {
  133. sandboxPrototype: win,
  134. wantComponents: false,
  135. wantXHRConstructor: false
  136. });
  137. win.self = win;
  138. // anonymous function needs to be used here
  139. win.injectScript = Cu.exportFunction(
  140. function(script, evalCode) {
  141. if ( evalCode ) {
  142. Cu.evalInSandbox(script, win);
  143. return;
  144. }
  145. Services.scriptloader.loadSubScript(script, win);
  146. },
  147. win
  148. );
  149. }
  150. win.sendAsyncMessage = messager.sendAsyncMessage;
  151. win.addMessageListener = messager.ublock_addMessageListener;
  152. win.removeMessageListener = messager.ublock_removeMessageListener;
  153. return win;
  154. },
  155. observe: function(doc) {
  156. let win = doc.defaultView;
  157. if ( !win ) {
  158. return;
  159. }
  160. let loc = win.location;
  161. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  162. if ( loc.protocol === 'chrome:' && loc.host === appName ) {
  163. this.initContentScripts(win);
  164. }
  165. return;
  166. }
  167. let lss = Services.scriptloader.loadSubScript;
  168. win = this.initContentScripts(win, true);
  169. lss(this.contentBaseURI + 'vapi-client.js', win);
  170. lss(this.contentBaseURI + 'contentscript-start.js', win);
  171. let docReady = function(e) {
  172. this.removeEventListener(e.type, docReady, true);
  173. lss(contentObserver.contentBaseURI + 'contentscript-end.js', win);
  174. };
  175. win.document.addEventListener('DOMContentLoaded', docReady, true);
  176. }
  177. };
  178. /******************************************************************************/
  179. contentObserver.register();
  180. /******************************************************************************/