Skip to content

Commit ce9d6f5

Browse files
committed
Bug 1865845: refactor attribution code to clearly separate out flows for mac, nsis, and msix r=nalexander
This function has been difficult to read for awhile. This patch cleans it up in preparation to adjust it for the removal of the cache file on macOS. Differential Revision: https://phabricator.services.mozilla.com/D197203
1 parent e44cf24 commit ce9d6f5

File tree

1 file changed

+73
-63
lines changed

1 file changed

+73
-63
lines changed

browser/components/attribution/AttributionCode.sys.mjs

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,76 @@ export var AttributionCode = {
227227
return s;
228228
},
229229

230+
async _getMacAttrDataAsync() {
231+
let attributionFile = this.attributionFile;
232+
233+
// On macOS, we fish the attribution data from an extended attribute on
234+
// the .app bundle directory.
235+
try {
236+
let attrStr = await lazy.MacAttribution.getAttributionString();
237+
lazy.log.debug(
238+
`getAttrDataAsync: macOS attribution getAttributionString: "${attrStr}"`
239+
);
240+
241+
gCachedAttrData = this.parseAttributionCode(attrStr);
242+
} catch (ex) {
243+
// Avoid partial attribution data.
244+
gCachedAttrData = {};
245+
246+
// No attributions. Just `warn` 'cuz this isn't necessarily an error.
247+
lazy.log.warn("Caught exception fetching macOS attribution codes!", ex);
248+
249+
if (
250+
ex instanceof Ci.nsIException &&
251+
ex.result == Cr.NS_ERROR_UNEXPECTED
252+
) {
253+
// Bad quarantine data.
254+
Services.telemetry
255+
.getHistogramById("BROWSER_ATTRIBUTION_ERRORS")
256+
.add("quarantine_error");
257+
}
258+
}
259+
260+
lazy.log.debug(
261+
`macOS attribution data is ${JSON.stringify(gCachedAttrData)}`
262+
);
263+
264+
// We only want to try to fetch the attribution string once on macOS
265+
try {
266+
let code = this.serializeAttributionData(gCachedAttrData);
267+
lazy.log.debug(`macOS attribution data serializes as "${code}"`);
268+
await this.writeAttributionFile(code);
269+
} catch (ex) {
270+
lazy.log.debug(`Caught exception writing "${attributionFile.path}"`, ex);
271+
Services.telemetry
272+
.getHistogramById("BROWSER_ATTRIBUTION_ERRORS")
273+
.add("write_error");
274+
return gCachedAttrData;
275+
}
276+
277+
lazy.log.debug(
278+
`Returning after successfully writing "${attributionFile.path}"`
279+
);
280+
return gCachedAttrData;
281+
},
282+
283+
async _getWindowsNSISAttrDataAsync() {
284+
return AttributionIOUtils.read(this.attributionFile.path);
285+
},
286+
287+
async _getWindowsMSIXAttrDataAsync() {
288+
// This comes out of windows-package-manager _not_ URL encoded or in an ArrayBuffer,
289+
// but the parsing code wants it that way. It's easier to just provide that
290+
// than have the parsing code support both.
291+
lazy.log.debug(
292+
`winPackageFamilyName is: ${Services.sysinfo.getProperty(
293+
"winPackageFamilyName"
294+
)}`
295+
);
296+
let encoder = new TextEncoder();
297+
return encoder.encode(encodeURIComponent(await this.msixCampaignId()));
298+
},
299+
230300
/**
231301
* Reads the attribution code, either from disk or a cached version.
232302
* Returns a promise that fulfills with an object containing the parsed
@@ -275,58 +345,7 @@ export var AttributionCode = {
275345
lazy.log.debug(
276346
`getAttrDataAsync: macOS && !exists("${attributionFile.path}")`
277347
);
278-
279-
// On macOS, we fish the attribution data from an extended attribute on
280-
// the .app bundle directory.
281-
try {
282-
let attrStr = await lazy.MacAttribution.getAttributionString();
283-
lazy.log.debug(
284-
`getAttrDataAsync: macOS attribution getAttributionString: "${attrStr}"`
285-
);
286-
287-
gCachedAttrData = this.parseAttributionCode(attrStr);
288-
} catch (ex) {
289-
// Avoid partial attribution data.
290-
gCachedAttrData = {};
291-
292-
// No attributions. Just `warn` 'cuz this isn't necessarily an error.
293-
lazy.log.warn("Caught exception fetching macOS attribution codes!", ex);
294-
295-
if (
296-
ex instanceof Ci.nsIException &&
297-
ex.result == Cr.NS_ERROR_UNEXPECTED
298-
) {
299-
// Bad quarantine data.
300-
Services.telemetry
301-
.getHistogramById("BROWSER_ATTRIBUTION_ERRORS")
302-
.add("quarantine_error");
303-
}
304-
}
305-
306-
lazy.log.debug(
307-
`macOS attribution data is ${JSON.stringify(gCachedAttrData)}`
308-
);
309-
310-
// We only want to try to fetch the attribution string once on macOS
311-
try {
312-
let code = this.serializeAttributionData(gCachedAttrData);
313-
lazy.log.debug(`macOS attribution data serializes as "${code}"`);
314-
await this.writeAttributionFile(code);
315-
} catch (ex) {
316-
lazy.log.debug(
317-
`Caught exception writing "${attributionFile.path}"`,
318-
ex
319-
);
320-
Services.telemetry
321-
.getHistogramById("BROWSER_ATTRIBUTION_ERRORS")
322-
.add("write_error");
323-
return gCachedAttrData;
324-
}
325-
326-
lazy.log.debug(
327-
`Returning after successfully writing "${attributionFile.path}"`
328-
);
329-
return gCachedAttrData;
348+
return this._getMacAttrDataAsync();
330349
}
331350

332351
lazy.log.debug(
@@ -339,18 +358,9 @@ export var AttributionCode = {
339358
AppConstants.platform === "win" &&
340359
Services.sysinfo.getProperty("hasWinPackageId")
341360
) {
342-
// This comes out of windows-package-manager _not_ URL encoded or in an ArrayBuffer,
343-
// but the parsing code wants it that way. It's easier to just provide that
344-
// than have the parsing code support both.
345-
lazy.log.debug(
346-
`winPackageFamilyName is: ${Services.sysinfo.getProperty(
347-
"winPackageFamilyName"
348-
)}`
349-
);
350-
let encoder = new TextEncoder();
351-
bytes = encoder.encode(encodeURIComponent(await this.msixCampaignId()));
361+
bytes = await this._getWindowsMSIXAttrDataAsync();
352362
} else {
353-
bytes = await AttributionIOUtils.read(attributionFile.path);
363+
bytes = await this._getWindowsNSISAttrDataAsync();
354364
}
355365
} catch (ex) {
356366
if (DOMException.isInstance(ex) && ex.name == "NotFoundError") {

0 commit comments

Comments
 (0)