vapi-cookies.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-2022 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. /******************************************************************************/
  20. (function () {
  21. vAPI.cookies = {};
  22. vAPI.cookies.CookieEntry = function (ffCookie) {
  23. this.domain = ffCookie.host;
  24. this.name = ffCookie.name;
  25. this.path = ffCookie.path;
  26. this.secure = ffCookie.isSecure === true;
  27. this.session = ffCookie.expires === 0;
  28. this.value = ffCookie.value;
  29. };
  30. vAPI.cookies.start = function () {
  31. Services.obs.addObserver(this, 'cookie-changed', false);
  32. Services.obs.addObserver(this, 'private-cookie-changed', false);
  33. vAPI.addCleanUpTask(this.stop.bind(this));
  34. };
  35. vAPI.cookies.stop = function () {
  36. Services.obs.removeObserver(this, 'cookie-changed');
  37. Services.obs.removeObserver(this, 'private-cookie-changed');
  38. };
  39. vAPI.cookies.observe = function (subject, topic, reason) {
  40. //if ( topic !== 'cookie-changed' && topic !== 'private-cookie-changed' ) {
  41. // return;
  42. //}
  43. //
  44. if (reason === 'cleared' && typeof this.onAllRemoved === 'function') {
  45. this.onAllRemoved();
  46. return;
  47. }
  48. if (subject === null) {
  49. return;
  50. }
  51. if (subject instanceof Ci.nsICookie2 === false) {
  52. try {
  53. subject = subject.QueryInterface(Ci.nsICookie2);
  54. } catch (ex) {
  55. return;
  56. }
  57. }
  58. if (reason === 'deleted') {
  59. if (typeof this.onRemoved === 'function') {
  60. this.onRemoved(new this.CookieEntry(subject));
  61. }
  62. return;
  63. }
  64. if (typeof this.onChanged === 'function') {
  65. this.onChanged(new this.CookieEntry(subject));
  66. }
  67. };
  68. vAPI.cookies.getAll = function(callback) {
  69. // Meant and expected to be asynchronous.
  70. if (typeof callback !== 'function') {
  71. return;
  72. }
  73. let onAsync = function () {
  74. let out = [];
  75. let enumerator = Services.cookies.enumerator;
  76. let ffcookie;
  77. while (enumerator.hasMoreElements()) {
  78. ffcookie = enumerator.getNext();
  79. if (ffcookie instanceof Ci.nsICookie) {
  80. out.push(new this.CookieEntry(ffcookie));
  81. }
  82. }
  83. callback(out);
  84. };
  85. vAPI.setTimeout(onAsync.bind(this), 0);
  86. };
  87. vAPI.cookies.remove = function (details, callback) {
  88. let uri = Services.io.newURI(details.url, null, null);
  89. let cookies = Services.cookies;
  90. cookies.remove(uri.asciiHost, details.name, uri.path, false, {});
  91. cookies.remove( '.' + uri.asciiHost, details.name, uri.path, false, {});
  92. if (typeof callback === 'function') {
  93. callback({
  94. domain: uri.asciiHost,
  95. name: details.name,
  96. path: uri.path
  97. });
  98. }
  99. };
  100. })();