Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / manifest / Manifest.sys.mjs
blob15e1e2ef9303adedffd40c8c0cf49b158fc72793
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/. */
5 /*
6  * Manifest.sys.mjs is the top level api for managing installed web applications
7  * https://www.w3.org/TR/appmanifest/
8  *
9  * It is used to trigger the installation of a web application via .install()
10  * and to access the manifest data (including icons).
11  *
12  * TODO:
13  *  - Trigger appropriate app installed events
14  */
16 import { ManifestObtainer } from "resource://gre/modules/ManifestObtainer.sys.mjs";
18 import { ManifestIcons } from "resource://gre/modules/ManifestIcons.sys.mjs";
20 const lazy = {};
22 ChromeUtils.defineESModuleGetters(lazy, {
23   JSONFile: "resource://gre/modules/JSONFile.sys.mjs",
24 });
26 /**
27  * Generates an hash for the given string.
28  *
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.
31  */
32 function generateHash(aString) {
33   const cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
34     Ci.nsICryptoHash
35   );
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, "-");
46 /**
47  * Trims the query parameters from a url
48  */
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";
60 /**
61  * Manifest object
62  */
64 class Manifest {
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;
72   }
74   get browser() {
75     return this._browser;
76   }
78   set browser(aBrowser) {
79     this._browser = aBrowser;
80   }
82   async initialize() {
83     this._store = new lazy.JSONFile({ path: this._path, saveDelayMs: 100 });
84     await this._store.load();
85   }
87   async prefetch(browser) {
88     const manifestData = await ManifestObtainer.browserObtainManifest(browser);
89     const icon = await ManifestIcons.browserFetchIcon(
90       browser,
91       manifestData,
92       192
93     );
94     const data = {
95       installed: false,
96       manifest: manifestData,
97       cached_icon: icon,
98     };
99     return data;
100   }
102   async install() {
103     const manifestData = await ManifestObtainer.browserObtainManifest(
104       this._browser
105     );
106     this._store.data = {
107       installed: true,
108       manifest: manifestData,
109     };
110     Manifests.manifestInstalled(this);
111     this._store.saveSoon();
112   }
114   async icon(expectedSize) {
115     if ("cached_icon" in this._store.data) {
116       return this._store.data.cached_icon;
117     }
118     const icon = await ManifestIcons.browserFetchIcon(
119       this._browser,
120       this._store.data.manifest,
121       expectedSize
122     );
123     // Cache the icon so future requests do not go over the network
124     this._store.data.cached_icon = icon;
125     this._store.saveSoon();
126     return icon;
127   }
129   get scope() {
130     const scope =
131       this._store.data.manifest.scope || this._store.data.manifest.start_url;
132     return stripQuery(scope);
133   }
135   get name() {
136     return (
137       this._store.data.manifest.short_name ||
138       this._store.data.manifest.name ||
139       this._store.data.manifest.short_url
140     );
141   }
143   get url() {
144     return this._manifestUrl;
145   }
147   get installed() {
148     return (this._store.data && this._store.data.installed) || false;
149   }
151   get start_url() {
152     return this._store.data.manifest.start_url;
153   }
155   get path() {
156     return this._path;
157   }
161  * Manifests maintains the list of installed manifests
162  */
163 export var Manifests = {
164   async _initialize() {
165     if (this._readyPromise) {
166       return this._readyPromise;
167     }
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();
182       }
183     })();
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;
189   },
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();
196   },
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];
204       }
205     }
206     return null;
207   },
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();
215     }
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
219     // manifests
220     if (!manifestUrl) {
221       const url = stripQuery(browser.currentURI.spec);
222       manifestUrl = this.findManifestUrl(url);
223     }
225     // No matches so no manifest
226     if (manifestUrl === null) {
227       return null;
228     }
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;
235       }
236       return manifest;
237     }
239     // Otherwise create a new manifest object
240     const manifest = new Manifest(browser, manifestUrl);
241     this.manifestObjs.set(manifestUrl, manifest);
242     await manifest.initialize();
243     return manifest;
244   },