]> git.parisson.com Git - pdf.js.git/commitdiff
Make the add-on truly restartless.
authorBrendan Dahl <brendan.dahl@gmail.com>
Wed, 8 Feb 2012 00:39:07 +0000 (16:39 -0800)
committerBrendan Dahl <brendan.dahl@gmail.com>
Wed, 8 Feb 2012 00:39:07 +0000 (16:39 -0800)
Makefile
extensions/firefox/bootstrap.js
extensions/firefox/chrome.manifest [deleted file]
extensions/firefox/components/PdfStreamConverter.js

index c1062d49de33e052b57b7d86ff6318f7a72612f7..043cb7fb55cb792150198435ba4e6e97fb68fa9b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -223,14 +223,12 @@ FIREFOX_CONTENT_DIR := $(EXTENSION_SRC)/firefox/$(CONTENT_DIR)/
 FIREFOX_EXTENSION_FILES_TO_COPY = \
        *.js \
        *.rdf \
-       chrome.manifest \
        components \
        $(NULL)
 FIREFOX_EXTENSION_FILES = \
        content \
        *.js \
        install.rdf \
-       chrome.manifest \
        components \
        content \
        $(NULL)
index f1a712c0c268d51d5fd4b2c86d54d3804a2b7b74..fbea7020328b61d705c3863155160a223a4f0b99 100644 (file)
@@ -3,8 +3,9 @@
 
 'use strict';
 
+const RESOURCE_NAME = 'pdf.js';
 const EXT_PREFIX = 'extensions.uriloader@pdf.js';
-const PDFJS_EVENT_ID = 'pdf.js.message';
+
 let Cc = Components.classes;
 let Ci = Components.interfaces;
 let Cm = Components.manager;
@@ -16,24 +17,71 @@ function log(str) {
   dump(str + '\n');
 }
 
+// Register/unregister a class as a component.
+let Factory = {
+  registrar: null,
+  aClass: null,
+  register: function(aClass) {
+    if (this.aClass) {
+      log('Cannot register more than one class');
+      return;
+    }
+    this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
+    this.aClass = aClass;
+    var proto = aClass.prototype;
+    this.registrar.registerFactory(proto.classID, proto.classDescription,
+      proto.contractID, this);
+  },
+  unregister: function() {
+    if (!this.aClass) {
+      log('Class was never registered.');
+      return;
+    }
+    var proto = this.aClass.prototype;
+    this.registrar.unregisterFactory(proto.classID, this);
+    this.aClass = null;
+  },
+  // nsIFactory::createInstance
+  createInstance: function(outer, iid) {
+    if (outer !== null)
+      throw Cr.NS_ERROR_NO_AGGREGATION;
+    return (new (this.aClass)).QueryInterface(iid);
+  }
+};
+
+// As of Firefox 13 bootstrapped add-ons don't support automatic registering and
+// unregistering of resource urls and components/contracts. Until then we do
+// it programatically. See ManifestDirective ManifestParser.cpp for support.
 
 function startup(aData, aReason) {
-  let manifestPath = 'chrome.manifest';
-  let manifest = Cc['@mozilla.org/file/local;1']
-                   .createInstance(Ci.nsILocalFile);
-  try {
-    manifest.initWithPath(aData.installPath.path);
-    manifest.append(manifestPath);
-    Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(manifest);
-    Services.prefs.setBoolPref('extensions.pdf.js.active', true);
-  } catch (e) {
-    log(e);
-  }
+  // Setup the resource url.
+  var ioService = Services.io;
+  var resProt = ioService.getProtocolHandler('resource')
+                  .QueryInterface(Ci.nsIResProtocolHandler);
+  var aliasFile = Cc['@mozilla.org/file/local;1']
+                    .createInstance(Ci.nsILocalFile);
+  var componentPath = aData.installPath.clone();
+  componentPath.append('content');
+  aliasFile.initWithPath(componentPath.path);
+  var aliasURI = ioService.newFileURI(aliasFile);
+  resProt.setSubstitution(RESOURCE_NAME, aliasURI);
+
+  // Load the component and register it.
+  Cu.import(aData.resourceURI.spec + 'components/PdfStreamConverter.js');
+  Factory.register(PdfStreamConverter);
+  Services.prefs.setBoolPref('extensions.pdf.js.active', true);
 }
 
 function shutdown(aData, aReason) {
   if (Services.prefs.getBoolPref('extensions.pdf.js.active'))
     Services.prefs.setBoolPref('extensions.pdf.js.active', false);
+  var ioService = Services.io;
+  var resProt = ioService.getProtocolHandler('resource')
+                  .QueryInterface(Ci.nsIResProtocolHandler);
+  // Remove the resource url.
+  resProt.setSubstitution(RESOURCE_NAME, null);
+  // Remove the contract/component.
+  Factory.unregister();
 }
 
 function install(aData, aReason) {
diff --git a/extensions/firefox/chrome.manifest b/extensions/firefox/chrome.manifest
deleted file mode 100644 (file)
index 5351257..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-resource pdf.js content/
-
-component {6457a96b-2d68-439a-bcfa-44465fbcdbb1} components/PdfStreamConverter.js
-contract @mozilla.org/streamconv;1?from=application/pdf&to=*/* {6457a96b-2d68-439a-bcfa-44465fbcdbb1}
-
index 78a1f5a46ff31cb0c35ca6cc51cf8b61c34d87e3..7b8cdab49a5f70573ef422523c7e0229046fa11d 100644 (file)
@@ -3,6 +3,8 @@
 
 'use strict';
 
+var EXPORTED_SYMBOLS = ['PdfStreamConverter'];
+
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;