PendingRequests.jsm 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-2020-2021 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. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. 'use strict';
  19. var EXPORTED_SYMBOLS = ['PendingRequestBuffer'];
  20. function PendingRequest() {
  21. this.rawType = 0;
  22. this.tabId = 0;
  23. this._key = '';
  24. }
  25. var bufferLength = 1024
  26. var urlToIndex = new Map();
  27. var writePointer = 0;
  28. var ringBuffer = new Array(bufferLength);
  29. for (let i=0; i<bufferLength; ++i) {
  30. ringBuffer[i] = new PendingRequest();
  31. }
  32. var PendingRequestBuffer = {
  33. createRequest: function (url) {
  34. // URL to ring buffer index map:
  35. // { k = URL, s = ring buffer indices }
  36. //
  37. // s is a string which character codes map to ring buffer
  38. // indices -- for when the same URL is received multiple times
  39. // by shouldLoadListener() before the existing one is serviced
  40. // by the network request observer. I believe the use of a
  41. // string in lieu of an array reduces memory churning.
  42. let bucket;
  43. let i = writePointer;
  44. writePointer = (i + 1) % bufferLength;
  45. let req = ringBuffer[i];
  46. let str = String.fromCharCode(i);
  47. if (req._key !== '') {
  48. bucket = urlToIndex.get(req._key);
  49. if (bucket.lenght === 1) {
  50. urlToIndex.delete(req._key);
  51. } else {
  52. let pos = bucket.indexOf(str);
  53. urlToIndex.set(req._key,
  54. bucket.slice(0, pos)+bucket.slice(pos+1));
  55. }
  56. }
  57. bucket = urlToIndex.get(url);
  58. urlToIndex.set(url,
  59. (bucket === undefined) ? str : bucket + str);
  60. req._key = url;
  61. return req;
  62. },
  63. lookupRequest: function (url) {
  64. let bucket = urlToIndex.get(url);
  65. if (bucket === undefined) {
  66. return null;
  67. }
  68. let i = bucket.charCodeAt(0);
  69. if (bucket.length === 1) {
  70. urlToIndex.delete(url);
  71. } else {
  72. urlToIndex.set(url, bucket.slice(1));
  73. }
  74. let req = ringBuffer[i];
  75. req._key = '';
  76. return req;
  77. },
  78. };