]> git.gir.st - LegacyFox.git/blob - legacy/RDFManifestConverter.jsm
initial commit
[LegacyFox.git] / legacy / RDFManifestConverter.jsm
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
5
6 var EXPORTED_SYMBOLS = ["InstallRDF"];
7
8 ChromeUtils.defineModuleGetter(this, "RDFDataSource",
9 "chrome://legacy/content/RDFDataSource.jsm");
10
11 const RDFURI_INSTALL_MANIFEST_ROOT = "urn:mozilla:install-manifest";
12
13 function EM_R(aProperty) {
14 return `http://www.mozilla.org/2004/em-rdf#${aProperty}`;
15 }
16
17 function getValue(literal) {
18 return literal && literal.getValue();
19 }
20
21 function getProperty(resource, property) {
22 return getValue(resource.getProperty(EM_R(property)));
23 }
24
25 class Manifest {
26 constructor(ds) {
27 this.ds = ds;
28 }
29
30 static loadFromString(text) {
31 return new this(RDFDataSource.loadFromString(text));
32 }
33
34 static loadFromBuffer(buffer) {
35 return new this(RDFDataSource.loadFromBuffer(buffer));
36 }
37
38 static async loadFromFile(uri) {
39 return new this(await RDFDataSource.loadFromFile(uri));
40 }
41 }
42
43 class InstallRDF extends Manifest {
44 _readProps(source, obj, props) {
45 for (let prop of props) {
46 let val = getProperty(source, prop);
47 if (val != null) {
48 obj[prop] = val;
49 }
50 }
51 }
52
53 _readArrayProp(source, obj, prop, target, decode = getValue) {
54 let result = Array.from(source.getObjects(EM_R(prop)),
55 target => decode(target));
56 if (result.length) {
57 obj[target] = result;
58 }
59 }
60
61 _readArrayProps(source, obj, props, decode = getValue) {
62 for (let [prop, target] of Object.entries(props)) {
63 this._readArrayProp(source, obj, prop, target, decode);
64 }
65 }
66
67 _readLocaleStrings(source, obj) {
68 this._readProps(source, obj, ["name", "description", "creator", "homepageURL"]);
69 this._readArrayProps(source, obj, {
70 locale: "locales",
71 developer: "developers",
72 translator: "translators",
73 contributor: "contributors",
74 });
75 }
76
77 decode() {
78 let root = this.ds.getResource(RDFURI_INSTALL_MANIFEST_ROOT);
79 let result = {};
80
81 let props = ["id", "version", "type", "updateURL", "optionsURL",
82 "optionsType", "aboutURL", "iconURL",
83 "bootstrap", "unpack", "strictCompatibility"];
84 this._readProps(root, result, props);
85
86 let decodeTargetApplication = source => {
87 let app = {};
88 this._readProps(source, app, ["id", "minVersion", "maxVersion"]);
89 return app;
90 };
91
92 let decodeLocale = source => {
93 let localized = {};
94 this._readLocaleStrings(source, localized);
95 return localized;
96 };
97
98 this._readLocaleStrings(root, result);
99
100 this._readArrayProps(root, result, {"targetPlatform": "targetPlatforms"});
101 this._readArrayProps(root, result, {"targetApplication": "targetApplications"},
102 decodeTargetApplication);
103 this._readArrayProps(root, result, {"localized": "localized"},
104 decodeLocale);
105 this._readArrayProps(root, result, {"dependency": "dependencies"},
106 source => getProperty(source, "id"));
107
108 return result;
109 }
110 }
Imprint / Impressum