2014-10-31 21:24:47 +03:00
|
|
|
/* 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
|
2015-03-26 18:53:51 +03:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2015-05-19 15:20:00 +03:00
|
|
|
/*
|
2014-10-31 21:24:47 +03:00
|
|
|
* ManifestProcessor
|
|
|
|
* Implementation of processing algorithms from:
|
|
|
|
* http://www.w3.org/2008/webapps/manifest/
|
|
|
|
*
|
|
|
|
* Creates manifest processor that lets you process a JSON file
|
|
|
|
* or individual parts of a manifest object. A manifest is just a
|
|
|
|
* standard JS object that has been cleaned up.
|
|
|
|
*
|
2015-04-02 18:22:00 +03:00
|
|
|
* .process({jsonText,manifestURL,docURL});
|
2014-10-31 21:24:47 +03:00
|
|
|
*
|
2015-05-27 00:04:59 +03:00
|
|
|
* Depends on ImageObjectProcessor to process things like
|
2015-05-19 15:20:00 +03:00
|
|
|
* icons and splash_screens.
|
|
|
|
*
|
2014-10-31 21:24:47 +03:00
|
|
|
* TODO: The constructor should accept the UA's supported orientations.
|
|
|
|
* TODO: The constructor should accept the UA's supported display modes.
|
2015-04-02 18:22:00 +03:00
|
|
|
* TODO: hook up developer tools to console. (1086997).
|
2014-10-31 21:24:47 +03:00
|
|
|
*/
|
2015-07-30 18:56:12 +03:00
|
|
|
/*globals Components, ValueExtractor, ImageObjectProcessor, ConsoleAPI*/
|
2014-10-31 21:24:47 +03:00
|
|
|
'use strict';
|
|
|
|
const {
|
2015-07-30 18:56:12 +03:00
|
|
|
utils: Cu
|
2014-10-31 21:24:47 +03:00
|
|
|
} = Components;
|
2018-05-26 03:02:29 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ['URL']);
|
2015-04-02 18:22:00 +03:00
|
|
|
const displayModes = new Set(['fullscreen', 'standalone', 'minimal-ui',
|
2014-10-31 21:24:47 +03:00
|
|
|
'browser'
|
|
|
|
]);
|
2015-04-02 18:22:00 +03:00
|
|
|
const orientationTypes = new Set(['any', 'natural', 'landscape', 'portrait',
|
|
|
|
'portrait-primary', 'portrait-secondary', 'landscape-primary',
|
2014-10-31 21:24:47 +03:00
|
|
|
'landscape-secondary'
|
|
|
|
]);
|
2016-04-13 22:55:00 +03:00
|
|
|
const textDirections = new Set(['ltr', 'rtl', 'auto']);
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import('resource://gre/modules/Console.jsm');
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2015-05-27 00:04:59 +03:00
|
|
|
// ValueExtractor is used by the various processors to get values
|
|
|
|
// from the manifest and to report errors.
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import('resource://gre/modules/ValueExtractor.jsm');
|
2015-05-27 00:04:59 +03:00
|
|
|
// ImageObjectProcessor is used to process things like icons and images
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import('resource://gre/modules/ImageObjectProcessor.jsm');
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var ManifestProcessor = { // jshint ignore:line
|
2015-07-30 18:56:12 +03:00
|
|
|
get defaultDisplayMode() {
|
|
|
|
return 'browser';
|
2015-07-29 17:58:00 +03:00
|
|
|
},
|
2015-07-30 18:56:12 +03:00
|
|
|
get displayModes() {
|
|
|
|
return displayModes;
|
|
|
|
},
|
|
|
|
get orientationTypes() {
|
|
|
|
return orientationTypes;
|
2015-04-02 18:22:00 +03:00
|
|
|
},
|
2016-04-13 22:55:00 +03:00
|
|
|
get textDirections() {
|
|
|
|
return textDirections;
|
|
|
|
},
|
2015-05-20 17:58:00 +03:00
|
|
|
// process() method processes JSON text into a clean manifest
|
2015-04-02 18:22:00 +03:00
|
|
|
// that conforms with the W3C specification. Takes an object
|
|
|
|
// expecting the following dictionary items:
|
2015-05-27 00:04:59 +03:00
|
|
|
// * jsonText: the JSON string to be processed.
|
|
|
|
// * manifestURL: the URL of the manifest, to resolve URLs.
|
|
|
|
// * docURL: the URL of the owner doc, for security checks
|
2015-04-02 18:22:00 +03:00
|
|
|
process({
|
2015-05-27 00:04:59 +03:00
|
|
|
jsonText,
|
2015-04-14 17:00:00 +03:00
|
|
|
manifestURL: aManifestURL,
|
|
|
|
docURL: aDocURL
|
2015-04-02 18:22:00 +03:00
|
|
|
}) {
|
2016-02-03 03:47:51 +03:00
|
|
|
const domBundle = Services.strings.createBundle("chrome://global/locale/dom/dom.properties");
|
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
const console = new ConsoleAPI({
|
2016-01-19 15:48:59 +03:00
|
|
|
prefix: 'Web Manifest'
|
2015-04-02 18:22:00 +03:00
|
|
|
});
|
2015-05-20 17:58:00 +03:00
|
|
|
const manifestURL = new URL(aManifestURL);
|
|
|
|
const docURL = new URL(aDocURL);
|
2015-04-02 18:22:00 +03:00
|
|
|
let rawManifest = {};
|
|
|
|
try {
|
2015-05-27 00:04:59 +03:00
|
|
|
rawManifest = JSON.parse(jsonText);
|
2015-04-02 18:22:00 +03:00
|
|
|
} catch (e) {}
|
|
|
|
if (typeof rawManifest !== 'object' || rawManifest === null) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestShouldBeObject'));
|
2015-04-02 18:22:00 +03:00
|
|
|
rawManifest = {};
|
|
|
|
}
|
2016-02-03 03:47:51 +03:00
|
|
|
const extractor = new ValueExtractor(console, domBundle);
|
2015-05-27 00:04:59 +03:00
|
|
|
const imgObjProcessor = new ImageObjectProcessor(console, extractor);
|
2015-04-02 18:22:00 +03:00
|
|
|
const processedManifest = {
|
2016-04-13 22:55:00 +03:00
|
|
|
'dir': processDirMember.call(this),
|
2015-05-27 00:04:59 +03:00
|
|
|
'lang': processLangMember(),
|
|
|
|
'start_url': processStartURLMember(),
|
2015-07-30 18:56:12 +03:00
|
|
|
'display': processDisplayMember.call(this),
|
|
|
|
'orientation': processOrientationMember.call(this),
|
2015-05-27 00:04:59 +03:00
|
|
|
'name': processNameMember(),
|
2015-05-20 17:58:00 +03:00
|
|
|
'icons': imgObjProcessor.process(
|
|
|
|
rawManifest, manifestURL, 'icons'
|
2015-05-19 15:20:00 +03:00
|
|
|
),
|
2015-05-27 00:04:59 +03:00
|
|
|
'short_name': processShortNameMember(),
|
|
|
|
'theme_color': processThemeColorMember(),
|
2016-01-19 21:16:02 +03:00
|
|
|
'background_color': processBackgroundColorMember(),
|
2015-03-26 18:53:51 +03:00
|
|
|
};
|
2015-05-27 00:04:59 +03:00
|
|
|
processedManifest.scope = processScopeMember();
|
2015-04-02 18:22:00 +03:00
|
|
|
return processedManifest;
|
2015-01-16 03:46:00 +03:00
|
|
|
|
2016-04-13 22:55:00 +03:00
|
|
|
function processDirMember() {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: rawManifest,
|
|
|
|
property: 'dir',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true,
|
|
|
|
};
|
|
|
|
const value = extractor.extractValue(spec);
|
|
|
|
if (this.textDirections.has(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return 'auto';
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processNameMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
2015-03-19 13:04:00 +03:00
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'name',
|
2015-03-19 13:04:00 +03:00
|
|
|
expectedType: 'string',
|
2015-04-02 18:22:00 +03:00
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
return extractor.extractValue(spec);
|
2015-01-16 03:46:00 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processShortNameMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'short_name',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
return extractor.extractValue(spec);
|
2015-03-19 13:04:00 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processOrientationMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'orientation',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2016-05-26 01:44:00 +03:00
|
|
|
if (value && typeof value === "string" && this.orientationTypes.has(value.toLowerCase())) {
|
|
|
|
return value.toLowerCase();
|
2015-04-02 18:22:00 +03:00
|
|
|
}
|
2016-05-26 01:35:00 +03:00
|
|
|
return undefined;
|
2014-10-31 21:24:47 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processDisplayMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'display',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2016-05-26 01:44:00 +03:00
|
|
|
if (value && typeof value === "string" && displayModes.has(value.toLowerCase())) {
|
|
|
|
return value.toLowerCase();
|
2015-04-02 18:22:00 +03:00
|
|
|
}
|
2015-07-30 18:56:12 +03:00
|
|
|
return this.defaultDisplayMode;
|
2015-03-26 18:53:51 +03:00
|
|
|
}
|
2015-03-19 13:04:00 +03:00
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processScopeMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'scope',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
|
|
|
let scopeURL;
|
2015-05-27 00:04:59 +03:00
|
|
|
const startURL = new URL(processedManifest.start_url);
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-04-14 17:00:00 +03:00
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return undefined;
|
|
|
|
}
|
2015-04-02 18:22:00 +03:00
|
|
|
try {
|
2015-05-27 00:04:59 +03:00
|
|
|
scopeURL = new URL(value, manifestURL);
|
2015-04-02 18:22:00 +03:00
|
|
|
} catch (e) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestScopeURLInvalid'));
|
2015-04-02 18:22:00 +03:00
|
|
|
return undefined;
|
|
|
|
}
|
2015-05-27 00:04:59 +03:00
|
|
|
if (scopeURL.origin !== docURL.origin) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestScopeNotSameOrigin'));
|
2015-04-02 18:22:00 +03:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// If start URL is not within scope of scope URL:
|
2015-05-27 00:04:59 +03:00
|
|
|
let isSameOrigin = startURL && startURL.origin !== scopeURL.origin;
|
|
|
|
if (isSameOrigin || !startURL.pathname.startsWith(scopeURL.pathname)) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLOutsideScope'));
|
2015-04-02 18:22:00 +03:00
|
|
|
return undefined;
|
|
|
|
}
|
2015-04-14 17:00:00 +03:00
|
|
|
return scopeURL.href;
|
2015-03-26 18:53:51 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processStartURLMember() {
|
2015-04-02 18:22:00 +03:00
|
|
|
const spec = {
|
2015-03-26 18:53:51 +03:00
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-04-02 18:22:00 +03:00
|
|
|
property: 'start_url',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
2015-05-27 00:04:59 +03:00
|
|
|
let result = new URL(docURL).href;
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-04-02 18:22:00 +03:00
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
let potentialResult;
|
|
|
|
try {
|
2015-05-27 00:04:59 +03:00
|
|
|
potentialResult = new URL(value, manifestURL);
|
2015-04-02 18:22:00 +03:00
|
|
|
} catch (e) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLInvalid'))
|
2015-04-02 18:22:00 +03:00
|
|
|
return result;
|
|
|
|
}
|
2015-05-27 00:04:59 +03:00
|
|
|
if (potentialResult.origin !== docURL.origin) {
|
2016-02-03 03:47:51 +03:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLShouldBeSameOrigin'));
|
2015-04-02 18:22:00 +03:00
|
|
|
} else {
|
2015-04-14 17:00:00 +03:00
|
|
|
result = potentialResult.href;
|
2015-03-26 18:53:51 +03:00
|
|
|
}
|
2015-04-02 18:22:00 +03:00
|
|
|
return result;
|
2014-10-31 21:24:47 +03:00
|
|
|
}
|
2015-05-20 17:58:00 +03:00
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processThemeColorMember() {
|
2015-05-20 17:58:00 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-05-20 17:58:00 +03:00
|
|
|
property: 'theme_color',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractor.extractColorValue(spec);
|
|
|
|
}
|
2015-05-25 23:35:33 +03:00
|
|
|
|
2016-01-19 21:16:02 +03:00
|
|
|
function processBackgroundColorMember() {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: rawManifest,
|
|
|
|
property: 'background_color',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractor.extractColorValue(spec);
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
function processLangMember() {
|
2015-05-25 23:35:33 +03:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-27 00:04:59 +03:00
|
|
|
object: rawManifest,
|
2015-05-25 23:35:33 +03:00
|
|
|
property: 'lang',
|
2015-07-30 18:56:12 +03:00
|
|
|
expectedType: 'string', trim: true
|
2015-05-25 23:35:33 +03:00
|
|
|
};
|
|
|
|
let tag = extractor.extractValue(spec);
|
|
|
|
// TODO: Check if tag is structurally valid.
|
|
|
|
// Cannot do this because we don't support Intl API on Android.
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=864843
|
|
|
|
// https://github.com/tc39/ecma402/issues/5
|
|
|
|
// TODO: perform canonicalization on the tag.
|
|
|
|
// Can't do this today because there is no direct means to
|
|
|
|
// access canonicalization algorithms through Intl API.
|
|
|
|
// https://github.com/tc39/ecma402/issues/5
|
|
|
|
return tag;
|
|
|
|
}
|
2015-04-02 18:22:00 +03:00
|
|
|
}
|
|
|
|
};
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ['ManifestProcessor']; // jshint ignore:line
|