-
Notifications
You must be signed in to change notification settings - Fork 5
iframe install messaging API
As the name implies, iframe-install.html is an iframe that handles app installs in Marketplace. But as it grew we added a bunch of features to it, which are documented below.
We have to use a central origin for all app manipulation done by the Marketplace. That allows us to do navigator.mozApps.getInstalled()
calls to fetch all apps we have installed.
Doing everything from https://marketplace.firefox.com origin instead of app://marketplace.firefox.com has several benefits:
- Some people tweak
allowed_from
in their manifests. When they do, they usually whitelist our https:// origin and not our app:// origin. - It means we are not dependent on our Marketplace app. Everything can be done from Marketplace in your browser.
- We can allow access to this proxy to other apps/websites by whitelisting their origin
Assuming your origin is whitelisted, it's easy:
// First, set up a handler for the messages the iframe is going to send:
window.onmessage = function(e) {
console.log(e);
}
// Then, add the iframe...
var iframe = document.createElement('iframe');
iframe.src = 'https://marketplace.firefox.com/iframe-install.html';
document.body.appendChild(iframe);
// ... And start communicating with it!
iframe.contentWindow.postMessage({name: 'getInstalled'}, 'https://marketplace.firefox.com')
The communication is done by sending an object through postMessage()
. The object sent needs to have a name
property indicating the command you want to send, and in some cases additional properties for parameters.
When a response is returned, it will also be done through a postMessage()
back to the caller. The message will be an object containing various properties depending on the command used. Multiple messages could be sent back, particularly in the case of errors. If an error occurs, an additional error
property will be present, containing an object formed like this: {error: <ERROR_NAME>}
where <ERROR_NAME>
will be a string identifier.
Here are the available commands and their parameters:
Returns an array containing all the manifest URLs for the apps installed by the iframe origin. Uses navigator.mozApps.getInstalled()
behind the scenes.
iframe.contentWindow.postMessage({name: 'getInstalled'}, ...)
{name: 'getInstalled', result: <INSTALLED>}
, where <INSTALLED>
is an array containing all the manifest URLs for the apps installed by the iframe origin.
Launches an app already installed.
-
manifestURL
: the manifest URL corresponding to the app we want to launch
iframe.contentWindow.postMessage({name: 'launch-app', manifestURL: 'https://example.com/manifest.webapp'}, ...)
Installs an app. Contrary to what the name implies, it supports both packaged and hosted apps, as long as the data.product.is_packaged
parameter is passed correctly. Uses navigator.mozApps.installPackage()
or navigator.mozApps.install()
behind the scenes.
-
data
an object containing: -
product
: an object should contain the following properties:is_packaged
,manifest_url
,id
andname
. -
opt
: an optional object. If it has adata
property, that will be passed directly to the underlying DOM method used.
iframe.contentWindow.postMessage({name: 'install-package', data: {product: {id: 42, name: 'Example App', is_packaged: true, manifest_url: 'https://example.com/manifest.webapp'}}}, ...)
{appId: <PRODUCT>.id, product: <PRODUCT>, 'name': 'install-package'}
.
Check if an update is available for a particular app. Uses <app>.checkForUpdate()
behind the scenes.
-
manifestURL
: the manifest URL corresponding to the app we want to check updates for
iframe.contentWindow.postMessage({name: 'check-for-update', manifestURL: 'https://example.com/manifest.webapp'}, ...)
{manifestURL: <MANIFEST_URL>, result: <UPDATE_AVAILABLE>, type: <TYPE>, name: 'check-for-update'}
. <UPDATE_AVAILABLE>
will contain the boolean indicating whether an app is available at this time. <TYPE>
is mostly useful for debugging and contains the original event type that followed the underlying checkForUpdate()
call. Those two properties will not be returned when an error occurs.
Downloads an update for an app. Uses <app>.download() behind the scenes
.
-
manifestURL
: the manifest URL corresponding to the app we want to update.
iframe.contentWindow.postMessage({name: 'apply-update', manifestURL: 'https://example.com/manifest.webapp'}, ...)
{manifestURL: <MANIFEST_URL>, name: 'apply-update'}
, when the download is finished sucessfully.