1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Manifest.sys.mjs is the top level api for managing installed web applications
7 * https://www.w3.org/TR/appmanifest/
9 * It is used to trigger the installation of a web application via .install()
10 * and to access the manifest data (including icons).
13 * - Trigger appropriate app installed events
16 import { ManifestObtainer } from "resource://gre/modules/ManifestObtainer.sys.mjs";
18 import { ManifestIcons } from "resource://gre/modules/ManifestIcons.sys.mjs";
22 ChromeUtils.defineESModuleGetters(lazy, {
23 JSONFile: "resource://gre/modules/JSONFile.sys.mjs",
27 * Generates an hash for the given string.
29 * @note The generated hash is returned in base64 form. Mind the fact base64
30 * is case-sensitive if you are going to reuse this code.
32 function generateHash(aString) {
33 const cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
36 cryptoHash.init(Ci.nsICryptoHash.MD5);
37 const stringStream = Cc[
38 "@mozilla.org/io/string-input-stream;1"
39 ].createInstance(Ci.nsIStringInputStream);
40 stringStream.data = aString;
41 cryptoHash.updateFromStream(stringStream, -1);
42 // base64 allows the '/' char, but we can't use it for filenames.
43 return cryptoHash.finish(true).replace(/\//g, "-");
47 * Trims the query parameters from a url
49 function stripQuery(url) {
50 return url.split("?")[0];
53 // Folder in which we store the manifest files
54 const MANIFESTS_DIR = PathUtils.join(PathUtils.profileDir, "manifests");
56 // We maintain a list of scopes for installed webmanifests so we can determine
57 // whether a given url is within the scope of a previously installed manifest
58 const MANIFESTS_FILE = "manifest-scopes.json";
65 constructor(browser, manifestUrl) {
66 this._manifestUrl = manifestUrl;
67 // The key for this is the manifests URL that is required to be unique.
68 // However arbitrary urls are not safe file paths so lets hash it.
69 const fileName = generateHash(manifestUrl) + ".json";
70 this._path = PathUtils.join(MANIFESTS_DIR, fileName);
71 this.browser = browser;
78 set browser(aBrowser) {
79 this._browser = aBrowser;
83 this._store = new lazy.JSONFile({ path: this._path, saveDelayMs: 100 });
84 await this._store.load();
87 async prefetch(browser) {
88 const manifestData = await ManifestObtainer.browserObtainManifest(browser);
89 const icon = await ManifestIcons.browserFetchIcon(
96 manifest: manifestData,
103 const manifestData = await ManifestObtainer.browserObtainManifest(
108 manifest: manifestData,
110 Manifests.manifestInstalled(this);
111 this._store.saveSoon();
114 async icon(expectedSize) {
115 if ("cached_icon" in this._store.data) {
116 return this._store.data.cached_icon;
118 const icon = await ManifestIcons.browserFetchIcon(
120 this._store.data.manifest,
123 // Cache the icon so future requests do not go over the network
124 this._store.data.cached_icon = icon;
125 this._store.saveSoon();
131 this._store.data.manifest.scope || this._store.data.manifest.start_url;
132 return stripQuery(scope);
137 this._store.data.manifest.short_name ||
138 this._store.data.manifest.name ||
139 this._store.data.manifest.short_url
144 return this._manifestUrl;
148 return (this._store.data && this._store.data.installed) || false;
152 return this._store.data.manifest.start_url;
161 * Manifests maintains the list of installed manifests
163 export var Manifests = {
164 async _initialize() {
165 if (this._readyPromise) {
166 return this._readyPromise;
169 // Prevent multiple initializations
170 this._readyPromise = (async () => {
171 // Make sure the manifests have the folder needed to save into
172 await IOUtils.makeDirectory(MANIFESTS_DIR, { ignoreExisting: true });
174 // Ensure any existing scope data we have about manifests is loaded
175 this._path = PathUtils.join(PathUtils.profileDir, MANIFESTS_FILE);
176 this._store = new lazy.JSONFile({ path: this._path });
177 await this._store.load();
179 // If we don't have any existing data, initialize empty
180 if (!this._store.data.hasOwnProperty("scopes")) {
181 this._store.data.scopes = new Map();
185 // Cache the Manifest objects creates as they are references to files
186 // and we do not want multiple file handles
187 this.manifestObjs = new Map();
188 return this._readyPromise;
191 // When a manifest is installed, we save its scope so we can determine if
192 // future visits fall within this manifests scope
193 manifestInstalled(manifest) {
194 this._store.data.scopes[manifest.scope] = manifest.url;
195 this._store.saveSoon();
198 // Given a url, find if it is within an installed manifests scope and if so
199 // return that manifests url
200 findManifestUrl(url) {
201 for (let scope in this._store.data.scopes) {
202 if (url.startsWith(scope)) {
203 return this._store.data.scopes[scope];
209 // Get the manifest given a url, or if not look for a manifest that is
210 // tied to the current page
211 async getManifest(browser, manifestUrl) {
212 // Ensure we have all started up
213 if (!this._readyPromise) {
214 await this._initialize();
217 // If the client does not already know its manifestUrl, we take the
218 // url of the client and see if it matches the scope of any installed
221 const url = stripQuery(browser.currentURI.spec);
222 manifestUrl = this.findManifestUrl(url);
225 // No matches so no manifest
226 if (manifestUrl === null) {
230 // If we have already created this manifest return cached
231 if (this.manifestObjs.has(manifestUrl)) {
232 const manifest = this.manifestObjs.get(manifestUrl);
233 if (manifest.browser !== browser) {
234 manifest.browser = browser;
239 // Otherwise create a new manifest object
240 const manifest = new Manifest(browser, manifestUrl);
241 this.manifestObjs.set(manifestUrl, manifest);
242 await manifest.initialize();