2015-04-22 20:46:00 +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
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*/
|
|
|
|
/*
|
|
|
|
* Manifest obtainer frame script implementation of:
|
2015-05-27 00:04:59 +03:00
|
|
|
* http://www.w3.org/TR/appmanifest/#obtaining
|
2015-04-22 20:46:00 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2015-07-30 18:56:12 +03:00
|
|
|
/*globals Task, ManifestObtainer, ManifestFinder, content, sendAsyncMessage, addMessageListener, Components*/
|
|
|
|
"use strict";
|
2015-04-22 20:46:00 +03:00
|
|
|
const {
|
2015-06-02 22:42:19 +03:00
|
|
|
utils: Cu,
|
2015-04-22 20:46:00 +03:00
|
|
|
} = Components;
|
2015-07-30 18:56:12 +03:00
|
|
|
Cu.import("resource://gre/modules/ManifestObtainer.jsm");
|
|
|
|
Cu.import("resource://gre/modules/ManifestFinder.jsm");
|
2017-01-17 21:24:53 +03:00
|
|
|
Cu.import("resource://gre/modules/ManifestIcons.jsm");
|
2015-07-30 18:56:12 +03:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2015-04-22 20:46:00 +03:00
|
|
|
|
2015-07-30 18:56:12 +03:00
|
|
|
const MessageHandler = {
|
|
|
|
registerListeners() {
|
|
|
|
addMessageListener(
|
|
|
|
"DOM:WebManifest:hasManifestLink",
|
|
|
|
this.hasManifestLink.bind(this)
|
|
|
|
);
|
|
|
|
addMessageListener(
|
|
|
|
"DOM:ManifestObtainer:Obtain",
|
|
|
|
this.obtainManifest.bind(this)
|
|
|
|
);
|
2016-05-30 19:52:00 +03:00
|
|
|
addMessageListener(
|
2016-10-12 22:45:19 +03:00
|
|
|
"DOM:Manifest:FireAppInstalledEvent",
|
|
|
|
this.fireAppInstalledEvent.bind(this)
|
2016-05-30 19:52:00 +03:00
|
|
|
);
|
2017-01-17 21:24:53 +03:00
|
|
|
addMessageListener(
|
|
|
|
"DOM:WebManifest:fetchIcon",
|
|
|
|
this.fetchIcon.bind(this)
|
|
|
|
);
|
2015-07-30 18:56:12 +03:00
|
|
|
},
|
2015-07-29 17:58:00 +03:00
|
|
|
|
2015-07-30 18:56:12 +03:00
|
|
|
/**
|
|
|
|
* Check if the content document includes a link to a web manifest.
|
|
|
|
* @param {Object} aMsg The IPC message, which is destructured to just
|
|
|
|
* get the id.
|
|
|
|
*/
|
|
|
|
hasManifestLink({data: {id}}) {
|
|
|
|
const response = makeMsgResponse(id);
|
|
|
|
response.result = ManifestFinder.contentHasManifestLink(content);
|
|
|
|
response.success = true;
|
|
|
|
sendAsyncMessage("DOM:WebManifest:hasManifestLink", response);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously obtains a web manifest from content by using the
|
|
|
|
* ManifestObtainer and messages back the result.
|
|
|
|
* @param {Object} aMsg The IPC message, which is destructured to just
|
|
|
|
* get the id.
|
|
|
|
*/
|
|
|
|
obtainManifest: Task.async(function* ({data: {id}}) {
|
|
|
|
const response = makeMsgResponse(id);
|
|
|
|
try {
|
|
|
|
response.result = yield ManifestObtainer.contentObtainManifest(content);
|
|
|
|
response.success = true;
|
|
|
|
} catch (err) {
|
|
|
|
response.result = serializeError(err);
|
|
|
|
}
|
|
|
|
sendAsyncMessage("DOM:ManifestObtainer:Obtain", response);
|
|
|
|
}),
|
2016-05-30 19:52:00 +03:00
|
|
|
|
2016-10-12 22:45:19 +03:00
|
|
|
fireAppInstalledEvent({data: {id}}){
|
|
|
|
const ev = new Event("appinstalled");
|
2016-05-30 19:52:00 +03:00
|
|
|
const response = makeMsgResponse(id);
|
|
|
|
if (!content || content.top !== content) {
|
|
|
|
const msg = "Can only dispatch install event on top-level browsing contexts.";
|
|
|
|
response.result = serializeError(new Error(msg));
|
|
|
|
} else {
|
|
|
|
response.success = true;
|
|
|
|
content.dispatchEvent(ev);
|
|
|
|
}
|
2016-10-12 22:45:19 +03:00
|
|
|
sendAsyncMessage("DOM:Manifest:FireAppInstalledEvent", response);
|
2017-01-17 21:24:53 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a manifest and an expected icon size, ask ManifestIcons
|
|
|
|
* to fetch the appropriate icon and send along result
|
|
|
|
*/
|
|
|
|
fetchIcon: Task.async(function* ({data: {id, manifest, iconSize}}) {
|
|
|
|
const response = makeMsgResponse(id);
|
|
|
|
try {
|
|
|
|
response.result =
|
|
|
|
yield ManifestIcons.contentFetchIcon(content, manifest, iconSize);
|
|
|
|
response.success = true;
|
|
|
|
} catch (err) {
|
|
|
|
response.result = serializeError(err);
|
|
|
|
}
|
|
|
|
sendAsyncMessage("DOM:WebManifest:fetchIcon", response);
|
|
|
|
}),
|
|
|
|
|
2015-07-30 18:56:12 +03:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Utility function to Serializes an JS Error, so it can be transferred over
|
|
|
|
* the message channel.
|
|
|
|
* FIX ME: https://bugzilla.mozilla.org/show_bug.cgi?id=1172586
|
|
|
|
* @param {Error} aError The error to serialize.
|
|
|
|
* @return {Object} The serialized object.
|
|
|
|
*/
|
|
|
|
function serializeError(aError) {
|
2015-07-17 04:45:59 +03:00
|
|
|
const clone = {
|
2015-07-30 18:56:12 +03:00
|
|
|
"fileName": aError.fileName,
|
|
|
|
"lineNumber": aError.lineNumber,
|
|
|
|
"columnNumber": aError.columnNumber,
|
|
|
|
"stack": aError.stack,
|
|
|
|
"message": aError.message,
|
|
|
|
"name": aError.name
|
2015-07-17 04:45:59 +03:00
|
|
|
};
|
|
|
|
return clone;
|
|
|
|
}
|
2015-07-08 06:26:32 +03:00
|
|
|
|
2015-07-30 18:56:12 +03:00
|
|
|
function makeMsgResponse(aId) {
|
|
|
|
return {
|
|
|
|
id: aId,
|
|
|
|
success: false,
|
|
|
|
result: undefined
|
2015-07-08 06:26:32 +03:00
|
|
|
};
|
2015-07-30 18:56:12 +03:00
|
|
|
}
|
2015-07-30 16:11:48 +03:00
|
|
|
|
2015-07-30 18:56:12 +03:00
|
|
|
MessageHandler.registerListeners();
|