From a4a8126a40261e4e8dfa175982d5744610b0d827 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Tue, 27 Sep 2011 12:57:33 -0400 Subject: [PATCH 1/5] PDFDoc() now detects argument type --- pdf.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pdf.js b/pdf.js index 449fd9c16..44ebf4c16 100644 --- a/pdf.js +++ b/pdf.js @@ -172,7 +172,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 +3801,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(); @@ -3822,7 +3836,7 @@ var PDFDoc = (function pdfDoc() { stream.pos += index; return true; /* found */ } - + constructor.prototype = { get linearization() { var length = this.stream.length; From 83854a085c8969266bb2e9a346d4dfdd37b7dca3 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Tue, 27 Sep 2011 13:30:47 -0400 Subject: [PATCH 2/5] Implemented getPdf(). Closes #516 Conflicts: examples/helloworld/hello.js --- examples/helloworld/hello.js | 25 +------------------------ pdf.js | 29 +++++++++++++++++++++++++++++ test/driver.js | 26 ++++++++------------------ web/viewer.js | 21 ++++----------------- 4 files changed, 42 insertions(+), 59 deletions(-) diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index 953700ae8..3b0f6dca7 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -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(data){ // // Instantiate PDFDoc with PDF data // diff --git a/pdf.js b/pdf.js index 449fd9c16..197c6e6d8 100644 --- a/pdf.js +++ b/pdf.js @@ -112,6 +112,35 @@ function stringToPDFString(str) { return str2; } +// +// getPdf() +// Convenience function to perform binary Ajax GET +// Usage: getPdf('http://...', callback) +// getPdf({url:String [,progress: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; + xhr.onprogress = params.progress || undefined; + + xhr.onreadystatechange = function() { + var data; + if (xhr.readyState === 4 && xhr.status === xhr.expected) { + 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); diff --git a/test/driver.js b/test/driver.js index 144b97589..e948835dd 100644 --- a/test/driver.js +++ b/test/driver.js @@ -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(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); + try { + task.pdfDoc = new PDFDoc(data); + } catch (e) { + failure = 'load PDF doc : ' + e.toString(); } - }; - r.send(null); + task.pageNum = 1; + nextPage(task, failure); + }); } function isLastPage(task) { diff --git a/web/viewer.js b/web/viewer.js index 482ac95e0..2cc3aaa4f 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -107,23 +107,10 @@ 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 = PDFView.progressLevel; - - xhr.onreadystatechange = function() { - if (xhr.readyState === 4 && xhr.status === xhr.expected) { - var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse || - xhr.responseArrayBuffer || xhr.response); - - document.getElementById('loading').style.display = 'none'; - PDFView.load(data, scale); - } - }; - - xhr.send(null); + getPdf({url:url, progress:PDFView.progressLevel}, function(data) { + document.getElementById('loading').style.display = 'none'; + PDFView.load(data, scale); + }); }, progressLevel: function(evt) { From de408dac70a56e9dd007557b5d89894ff5d1d6d0 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Tue, 27 Sep 2011 21:43:48 +0300 Subject: [PATCH 3/5] Fix lint warnings. And name anonymous functions. --- examples/helloworld/hello.js | 2 +- pdf.js | 10 +++++----- test/driver.js | 2 +- web/viewer.js | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index 3b0f6dca7..9c653ca24 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -7,7 +7,7 @@ 'use strict'; -getPdf('helloworld.pdf', function(data){ +getPdf('helloworld.pdf', function getPdfHelloWorld(data) { // // Instantiate PDFDoc with PDF data // diff --git a/pdf.js b/pdf.js index 197c6e6d8..cb4296c79 100644 --- a/pdf.js +++ b/pdf.js @@ -129,16 +129,16 @@ function getPdf(arg, callback) { xhr.mozResponseType = xhr.responseType = 'arraybuffer'; xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200; xhr.onprogress = params.progress || undefined; - - xhr.onreadystatechange = function() { + + xhr.onreadystatechange = function getPdfOnreadystatechange() { var data; if (xhr.readyState === 4 && xhr.status === xhr.expected) { data = (xhr.mozResponseArrayBuffer || xhr.mozResponse || - xhr.responseArrayBuffer || xhr.response); + xhr.responseArrayBuffer || xhr.response); callback(data); } }; - xhr.send(null); + xhr.send(null); } var Stream = (function streamStream() { @@ -4965,7 +4965,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; diff --git a/test/driver.js b/test/driver.js index e948835dd..7162af6f3 100644 --- a/test/driver.js +++ b/test/driver.js @@ -73,7 +73,7 @@ function nextTask() { log('Loading file "' + task.file + '"\n'); - getPdf(task.file, function(data){ + getPdf(task.file, function nextTaskGetPdf(data) { var failure; try { task.pdfDoc = new PDFDoc(data); diff --git a/web/viewer.js b/web/viewer.js index 2cc3aaa4f..7515979f2 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -107,7 +107,7 @@ var PDFView = { document.title = url; - getPdf({url:url, progress:PDFView.progressLevel}, function(data) { + getPdf({url: url, progress: PDFView.progressLevel}, function(data) { document.getElementById('loading').style.display = 'none'; PDFView.load(data, scale); }); From 753bfcf7c7f39fcee26ea23fc71554c2e6129f87 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Tue, 27 Sep 2011 22:15:06 +0300 Subject: [PATCH 4/5] Fix lint warnings. --- extensions/firefox/bootstrap.js | 27 ++++---- .../firefox/components/pdfContentHandler.js | 63 +++++++++++-------- pdf.js | 8 +-- 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/extensions/firefox/bootstrap.js b/extensions/firefox/bootstrap.js index 8dc13275a..5384a05df 100644 --- a/extensions/firefox/bootstrap.js +++ b/extensions/firefox/bootstrap.js @@ -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 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); Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(file); - } catch(e) { + } 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); +} diff --git a/extensions/firefox/components/pdfContentHandler.js b/extensions/firefox/components/pdfContentHandler.js index bafb83b12..92b663455 100644 --- a/extensions/firefox/components/pdfContentHandler.js +++ b/extensions/firefox/components/pdfContentHandler.js @@ -1,28 +1,33 @@ +/* -*- 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'); // TODO // Add some download progress event 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 loadDocument(aWindow, aDocumentUrl) { - let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] + let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1'] .createInstance(Ci.nsIXMLHttpRequest); - xhr.open("GET", aDocumentUrl); - xhr.mozResponseType = xhr.responseType = "arraybuffer"; + xhr.open('GET', aDocumentUrl); + xhr.mozResponseType = xhr.responseType = 'arraybuffer'; xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse || @@ -30,23 +35,23 @@ function loadDocument(aWindow, aDocumentUrl) { try { var view = new Uint8Array(data); - // I think accessing aWindow.wrappedJSObject returns a + // I think accessing aWindow.wrappedJSObject returns a // XPCSafeJSObjectWrapper and so it is safe but mrbkap can confirm that let window = aWindow.wrappedJSObject; var arrayBuffer = new window.ArrayBuffer(data.byteLength); var view2 = new window.Uint8Array(arrayBuffer); view2.set(view); - let evt = window.document.createEvent("CustomEvent"); - evt.initCustomEvent("pdfloaded", false, false, arrayBuffer); + let evt = window.document.createEvent('CustomEvent'); + evt.initCustomEvent('pdfloaded', false, false, arrayBuffer); window.document.dispatchEvent(evt); - } catch(e) { - log("Error - " + e); + } catch (e) { + log('Error - ' + e); } } }; xhr.send(null); -}; +} let WebProgressListener = { init: function(aWindow, aUrl) { @@ -64,11 +69,12 @@ let WebProgressListener = { .getInterface(Ci.nsIWebProgress); try { webProgress.removeProgressListener(this); - } catch(e) {} + } catch (e) {} 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) { @@ -77,14 +83,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) { @@ -127,16 +136,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 - " + e); + } catch (e) { + log('Error - ' + 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]); diff --git a/pdf.js b/pdf.js index 7129c53e9..c173166c8 100644 --- a/pdf.js +++ b/pdf.js @@ -3830,7 +3830,7 @@ var Catalog = (function catalogCatalog() { })(); var PDFDoc = (function pdfDoc() { - function constructor(arg, callback) { + function constructor(arg, callback) { // Stream argument if (typeof arg.isStream !== 'undefined') { init.call(this, arg); @@ -3841,10 +3841,10 @@ var PDFDoc = (function pdfDoc() { } else { error('Unknown argument type'); - } + } } - function init(stream){ + function init(stream) { assertWellFormed(stream.length > 0, 'stream must have data'); this.stream = stream; this.setup(); @@ -3865,7 +3865,7 @@ var PDFDoc = (function pdfDoc() { stream.pos += index; return true; /* found */ } - + constructor.prototype = { get linearization() { var length = this.stream.length; From 24d642a3cfa551b8b381e9c62ef3d982003bef3a Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Tue, 27 Sep 2011 22:20:58 +0300 Subject: [PATCH 5/5] Add examples and extensions to lint. --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 56b597e5f..04586bd18 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,8 @@ browser-test: # To install gjslint, see: # # -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)