Browse Source

Refactoring Settings manager, now with limit on memory usage

Saebekassebil 14 years ago
parent
commit
e147485262
  1. 10
      src/core.js
  2. 105
      web/viewer.js

10
src/core.js

@ -527,8 +527,7 @@ var PDFDocModel = (function PDFDocModelClosure() {
this.startXRef, this.startXRef,
this.mainXRefEntriesOffset); this.mainXRefEntriesOffset);
this.catalog = new Catalog(this.xref); this.catalog = new Catalog(this.xref);
if (this.xref.trailer && this.xref.trailer.has('ID')) {
if(this.xref.trailer && this.xref.trailer.has('ID')) {
var fileID = ''; var fileID = '';
this.xref.trailer.get('ID')[0].split('').forEach(function(el) { this.xref.trailer.get('ID')[0].split('').forEach(function(el) {
fileID += Number(el.charCodeAt(0)).toString(16); fileID += Number(el.charCodeAt(0)).toString(16);
@ -543,14 +542,15 @@ var PDFDocModel = (function PDFDocModelClosure() {
return shadow(this, 'numPages', num); return shadow(this, 'numPages', num);
}, },
getFingerprint: function pdfDocGetFingerprint() { getFingerprint: function pdfDocGetFingerprint() {
if(this.fileID) { if (this.fileID) {
return this.fileID; return this.fileID;
} else { } else {
// If we got no fileID, then we generate one, from the first 100 bytes of PDF // If we got no fileID, then we generate one,
// from the first 100 bytes of PDF
var data = this.stream.bytes.subarray(0, 100); var data = this.stream.bytes.subarray(0, 100);
var hash = calculateMD5(data, 0, data.length); var hash = calculateMD5(data, 0, data.length);
var strHash = ''; var strHash = '';
for(var i = 0, length = hash.length; i < length; i++) { for (var i = 0, length = hash.length; i < length; i++) {
strHash += Number(hash[i]).toString(16); strHash += Number(hash[i]).toString(16);
} }

105
web/viewer.js

@ -28,12 +28,7 @@ var Cache = function cacheCache(size) {
// Settings Manager - This is a utility for saving settings // Settings Manager - This is a utility for saving settings
// First we see if localStorage is available, FF bug #495747 // First we see if localStorage is available, FF bug #495747
// If not, we use FUEL in FF and fallback to Cookies for other browsers. // If not, we use FUEL in FF and fallback to Cookies for other browsers.
var Settings = (function settingsClosure() { var Settings = (function SettingsClosure() {
var isCookiesEnabled = (function cookiesEnabledTest() {
document.cookie = 'they=work';
return document.cookie.length > 0;
})();
var isLocalStorageEnabled = (function localStorageEnabledTest() { var isLocalStorageEnabled = (function localStorageEnabledTest() {
try { try {
localStorage; localStorage;
@ -42,36 +37,60 @@ var Settings = (function settingsClosure() {
} }
return true; return true;
})(); })();
var extPrefix = 'extensions.uriloader@pdf.js'; var extPrefix = 'extensions.uriloader@pdf.js';
var isExtension = location.protocol == 'chrome:' && !isLocalStorageEnabled;
function Settings(fingerprint) {
var database = null;
var index;
if (isExtension)
database = Application.prefs.getValue(extPrefix + '.database', '{}');
else if (isLocalStorageEnabled)
database = localStorage.getItem('database') || '{}';
else
return false;
database = JSON.parse(database);
if (!('files' in database))
database.files = [];
if (database.files.length >= 20)
database.files.shift();
for (var i = 0, length = database.files.length; i < length; i++) {
var branch = database.files[i];
if (branch.fingerprint == fingerprint) {
index = i;
break;
}
}
if (typeof index != 'number')
index = database.files.push({fingerprint: fingerprint}) - 1;
this.file = database.files[index];
this.database = database;
if (isExtension)
Application.prefs.setValue(extPrefix + '.database',
JSON.stringify(database));
else if (isLocalStorageEnabled)
localStorage.setItem('database', JSON.stringify(database));
}
return { Settings.prototype = {
set: function settingsSet(name, val) { set: function settingsSet(name, val) {
if (location.protocol == 'chrome:' && !isLocalStorageEnabled) { var file = this.file;
Application.prefs.setValue(extPrefix + '.' + name, val); file[name] = val;
} else if (isLocalStorageEnabled) { if (isExtension) {
localStorage.setItem(name, val); Application.prefs.setValue(extPrefix + '.database',
} else if (isCookiesEnabled) { JSON.stringify(this.database));
var cookieString = name + '=' + escape(val);
var expire = new Date();
expire.setTime(expire.getTime() + 1000 * 60 * 60 * 24 * 365);
cookieString += '; expires=' + expire.toGMTString();
document.cookie = cookieString;
} }
else if (isLocalStorageEnabled)
localStorage.setItem('database', JSON.stringify(this.database));
}, },
get: function settingsGet(name, defaultValue) { get: function settingsGet(name, defaultValue) {
if (location.protocol == 'chrome:' && !isLocalStorageEnabled) { return this.file[name] || defaultValue;
var preferenceName = extPrefix + '.' + name;
return Application.prefs.getValue(preferenceName, defaultValue);
} else if (isLocalStorageEnabled) {
return localStorage.getItem(name) || defaultValue;
} else if (isCookiesEnabled) {
var res = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return res ? unescape(res[2]) : defaultValue;
}
} }
}; };
return Settings;
})(); })();
var cache = new Cache(kCacheSize); var cache = new Cache(kCacheSize);
@ -326,15 +345,14 @@ var PDFView = {
document.getElementById('numPages').innerHTML = pagesCount; document.getElementById('numPages').innerHTML = pagesCount;
document.getElementById('pageNumber').max = pagesCount; document.getElementById('pageNumber').max = pagesCount;
PDFView.documentFingerprint = id; PDFView.documentFingerprint = id;
var store = PDFView.store = new Settings(id);
if (Settings.get(id + '.exists', false)) { if (store.get('exists', false)) {
var page = Settings.get(id + '.page', '1'); var page = store.get('page', '1');
var zoom = Settings.get(id + '.zoom', PDFView.currentScale); var zoom = store.get('zoom', PDFView.currentScale);
var left = Settings.get(id + '.scrollLeft', '0'); var left = store.get('scrollLeft', '0');
var top = Settings.get(id + '.scrollTop', '0'); var top = store.get('scrollTop', '0');
storedHash = 'page=' + page + '&zoom=' + Math.round(zoom * 100); storedHash = 'page=' + page + '&zoom=' + zoom + ',' + left + ',' + top;
storedHash += ',' + left + ',' + top;
} }
var pages = this.pages = []; var pages = this.pages = [];
@ -371,7 +389,8 @@ var PDFView = {
} }
else if (storedHash) { else if (storedHash) {
this.setHash(storedHash); this.setHash(storedHash);
} } else
window.scrollTo(0, 0); // Scroll to top is default.
}, },
setHash: function pdfViewSetHash(hash) { setHash: function pdfViewSetHash(hash) {
@ -890,12 +909,12 @@ function updateViewarea() {
window.pageYOffset - firstPage.y - kViewerTopMargin); window.pageYOffset - firstPage.y - kViewerTopMargin);
pdfOpenParams += ',' + Math.round(topLeft.x) + ',' + Math.round(topLeft.y); pdfOpenParams += ',' + Math.round(topLeft.x) + ',' + Math.round(topLeft.y);
var id = PDFView.documentFingerprint; var store = PDFView.store;
Settings.set(id + '.exists', true); store.set('exists', true);
Settings.set(id + '.page', pageNumber); store.set('page', pageNumber);
Settings.set(id + '.zoom', PDFView.currentScale); store.set('zoom', Math.round(PDFView.currentScale * 100));
Settings.set(id + '.scrollLeft', Math.round(topLeft.x)); store.set('scrollLeft', Math.round(topLeft.x));
Settings.set(id + '.scrollTop', Math.round(topLeft.y)); store.set('scrollTop', Math.round(topLeft.y));
document.getElementById('viewBookmark').href = pdfOpenParams; document.getElementById('viewBookmark').href = pdfOpenParams;
} }

Loading…
Cancel
Save