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.

105 lines
3.0 KiB

  1. /*******************************************************************************
  2. µMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014 Raymond Hill
  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. /* global µMatrix */
  17. /******************************************************************************/
  18. // Automatic update of non-user assets
  19. // https://github.com/gorhill/httpswitchboard/issues/334
  20. µMatrix.updater = (function() {
  21. /******************************************************************************/
  22. var µm = µMatrix;
  23. var jobCallback = function() {
  24. // Simpler to fire restart here, and safe given how far this will happen
  25. // in the future.
  26. restart();
  27. // If auto-update is disabled, check again in a while.
  28. if ( µm.userSettings.autoUpdate !== true ) {
  29. return;
  30. }
  31. var onMetadataReady = function(metadata) {
  32. // Check PSL
  33. var mdEntry = metadata[µm.pslPath];
  34. if ( mdEntry.repoObsolete ) {
  35. µm.loadUpdatableAssets(true);
  36. return;
  37. }
  38. // Check used hosts files
  39. var hostsFiles = µm.liveHostsFiles;
  40. for ( var path in hostsFiles ) {
  41. if ( hostsFiles.hasOwnProperty(path) === false ) {
  42. continue;
  43. }
  44. if ( hostsFiles[path].off ) {
  45. continue;
  46. }
  47. if ( metadata.hasOwnProperty(path) === false ) {
  48. continue;
  49. }
  50. mdEntry = metadata[path];
  51. if ( mdEntry.cacheObsolete || mdEntry.repoObsolete ) {
  52. µm.loadUpdatableAssets(true);
  53. return;
  54. }
  55. }
  56. // console.log('updater.js > all is up to date');
  57. };
  58. µm.assets.metadata(onMetadataReady);
  59. };
  60. // https://www.youtube.com/watch?v=cIrGQD84F1g
  61. /******************************************************************************/
  62. var restart = function(after) {
  63. if ( after === undefined ) {
  64. after = µm.nextUpdateAfter;
  65. }
  66. µm.asyncJobs.add(
  67. 'autoUpdateAssets',
  68. null,
  69. jobCallback,
  70. after,
  71. false
  72. );
  73. };
  74. /******************************************************************************/
  75. return {
  76. restart: restart
  77. };
  78. /******************************************************************************/
  79. })();
  80. /******************************************************************************/