gecko/dom/ipc/manifestMessages.js
Marcos Caceres 6dad8fe2be Bug 1166405 - Consolidate classes into a general web manifest module. r=ehsan.
Bound EXPORTED_SYMBOLS to `this` in WebManifest.jsm
Reduced number of iterations on random tests

---
 dom/ipc/manifestMessages.js                        |  17 +---
 ...ObjectProcessor.jsm => ImageObjectProcessor.js} |  35 ++++---
 .../{ManifestObtainer.jsm => ManifestObtainer.js}  |   4 +-
 ...{ManifestProcessor.jsm => ManifestProcessor.js} | 109 ++++++++++-----------
 ...anifestValueExtractor.jsm => ValueExtractor.js} |  25 +++--
 dom/manifest/WebManifest.jsm                       |  19 ++++
 dom/manifest/moz.build                             |   9 +-
 .../test/browser_ManifestObtainer_obtain.js        |   9 +-
 dom/manifest/test/common.js                        |  32 +++---
 9 files changed, 135 insertions(+), 124 deletions(-)
 rename dom/manifest/{ManifestImageObjectProcessor.jsm => ImageObjectProcessor.js} (81%)
 rename dom/manifest/{ManifestObtainer.jsm => ManifestObtainer.js} (95%)
 rename dom/manifest/{ManifestProcessor.jsm => ManifestProcessor.js} (69%)
 rename dom/manifest/{ManifestValueExtractor.jsm => ValueExtractor.js} (77%)
 create mode 100644 dom/manifest/WebManifest.jsm
2015-05-26 17:04:59 -04:00

104 lines
3.1 KiB
JavaScript

/* 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
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*/
/*
* Manifest obtainer frame script implementation of:
* http://www.w3.org/TR/appmanifest/#obtaining
*
* It searches a top-level browsing context for
* a <link rel=manifest> element. Then fetches
* and processes the linked manifest.
*
* BUG: https://bugzilla.mozilla.org/show_bug.cgi?id=1083410
*/
/*globals content, sendAsyncMessage, addMessageListener, Components*/
'use strict';
const {
utils: Cu
} = Components;
const {
ManifestProcessor
} = Cu.import('resource://gre/modules/WebManifest.jsm', {});
addMessageListener('DOM:ManifestObtainer:Obtain', (aMsg) => {
fetchManifest()
.then(
manifest => sendAsyncMessage('DOM:ManifestObtainer:Obtain', {
success: true,
result: manifest,
msgId: aMsg.data.msgId
}),
error => sendAsyncMessage('DOM:ManifestObtainer:Obtain', {
success: false,
result: cloneError(error),
msgId: aMsg.data.msgId
})
);
});
function cloneError(aError) {
const clone = {
'fileName': String(aError.fileName),
'lineNumber': String(aError.lineNumber),
'columnNumber': String(aError.columnNumber),
'stack': String(aError.stack),
'message': String(aError.message),
'name': String(aError.name)
};
return clone;
}
function fetchManifest() {
const manifestQuery = 'link[rel~="manifest"]';
return new Promise((resolve, reject) => {
if (!content || content.top !== content) {
let msg = 'Content window must be a top-level browsing context.';
return reject(new Error(msg));
}
const elem = content.document.querySelector(manifestQuery);
if (!elem || !elem.getAttribute('href')) {
let msg = 'No manifest to fetch.';
return reject(new Error(msg));
}
// Will throw on "about:blank" and possibly other invalid URIs.
const manifestURL = new content.URL(elem.href, elem.baseURI);
const reqInit = {
mode: 'cors'
};
if (elem.crossOrigin === 'use-credentials') {
reqInit.credentials = 'include';
}
const req = new content.Request(manifestURL, reqInit);
req.setContext('manifest');
content
.fetch(req)
.then(resp => processResponse(resp, content))
.then(resolve)
.catch(reject);
});
}
function processResponse(aResp, aContentWindow) {
const manifestURL = aResp.url;
return new Promise((resolve, reject) => {
const badStatus = aResp.status < 200 || aResp.status >= 300;
if (aResp.type === 'error' || badStatus) {
let msg =
`Fetch error: ${aResp.status} - ${aResp.statusText} at ${aResp.url}`;
return reject(new Error(msg));
}
aResp
.text()
.then((text) => {
const args = {
jsonText: text,
manifestURL: manifestURL,
docURL: aContentWindow.location.href
};
const processor = new ManifestProcessor();
const manifest = processor.process(args);
resolve(Cu.cloneInto(manifest, content));
}, reject);
});
}