Backed out 2 changesets (bug 1874186, bug 1822097) for causing mochitests failures...
[gecko.git] / browser / components / attribution / MacAttribution.sys.mjs
blob09ec083a44a9761c5deee8995c48419701406d9f
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 const NUL = 0x0;
6 const TAB = 0x9;
8 export var MacAttribution = {
9   /**
10    * The file path to the `.app` directory.
11    */
12   get applicationPath() {
13     // On macOS, `GreD` is like "App.app/Contents/macOS".  Return "App.app".
14     return Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent.path;
15   },
17   async setAttributionString(aAttrStr, path = this.applicationPath) {
18     return IOUtils.setMacXAttr(
19       path,
20       "com.apple.application-instance",
21       new TextEncoder().encode(`__MOZCUSTOM__${aAttrStr}`)
22     );
23   },
25   async getAttributionString(path = this.applicationPath) {
26     let promise = IOUtils.getMacXAttr(path, "com.apple.application-instance");
27     return promise.then(bytes => {
28       // We need to process the extended attribute a little bit to isolate
29       // the attribution string:
30       // - nul bytes and tabs may be present in raw attribution strings, but are
31       //   never part of the attribution data
32       // - attribution data is expected to be preceeded by the string `__MOZCUSTOM__`
33       let attrStr = new TextDecoder().decode(
34         bytes.filter(b => b != NUL && b != TAB)
35       );
37       if (attrStr.startsWith("__MOZCUSTOM__")) {
38         // Return everything after __MOZCUSTOM__
39         return attrStr.slice(13);
40       }
42       throw new Error(`No attribution data found in ${path}`);
43     });
44   },
46   async delAttributionString(path = this.applicationPath) {
47     return IOUtils.delMacXAttr(path, "com.apple.application-instance");
48   },