vapi-cloud.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.cloud = (function () {
  22. let extensionBranchPath = 'extensions.' + location.host;
  23. let cloudBranchPath = extensionBranchPath + '.cloudStorage';
  24. // https://github.com/gorhill/uBlock/issues/80#issuecomment-132081658
  25. // We must use get/setComplexValue in order to properly handle strings
  26. // with unicode characters.
  27. let iss = Ci.nsISupportsString;
  28. let argstr = Components.classes['@mozilla.org/supports-string;1']
  29. .createInstance(iss);
  30. let options = {
  31. defaultDeviceName: '',
  32. deviceName: ''
  33. };
  34. // User-supplied device name.
  35. try {
  36. options.deviceName = Services.prefs
  37. .getBranch(extensionBranchPath + '.')
  38. .getComplexValue('deviceName', iss)
  39. .data;
  40. } catch(ex) {
  41. // Ignore
  42. }
  43. var getDefaultDeviceName = function() {
  44. var name = '';
  45. try {
  46. name = Services.prefs
  47. .getBranch('services.sync.client.')
  48. .getComplexValue('name', iss)
  49. .data;
  50. } catch(ex) {
  51. // Ignore
  52. }
  53. return name || window.navigator.platform || window.navigator.oscpu;
  54. };
  55. let start = function (dataKeys) {
  56. let extensionBranch =
  57. Services.prefs.getBranch(extensionBranchPath + '.');
  58. let syncBranch =
  59. Services.prefs.getBranch('services.sync.prefs.sync.');
  60. // Mark config entries as syncable
  61. argstr.data = '';
  62. let dataKey;
  63. for (let i=0; i<dataKeys.length; ++i) {
  64. dataKey = dataKeys[i];
  65. if (extensionBranch.prefHasUserValue('cloudStorage.' + dataKey)
  66. === false) {
  67. extensionBranch.setComplexValue('cloudStorage.' + dataKey,
  68. iss, argstr);
  69. }
  70. syncBranch.setBoolPref(cloudBranchPath + '.' + dataKey, true);
  71. }
  72. };
  73. let push = function (datakey, data, callback) {
  74. let branch = Services.prefs.getBranch(cloudBranchPath + '.');
  75. let bin = {
  76. 'source': options.deviceName || getDefaultDeviceName(),
  77. 'tstamp': Date.now(),
  78. 'data': data,
  79. 'size': 0
  80. };
  81. bin.size = JSON.stringify(bin).length;
  82. argstr.data = JSON.stringify(bin);
  83. branch.setComplexValue(datakey, iss, argstr);
  84. if (typeof callback === 'function') {
  85. callback();
  86. }
  87. };
  88. let pull = function (datakey, callback) {
  89. let result = null;
  90. let branch = Services.prefs.getBranch(cloudBranchPath + '.');
  91. try {
  92. let json = branch.getComplexValue(datakey, iss).data;
  93. if (typeof json === 'string') {
  94. result = JSON.parse(json);
  95. }
  96. } catch(ex) {
  97. // Ignore
  98. }
  99. callback(result);
  100. };
  101. let getOptions = function (callback) {
  102. if (typeof callback !== 'function') {
  103. return;
  104. }
  105. options.defaultDeviceName = getDefaultDeviceName();
  106. callback(options);
  107. };
  108. let setOptions = function (details, callback) {
  109. if (typeof details !== 'object' || details === null) {
  110. return;
  111. }
  112. let branch = Services.prefs.getBranch(extensionBranchPath + '.');
  113. if (typeof details.deviceName === 'string') {
  114. argstr.data = details.deviceName;
  115. branch.setComplexValue('deviceName', iss, argstr);
  116. options.deviceName = details.deviceName;
  117. }
  118. getOptions(callback);
  119. };
  120. return {
  121. start: start,
  122. push: push,
  123. pull: pull,
  124. getOptions: getOptions,
  125. setOptions: setOptions
  126. };
  127. })();
  128. })();