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: # To install gjslint, see:
# #
# <http://code.google.com/closure/utilities/docs/linter_howto.html> # <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)) GJSLINT_FILES = $(foreach DIR,$(SRC_DIRS),$(wildcard $(DIR)/*.js))
lint: lint:
gjslint $(GJSLINT_FILES) gjslint $(GJSLINT_FILES)

View File

@ -7,30 +7,7 @@
'use strict'; 'use strict';
// getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
// 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) {
// //
// Instantiate PDFDoc with PDF data // Instantiate PDFDoc with PDF data
// //

View File

@ -1,31 +1,36 @@
/* -*- 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 Cc = Components.classes;
let Ci = Components.interfaces; let Ci = Components.interfaces;
let Cm = Components.manager; let Cm = Components.manager;
let Cu = Components.utils; let Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm"); Cu.import('resource://gre/modules/Services.jsm');
function log(str) { function log(str) {
dump(str + "\n"); dump(str + '\n');
}; }
function startup(aData, aReason) { function startup(aData, aReason) {
let manifestPath = "chrome.manifest"; let manifestPath = 'chrome.manifest';
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
try { try {
file.initWithPath(aData.installPath.path); file.initWithPath(aData.installPath.path);
file.append(manifestPath); file.append(manifestPath);
Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(file); Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(file);
} catch(e) { } catch (e) {
log(e); log(e);
} }
}; }
function shutdown(aData, aReason) { function shutdown(aData, aReason) {
}; }
function install(aData, aReason) { function install(aData, aReason) {
let url = "chrome://pdf.js/content/web/viewer.html?file=%s"; let url = 'chrome://pdf.js/content/web/viewer.html?file=%s';
Services.prefs.setCharPref("extensions.pdf.js.url", url); 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 Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results; const Cr = Components.results;
const Cu = Components.utils; 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/XPCOMUtils.jsm');
Cu.import("resource://gre/modules/Services.jsm"); Cu.import('resource://gre/modules/Services.jsm');
function log(aMsg) { function log(aMsg) {
let msg = "pdfContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg); let msg = 'pdfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService) Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
.logStringMessage(msg); .logStringMessage(msg);
dump(msg + "\n"); dump(msg + '\n');
}; }
function fireEventTo(aName, aData, aWindow) { function fireEventTo(aName, aData, aWindow) {
let window = aWindow.wrappedJSObject; let window = aWindow.wrappedJSObject;
let evt = window.document.createEvent("CustomEvent"); let evt = window.document.createEvent('CustomEvent');
evt.initCustomEvent("pdf" + aName, false, false, aData); evt.initCustomEvent('pdf' + aName, false, false, aData);
window.document.dispatchEvent(evt); window.document.dispatchEvent(evt);
}; }
function loadDocument(aWindow, aDocumentUrl) { function loadDocument(aWindow, aDocumentUrl) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
.createInstance(Ci.nsIXMLHttpRequest); .createInstance(Ci.nsIXMLHttpRequest);
xhr.onprogress = function updateProgress(evt) { xhr.onprogress = function updateProgress(evt) {
if (evt.lengthComputable) if (evt.lengthComputable)
fireEventTo(evt.type, evt.loaded / evt.total, aWindow); fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
@ -47,15 +51,15 @@ function loadDocument(aWindow, aDocumentUrl) {
view2.set(view); view2.set(view);
fireEventTo(evt.type, arrayBuffer, aWindow); fireEventTo(evt.type, arrayBuffer, aWindow);
} catch(e) { } catch (e) {
log("Error - " + e); log('Error - ' + e);
} }
}; };
xhr.open("GET", aDocumentUrl); xhr.open('GET', aDocumentUrl);
xhr.responseType = "arraybuffer"; xhr.responseType = 'arraybuffer';
xhr.send(null); xhr.send(null);
}; }
let WebProgressListener = { let WebProgressListener = {
init: function(aWindow, aUrl) { init: function(aWindow, aUrl) {
@ -73,11 +77,12 @@ let WebProgressListener = {
.getInterface(Ci.nsIWebProgress); .getInterface(Ci.nsIWebProgress);
try { try {
webProgress.removeProgressListener(this); webProgress.removeProgressListener(this);
} catch(e) {} } catch (e) {}
webProgress.addProgressListener(this, flags); 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 + const complete = Ci.nsIWebProgressListener.STATE_IS_WINDOW +
Ci.nsIWebProgressListener.STATE_STOP; Ci.nsIWebProgressListener.STATE_STOP;
if ((aStateFlags & complete) == complete && this._locationHasChanged) { 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; this._locationHasChanged = true;
}, },
onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus, aMessage) { onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus,
aMessage) {
}, },
onSecurityChange: function onSecurityChange(aWebProgress, aRequest, aState) { onSecurityChange: function onSecurityChange(aWebProgress, aRequest, aState) {
@ -136,16 +144,16 @@ pdfContentHandler.prototype = {
WebProgressListener.init(window, uri.spec); WebProgressListener.init(window, uri.spec);
try { try {
let url = Services.prefs.getCharPref("extensions.pdf.js.url"); let url = Services.prefs.getCharPref('extensions.pdf.js.url');
url = url.replace("%s", uri.spec); url = url.replace('%s', uri.spec);
window.location = url; window.location = url;
} catch(e) { } 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}"), classID: Components.ID('{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}'),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]), QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler])
}; };
var NSGetFactory = XPCOMUtils.generateNSGetFactory([pdfContentHandler]); var NSGetFactory = XPCOMUtils.generateNSGetFactory([pdfContentHandler]);

58
pdf.js
View File

@ -112,6 +112,42 @@ function stringToPDFString(str) {
return str2; 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() { var Stream = (function streamStream() {
function constructor(arrayBuffer, start, length, dict) { function constructor(arrayBuffer, start, length, dict) {
this.bytes = new Uint8Array(arrayBuffer); this.bytes = new Uint8Array(arrayBuffer);
@ -172,7 +208,8 @@ var Stream = (function streamStream() {
}, },
makeSubStream: function stream_makeSubstream(start, length, dict) { makeSubStream: function stream_makeSubstream(start, length, dict) {
return new Stream(this.bytes.buffer, start, length, dict); return new Stream(this.bytes.buffer, start, length, dict);
} },
isStream: true
}; };
return constructor; return constructor;
@ -3800,8 +3837,21 @@ var Catalog = (function catalogCatalog() {
})(); })();
var PDFDoc = (function pdfDoc() { var PDFDoc = (function pdfDoc() {
function constructor(data) { function constructor(arg, callback) {
var stream = new Stream(data); // 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'); assertWellFormed(stream.length > 0, 'stream must have data');
this.stream = stream; this.stream = stream;
this.setup(); this.setup();
@ -4936,7 +4986,7 @@ var CanvasGraphics = (function canvasGraphics() {
if (IsDict(extGState) && extGState.has(dictName.name)) { if (IsDict(extGState) && extGState.has(dictName.name)) {
var gsState = this.xref.fetchIfRef(extGState.get(dictName.name)); var gsState = this.xref.fetchIfRef(extGState.get(dictName.name));
var self = this; var self = this;
gsState.forEach(function(key, value) { gsState.forEach(function canvasGraphicsSetGStateForEach(key, value) {
switch (key) { switch (key) {
case 'Type': case 'Type':
break; break;

View File

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

View File

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