]> git.parisson.com Git - pdf.js.git/commitdiff
Initial import of the Firefox extension
authorVivien Nicolas <21@vingtetun.org>
Tue, 6 Sep 2011 22:00:24 +0000 (00:00 +0200)
committerVivien Nicolas <21@vingtetun.org>
Tue, 6 Sep 2011 22:00:24 +0000 (00:00 +0200)
Makefile
extensions/firefox/bootstrap.js [new file with mode: 0644]
extensions/firefox/chrome.manifest [new file with mode: 0644]
extensions/firefox/components/pdfContentHandler.js [new file with mode: 0644]
extensions/firefox/install.rdf [new file with mode: 0644]

index 34af3b24c7ecd4cc1074d563c7a60f863c206094..0ca26964267309cae24a36414c5d7f9a62fea423 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -146,6 +146,32 @@ $(BUILD_DIR)/compiler.zip: | $(BUILD_DIR)
        curl $(COMPILER_URL) > $(BUILD_DIR)/compiler.zip;
        cd $(BUILD_DIR); unzip compiler.zip compiler.jar;
 
+# make firefox-extension
+#
+# This target produce a restartless firefox extension containing a
+# copy of the pdf.js source.
+CONTENT_DIR := content
+EXTENSION_SRC := ./extensions/firefox
+EXTENSION_NAME := pdf.js.xpi
+PDF_WEB_FILES = \
+       web/images \
+       web/compatibility.js \
+       web/viewer.css \
+       web/viewer.js \
+       web/viewer.html \
+       $(NULL)
+extension:
+       # Copy a standalone version of pdf.js inside the content directory
+       @rm -Rf $(EXTENSION_SRC)/$(CONTENT_DIR)/
+       @mkdir $(EXTENSION_SRC)/$(CONTENT_DIR)/
+       @cp $(PDF_JS_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
+       @cp -r $(PDF_WEB_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
+
+       # Create the xpi
+       @cd $(EXTENSION_SRC); zip -r $(EXTENSION_NAME) *
+       @echo "extension created: " $(EXTENSION_NAME)
+
+
 # Make sure there's a build directory.
 $(BUILD_DIR):
        mkdir -p $(BUILD_DIR)
diff --git a/extensions/firefox/bootstrap.js b/extensions/firefox/bootstrap.js
new file mode 100644 (file)
index 0000000..8dc1327
--- /dev/null
@@ -0,0 +1,31 @@
+let Cc = Components.classes;
+let Ci = Components.interfaces;
+let Cm = Components.manager;
+let Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+function log(str) {
+  dump(str + "\n");
+};
+
+function startup(aData, aReason) {
+  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) {
+    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);
+};
+
diff --git a/extensions/firefox/chrome.manifest b/extensions/firefox/chrome.manifest
new file mode 100644 (file)
index 0000000..d7db20b
--- /dev/null
@@ -0,0 +1,5 @@
+content pdf.js content/
+
+component {2278dfd0-b75c-11e0-8257-1ba3d93c9f1a} components/pdfContentHandler.js
+contract @mozilla.org/uriloader/content-handler;1?type=application/pdf {2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}
+
diff --git a/extensions/firefox/components/pdfContentHandler.js b/extensions/firefox/components/pdfContentHandler.js
new file mode 100644 (file)
index 0000000..bafb83b
--- /dev/null
@@ -0,0 +1,144 @@
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cr = Components.results;
+const Cu = Components.utils;
+
+const PDF_CONTENT_TYPE = "application/pdf";
+
+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)
+                                     .logStringMessage(msg);
+  dump(msg + "\n");
+};
+
+function loadDocument(aWindow, aDocumentUrl) {
+  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.send(null);
+};
+
+let WebProgressListener = {
+  init: function(aWindow, aUrl) {
+    this._locationHasChanged = false;
+    this._documentUrl = aUrl;
+
+    let flags = Ci.nsIWebProgress.NOTIFY_LOCATION |
+                Ci.nsIWebProgress.NOTIFY_STATE_NETWORK |
+                Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT;
+
+    let docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+                          .getInterface(Ci.nsIWebNavigation)
+                          .QueryInterface(Ci.nsIDocShell);
+    let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                              .getInterface(Ci.nsIWebProgress);
+    try {
+      webProgress.removeProgressListener(this);
+    } catch(e) {}
+    webProgress.addProgressListener(this, flags);
+  },
+
+  onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
+    const complete = Ci.nsIWebProgressListener.STATE_IS_WINDOW +
+                     Ci.nsIWebProgressListener.STATE_STOP;
+    if ((aStateFlags & complete) == complete && this._locationHasChanged) {
+      aWebProgress.removeProgressListener(this);
+      loadDocument(aWebProgress.DOMWindow, this._documentUrl);
+    }
+  },
+
+  onProgressChange: function onProgressChange(aWebProgress, aRequest, aCurSelf, aMaxSelf, aCurTotal, aMaxTotal) {
+  },
+
+  onLocationChange: function onLocationChange(aWebProgress, aRequest, aLocationURI) {
+    this._locationHasChanged = true;
+  },
+
+  onStatusChange: function onStatusChange(aWebProgress, aRequest, aStatus, aMessage) {
+  },
+
+  onSecurityChange: function onSecurityChange(aWebProgress, aRequest, aState) {
+  },
+
+  QueryInterface: function QueryInterface(aIID) {
+    if (aIID.equals(Ci.nsIWebProgressListener) ||
+        aIID.equals(Ci.nsISupportsWeakReference) ||
+        aIID.equals(Ci.nsISupports)) {
+        return this;
+    }
+
+    throw Components.results.NS_ERROR_NO_INTERFACE;
+  }
+};
+
+
+function pdfContentHandler() {
+}
+
+pdfContentHandler.prototype = {
+  handleContent: function handleContent(aMimetype, aContext, aRequest) {
+    if (aMimetype != PDF_CONTENT_TYPE)
+      throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
+
+    if (!(aRequest instanceof Ci.nsIChannel))
+      throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
+
+    let window = null;
+    let callbacks = aRequest.notificationCallbacks ?
+                    aRequest.notificationCallbacks :
+                    aRequest.loadGroup.notificationCallbacks;
+    if (!callbacks)
+      return;
+
+    aRequest.cancel(Cr.NS_BINDING_ABORTED);
+    let uri = aRequest.URI;
+
+    window = callbacks.getInterface(Ci.nsIDOMWindow);
+    WebProgressListener.init(window, uri.spec);
+
+    try {
+      let url = Services.prefs.getCharPref("extensions.pdf.js.url");
+      url = url.replace("%s", uri.spec);
+      window.location = url;
+    } catch(e) {
+      log("Error - " + e);
+    }
+  },
+
+  classID: Components.ID("{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]),
+};
+
+var NSGetFactory = XPCOMUtils.generateNSGetFactory([pdfContentHandler]);
+
+
diff --git a/extensions/firefox/install.rdf b/extensions/firefox/install.rdf
new file mode 100644 (file)
index 0000000..5d4f966
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+     xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+
+  <Description about="urn:mozilla:install-manifest">
+    <em:id>uriloader@pdf.js</em:id>
+    <em:name>pdf.js</em:name>
+    <em:version>0.1</em:version>
+         <em:iconURL>chrome://pdf.js/skin/logo.png</em:iconURL>
+    <em:targetApplication>
+      <Description>
+       <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+       <em:minVersion>6.0</em:minVersion>
+       <em:maxVersion>9.0.*</em:maxVersion>
+     </Description>
+    </em:targetApplication>
+    <em:bootstrap>true</em:bootstrap>
+    <em:unpack>true</em:unpack>
+    <em:creator>Vivien Nicolas</em:creator>
+    <em:description>pdf.js uri loader</em:description>
+    <em:homepageURL>https://github.com/andreasgal/pdf.js/</em:homepageURL>
+  </Description>
+</RDF>