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. getMessageManager(context).sendSyncMessage(this.messageName, {
  119. opener: opener || null,
  120. url: location.spec,
  121. type: type,
  122. frameId: type === this.MAIN_FRAME ? -1 : (context === context.top ? 0 : 1),
  123. parentFrameId: context === context.top ? -1 : 0
  124. });
  125. }
  126. return this.ACCEPT;
  127. },
  128. initContentScripts: function(win, sandbox) {
  129. let messager = getMessageManager(win);
  130. if ( sandbox ) {
  131. win = Cu.Sandbox([win], {
  132. sandboxPrototype: win,
  133. wantComponents: false,
  134. wantXHRConstructor: false
  135. });
  136. win.self = win;
  137. // anonymous function needs to be used here
  138. win.injectScript = Cu.exportFunction(
  139. function(script, evalCode) {
  140. if ( evalCode ) {
  141. Cu.evalInSandbox(script, win);
  142. return;
  143. }
  144. Services.scriptloader.loadSubScript(script, win);
  145. },
  146. win
  147. );
  148. }
  149. win.sendAsyncMessage = messager.sendAsyncMessage;
  150. win.addMessageListener = messager.ublock_addMessageListener;
  151. win.removeMessageListener = messager.ublock_removeMessageListener;
  152. return win;
  153. },
  154. observe: function(doc) {
  155. let win = doc.defaultView;
  156. if ( !win ) {
  157. return;
  158. }
  159. let loc = win.location;
  160. if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' ) {
  161. if ( loc.protocol === 'chrome:' && loc.host === appName ) {
  162. this.initContentScripts(win);
  163. }
  164. return;
  165. }
  166. let lss = Services.scriptloader.loadSubScript;
  167. win = this.initContentScripts(win, true);
  168. lss(this.contentBaseURI + 'vapi-client.js', win);
  169. lss(this.contentBaseURI + 'contentscript-start.js', win);
  170. let docReady = function(e) {
  171. this.removeEventListener(e.type, docReady, true);
  172. lss(contentObserver.contentBaseURI + 'contentscript-end.js', win);
  173. };
  174. win.document.addEventListener('DOMContentLoaded', docReady, true);
  175. }
  176. };
  177. /******************************************************************************/
  178. contentObserver.register();
  179. /******************************************************************************/