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.

190 lines
5.7 KiB

  1. /* global Services, Components, XPCOMUtils */
  2. 'use strict';
  3. this.EXPORTED_SYMBOLS = ['contentPolicy', 'docObserver'];
  4. Components.utils['import']('resource://gre/modules/Services.jsm');
  5. Components.utils['import']('resource://gre/modules/XPCOMUtils.jsm');
  6. // Components.utils['import']('resource://gre/modules/devtools/Console.jsm');
  7. const Ci = Components.interfaces;
  8. let appName;
  9. try { throw new Error; } catch (ex) {
  10. appName = ex.fileName.match(/:\/\/([^\/]+)/)[1];
  11. }
  12. let getMessager = function(win) {
  13. try {
  14. // e10s
  15. return win
  16. .QueryInterface(Ci.nsIInterfaceRequestor)
  17. .getInterface(Ci.nsIWebNavigation)
  18. .QueryInterface(Ci.nsIDocShellTreeItem)
  19. .rootTreeItem
  20. .QueryInterface(Ci.nsIInterfaceRequestor)
  21. .getInterface(Ci.nsIContentFrameMessageManager);
  22. } catch (ex) {
  23. return win
  24. .QueryInterface(Ci.nsIInterfaceRequestor)
  25. .getInterface(Ci.nsIWebNavigation)
  26. .QueryInterface(Ci.nsIDocShell)
  27. .QueryInterface(Ci.nsIInterfaceRequestor)
  28. .getInterface(Ci.nsIContentFrameMessageManager);
  29. }
  30. };
  31. let contentPolicy = {
  32. classDescription: 'content-policy implementation for ' + appName,
  33. classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
  34. contractID: '@' + appName + '/content-policy;1',
  35. ACCEPT: Ci.nsIContentPolicy.ACCEPT,
  36. REJECT: Ci.nsIContentPolicy.REJECT_REQUEST,
  37. requestMessageName: appName + ':onBeforeRequest',
  38. get componentRegistrar() {
  39. return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  40. },
  41. get categoryManager() {
  42. return Components.classes['@mozilla.org/categorymanager;1']
  43. .getService(Ci.nsICategoryManager);
  44. },
  45. QueryInterface: XPCOMUtils.generateQI([
  46. Ci.nsIFactory,
  47. Ci.nsIContentPolicy,
  48. Ci.nsISupportsWeakReference
  49. ]),
  50. createInstance: function(outer, iid) {
  51. if (outer) {
  52. throw Components.results.NS_ERROR_NO_AGGREGATION;
  53. }
  54. return this.QueryInterface(iid);
  55. },
  56. register: function() {
  57. this.componentRegistrar.registerFactory(
  58. this.classID,
  59. this.classDescription,
  60. this.contractID,
  61. this
  62. );
  63. this.categoryManager.addCategoryEntry(
  64. 'content-policy',
  65. this.contractID,
  66. this.contractID,
  67. false,
  68. true
  69. );
  70. },
  71. unregister: function() {
  72. this.componentRegistrar.unregisterFactory(this.classID, this);
  73. this.categoryManager.deleteCategoryEntry(
  74. 'content-policy',
  75. this.contractID,
  76. false
  77. );
  78. },
  79. shouldLoad: function(type, location, origin, context) {
  80. if (!context || !/^https?$/.test(location.scheme)) {
  81. return this.ACCEPT;
  82. }
  83. let win = type === 6
  84. ? context.contentWindow || context
  85. : (context.ownerDocument || context).defaultView;
  86. if (!win) {
  87. return this.ACCEPT;
  88. }
  89. let result = getMessager(win).sendSyncMessage(this.requestMessageName, {
  90. url: location.spec,
  91. type: type,
  92. tabId: -1,
  93. frameId: type === 6 ? -1 : (win === win.top ? 0 : 1),
  94. parentFrameId: win === win.top ? -1 : 0
  95. })[0];
  96. return result === true ? this.REJECT : this.ACCEPT;
  97. }/*,
  98. shouldProcess: function() {
  99. return this.ACCEPT;
  100. }*/
  101. };
  102. let docObserver = {
  103. contentBaseURI: 'chrome://' + appName + '/content/',
  104. initContext: function(win, sandbox) {
  105. let messager = getMessager(win);
  106. if (sandbox) {
  107. win = Components.utils.Sandbox([win], {
  108. sandboxPrototype: win,
  109. wantComponents: false,
  110. wantXHRConstructor: false
  111. });
  112. win.self = win;
  113. // anonymous function needs to be used here
  114. win.injectScript = Components.utils.exportFunction(
  115. function(script, evalCode) {
  116. if (evalCode) {
  117. Components.utils.evalInSandbox(script, win);
  118. return;
  119. }
  120. Services.scriptloader.loadSubScript(
  121. docObserver.contentBaseURI + script,
  122. win
  123. );
  124. },
  125. win
  126. );
  127. }
  128. win.sendAsyncMessage = messager.sendAsyncMessage;
  129. win.addMessageListener = messager.ublock_addMessageListener;
  130. win.removeMessageListener = messager.ublock_removeMessageListener;
  131. return win;
  132. },
  133. register: function() {
  134. Services.obs.addObserver(this, 'document-element-inserted', false);
  135. },
  136. unregister: function() {
  137. Services.obs.removeObserver(this, 'document-element-inserted');
  138. },
  139. observe: function(doc) {
  140. let win = doc.defaultView;
  141. if (!win) {
  142. return;
  143. }
  144. if (!/^https?:$/.test(win.location.protocol)) {
  145. if (win.location.protocol === 'chrome:'
  146. && win.location.host === appName) {
  147. this.initContext(win);
  148. }
  149. return;
  150. }
  151. let lss = Services.scriptloader.loadSubScript;
  152. win = this.initContext(win, true);
  153. lss(this.contentBaseURI + 'js/vapi-client.js', win);
  154. lss(this.contentBaseURI + 'js/contentscript-start.js', win);
  155. let docReady = function(e) {
  156. this.removeEventListener(e.type, docReady, true);
  157. lss(docObserver.contentBaseURI + 'js/contentscript-end.js', win);
  158. };
  159. doc.addEventListener('DOMContentLoaded', docReady, true);
  160. }
  161. };
  162. contentPolicy.register();
  163. docObserver.register();