From 88413358677fd59dc360076e0a62c2c559f50e38 Mon Sep 17 00:00:00 2001 From: girst Date: Sun, 3 Mar 2024 12:59:18 +0100 Subject: port legacyfox to ecmascript modules mozilla calls this 'esm-ification'. the last relevant to us modules were ported in mozilla124[1], so this is our new minimum version. the old version will likely be compatible with firefox up to version 128esr[2]. static imports are not supported in autoconfig scripts. i tried to keep the diff as small as possible w.r.t comm-central's final JSMs. because of this, we don't lazy-load any modules any more, nor provide any lazy getters, as they would need to be loaded into a `lazy` object instead the global (`this`) namespace, causing more churn. other than that, the largest change was removing the now-useless Services.jsm workaround as well as removing globalGetters for objects already loaded automatically. [1]: https://hg.mozilla.org/mozilla-central/rev/68ba071ff6fb9978937496f9adc48e378957f594 [2]: bugzil.la/1881890 --- legacy/RDFManifestConverter.sys.mjs | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 legacy/RDFManifestConverter.sys.mjs (limited to 'legacy/RDFManifestConverter.sys.mjs') diff --git a/legacy/RDFManifestConverter.sys.mjs b/legacy/RDFManifestConverter.sys.mjs new file mode 100644 index 0000000..12abd27 --- /dev/null +++ b/legacy/RDFManifestConverter.sys.mjs @@ -0,0 +1,109 @@ + /* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +var EXPORTED_SYMBOLS = ["InstallRDF"]; + +import {RDFDataSource} from "resource://legacy/RDFDataSource.sys.mjs"; + +const RDFURI_INSTALL_MANIFEST_ROOT = "urn:mozilla:install-manifest"; + +function EM_R(aProperty) { + return `http://www.mozilla.org/2004/em-rdf#${aProperty}`; +} + +function getValue(literal) { + return literal && literal.getValue(); +} + +function getProperty(resource, property) { + return getValue(resource.getProperty(EM_R(property))); +} + +class Manifest { + constructor(ds) { + this.ds = ds; + } + + static loadFromString(text) { + return new this(RDFDataSource.loadFromString(text)); + } + + static loadFromBuffer(buffer) { + return new this(RDFDataSource.loadFromBuffer(buffer)); + } + + static async loadFromFile(uri) { + return new this(await RDFDataSource.loadFromFile(uri)); + } +} + +export class InstallRDF extends Manifest { + _readProps(source, obj, props) { + for (let prop of props) { + let val = getProperty(source, prop); + if (val != null) { + obj[prop] = val; + } + } + } + + _readArrayProp(source, obj, prop, target, decode = getValue) { + let result = Array.from(source.getObjects(EM_R(prop)), + target => decode(target)); + if (result.length) { + obj[target] = result; + } + } + + _readArrayProps(source, obj, props, decode = getValue) { + for (let [prop, target] of Object.entries(props)) { + this._readArrayProp(source, obj, prop, target, decode); + } + } + + _readLocaleStrings(source, obj) { + this._readProps(source, obj, ["name", "description", "creator", "homepageURL"]); + this._readArrayProps(source, obj, { + locale: "locales", + developer: "developers", + translator: "translators", + contributor: "contributors", + }); + } + + decode() { + let root = this.ds.getResource(RDFURI_INSTALL_MANIFEST_ROOT); + let result = {}; + + let props = ["id", "version", "type", "updateURL", "optionsURL", + "optionsType", "aboutURL", "iconURL", + "bootstrap", "unpack", "strictCompatibility"]; + this._readProps(root, result, props); + + let decodeTargetApplication = source => { + let app = {}; + this._readProps(source, app, ["id", "minVersion", "maxVersion"]); + return app; + }; + + let decodeLocale = source => { + let localized = {}; + this._readLocaleStrings(source, localized); + return localized; + }; + + this._readLocaleStrings(root, result); + + this._readArrayProps(root, result, {"targetPlatform": "targetPlatforms"}); + this._readArrayProps(root, result, {"targetApplication": "targetApplications"}, + decodeTargetApplication); + this._readArrayProps(root, result, {"localized": "localized"}, + decodeLocale); + this._readArrayProps(root, result, {"dependency": "dependencies"}, + source => getProperty(source, "id")); + + return result; + } +} -- cgit v1.2.3