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.

203 lines
6.4 KiB

  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 = ['contentPolicy', 'docObserver'];
  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 contentPolicy = {
  37. classDescription: 'content-policy implementation for ' + appName,
  38. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  39. contractID: '@' + appName + '/content-policy;1',
  40. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  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: XPCOMUtils.generateQI([
  50. Ci.nsIFactory,
  51. Ci.nsIContentPolicy,
  52. Ci.nsISupportsWeakReference
  53. ]),
  54. createInstance: function(outer, iid) {
  55. if (outer) {
  56. throw Components.results.NS_ERROR_NO_AGGREGATION;
  57. }
  58. return this.QueryInterface(iid);
  59. },
  60. register: function() {
  61. this.componentRegistrar.registerFactory(
  62. this.classID,
  63. this.classDescription,
  64. this.contractID,
  65. this
  66. );
  67. this.categoryManager.addCategoryEntry(
  68. 'content-policy',
  69. this.contractID,
  70. this.contractID,
  71. false,
  72. true
  73. );
  74. },
  75. unregister: function() {
  76. this.componentRegistrar.unregisterFactory(this.classID, this);
  77. this.categoryManager.deleteCategoryEntry(
  78. 'content-policy',
  79. this.contractID,
  80. false
  81. );
  82. },
  83. // https://bugzil.la/612921
  84. shouldLoad: function(type, location, origin, context) {
  85. if (!context || !/^https?$/.test(location.scheme)) {
  86. return this.ACCEPT;
  87. }
  88. let win = type === 6
  89. ? context.contentWindow || context
  90. : (context.ownerDocument || context).defaultView;
  91. if (win) {
  92. getMessageManager(win).sendSyncMessage(this.messageName, {
  93. url: location.spec,
  94. type: type,
  95. frameId: type === 6 ? -1 : (win === win.top ? 0 : 1),
  96. parentFrameId: win === win.top ? -1 : 0
  97. });
  98. }
  99. return this.ACCEPT;
  100. }
  101. };
  102. /******************************************************************************/
  103. const docObserver = {
  104. contentBaseURI: 'chrome://' + appName + '/content/',
  105. initContext: function(win, sandbox) {
  106. let messager = getMessageManager(win);
  107. if (sandbox) {
  108. win = Cu.Sandbox([win], {
  109. sandboxPrototype: win,
  110. wantComponents: false,
  111. wantXHRConstructor: false
  112. });
  113. win.self = win;
  114. // anonymous function needs to be used here
  115. win.injectScript = Cu.exportFunction(
  116. function(script, evalCode) {
  117. if (evalCode) {
  118. Cu.evalInSandbox(script, win);
  119. return;
  120. }
  121. Services.scriptloader.loadSubScript(
  122. docObserver.contentBaseURI + script,
  123. win
  124. );
  125. },
  126. win
  127. );
  128. }
  129. win.sendAsyncMessage = messager.sendAsyncMessage;
  130. win.addMessageListener = messager.ublock_addMessageListener;
  131. win.removeMessageListener = messager.ublock_removeMessageListener;
  132. return win;
  133. },
  134. register: function() {
  135. Services.obs.addObserver(this, 'document-element-inserted', false);
  136. },
  137. unregister: function() {
  138. Services.obs.removeObserver(this, 'document-element-inserted');
  139. },
  140. observe: function(doc) {
  141. let win = doc.defaultView;
  142. if (!win) {
  143. return;
  144. }
  145. if (!/^https?:$/.test(win.location.protocol)) {
  146. if (win.location.protocol === 'chrome:'
  147. && win.location.host === appName) {
  148. this.initContext(win);
  149. }
  150. return;
  151. }
  152. let lss = Services.scriptloader.loadSubScript;
  153. win = this.initContext(win, true);
  154. lss(this.contentBaseURI + 'js/vapi-client.js', win);
  155. lss(this.contentBaseURI + 'js/contentscript-start.js', win);
  156. let docReady = function(e) {
  157. this.removeEventListener(e.type, docReady, true);
  158. lss(docObserver.contentBaseURI + 'js/contentscript-end.js', win);
  159. };
  160. doc.addEventListener('DOMContentLoaded', docReady, true);
  161. }
  162. };
  163. /******************************************************************************/
  164. contentPolicy.register();
  165. docObserver.register();
  166. /******************************************************************************/