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.

254 lines
7.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2017 The uMatrix/uBlock Origin 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/uMatrix
  15. */
  16. // For non background pages
  17. 'use strict';
  18. /******************************************************************************/
  19. (function(self) {
  20. /******************************************************************************/
  21. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  22. if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
  23. self.vAPI = { uMatrix: true };
  24. }
  25. var vAPI = self.vAPI;
  26. var chrome = self.chrome;
  27. // https://github.com/chrisaljoudi/uBlock/issues/456
  28. // Already injected?
  29. if ( vAPI.vapiClientInjected ) {
  30. //console.debug('vapi-client.js already injected: skipping.');
  31. return;
  32. }
  33. vAPI.vapiClientInjected = true;
  34. vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
  35. Math.random().toString(36).slice(2);
  36. vAPI.chrome = true;
  37. /******************************************************************************/
  38. vAPI.shutdown = (function() {
  39. var jobs = [];
  40. var add = function(job) {
  41. jobs.push(job);
  42. };
  43. var exec = function() {
  44. //console.debug('Shutting down...');
  45. var job;
  46. while ( (job = jobs.pop()) ) {
  47. job();
  48. }
  49. };
  50. return {
  51. add: add,
  52. exec: exec
  53. };
  54. })();
  55. /******************************************************************************/
  56. vAPI.messaging = {
  57. port: null,
  58. portTimer: null,
  59. portTimerDelay: 10000,
  60. listeners: new Set(),
  61. pending: new Map(),
  62. auxProcessId: 1,
  63. shuttingDown: false,
  64. shutdown: function() {
  65. this.shuttingDown = true;
  66. this.destroyPort();
  67. },
  68. disconnectListener: function() {
  69. this.port = null;
  70. vAPI.shutdown.exec();
  71. },
  72. disconnectListenerBound: null,
  73. messageListener: function(details) {
  74. if ( !details ) { return; }
  75. // Sent to all listeners
  76. if ( details.broadcast ) {
  77. this.sendToListeners(details.msg);
  78. return;
  79. }
  80. // Response to specific message previously sent
  81. var listener;
  82. if ( details.auxProcessId ) {
  83. listener = this.pending.get(details.auxProcessId);
  84. if ( listener !== undefined ) {
  85. this.pending.delete(details.auxProcessId);
  86. listener(details.msg);
  87. return;
  88. }
  89. }
  90. },
  91. messageListenerCallback: null,
  92. portPoller: function() {
  93. this.portTimer = null;
  94. if (
  95. this.port !== null &&
  96. this.listeners.size === 0 &&
  97. this.pending.size === 0
  98. ) {
  99. return this.destroyPort();
  100. }
  101. this.portTimer = vAPI.setTimeout(this.portPollerBound, this.portTimerDelay);
  102. this.portTimerDelay = Math.min(this.portTimerDelay * 2, 60 * 60 * 1000);
  103. },
  104. portPollerBound: null,
  105. destroyPort: function() {
  106. if ( this.portTimer !== null ) {
  107. clearTimeout(this.portTimer);
  108. this.portTimer = null;
  109. }
  110. var port = this.port;
  111. if ( port !== null ) {
  112. port.disconnect();
  113. port.onMessage.removeListener(this.messageListenerCallback);
  114. port.onDisconnect.removeListener(this.disconnectListenerBound);
  115. this.port = null;
  116. }
  117. this.listeners.clear();
  118. // service pending callbacks
  119. if ( this.pending.size !== 0 ) {
  120. var pending = this.pending;
  121. this.pending = new Map();
  122. for ( var callback of pending.values() ) {
  123. if ( typeof callback === 'function' ) {
  124. callback(null);
  125. }
  126. }
  127. }
  128. },
  129. createPort: function() {
  130. if ( this.shuttingDown ) { return null; }
  131. if ( this.messageListenerCallback === null ) {
  132. this.messageListenerCallback = this.messageListener.bind(this);
  133. this.disconnectListenerBound = this.disconnectListener.bind(this);
  134. this.portPollerBound = this.portPoller.bind(this);
  135. }
  136. try {
  137. this.port = chrome.runtime.connect({name: vAPI.sessionId}) || null;
  138. } catch (ex) {
  139. this.port = null;
  140. }
  141. if ( this.port !== null ) {
  142. this.port.onMessage.addListener(this.messageListenerCallback);
  143. this.port.onDisconnect.addListener(this.disconnectListenerBound);
  144. this.portTimerDelay = 10000;
  145. if ( this.portTimer === null ) {
  146. this.portTimer = vAPI.setTimeout(
  147. this.portPollerBound,
  148. this.portTimerDelay
  149. );
  150. }
  151. }
  152. return this.port;
  153. },
  154. getPort: function() {
  155. return this.port !== null ? this.port : this.createPort();
  156. },
  157. send: function(channelName, message, callback) {
  158. // Too large a gap between the last request and the last response means
  159. // the main process is no longer reachable: memory leaks and bad
  160. // performance become a risk -- especially for long-lived, dynamic
  161. // pages. Guard against this.
  162. if ( this.pending.size > 25 ) {
  163. vAPI.shutdown.exec();
  164. }
  165. var port = this.getPort();
  166. if ( port === null ) {
  167. if ( typeof callback === 'function' ) { callback(); }
  168. return;
  169. }
  170. var auxProcessId;
  171. if ( callback ) {
  172. auxProcessId = this.auxProcessId++;
  173. this.pending.set(auxProcessId, callback);
  174. }
  175. port.postMessage({
  176. channelName: channelName,
  177. auxProcessId: auxProcessId,
  178. msg: message
  179. });
  180. },
  181. addListener: function(listener) {
  182. this.listeners.add(listener);
  183. this.getPort();
  184. },
  185. removeListener: function(listener) {
  186. this.listeners.delete(listener);
  187. },
  188. removeAllListeners: function() {
  189. this.listeners.clear();
  190. },
  191. sendToListeners: function(msg) {
  192. for ( var listener of this.listeners ) {
  193. listener(msg);
  194. }
  195. }
  196. };
  197. /******************************************************************************/
  198. // No need to have vAPI client linger around after shutdown if
  199. // we are not a top window (because element picker can still
  200. // be injected in top window).
  201. if ( window !== window.top ) {
  202. vAPI.shutdown.add(function() {
  203. vAPI = null;
  204. });
  205. }
  206. /******************************************************************************/
  207. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
  208. setTimeout(function() { callback(); }, delay);
  209. };
  210. /******************************************************************************/
  211. })(this); // jshint ignore: line
  212. /******************************************************************************/