gecko/dom/manifest/ManifestFinder.jsm
Marcos Caceres 1d3b143a7f Bug 1171200 - Add means of checking if a document links to a manifest. r=billm.
---
 dom/ipc/manifestMessages.js                        | 166 +++++++++-----------
 ...ObjectProcessor.js => ImageObjectProcessor.jsm} |   0
 dom/manifest/ManifestFinder.jsm                    |  58 +++++++
 dom/manifest/ManifestObtainer.js                   |  92 -----------
 dom/manifest/ManifestObtainer.jsm                  | 170 +++++++++++++++++++++
 ...{ManifestProcessor.js => ManifestProcessor.jsm} |  18 +--
 .../{ValueExtractor.js => ValueExtractor.jsm}      |   4 +-
 dom/manifest/WebManifest.jsm                       |  19 ---
 dom/manifest/moz.build                             |  10 +-
 dom/manifest/test/browser.ini                      |   3 +-
 .../test/browser_ManifestObtainer_obtain.js        |   2 +-
 dom/manifest/test/browser_hasManifestLink.js       | 109 +++++++++++++
 dom/manifest/test/common.js                        |   4 +-
 dom/security/test/csp/browser_test_web_manifest.js |  12 +-
 .../csp/browser_test_web_manifest_mixed_content.js |  10 +-
 toolkit/modules/PromiseMessage.jsm                 |  36 +++++
 toolkit/modules/moz.build                          |   1 +
 17 files changed, 467 insertions(+), 247 deletions(-)
 rename dom/manifest/{ImageObjectProcessor.js => ImageObjectProcessor.jsm} (100%)
 create mode 100644 dom/manifest/ManifestFinder.jsm
 delete mode 100644 dom/manifest/ManifestObtainer.js
 create mode 100644 dom/manifest/ManifestObtainer.jsm
 rename dom/manifest/{ManifestProcessor.js => ManifestProcessor.jsm} (95%)
 rename dom/manifest/{ValueExtractor.js => ValueExtractor.jsm} (96%)
 delete mode 100644 dom/manifest/WebManifest.jsm
 create mode 100644 dom/manifest/test/browser_hasManifestLink.js
 create mode 100644 toolkit/modules/PromiseMessage.jsm
2015-07-08 13:26:32 +10:00

59 lines
1.8 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 https://mozilla.org/MPL/2.0/. */
/* globals Components, Task, PromiseMessage */
'use strict';
const {
utils: Cu
} = Components;
Cu.import('resource://gre/modules/PromiseMessage.jsm');
Cu.import('resource://gre/modules/Task.jsm');
/**
* @constructor
*/
function ManifestFinder() {}
/**
* checks if a browser window's document has a conforming
* manifest link relationship.
* @param aWindowOrBrowser the XUL browser or window to check.
* @return {Promise}
*/
ManifestFinder.prototype.hasManifestLink = Task.async(
function* (aWindowOrBrowser) {
const msgKey = 'DOM:WebManifest:hasManifestLink';
if (!(aWindowOrBrowser && (aWindowOrBrowser.namespaceURI || aWindowOrBrowser.location))) {
throw new TypeError('Invalid input.');
}
if (isXULBrowser(aWindowOrBrowser)) {
const mm = aWindowOrBrowser.messageManager;
const reply = yield PromiseMessage.send(mm, msgKey);
return reply.data.result;
}
return checkForManifest(aWindowOrBrowser);
}
);
function isXULBrowser(aBrowser) {
const XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
return (aBrowser.namespaceURI && aBrowser.namespaceURI === XUL);
}
function checkForManifest(aWindow) {
// Only top-level browsing contexts are valid.
if (!aWindow || aWindow.top !== aWindow) {
return false;
}
const elem = aWindow.document.querySelector('link[rel~="manifest"]');
// Only if we have an element and a non-empty href attribute.
if (!elem || !elem.getAttribute('href')) {
return false;
}
return true;
}
this.EXPORTED_SYMBOLS = [ // jshint ignore:line
'ManifestFinder'
];