diff options
author | girst <girst@users.noreply.github.com> | 2019-06-24 07:36:36 +0200 |
---|---|---|
committer | girst <girst@users.noreply.github.com> | 2019-06-24 08:05:23 +0200 |
commit | b6d0fd2cda60196a8228d6fc3b9b330bb1871393 (patch) | |
tree | 5a709413f504a962a41f427a16e17fdf5c26efbb /legacy/RDFManifestConverter.jsm | |
download | legacywolf-b6d0fd2cda60196a8228d6fc3b9b330bb1871393.tar.gz legacywolf-b6d0fd2cda60196a8228d6fc3b9b330bb1871393.zip |
initial commit
Diffstat (limited to 'legacy/RDFManifestConverter.jsm')
-rw-r--r-- | legacy/RDFManifestConverter.jsm | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/legacy/RDFManifestConverter.jsm b/legacy/RDFManifestConverter.jsm new file mode 100644 index 0000000..6eda163 --- /dev/null +++ b/legacy/RDFManifestConverter.jsm @@ -0,0 +1,110 @@ + /* 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"]; + +ChromeUtils.defineModuleGetter(this, "RDFDataSource", + "chrome://legacy/content/RDFDataSource.jsm"); + +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)); + } +} + +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; + } +} |