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-19 15:20:00 +03:00
|
|
|
* Depends on ManifestImageObjectProcessor to process things like
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
/*exported EXPORTED_SYMBOLS */
|
2015-04-02 18:22:00 +03:00
|
|
|
/*JSLint options in comment below: */
|
2015-05-19 15:20:00 +03:00
|
|
|
/*globals Components, XPCOMUtils, extractValue*/
|
2014-10-31 21:24:47 +03:00
|
|
|
'use strict';
|
2015-05-19 15:20:00 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ['ManifestProcessor']; // jshint ignore:line
|
2015-04-02 18:22:00 +03:00
|
|
|
const imports = {};
|
2014-10-31 21:24:47 +03:00
|
|
|
const {
|
|
|
|
utils: Cu,
|
|
|
|
classes: Cc,
|
|
|
|
interfaces: Ci
|
|
|
|
} = Components;
|
2015-05-19 15:20:00 +03:00
|
|
|
const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1']
|
|
|
|
.getService(Ci.mozIJSSubScriptLoader);
|
|
|
|
//Add extractValue() helper to this context.
|
|
|
|
scriptLoader.loadSubScript(
|
|
|
|
'resource://gre/modules/manifestValueExtractor.js',
|
|
|
|
this); // jshint ignore:line
|
2015-04-02 18:22:00 +03:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2014-10-31 21:24:47 +03:00
|
|
|
Cu.importGlobalProperties(['URL']);
|
2015-04-02 18:22:00 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(imports, 'Services',
|
|
|
|
'resource://gre/modules/Services.jsm');
|
2015-05-19 15:20:00 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(imports, 'ManifestImageObjectProcessor',
|
|
|
|
'resource://gre/modules/ManifestImageObjectProcessor.jsm');
|
|
|
|
imports.netutil = Cc['@mozilla.org/network/util;1']
|
|
|
|
.getService(Ci.nsINetUtil);
|
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'
|
|
|
|
]);
|
2015-04-02 18:22:00 +03:00
|
|
|
const {
|
|
|
|
ConsoleAPI
|
|
|
|
} = Cu.import('resource://gre/modules/devtools/Console.jsm');
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function ManifestProcessor() {}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
// Static getters
|
|
|
|
Object.defineProperties(ManifestProcessor, {
|
|
|
|
'defaultDisplayMode': {
|
|
|
|
get: function() {
|
|
|
|
return 'browser';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'displayModes': {
|
|
|
|
get: function() {
|
|
|
|
return displayModes;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'orientationTypes': {
|
|
|
|
get: function() {
|
|
|
|
return orientationTypes;
|
2015-03-26 18:53:51 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
}
|
2015-04-02 18:22:00 +03:00
|
|
|
});
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
ManifestProcessor.prototype = {
|
2015-05-19 15:20: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-19 15:20:00 +03:00
|
|
|
// * aJsonText: the JSON string to be processed.
|
|
|
|
// * aManifestURL: the URL of the manifest, to resolve URLs.
|
|
|
|
// * aDocURL: the URL of the owner doc, for security checks.
|
2015-04-02 18:22:00 +03:00
|
|
|
process({
|
2015-04-14 17:00:00 +03:00
|
|
|
jsonText: aJsonText,
|
|
|
|
manifestURL: aManifestURL,
|
|
|
|
docURL: aDocURL
|
2015-04-02 18:22:00 +03:00
|
|
|
}) {
|
2015-04-14 17:00:00 +03:00
|
|
|
const manifestURL = new URL(aManifestURL);
|
|
|
|
const docURL = new URL(aDocURL);
|
2015-04-02 18:22:00 +03:00
|
|
|
const console = new ConsoleAPI({
|
|
|
|
prefix: 'Web Manifest: '
|
|
|
|
});
|
|
|
|
let rawManifest = {};
|
|
|
|
try {
|
2015-04-14 17:00:00 +03:00
|
|
|
rawManifest = JSON.parse(aJsonText);
|
2015-04-02 18:22:00 +03:00
|
|
|
} catch (e) {}
|
|
|
|
if (typeof rawManifest !== 'object' || rawManifest === null) {
|
|
|
|
let msg = 'Manifest needs to be an object.';
|
|
|
|
console.warn(msg);
|
|
|
|
rawManifest = {};
|
|
|
|
}
|
|
|
|
const processedManifest = {
|
2015-05-19 15:20:00 +03:00
|
|
|
'start_url': processStartURLMember(rawManifest, manifestURL, docURL),
|
|
|
|
'display': processDisplayMember(rawManifest),
|
|
|
|
'orientation': processOrientationMember(rawManifest),
|
|
|
|
'name': processNameMember(rawManifest),
|
|
|
|
'icons': imports.ManifestImageObjectProcessor.process(
|
|
|
|
rawManifest, manifestURL, 'icons', console
|
|
|
|
),
|
|
|
|
'splash_screens': imports.ManifestImageObjectProcessor.process(
|
|
|
|
rawManifest, manifestURL, 'splash_screens', console
|
|
|
|
),
|
|
|
|
'short_name': processShortNameMember(rawManifest),
|
2015-03-26 18:53:51 +03:00
|
|
|
};
|
2015-04-02 18:22:00 +03:00
|
|
|
processedManifest.scope = processScopeMember(rawManifest, manifestURL,
|
2015-05-19 15:20:00 +03:00
|
|
|
docURL, new URL(processedManifest['start_url'])); // jshint ignore:line
|
2015-04-14 17:00:00 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
return processedManifest;
|
2015-01-16 03:46:00 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function processNameMember(aManifest) {
|
|
|
|
const spec = {
|
2015-03-19 13:04:00 +03:00
|
|
|
objectName: 'manifest',
|
2015-04-02 18:22:00 +03:00
|
|
|
object: aManifest,
|
|
|
|
property: 'name',
|
2015-03-19 13:04:00 +03:00
|
|
|
expectedType: 'string',
|
2015-04-02 18:22:00 +03:00
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractValue(spec, console);
|
2015-01-16 03:46:00 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function processShortNameMember(aManifest) {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: aManifest,
|
|
|
|
property: 'short_name',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
return extractValue(spec, console);
|
2015-03-19 13:04:00 +03:00
|
|
|
}
|
2014-10-31 21:24:47 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function processOrientationMember(aManifest) {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: aManifest,
|
|
|
|
property: 'orientation',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
const value = extractValue(spec, console);
|
|
|
|
if (ManifestProcessor.orientationTypes.has(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
// The spec special-cases orientation to return the empty string.
|
|
|
|
return '';
|
2014-10-31 21:24:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function processDisplayMember(aManifest) {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: aManifest,
|
|
|
|
property: 'display',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: true
|
|
|
|
};
|
|
|
|
const value = extractValue(spec, console);
|
|
|
|
if (ManifestProcessor.displayModes.has(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return ManifestProcessor.defaultDisplayMode;
|
2015-03-26 18:53:51 +03:00
|
|
|
}
|
2015-03-19 13:04:00 +03:00
|
|
|
|
2015-04-02 18:22:00 +03:00
|
|
|
function processScopeMember(aManifest, aManifestURL, aDocURL, aStartURL) {
|
|
|
|
const spec = {
|
|
|
|
objectName: 'manifest',
|
|
|
|
object: aManifest,
|
|
|
|
property: 'scope',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
|
|
|
let scopeURL;
|
2015-04-14 17:00:00 +03:00
|
|
|
const value = extractValue(spec, console);
|
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return undefined;
|
|
|
|
}
|
2015-04-02 18:22:00 +03:00
|
|
|
try {
|
|
|
|
scopeURL = new URL(value, aManifestURL);
|
|
|
|
} catch (e) {
|
|
|
|
let msg = 'The URL of scope is invalid.';
|
|
|
|
console.warn(msg);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (scopeURL.origin !== aDocURL.origin) {
|
|
|
|
let msg = 'Scope needs to be same-origin as Document.';
|
|
|
|
console.warn(msg);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// If start URL is not within scope of scope URL:
|
|
|
|
let isSameOrigin = aStartURL && aStartURL.origin !== scopeURL.origin;
|
|
|
|
if (isSameOrigin || !aStartURL.pathname.startsWith(scopeURL.pathname)) {
|
|
|
|
let msg =
|
|
|
|
'The start URL is outside the scope, so scope is invalid.';
|
|
|
|
console.warn(msg);
|
|
|
|
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-04-02 18:22:00 +03:00
|
|
|
function processStartURLMember(aManifest, aManifestURL, aDocURL) {
|
|
|
|
const spec = {
|
2015-03-26 18:53:51 +03:00
|
|
|
objectName: 'manifest',
|
2015-04-02 18:22:00 +03:00
|
|
|
object: aManifest,
|
|
|
|
property: 'start_url',
|
|
|
|
expectedType: 'string',
|
|
|
|
trim: false
|
|
|
|
};
|
2015-04-14 17:00:00 +03:00
|
|
|
let result = new URL(aDocURL).href;
|
2015-04-02 18:22:00 +03:00
|
|
|
const value = extractValue(spec, console);
|
|
|
|
if (value === undefined || value === '') {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
let potentialResult;
|
|
|
|
try {
|
|
|
|
potentialResult = new URL(value, aManifestURL);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Invalid URL.');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (potentialResult.origin !== aDocURL.origin) {
|
|
|
|
let msg = 'start_url must be same origin as document.';
|
|
|
|
console.warn(msg);
|
|
|
|
} 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-04-02 18:22:00 +03:00
|
|
|
}
|
|
|
|
};
|
2015-05-19 15:20:00 +03:00
|
|
|
this.ManifestProcessor = ManifestProcessor; // jshint ignore:line
|