vapi-client.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
  4. Copyright (C) 2019 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. uMatrix Home: https://github.com/gorhill/uMatrix
  16. */
  17. /* jshint esnext: true */
  18. /* global addMessageListener, removeMessageListener, sendAsyncMessage */
  19. // For non background pages
  20. 'use strict';
  21. /******************************************************************************/
  22. (function(self) {
  23. /******************************************************************************/
  24. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  25. if ( self.vAPI === undefined || self.vAPI.eMatrix !== true ) {
  26. self.vAPI = { eMatrix: true };
  27. }
  28. var vAPI = self.vAPI;
  29. vAPI.firefox = true;
  30. vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
  31. Math.random().toString(36).slice(2);
  32. /******************************************************************************/
  33. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
  34. return setTimeout(function() { callback(); }, delay);
  35. };
  36. /******************************************************************************/
  37. vAPI.shutdown = (function() {
  38. var jobs = [];
  39. var add = function(job) {
  40. jobs.push(job);
  41. };
  42. var exec = function() {
  43. //console.debug('Shutting down...');
  44. var job;
  45. while ( (job = jobs.pop()) ) {
  46. job();
  47. }
  48. };
  49. return {
  50. add: add,
  51. exec: exec
  52. };
  53. })();
  54. /******************************************************************************/
  55. vAPI.messaging = {
  56. listeners: new Set(),
  57. pending: new Map(),
  58. requestId: 1,
  59. connected: false,
  60. start: function() {
  61. this.addListener(this.builtinListener);
  62. if ( this.toggleListenerCallback === null ) {
  63. this.toggleListenerCallback = this.toggleListener.bind(this);
  64. }
  65. window.addEventListener('pagehide', this.toggleListenerCallback, true);
  66. window.addEventListener('pageshow', this.toggleListenerCallback, true);
  67. },
  68. shutdown: function() {
  69. if ( this.toggleListenerCallback !== null ) {
  70. window.removeEventListener('pagehide', this.toggleListenerCallback, true);
  71. window.removeEventListener('pageshow', this.toggleListenerCallback, true);
  72. }
  73. this.removeAllListeners();
  74. //service pending callbacks
  75. var pending = this.pending;
  76. this.pending.clear();
  77. for ( var callback of pending.values() ) {
  78. if ( typeof callback === 'function' ) {
  79. callback(null);
  80. }
  81. }
  82. },
  83. connect: function() {
  84. if ( !this.connected ) {
  85. if ( this.messageListenerCallback === null ) {
  86. this.messageListenerCallback = this.messageListener.bind(this);
  87. }
  88. addMessageListener(this.messageListenerCallback);
  89. this.connected = true;
  90. }
  91. },
  92. disconnect: function() {
  93. if ( this.connected ) {
  94. removeMessageListener();
  95. this.connected = false;
  96. }
  97. },
  98. messageListener: function(msg) {
  99. var details = JSON.parse(msg);
  100. if ( !details ) {
  101. return;
  102. }
  103. if ( details.broadcast ) {
  104. this.sendToListeners(details.msg);
  105. return;
  106. }
  107. if ( details.requestId ) {
  108. var listener = this.pending.get(details.requestId);
  109. if ( listener !== undefined ) {
  110. this.pending.delete(details.requestId);
  111. listener(details.msg);
  112. return;
  113. }
  114. }
  115. },
  116. messageListenerCallback: null,
  117. builtinListener: function(msg) {
  118. if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) {
  119. var details = msg.details;
  120. if ( !details.allFrames && window !== window.top ) {
  121. return;
  122. }
  123. self.injectScript(details.file);
  124. }
  125. },
  126. send: function(channelName, message, callback) {
  127. this.connect()
  128. message = {
  129. channelName: self._sandboxId_ + '|' + channelName,
  130. msg: message
  131. };
  132. if ( callback ) {
  133. message.requestId = this.requestId++;
  134. this.pending.set(message.requestId, callback);
  135. }
  136. sendAsyncMessage('ematrix:background', message);
  137. },
  138. toggleListener: function({type, persisted}) {
  139. if ( type === 'pagehide' && !persisted ) {
  140. vAPI.shutdown.exec();
  141. this.shutdown();
  142. return;
  143. }
  144. if ( type === 'pagehide' ) {
  145. this.disconnect();
  146. } else /* if ( type === 'pageshow' ) */ {
  147. this.connect();
  148. }
  149. },
  150. toggleListenerCallback: null,
  151. sendToListeners: function(msg) {
  152. for ( var listener of this.listeners ) {
  153. listener(msg);
  154. }
  155. },
  156. addListener: function(listener) {
  157. this.listeners.add(listener);
  158. this.connect()
  159. },
  160. removeListener: function(listener) {
  161. this.listeners.delete(listener);
  162. },
  163. removeAllListeners: function() {
  164. this.disconnect();
  165. this.listeners.clear();;
  166. }
  167. };
  168. vAPI.messaging.start()
  169. /******************************************************************************/
  170. // No need to have vAPI client linger around after shutdown if
  171. // we are not a top window (because element picker can still
  172. // be injected in top window).
  173. // Needs more investigating
  174. /*if ( window !== window.top ) {
  175. vAPI.shutdown.add(function() {
  176. vAPI = null;
  177. });
  178. }*/
  179. /******************************************************************************/
  180. })(this);
  181. /******************************************************************************/