2019-11-13 11:26:32 +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/. */
|
|
|
|
|
2017-01-17 21:24:53 +03:00
|
|
|
/*
|
|
|
|
* Manifest.jsm is the top level api for managing installed web applications
|
|
|
|
* https://www.w3.org/TR/appmanifest/
|
|
|
|
*
|
|
|
|
* It is used to trigger the installation of a web application via .install()
|
|
|
|
* and to access the manifest data (including icons).
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - Trigger appropriate app installed events
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { ManifestObtainer } = ChromeUtils.import(
|
2019-01-17 21:18:31 +03:00
|
|
|
"resource://gre/modules/ManifestObtainer.jsm"
|
|
|
|
);
|
2017-01-17 21:24:53 +03:00
|
|
|
const { ManifestIcons } = ChromeUtils.import(
|
2019-01-17 21:18:31 +03:00
|
|
|
"resource://gre/modules/ManifestIcons.jsm"
|
|
|
|
);
|
2019-07-05 11:44:55 +03:00
|
|
|
|
2018-01-30 08:17:48 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(
|
2019-07-05 11:44:55 +03:00
|
|
|
this,
|
2018-01-30 08:17:48 +03:00
|
|
|
"JSONFile",
|
|
|
|
"resource://gre/modules/JSONFile.jsm"
|
|
|
|
);
|
2017-02-17 11:24:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an hash for the given string.
|
|
|
|
*
|
|
|
|
* @note The generated hash is returned in base64 form. Mind the fact base64
|
|
|
|
* is case-sensitive if you are going to reuse this code.
|
|
|
|
*/
|
|
|
|
function generateHash(aString) {
|
|
|
|
const cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
|
|
|
|
Ci.nsICryptoHash
|
|
|
|
);
|
|
|
|
cryptoHash.init(Ci.nsICryptoHash.MD5);
|
|
|
|
const stringStream = Cc[
|
|
|
|
"@mozilla.org/io/string-input-stream;1"
|
|
|
|
].createInstance(Ci.nsIStringInputStream);
|
|
|
|
stringStream.data = aString;
|
|
|
|
cryptoHash.updateFromStream(stringStream, -1);
|
|
|
|
// base64 allows the '/' char, but we can't use it for filenames.
|
|
|
|
return cryptoHash.finish(true).replace(/\//g, "-");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-24 07:58:13 +03:00
|
|
|
* Trims the query parameters from a url
|
2017-02-17 11:24:06 +03:00
|
|
|
*/
|
|
|
|
function stripQuery(url) {
|
|
|
|
return url.split("?")[0];
|
2017-01-17 21:24:53 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 11:24:06 +03:00
|
|
|
// Folder in which we store the manifest files
|
|
|
|
const MANIFESTS_DIR = OS.Path.join(OS.Constants.Path.profileDir, "manifests");
|
|
|
|
|
|
|
|
// We maintain a list of scopes for installed webmanifests so we can determine
|
|
|
|
// whether a given url is within the scope of a previously installed manifest
|
|
|
|
const MANIFESTS_FILE = "manifest-scopes.json";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manifest object
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Manifest {
|
|
|
|
constructor(browser, manifestUrl) {
|
|
|
|
this._manifestUrl = manifestUrl;
|
|
|
|
// The key for this is the manifests URL that is required to be unique.
|
|
|
|
// However arbitrary urls are not safe file paths so lets hash it.
|
|
|
|
const fileName = generateHash(manifestUrl) + ".json";
|
|
|
|
this._path = OS.Path.join(MANIFESTS_DIR, fileName);
|
2019-10-24 07:58:13 +03:00
|
|
|
this.browser = browser;
|
2017-02-17 11:24:06 +03:00
|
|
|
}
|
2017-01-17 21:24:53 +03:00
|
|
|
|
2019-10-24 07:58:13 +03:00
|
|
|
get browser() {
|
|
|
|
return this._browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
set browser(aBrowser) {
|
|
|
|
this._browser = aBrowser;
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize() {
|
2017-11-01 18:17:36 +03:00
|
|
|
this._store = new JSONFile({ path: this._path, saveDelayMs: 100 });
|
2017-02-17 11:24:06 +03:00
|
|
|
await this._store.load();
|
|
|
|
}
|
|
|
|
|
2017-10-18 19:59:58 +03:00
|
|
|
async prefetch(browser) {
|
|
|
|
const manifestData = await ManifestObtainer.browserObtainManifest(browser);
|
|
|
|
const icon = await ManifestIcons.browserFetchIcon(
|
|
|
|
browser,
|
|
|
|
manifestData,
|
|
|
|
192
|
|
|
|
);
|
|
|
|
const data = {
|
|
|
|
installed: false,
|
|
|
|
manifest: manifestData,
|
2018-12-18 23:38:43 +03:00
|
|
|
cached_icon: icon,
|
2017-10-18 19:59:58 +03:00
|
|
|
};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:24:06 +03:00
|
|
|
async install() {
|
|
|
|
const manifestData = await ManifestObtainer.browserObtainManifest(
|
|
|
|
this._browser
|
|
|
|
);
|
|
|
|
this._store.data = {
|
|
|
|
installed: true,
|
2018-12-18 23:38:43 +03:00
|
|
|
manifest: manifestData,
|
2017-02-17 11:24:06 +03:00
|
|
|
};
|
|
|
|
Manifests.manifestInstalled(this);
|
|
|
|
this._store.saveSoon();
|
|
|
|
}
|
|
|
|
|
|
|
|
async icon(expectedSize) {
|
2018-12-18 23:38:43 +03:00
|
|
|
if ("cached_icon" in this._store.data) {
|
2017-03-15 18:39:01 +03:00
|
|
|
return this._store.data.cached_icon;
|
|
|
|
}
|
|
|
|
const icon = await ManifestIcons.browserFetchIcon(
|
2017-02-17 11:24:06 +03:00
|
|
|
this._browser,
|
|
|
|
this._store.data.manifest,
|
|
|
|
expectedSize
|
|
|
|
);
|
2017-03-15 18:39:01 +03:00
|
|
|
// Cache the icon so future requests do not go over the network
|
|
|
|
this._store.data.cached_icon = icon;
|
|
|
|
this._store.saveSoon();
|
|
|
|
return icon;
|
2017-02-17 11:24:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get scope() {
|
|
|
|
const scope =
|
|
|
|
this._store.data.manifest.scope || this._store.data.manifest.start_url;
|
|
|
|
return stripQuery(scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return (
|
|
|
|
this._store.data.manifest.short_name ||
|
2017-10-05 10:20:04 +03:00
|
|
|
this._store.data.manifest.name ||
|
2017-02-17 11:24:06 +03:00
|
|
|
this._store.data.manifest.short_url
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
return this._manifestUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
get installed() {
|
|
|
|
return (this._store.data && this._store.data.installed) || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get start_url() {
|
|
|
|
return this._store.data.manifest.start_url;
|
|
|
|
}
|
2017-03-15 18:39:01 +03:00
|
|
|
|
|
|
|
get path() {
|
|
|
|
return this._path;
|
|
|
|
}
|
2017-01-17 21:24:53 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 11:24:06 +03:00
|
|
|
/*
|
|
|
|
* Manifests maintains the list of installed manifests
|
|
|
|
*/
|
|
|
|
var Manifests = {
|
2019-10-24 07:58:13 +03:00
|
|
|
async _initialize() {
|
|
|
|
if (this._readyPromise) {
|
|
|
|
return this._readyPromise;
|
2017-02-17 11:24:06 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 07:58:13 +03:00
|
|
|
// Prevent multiple initializations
|
|
|
|
this._readyPromise = (async () => {
|
2017-02-17 11:24:06 +03:00
|
|
|
// Make sure the manifests have the folder needed to save into
|
|
|
|
await OS.File.makeDir(MANIFESTS_DIR, { ignoreExisting: true });
|
|
|
|
|
|
|
|
// Ensure any existing scope data we have about manifests is loaded
|
|
|
|
this._path = OS.Path.join(OS.Constants.Path.profileDir, MANIFESTS_FILE);
|
|
|
|
this._store = new JSONFile({ path: this._path });
|
|
|
|
await this._store.load();
|
|
|
|
|
2019-10-24 07:58:13 +03:00
|
|
|
// If we don't have any existing data, initialize empty
|
2017-02-17 11:24:06 +03:00
|
|
|
if (!this._store.data.hasOwnProperty("scopes")) {
|
|
|
|
this._store.data.scopes = new Map();
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2019-10-24 07:58:13 +03:00
|
|
|
// Cache the Manifest objects creates as they are references to files
|
|
|
|
// and we do not want multiple file handles
|
|
|
|
this.manifestObjs = new Map();
|
|
|
|
return this._readyPromise;
|
2017-02-17 11:24:06 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// When a manifest is installed, we save its scope so we can determine if
|
2019-10-24 07:58:13 +03:00
|
|
|
// future visits fall within this manifests scope
|
2017-02-17 11:24:06 +03:00
|
|
|
manifestInstalled(manifest) {
|
|
|
|
this._store.data.scopes[manifest.scope] = manifest.url;
|
|
|
|
this._store.saveSoon();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Given a url, find if it is within an installed manifests scope and if so
|
|
|
|
// return that manifests url
|
|
|
|
findManifestUrl(url) {
|
|
|
|
for (let scope in this._store.data.scopes) {
|
|
|
|
if (url.startsWith(scope)) {
|
|
|
|
return this._store.data.scopes[scope];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get the manifest given a url, or if not look for a manifest that is
|
|
|
|
// tied to the current page
|
|
|
|
async getManifest(browser, manifestUrl) {
|
|
|
|
// Ensure we have all started up
|
2019-10-24 07:58:13 +03:00
|
|
|
if (!this._readyPromise) {
|
|
|
|
await this._initialize();
|
|
|
|
}
|
2017-02-17 11:24:06 +03:00
|
|
|
|
|
|
|
// If the client does not already know its manifestUrl, we take the
|
|
|
|
// url of the client and see if it matches the scope of any installed
|
|
|
|
// manifests
|
|
|
|
if (!manifestUrl) {
|
|
|
|
const url = stripQuery(browser.currentURI.spec);
|
|
|
|
manifestUrl = this.findManifestUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No matches so no manifest
|
|
|
|
if (manifestUrl === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have already created this manifest return cached
|
2019-10-24 07:58:13 +03:00
|
|
|
if (this.manifestObjs.has(manifestUrl)) {
|
|
|
|
const manifest = this.manifestObjs.get(manifestUrl);
|
|
|
|
if (manifest.browser !== browser) {
|
|
|
|
manifest.browser = browser;
|
|
|
|
}
|
|
|
|
return manifest;
|
2017-02-17 11:24:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create a new manifest object
|
2019-10-24 07:58:13 +03:00
|
|
|
const manifest = new Manifest(browser, manifestUrl);
|
|
|
|
this.manifestObjs.set(manifestUrl, manifest);
|
|
|
|
await manifest.initialize();
|
|
|
|
return manifest;
|
2018-12-18 23:38:43 +03:00
|
|
|
},
|
2017-02-17 11:24:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["Manifests"]; // jshint ignore:line
|