2014-09-18 06:14:04 +09:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
/* globals Components, Services, XPCOMUtils, PdfjsContentUtils,
|
|
|
|
PdfjsContentUtils, PdfStreamConverter, addMessageListener */
|
|
|
|
|
2017-01-24 07:41:56 +09:00
|
|
|
"use strict";
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
(function contentScriptClosure() {
|
|
|
|
// we need to use closure here -- we are running in the global context
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cm = Components.manager;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2017-01-24 07:41:56 +09:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
var isRemote = Services.appinfo.processType ===
|
|
|
|
Services.appinfo.PROCESS_TYPE_CONTENT;
|
2014-09-18 06:14:04 +09:00
|
|
|
|
[Firefox addon] Convert the code to be ES6 friendly, in order to better agree with mozilla-central coding conventions (issue 7957)
*Please note: ignoring whitespace changes is most likely necessary for the diff to be readable.*
This patch addresses all the current, in `mozilla-central`, linting failures in the addon. It should thus be possible to change the `.eslintignore` entry for PDF.js in `mozilla-central` from `browser/extensions/pdfjs/**` to `browser/extensions/pdfjs/build/**` and `browser/extensions/pdfjs/web/**` instead.
Note that we cannot, for backwards compatibility reason of the general PDF.js library, at this time make similar changes for files residing in the `build` and `web` directories in `mozilla-central`.
The main changes in this patch are that we now use [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) instead of our previous "class-like" functions, and also use the more compact [object shorthand notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015).
A couple of functions were also converted to [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), to reduced usages of `bind(this)` and `var self = this`.
One caveat with ES6 classes is that it's not (yet) possible to define private constants/helper functions within them, which is why the `NetworkManagerClosure` was kept to not change the visibility of those constant/functions.
Besides testing in Firefox Nightly 53, this patch has also been tested in Firefox ESR 45 and SeaMonkey 2.46.
However, I'd gladly welcome help with testing the patch more, to ensure that nothing has gone wrong during the refactoring.
Fixes the first bullet point of issue 7957.
2017-01-23 02:07:53 +09:00
|
|
|
// Factory that registers/unregisters a constructor as a component.
|
2014-09-20 03:58:12 +09:00
|
|
|
function Factory() {
|
2014-09-18 06:14:04 +09:00
|
|
|
}
|
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
Factory.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
|
|
|
|
_targetConstructor: null,
|
|
|
|
|
|
|
|
register: function register(targetConstructor) {
|
|
|
|
this._targetConstructor = targetConstructor;
|
|
|
|
var proto = targetConstructor.prototype;
|
|
|
|
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
registrar.registerFactory(proto.classID, proto.classDescription,
|
|
|
|
proto.contractID, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
unregister: function unregister() {
|
|
|
|
var proto = this._targetConstructor.prototype;
|
|
|
|
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
registrar.unregisterFactory(proto.classID, this);
|
|
|
|
this._targetConstructor = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIFactory
|
|
|
|
createInstance: function createInstance(aOuter, iid) {
|
|
|
|
if (aOuter !== null) {
|
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
return (new (this._targetConstructor)()).QueryInterface(iid);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIFactory
|
|
|
|
lockFactory: function lockFactory(lock) {
|
|
|
|
// No longer used as of gecko 1.7.
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var pdfStreamConverterFactory = new Factory();
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
function startup() {
|
2017-01-24 07:41:56 +09:00
|
|
|
Cu.import("resource://pdf.js/PdfjsContentUtils.jsm");
|
2014-09-20 03:58:12 +09:00
|
|
|
PdfjsContentUtils.init();
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2017-01-24 07:41:56 +09:00
|
|
|
Cu.import("resource://pdf.js/PdfStreamConverter.jsm");
|
2014-09-20 03:58:12 +09:00
|
|
|
pdfStreamConverterFactory.register(PdfStreamConverter);
|
|
|
|
}
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
function shutdown() {
|
|
|
|
// Remove the contract/component.
|
|
|
|
pdfStreamConverterFactory.unregister();
|
|
|
|
// Unload the converter
|
2017-01-24 07:41:56 +09:00
|
|
|
Cu.unload("resource://pdf.js/PdfStreamConverter.jsm");
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
PdfjsContentUtils.uninit();
|
2017-01-24 07:41:56 +09:00
|
|
|
Cu.unload("resource://pdf.js/PdfjsContentUtils.jsm");
|
2014-09-20 03:58:12 +09:00
|
|
|
}
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2014-09-20 03:58:12 +09:00
|
|
|
if (isRemote) {
|
|
|
|
startup();
|
2014-09-18 06:14:04 +09:00
|
|
|
|
2017-01-24 07:41:56 +09:00
|
|
|
addMessageListener("PDFJS:Child:shutdown", function() {
|
2014-09-20 03:58:12 +09:00
|
|
|
shutdown();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|