view_history.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright 2012 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. const DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  16. /**
  17. * View History - This is a utility for saving various view parameters for
  18. * recently opened files.
  19. *
  20. * The way that the view parameters are stored depends on how PDF.js is built,
  21. * for 'gulp <flag>' the following cases exist:
  22. * - MOZCENTRAL - uses sessionStorage.
  23. * - GENERIC or CHROME - uses localStorage, if it is available.
  24. */
  25. class ViewHistory {
  26. constructor(fingerprint, cacheSize = DEFAULT_VIEW_HISTORY_CACHE_SIZE) {
  27. this.fingerprint = fingerprint;
  28. this.cacheSize = cacheSize;
  29. this._initializedPromise = this._readFromStorage().then(databaseStr => {
  30. const database = JSON.parse(databaseStr || "{}");
  31. let index = -1;
  32. if (!Array.isArray(database.files)) {
  33. database.files = [];
  34. } else {
  35. while (database.files.length >= this.cacheSize) {
  36. database.files.shift();
  37. }
  38. for (let i = 0, ii = database.files.length; i < ii; i++) {
  39. const branch = database.files[i];
  40. if (branch.fingerprint === this.fingerprint) {
  41. index = i;
  42. break;
  43. }
  44. }
  45. }
  46. if (index === -1) {
  47. index = database.files.push({ fingerprint: this.fingerprint }) - 1;
  48. }
  49. this.file = database.files[index];
  50. this.database = database;
  51. });
  52. }
  53. async _writeToStorage() {
  54. const databaseStr = JSON.stringify(this.database);
  55. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  56. sessionStorage.setItem("pdfjs.history", databaseStr);
  57. return;
  58. }
  59. localStorage.setItem("pdfjs.history", databaseStr);
  60. }
  61. async _readFromStorage() {
  62. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  63. return sessionStorage.getItem("pdfjs.history");
  64. }
  65. return localStorage.getItem("pdfjs.history");
  66. }
  67. async set(name, val) {
  68. await this._initializedPromise;
  69. this.file[name] = val;
  70. return this._writeToStorage();
  71. }
  72. async setMultiple(properties) {
  73. await this._initializedPromise;
  74. for (const name in properties) {
  75. this.file[name] = properties[name];
  76. }
  77. return this._writeToStorage();
  78. }
  79. async get(name, defaultValue) {
  80. await this._initializedPromise;
  81. const val = this.file[name];
  82. return val !== undefined ? val : defaultValue;
  83. }
  84. async getMultiple(properties) {
  85. await this._initializedPromise;
  86. const values = Object.create(null);
  87. for (const name in properties) {
  88. const val = this.file[name];
  89. values[name] = val !== undefined ? val : properties[name];
  90. }
  91. return values;
  92. }
  93. }
  94. export { ViewHistory };