]> git.parisson.com Git - pdf.js.git/commitdiff
Merge with upstream
authorVivien Nicolas <21@vingtetun.org>
Wed, 28 Sep 2011 11:21:07 +0000 (13:21 +0200)
committerVivien Nicolas <21@vingtetun.org>
Wed, 28 Sep 2011 11:21:07 +0000 (13:21 +0200)
1  2 
Makefile
extensions/firefox/components/pdfContentHandler.js
pdf.js
web/viewer.js

diff --cc Makefile
Simple merge
index ab5ec9654e642da05ed8a4ceba80eb5ad903d077,92b663455fb898cb1dd238678367511025edaead..e4b6a2a55fd2391adab81fe4dc32d7b769e1097c
@@@ -3,59 -8,50 +8,58 @@@ 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');
+ }
  
-   let evt = window.document.createEvent("CustomEvent");
-   evt.initCustomEvent("pdf" + aName, false, false, aData);
 +function fireEventTo(aName, aData, aWindow) {
 +  let window = aWindow.wrappedJSObject;
- };
++  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.open('GET', aDocumentUrl);
 -  xhr.mozResponseType = xhr.responseType = 'arraybuffer';
 -  xhr.onreadystatechange = function() {
 -    if (xhr.readyState == 4 && xhr.status == 200) {
 -      let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 -                  xhr.responseArrayBuffer || xhr.response);
 -      try {
 -        var view = new Uint8Array(data);
 -
 -        // 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);
 -        window.document.dispatchEvent(evt);
 -      } catch (e) {
 -        log('Error - ' + e);
 -      }
 +  xhr.onprogress = function updateProgress(evt) {
 +    if (evt.lengthComputable)
 +      fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
 +  };
 +
 +  xhr.onerror = function error(evt) {
 +    fireEventTo(evt.type, false, aWindow);
 +  };
 +
 +  xhr.onload = function load(evt) {
 +    let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 +                xhr.responseArrayBuffer || xhr.response);
 +    try {
 +      let view = new Uint8Array(data);
 +
 +      let window = aWindow.wrappedJSObject;
 +      let arrayBuffer = new window.ArrayBuffer(data.byteLength);
 +      let view2 = new window.Uint8Array(arrayBuffer);
 +      view2.set(view);
 +
 +      fireEventTo(evt.type, arrayBuffer, aWindow);
-     } catch(e) {
-       log("Error - " + e);
++    } catch (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) {
@@@ -136,11 -136,11 +144,11 @@@ 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);
+     } catch (e) {
 -      log('Error - ' + e);
++      log('Error retrieving the pdf.js base url - ' + e);
      }
    },
  
diff --cc pdf.js
index 449fd9c16f858e29b69517d51efadb10c4eb8aa0,c173166c86a6afb2680946fb0581af31f902d0d9..a660118a978ff9ba355ec06eb691d1b1e727a624
--- 1/pdf.js
--- 2/pdf.js
+++ b/pdf.js
@@@ -112,6 -112,35 +112,42 @@@ function stringToPDFString(str) 
    return str2;
  }
  
 -//        getPdf({url:String [,progress:Function]}, callback)
+ //
+ // getPdf()
+ // Convenience function to perform binary Ajax GET
+ // Usage: getPdf('http://...', callback)
 -  if (typeof arg === 'string') {
 -    params = {url: arg};
 -  }
++//        getPdf({
++//                 url:String ,
++//                 [,progress:Function, error:Function]
++//               },
++//               callback)
+ //
+ function getPdf(arg, callback) {
+   var params = arg;
 -  xhr.onprogress = params.progress || undefined;
++  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;
 -    var data;
++
++  if ('progress' in params)
++    xhr.onprogrss = params.progress || undefined;
++
++  if ('error' in params)
++    xhr.onerror = params.error || undefined;
+   xhr.onreadystatechange = function getPdfOnreadystatechange() {
 -      data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 -              xhr.responseArrayBuffer || xhr.response);
+     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);
diff --cc web/viewer.js
index 68071103b27457d1c1fe43faec8f09ed964dd94f,7515979f2aa2a8bdf3eb580e42fcdc36491590d2..da41c1d0bd64f755ec593208eeef9be46880e845
@@@ -107,26 -107,15 +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) {
-       if (evt.lengthComputable)
-         PDFView.progress(evt.loaded / evt.total);
-     };
-     xhr.onerror = PDFView.error;
 -    getPdf({url: url, progress: PDFView.progressLevel}, function(data) {
 -      document.getElementById('loading').style.display = 'none';
 -      PDFView.load(data, scale);
 -    });
 -  },
--
-     xhr.onreadystatechange = function() {
-       if (xhr.readyState === 4 && xhr.status === xhr.expected) {
-         var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
-                     xhr.responseArrayBuffer || xhr.response);
 -  progressLevel: function(evt) {
 -    var p = Math.round((evt.loaded / evt.total) * 100);
 -    document.getElementById('loading').innerHTML = 'Loading... ' + p + '%';
++    getPdf(
++      {
++        url: url,
++        progress: function getPdfProgress(evt) {
++          if (evt.lengthComputable)
++            PDFView.progress(evt.loaded / evt.total);
++        },
++        error: PDFView.error
++      },
++      function getPdfLoad(data) {
 +        PDFView.load(data, scale);
-       }
-     };
-     xhr.send(null);
++      });
    },
  
    navigateTo: function(dest) {