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.

174 lines
5.1 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: 'ContentPolicy implementation',
  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
  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/js/',
  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. }
  114. win.sendAsyncMessage = messager.sendAsyncMessage;
  115. win.addMessageListener = messager.ublock_addMessageListener;
  116. win.removeMessageListener = messager.ublock_removeMessageListener;
  117. return win;
  118. },
  119. register: function() {
  120. Services.obs.addObserver(this, 'document-element-inserted', false);
  121. },
  122. unregister: function() {
  123. Services.obs.removeObserver(this, 'document-element-inserted');
  124. },
  125. observe: function(doc) {
  126. let win = doc.defaultView;
  127. if (!win) {
  128. return;
  129. }
  130. if (!/^https?:$/.test(win.location.protocol)) {
  131. if (win.location.protocol === 'chrome:'
  132. && win.location.host === appName) {
  133. this.initContext(win);
  134. }
  135. return;
  136. }
  137. let lss = Services.scriptloader.loadSubScript;
  138. win = this.initContext(win, true);
  139. lss(this.contentBaseURI + 'vapi-client.js', win);
  140. lss(this.contentBaseURI + 'contentscript-start.js', win);
  141. let docReady = function(e) {
  142. this.removeEventListener(e.type, docReady, true);
  143. lss(docObserver.contentBaseURI + 'contentscript-end.js', win);
  144. };
  145. doc.addEventListener('DOMContentLoaded', docReady, true);
  146. }
  147. };
  148. contentPolicy.register();
  149. docObserver.register();