2014-10-31 18:24:47 +00: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 15:53:51 +00:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2015-05-19 12:20:00 +00:00
|
|
|
/*
|
2014-10-31 18:24:47 +00: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 15:22:00 +00:00
|
|
|
* .process({jsonText,manifestURL,docURL});
|
2014-10-31 18:24:47 +00:00
|
|
|
*
|
2015-05-26 21:04:59 +00:00
|
|
|
* Depends on ImageObjectProcessor to process things like
|
2015-05-19 12:20:00 +00:00
|
|
|
* icons and splash_screens.
|
|
|
|
*
|
2014-10-31 18:24:47 +00: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 15:22:00 +00:00
|
|
|
* TODO: hook up developer tools to console. (1086997).
|
2014-10-31 18:24:47 +00:00
|
|
|
*/
|
2015-07-30 15:56:12 +00:00
|
|
|
/*globals Components, ValueExtractor, ImageObjectProcessor, ConsoleAPI*/
|
2014-10-31 18:24:47 +00:00
|
|
|
'use strict';
|
|
|
|
const {
|
2015-07-30 15:56:12 +00:00
|
|
|
utils: Cu
|
2014-10-31 18:24:47 +00:00
|
|
|
} = Components;
|
|
|
|
Cu.importGlobalProperties(['URL']);
|
2015-04-02 15:22:00 +00:00
|
|
|
const displayModes = new Set(['fullscreen', 'standalone', 'minimal-ui',
|
2014-10-31 18:24:47 +00:00
|
|
|
'browser'
|
|
|
|
]);
|
2015-04-02 15:22:00 +00:00
|
|
|
const orientationTypes = new Set(['any', 'natural', 'landscape', 'portrait',
|
|
|
|
'portrait-primary', 'portrait-secondary', 'landscape-primary',
|
2014-10-31 18:24:47 +00:00
|
|
|
'landscape-secondary'
|
|
|
|
]);
|
2016-04-13 19:55:00 +00:00
|
|
|
const textDirections = new Set(['ltr', 'rtl', 'auto']);
|
|
|
|
|
2015-10-15 10:45:22 +00:00
|
|
|
Cu.import('resource://gre/modules/Console.jsm');
|
2016-02-03 00:47:51 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2015-05-26 21:04:59 +00:00
|
|
|
// ValueExtractor is used by the various processors to get values
|
|
|
|
// from the manifest and to report errors.
|
2015-07-30 15:56:12 +00:00
|
|
|
Cu.import('resource://gre/modules/ValueExtractor.jsm');
|
2015-05-26 21:04:59 +00:00
|
|
|
// ImageObjectProcessor is used to process things like icons and images
|
2015-07-30 15:56:12 +00:00
|
|
|
Cu.import('resource://gre/modules/ImageObjectProcessor.jsm');
|
2014-10-31 18:24:47 +00:00
|
|
|
|
2015-07-30 15:56:12 +00:00
|
|
|
this.ManifestProcessor = { // jshint ignore:line
|
|
|
|
get defaultDisplayMode() {
|
|
|
|
return 'browser';
|
2015-07-29 14:58:00 +00:00
|
|
|
},
|
2015-07-30 15:56:12 +00:00
|
|
|
get displayModes() {
|
|
|
|
return displayModes;
|
|
|
|
},
|
|
|
|
get orientationTypes() {
|
|
|
|
return orientationTypes;
|
2015-04-02 15:22:00 +00:00
|
|
|
},
|
2016-04-13 19:55:00 +00:00
|
|
|
get textDirections() {
|
|
|
|
return textDirections;
|
|
|
|
},
|
2015-05-20 14:58:00 +00:00
|
|
|
// process() method processes JSON text into a clean manifest
|
2015-04-02 15:22:00 +00:00
|
|
|
// that conforms with the W3C specification. Takes an object
|
|
|
|
// expecting the following dictionary items:
|
2015-05-26 21:04:59 +00: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 15:22:00 +00:00
|
|
|
process({
|
2015-05-26 21:04:59 +00:00
|
|
|
jsonText,
|
2015-04-14 14:00:00 +00:00
|
|
|
manifestURL: aManifestURL,
|
|
|
|
docURL: aDocURL
|
2015-04-02 15:22:00 +00:00
|
|
|
}) {
|
2016-02-03 00:47:51 +00:00
|
|
|
const domBundle = Services.strings.createBundle("chrome://global/locale/dom/dom.properties");
|
|
|
|
|
2015-04-02 15:22:00 +00:00
|
|
|
const console = new ConsoleAPI({
|
2016-01-19 12:48:59 +00:00
|
|
|
prefix: 'Web Manifest'
|
2015-04-02 15:22:00 +00:00
|
|
|
});
|
2015-05-20 14:58:00 +00:00
|
|
|
const manifestURL = new URL(aManifestURL);
|
|
|
|
const docURL = new URL(aDocURL);
|
2015-04-02 15:22:00 +00:00
|
|
|
let rawManifest = {};
|
|
|
|
try {
|
2015-05-26 21:04:59 +00:00
|
|
|
rawManifest = JSON.parse(jsonText);
|
2015-04-02 15:22:00 +00:00
|
|
|
} catch (e) {}
|
|
|
|
if (typeof rawManifest !== 'object' || rawManifest === null) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestShouldBeObject'));
|
2015-04-02 15:22:00 +00:00
|
|
|
rawManifest = {};
|
|
|
|
}
|
2016-02-03 00:47:51 +00:00
|
|
|
const extractor = new ValueExtractor(console, domBundle);
|
2015-05-26 21:04:59 +00:00
|
|
|
const imgObjProcessor = new ImageObjectProcessor(console, extractor);
|
2015-04-02 15:22:00 +00:00
|
|
|
const processedManifest = {
|
2016-04-13 19:55:00 +00:00
|
|
|
'dir': processDirMember.call(this),
|
2015-05-26 21:04:59 +00:00
|
|
|
'lang': processLangMember(),
|
|
|
|
'start_url': processStartURLMember(),
|
2015-07-30 15:56:12 +00:00
|
|
|
'display': processDisplayMember.call(this),
|
|
|
|
'orientation': processOrientationMember.call(this),
|
2015-05-26 21:04:59 +00:00
|
|
|
'name': processNameMember(),
|
2015-05-20 14:58:00 +00:00
|
|
|
'icons': imgObjProcessor.process(
|
|
|
|
rawManifest, manifestURL, 'icons'
|
2015-05-19 12:20:00 +00:00
|
|
|
),
|
2015-05-26 21:04:59 +00:00
|
|
|
'short_name': processShortNameMember(),
|
|
|
|
'theme_color': processThemeColorMember(),
|
2016-01-19 18:16:02 +00:00
|
|
|
'background_color': processBackgroundColorMember(),
|
2015-03-26 15:53:51 +00:00
|
|
|
};
|
2015-05-26 21:04:59 +00:00
|
|
|
processedManifest.scope = processScopeMember();
|
2015-04-02 15:22:00 +00:00
|
|
|
return processedManifest;
|
2015-01-16 00:46:00 +00:00
|
|
|
|
2016-04-13 19:55:00 +00: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-26 21:04:59 +00:00
|
|
|
function processNameMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
2015-03-19 10:04:00 +00:00
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'name',
|
2015-03-19 10:04:00 +00:00
|
|
|
expectedType: 'string',
|
2015-04-02 15:22:00 +00:00
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 14:58:00 +00:00
|
|
|
return extractor.extractValue(spec);
|
2015-01-16 00:46:00 +00:00
|
|
|
}
|
2014-10-31 18:24:47 +00:00
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processShortNameMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'short_name',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 14:58:00 +00:00
|
|
|
return extractor.extractValue(spec);
|
2015-03-19 10:04:00 +00:00
|
|
|
}
|
2014-10-31 18:24:47 +00:00
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processOrientationMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'orientation',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 14:58:00 +00:00
|
|
|
const value = extractor.extractValue(spec);
|
2016-05-25 22:44:00 +00:00
|
|
|
if (value && typeof value === "string" && this.orientationTypes.has(value.toLowerCase())) {
|
|
|
|
return value.toLowerCase();
|
2015-04-02 15:22:00 +00:00
|
|
|
}
|
2016-05-25 22:35:00 +00:00
|
|
|
return undefined;
|
2014-10-31 18:24:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processDisplayMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'display',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
2015-05-20 14:58:00 +00:00
|
|
|
const value = extractor.extractValue(spec);
|
2016-05-25 22:44:00 +00:00
|
|
|
if (value && typeof value === "string" && displayModes.has(value.toLowerCase())) {
|
|
|
|
return value.toLowerCase();
|
2015-04-02 15:22:00 +00:00
|
|
|
}
|
2015-07-30 15:56:12 +00:00
|
|
|
return this.defaultDisplayMode;
|
2015-03-26 15:53:51 +00:00
|
|
|
}
|
2015-03-19 10:04:00 +00:00
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processScopeMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'scope',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
|
|
|
let scopeURL;
|
2015-05-26 21:04:59 +00:00
|
|
|
const startURL = new URL(processedManifest.start_url);
|
2015-05-20 14:58:00 +00:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-04-14 14:00:00 +00:00
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return undefined;
|
|
|
|
}
|
2015-04-02 15:22:00 +00:00
|
|
|
try {
|
2015-05-26 21:04:59 +00:00
|
|
|
scopeURL = new URL(value, manifestURL);
|
2015-04-02 15:22:00 +00:00
|
|
|
} catch (e) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestScopeURLInvalid'));
|
2015-04-02 15:22:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2015-05-26 21:04:59 +00:00
|
|
|
if (scopeURL.origin !== docURL.origin) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestScopeNotSameOrigin'));
|
2015-04-02 15:22:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// If start URL is not within scope of scope URL:
|
2015-05-26 21:04:59 +00:00
|
|
|
let isSameOrigin = startURL && startURL.origin !== scopeURL.origin;
|
|
|
|
if (isSameOrigin || !startURL.pathname.startsWith(scopeURL.pathname)) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLOutsideScope'));
|
2015-04-02 15:22:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2015-04-14 14:00:00 +00:00
|
|
|
return scopeURL.href;
|
2015-03-26 15:53:51 +00:00
|
|
|
}
|
2014-10-31 18:24:47 +00:00
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processStartURLMember() {
|
2015-04-02 15:22:00 +00:00
|
|
|
const spec = {
|
2015-03-26 15:53:51 +00:00
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-04-02 15:22:00 +00:00
|
|
|
property: 'start_url',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
2015-05-26 21:04:59 +00:00
|
|
|
let result = new URL(docURL).href;
|
2015-05-20 14:58:00 +00:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-04-02 15:22:00 +00:00
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
let potentialResult;
|
|
|
|
try {
|
2015-05-26 21:04:59 +00:00
|
|
|
potentialResult = new URL(value, manifestURL);
|
2015-04-02 15:22:00 +00:00
|
|
|
} catch (e) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLInvalid'))
|
2015-04-02 15:22:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-05-26 21:04:59 +00:00
|
|
|
if (potentialResult.origin !== docURL.origin) {
|
2016-02-03 00:47:51 +00:00
|
|
|
console.warn(domBundle.GetStringFromName('ManifestStartURLShouldBeSameOrigin'));
|
2015-04-02 15:22:00 +00:00
|
|
|
} else {
|
2015-04-14 14:00:00 +00:00
|
|
|
result = potentialResult.href;
|
2015-03-26 15:53:51 +00:00
|
|
|
}
|
2015-04-02 15:22:00 +00:00
|
|
|
return result;
|
2014-10-31 18:24:47 +00:00
|
|
|
}
|
2015-05-20 14:58:00 +00:00
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processThemeColorMember() {
|
2015-05-20 14:58:00 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-05-20 14:58:00 +00:00
|
|
|
property: 'theme_color',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractor.extractColorValue(spec);
|
|
|
|
}
|
2015-05-25 20:35:33 +00:00
|
|
|
|
2016-01-19 18:16:02 +00:00
|
|
|
function processBackgroundColorMember() {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: rawManifest,
|
|
|
|
property: 'background_color',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractor.extractColorValue(spec);
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:04:59 +00:00
|
|
|
function processLangMember() {
|
2015-05-25 20:35:33 +00:00
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
2015-05-26 21:04:59 +00:00
|
|
|
object: rawManifest,
|
2015-05-25 20:35:33 +00:00
|
|
|
property: 'lang',
|
2015-07-30 15:56:12 +00:00
|
|
|
expectedType: 'string', trim: true
|
2015-05-25 20:35:33 +00: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 15:22:00 +00:00
|
|
|
}
|
|
|
|
};
|
2015-05-26 21:04:59 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ['ManifestProcessor']; // jshint ignore:line
|