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/. */
8 export var MacAttribution = {
10 * The file path to the `.app` directory.
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;
17 async setAttributionString(aAttrStr, path = this.applicationPath) {
18 return IOUtils.setMacXAttr(
20 "com.apple.application-instance",
21 new TextEncoder().encode(`__MOZCUSTOM__${aAttrStr}`)
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)
37 if (attrStr.startsWith("__MOZCUSTOM__")) {
38 // Return everything after __MOZCUSTOM__
39 return attrStr.slice(13);
42 throw new Error(`No attribution data found in ${path}`);
46 async delAttributionString(path = this.applicationPath) {
47 return IOUtils.delMacXAttr(path, "com.apple.application-instance");