]> git.gir.st - LegacyFox.git/blob - legacy/RDFManifestConverter.sys.mjs
provide easy way to find modifications to BootstrapLoader
[LegacyFox.git] / legacy / RDFManifestConverter.sys.mjs
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 import {RDFDataSource} from "resource://legacy/RDFDataSource.sys.mjs";
9
10 const RDFURI_INSTALL_MANIFEST_ROOT = "urn:mozilla:install-manifest";
11
12 function EM_R(aProperty) {
13 return `http://www.mozilla.org/2004/em-rdf#${aProperty}`;
14 }
15
16 function getValue(literal) {
17 return literal && literal.getValue();
18 }
19
20 function getProperty(resource, property) {
21 return getValue(resource.getProperty(EM_R(property)));
22 }
23
24 class Manifest {
25 constructor(ds) {
26 this.ds = ds;
27 }
28
29 static loadFromString(text) {
30 return new this(RDFDataSource.loadFromString(text));
31 }
32
33 static loadFromBuffer(buffer) {
34 return new this(RDFDataSource.loadFromBuffer(buffer));
35 }
36
37 static async loadFromFile(uri) {
38 return new this(await RDFDataSource.loadFromFile(uri));
39 }
40 }
41
42 export class InstallRDF extends Manifest {
43 _readProps(source, obj, props) {
44 for (let prop of props) {
45 let val = getProperty(source, prop);
46 if (val != null) {
47 obj[prop] = val;
48 }
49 }
50 }
51
52 _readArrayProp(source, obj, prop, target, decode = getValue) {
53 let result = Array.from(source.getObjects(EM_R(prop)),
54 target => decode(target));
55 if (result.length) {
56 obj[target] = result;
57 }
58 }
59
60 _readArrayProps(source, obj, props, decode = getValue) {
61 for (let [prop, target] of Object.entries(props)) {
62 this._readArrayProp(source, obj, prop, target, decode);
63 }
64 }
65
66 _readLocaleStrings(source, obj) {
67 this._readProps(source, obj, ["name", "description", "creator", "homepageURL"]);
68 this._readArrayProps(source, obj, {
69 locale: "locales",
70 developer: "developers",
71 translator: "translators",
72 contributor: "contributors",
73 });
74 }
75
76 decode() {
77 let root = this.ds.getResource(RDFURI_INSTALL_MANIFEST_ROOT);
78 let result = {};
79
80 let props = ["id", "version", "type", "updateURL", "optionsURL",
81 "optionsType", "aboutURL", "iconURL",
82 "bootstrap", "unpack", "strictCompatibility"];
83 this._readProps(root, result, props);
84
85 let decodeTargetApplication = source => {
86 let app = {};
87 this._readProps(source, app, ["id", "minVersion", "maxVersion"]);
88 return app;
89 };
90
91 let decodeLocale = source => {
92 let localized = {};
93 this._readLocaleStrings(source, localized);
94 return localized;
95 };
96
97 this._readLocaleStrings(root, result);
98
99 this._readArrayProps(root, result, {"targetPlatform": "targetPlatforms"});
100 this._readArrayProps(root, result, {"targetApplication": "targetApplications"},
101 decodeTargetApplication);
102 this._readArrayProps(root, result, {"localized": "localized"},
103 decodeLocale);
104 this._readArrayProps(root, result, {"dependency": "dependencies"},
105 source => getProperty(source, "id"));
106
107 return result;
108 }
109 }
Imprint / Impressum