view_history.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  17. var ViewHistory = function ViewHistoryClosure() {
  18. function ViewHistory(fingerprint, cacheSize) {
  19. this.fingerprint = fingerprint;
  20. this.cacheSize = cacheSize || DEFAULT_VIEW_HISTORY_CACHE_SIZE;
  21. this.isInitializedPromiseResolved = false;
  22. this.initializedPromise = this._readFromStorage().then(function (databaseStr) {
  23. this.isInitializedPromiseResolved = true;
  24. var database = JSON.parse(databaseStr || '{}');
  25. if (!('files' in database)) {
  26. database.files = [];
  27. }
  28. if (database.files.length >= this.cacheSize) {
  29. database.files.shift();
  30. }
  31. var index;
  32. for (var i = 0, length = database.files.length; i < length; i++) {
  33. var branch = database.files[i];
  34. if (branch.fingerprint === this.fingerprint) {
  35. index = i;
  36. break;
  37. }
  38. }
  39. if (typeof index !== 'number') {
  40. index = database.files.push({ fingerprint: this.fingerprint }) - 1;
  41. }
  42. this.file = database.files[index];
  43. this.database = database;
  44. }.bind(this));
  45. }
  46. ViewHistory.prototype = {
  47. _writeToStorage: function ViewHistory_writeToStorage() {
  48. return new Promise(function (resolve) {
  49. var databaseStr = JSON.stringify(this.database);
  50. localStorage.setItem('pdfjs.history', databaseStr);
  51. resolve();
  52. }.bind(this));
  53. },
  54. _readFromStorage: function ViewHistory_readFromStorage() {
  55. return new Promise(function (resolve) {
  56. var value = localStorage.getItem('pdfjs.history');
  57. if (!value) {
  58. var databaseStr = localStorage.getItem('database');
  59. if (databaseStr) {
  60. try {
  61. var database = JSON.parse(databaseStr);
  62. if (typeof database.files[0].fingerprint === 'string') {
  63. localStorage.setItem('pdfjs.history', databaseStr);
  64. localStorage.removeItem('database');
  65. value = databaseStr;
  66. }
  67. } catch (ex) {
  68. }
  69. }
  70. }
  71. resolve(value);
  72. });
  73. },
  74. set: function ViewHistory_set(name, val) {
  75. if (!this.isInitializedPromiseResolved) {
  76. return;
  77. }
  78. this.file[name] = val;
  79. return this._writeToStorage();
  80. },
  81. setMultiple: function ViewHistory_setMultiple(properties) {
  82. if (!this.isInitializedPromiseResolved) {
  83. return;
  84. }
  85. for (var name in properties) {
  86. this.file[name] = properties[name];
  87. }
  88. return this._writeToStorage();
  89. },
  90. get: function ViewHistory_get(name, defaultValue) {
  91. if (!this.isInitializedPromiseResolved) {
  92. return defaultValue;
  93. }
  94. return this.file[name] || defaultValue;
  95. }
  96. };
  97. return ViewHistory;
  98. }();
  99. exports.ViewHistory = ViewHistory;