Merge with upstream

This commit is contained in:
Vivien Nicolas 2011-09-28 13:21:07 +02:00
commit 132ee01bc0
7 changed files with 128 additions and 105 deletions

View File

@ -88,7 +88,8 @@ browser-test:
# To install gjslint, see:
#
# <http://code.google.com/closure/utilities/docs/linter_howto.html>
SRC_DIRS := . utils worker web test
SRC_DIRS := . utils worker web test examples/helloworld extensions/firefox \
extensions/firefox/components
GJSLINT_FILES = $(foreach DIR,$(SRC_DIRS),$(wildcard $(DIR)/*.js))
lint:
gjslint $(GJSLINT_FILES)

View File

@ -7,30 +7,7 @@
'use strict';
//
// Ajax GET request, for binary files
// (like jQuery's $.get(), but supports the binary type ArrayBuffer)
//
var ajaxGet = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
callback(data);
}
};
xhr.send(null);
};
//
// This is where the fun happens
//
ajaxGet('helloworld.pdf', function ajaxGetHelloWorld(data) {
getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
//
// Instantiate PDFDoc with PDF data
//

View File

@ -1,17 +1,22 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
let Cc = Components.classes;
let Ci = Components.interfaces;
let Cm = Components.manager;
let Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import('resource://gre/modules/Services.jsm');
function log(str) {
dump(str + "\n");
};
dump(str + '\n');
}
function startup(aData, aReason) {
let manifestPath = "chrome.manifest";
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
let manifestPath = 'chrome.manifest';
let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
try {
file.initWithPath(aData.installPath.path);
file.append(manifestPath);
@ -19,13 +24,13 @@ function startup(aData, aReason) {
} catch (e) {
log(e);
}
};
}
function shutdown(aData, aReason) {
};
}
function install(aData, aReason) {
let url = "chrome://pdf.js/content/web/viewer.html?file=%s";
Services.prefs.setCharPref("extensions.pdf.js.url", url);
};
let url = 'chrome://pdf.js/content/web/viewer.html?file=%s';
Services.prefs.setCharPref('extensions.pdf.js.url', url);
}

View File

@ -1,31 +1,35 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
const PDF_CONTENT_TYPE = "application/pdf";
const PDF_CONTENT_TYPE = 'application/pdf';
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
function log(aMsg) {
let msg = "pdfContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
let msg = 'pdfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
.logStringMessage(msg);
dump(msg + "\n");
};
dump(msg + '\n');
}
function fireEventTo(aName, aData, aWindow) {
let window = aWindow.wrappedJSObject;
let evt = window.document.createEvent("CustomEvent");
evt.initCustomEvent("pdf" + aName, false, false, aData);
let evt = window.document.createEvent('CustomEvent');
evt.initCustomEvent('pdf' + aName, false, false, aData);
window.document.dispatchEvent(evt);
};
}
function loadDocument(aWindow, aDocumentUrl) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
.createInstance(Ci.nsIXMLHttpRequest);
xhr.onprogress = function updateProgress(evt) {
if (evt.lengthComputable)
fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
@ -48,14 +52,14 @@ function loadDocument(aWindow, aDocumentUrl) {
fireEventTo(evt.type, arrayBuffer, aWindow);
} catch (e) {
log("Error - " + e);
log('Error - ' + e);
}
};
xhr.open("GET", aDocumentUrl);
xhr.responseType = "arraybuffer";
xhr.open('GET', aDocumentUrl);
xhr.responseType = 'arraybuffer';
xhr.send(null);
};
}
let WebProgressListener = {
init: function(aWindow, aUrl) {
@ -77,7 +81,8 @@ let WebProgressListener = {
webProgress.addProgressListener(this, flags);
},
onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags,
aStatus) {
const complete = Ci.nsIWebProgressListener.STATE_IS_WINDOW +
Ci.nsIWebProgressListener.STATE_STOP;
if ((aStateFlags & complete) == complete && this._locationHasChanged) {
@ -86,14 +91,17 @@ let WebProgressListener = {
}
},
onProgressChange: function onProgressChange(aWebProgress, aRequest, aCurSelf, aMaxSelf, aCurTotal, aMaxTotal) {
onProgressChange: function onProgressChange(aWebProgress, aRequest, aCurSelf,
aMaxSelf, aCurTotal, aMaxTotal) {
},
onLocationChange: function onLocationChange(aWebProgress, aRequest, aLocationURI) {
onLocationChange: function onLocationChange(aWebProgress, aRequest,
aLocationURI) {
this._locationHasChanged = true;
},
onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus, aMessage) {
onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus,
aMessage) {
},
onSecurityChange: function onSecurityChange(aWebProgress, aRequest, aState) {
@ -136,16 +144,16 @@ pdfContentHandler.prototype = {
WebProgressListener.init(window, uri.spec);
try {
let url = Services.prefs.getCharPref("extensions.pdf.js.url");
url = url.replace("%s", uri.spec);
let url = Services.prefs.getCharPref('extensions.pdf.js.url');
url = url.replace('%s', uri.spec);
window.location = url;
} catch (e) {
log("Error retrieving the pdf.js base url - " + e);
log('Error retrieving the pdf.js base url - ' + e);
}
},
classID: Components.ID("{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]),
classID: Components.ID('{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}'),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler])
};
var NSGetFactory = XPCOMUtils.generateNSGetFactory([pdfContentHandler]);

58
pdf.js
View File

@ -112,6 +112,42 @@ function stringToPDFString(str) {
return str2;
}
//
// getPdf()
// Convenience function to perform binary Ajax GET
// Usage: getPdf('http://...', callback)
// getPdf({
// url:String ,
// [,progress:Function, error:Function]
// },
// callback)
//
function getPdf(arg, callback) {
var params = arg;
if (typeof arg === 'string')
params = { url: arg };
var xhr = new XMLHttpRequest();
xhr.open('GET', params.url);
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
if ('progress' in params)
xhr.onprogrss = params.progress || undefined;
if ('error' in params)
xhr.onerror = params.error || undefined;
xhr.onreadystatechange = function getPdfOnreadystatechange() {
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
callback(data);
}
};
xhr.send(null);
}
var Stream = (function streamStream() {
function constructor(arrayBuffer, start, length, dict) {
this.bytes = new Uint8Array(arrayBuffer);
@ -172,7 +208,8 @@ var Stream = (function streamStream() {
},
makeSubStream: function stream_makeSubstream(start, length, dict) {
return new Stream(this.bytes.buffer, start, length, dict);
}
},
isStream: true
};
return constructor;
@ -3800,8 +3837,21 @@ var Catalog = (function catalogCatalog() {
})();
var PDFDoc = (function pdfDoc() {
function constructor(data) {
var stream = new Stream(data);
function constructor(arg, callback) {
// Stream argument
if (typeof arg.isStream !== 'undefined') {
init.call(this, arg);
}
// ArrayBuffer argument
else if (typeof arg.byteLength !== 'undefined') {
init.call(this, new Stream(arg));
}
else {
error('Unknown argument type');
}
}
function init(stream) {
assertWellFormed(stream.length > 0, 'stream must have data');
this.stream = stream;
this.setup();
@ -4936,7 +4986,7 @@ var CanvasGraphics = (function canvasGraphics() {
if (IsDict(extGState) && extGState.has(dictName.name)) {
var gsState = this.xref.fetchIfRef(extGState.get(dictName.name));
var self = this;
gsState.forEach(function(key, value) {
gsState.forEach(function canvasGraphicsSetGStateForEach(key, value) {
switch (key) {
case 'Type':
break;

View File

@ -73,26 +73,16 @@ function nextTask() {
log('Loading file "' + task.file + '"\n');
var r = new XMLHttpRequest();
r.open('GET', task.file);
r.mozResponseType = r.responseType = 'arraybuffer';
r.onreadystatechange = function nextTaskOnreadystatechange() {
getPdf(task.file, function nextTaskGetPdf(data) {
var failure;
if (r.readyState == 4) {
var data = r.mozResponseArrayBuffer || r.mozResponse ||
r.responseArrayBuffer || r.response;
try {
task.pdfDoc = new PDFDoc(data);
} catch (e) {
failure = 'load PDF doc : ' + e.toString();
}
task.pageNum = 1;
nextPage(task, failure);
}
};
r.send(null);
});
}
function isLastPage(task) {

View File

@ -107,26 +107,18 @@ var PDFView = {
document.title = url;
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
xhr.onprogress = function updateProgress(evt) {
getPdf(
{
url: url,
progress: function getPdfProgress(evt) {
if (evt.lengthComputable)
PDFView.progress(evt.loaded / evt.total);
};
xhr.onerror = PDFView.error;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
},
error: PDFView.error
},
function getPdfLoad(data) {
PDFView.load(data, scale);
}
};
xhr.send(null);
});
},
navigateTo: function(dest) {