MacAttribution.sys.mjs (1690B)
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/. */ 4 5 const NUL = 0x0; 6 const TAB = 0x9; 7 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 }, 16 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 }, 24 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 ); 36 37 if (attrStr.startsWith("__MOZCUSTOM__")) { 38 // Return everything after __MOZCUSTOM__ 39 return attrStr.slice(13); 40 } 41 42 throw new Error(`No attribution data found in ${path}`); 43 }); 44 }, 45 46 async delAttributionString(path = this.applicationPath) { 47 return IOUtils.delMacXAttr(path, "com.apple.application-instance"); 48 }, 49 };