pdf.js/extensions/firefox/bootstrap.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2011-09-28 04:15:06 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
2012-02-08 09:39:07 +09:00
const RESOURCE_NAME = 'pdf.js';
const EXT_PREFIX = 'extensions.uriloader@pdf.js';
2012-02-08 09:39:07 +09:00
let Cc = Components.classes;
let Ci = Components.interfaces;
let Cm = Components.manager;
let Cu = Components.utils;
2012-03-21 01:39:33 +09:00
let application = Cc['@mozilla.org/fuel/application;1']
.getService(Ci.fuelIApplication);
2011-09-28 04:15:06 +09:00
Cu.import('resource://gre/modules/Services.jsm');
function log(str) {
2012-03-21 01:39:33 +09:00
if (!application.prefs.getValue(EXT_PREFIX + '.pdfBugEnabled', false))
return;
2011-09-28 04:15:06 +09:00
dump(str + '\n');
}
2012-02-08 09:39:07 +09:00
// 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);
}
};
let pdfStreamConverterUrl = null;
2012-02-08 09:39:07 +09:00
// 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) {
2012-02-08 09:39:07 +09:00
// Setup the resource url.
var ioService = Services.io;
var resProt = ioService.getProtocolHandler('resource')
.QueryInterface(Ci.nsIResProtocolHandler);
2012-03-21 01:39:33 +09:00
var aliasURI = ioService.newURI('content/', 'UTF-8', aData.resourceURI);
2012-02-08 09:39:07 +09:00
resProt.setSubstitution(RESOURCE_NAME, aliasURI);
// Load the component and register it.
pdfStreamConverterUrl = aData.resourceURI.spec +
'components/PdfStreamConverter.js';
Cu.import(pdfStreamConverterUrl);
2012-02-08 09:39:07 +09:00
Factory.register(PdfStreamConverter);
2011-09-28 04:15:06 +09:00
}
function shutdown(aData, aReason) {
2012-03-16 12:08:39 +09:00
if (aReason == APP_SHUTDOWN)
return;
2012-02-08 09:39:07 +09:00
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();
// Unload the converter
2012-03-21 01:39:33 +09:00
Cu.unload(pdfStreamConverterUrl);
pdfStreamConverterUrl = null;
2011-09-28 04:15:06 +09:00
}
function install(aData, aReason) {
}
function uninstall(aData, aReason) {
application.prefs.setValue(EXT_PREFIX + '.database', '{}');
2011-09-28 04:15:06 +09:00
}