2016-08-19 00:40:08 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["AttributionCode"];
|
2016-08-19 00:40:08 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"AppConstants",
|
2017-01-17 18:48:17 +03:00
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"Services",
|
2017-01-17 18:48:17 +03:00
|
|
|
"resource://gre/modules/Services.jsm"
|
|
|
|
);
|
2018-07-23 17:01:26 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
|
2016-08-19 00:40:08 +03:00
|
|
|
|
|
|
|
const ATTR_CODE_MAX_LENGTH = 200;
|
|
|
|
const ATTR_CODE_VALUE_REGEX = /[a-zA-Z0-9_%\\-\\.\\(\\)]*/;
|
|
|
|
const ATTR_CODE_FIELD_SEPARATOR = "%26"; // URL-encoded &
|
|
|
|
const ATTR_CODE_KEY_VALUE_SEPARATOR = "%3D"; // URL-encoded =
|
2019-07-23 01:06:46 +03:00
|
|
|
const ATTR_CODE_KEYS = [
|
|
|
|
"source",
|
|
|
|
"medium",
|
|
|
|
"campaign",
|
|
|
|
"content",
|
|
|
|
"experiment",
|
|
|
|
"variation",
|
|
|
|
];
|
2016-08-19 00:40:08 +03:00
|
|
|
|
|
|
|
let gCachedAttrData = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an nsIFile for the file containing the attribution data.
|
|
|
|
*/
|
|
|
|
function getAttributionFile() {
|
|
|
|
let file = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
|
|
|
|
// appinfo does not exist in xpcshell, so we need defaults.
|
|
|
|
file.append(Services.appinfo.vendor || "mozilla");
|
|
|
|
file.append(AppConstants.MOZ_APP_NAME);
|
|
|
|
file.append("postSigningData");
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:06:46 +03:00
|
|
|
var AttributionCode = {
|
|
|
|
/**
|
|
|
|
* Returns an object containing a key-value pair for each piece of attribution
|
|
|
|
* data included in the passed-in attribution code string.
|
|
|
|
* If the string isn't a valid attribution code, returns an empty object.
|
|
|
|
*/
|
|
|
|
parseAttributionCode(code) {
|
|
|
|
if (code.length > ATTR_CODE_MAX_LENGTH) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-08-19 00:40:08 +03:00
|
|
|
|
2019-07-23 01:06:46 +03:00
|
|
|
let isValid = true;
|
|
|
|
let parsed = {};
|
|
|
|
for (let param of code.split(ATTR_CODE_FIELD_SEPARATOR)) {
|
|
|
|
let [key, value] = param.split(ATTR_CODE_KEY_VALUE_SEPARATOR, 2);
|
|
|
|
if (key && ATTR_CODE_KEYS.includes(key)) {
|
|
|
|
if (value && ATTR_CODE_VALUE_REGEX.test(value)) {
|
|
|
|
parsed[key] = value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isValid = false;
|
|
|
|
break;
|
2016-08-19 00:40:08 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-23 01:06:46 +03:00
|
|
|
return isValid ? parsed : {};
|
|
|
|
},
|
2016-08-19 00:40:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the attribution code, either from disk or a cached version.
|
|
|
|
* Returns a promise that fulfills with an object containing the parsed
|
|
|
|
* attribution data if the code could be read and is valid,
|
|
|
|
* or an empty object otherwise.
|
2018-07-23 17:01:26 +03:00
|
|
|
*
|
|
|
|
* On windows the attribution service converts utm_* keys, removing "utm_".
|
|
|
|
* On OSX the attributions are set directly on download and retain "utm_". We
|
|
|
|
* strip "utm_" while retrieving the params.
|
2016-08-19 00:40:08 +03:00
|
|
|
*/
|
2019-07-23 01:06:46 +03:00
|
|
|
async getAttrDataAsync() {
|
|
|
|
if (gCachedAttrData != null) {
|
|
|
|
return gCachedAttrData;
|
|
|
|
}
|
2016-08-19 00:40:08 +03:00
|
|
|
|
2019-07-23 01:06:46 +03:00
|
|
|
gCachedAttrData = {};
|
|
|
|
if (AppConstants.platform == "win") {
|
|
|
|
try {
|
|
|
|
let bytes = await OS.File.read(getAttributionFile().path);
|
|
|
|
let decoder = new TextDecoder();
|
|
|
|
let code = decoder.decode(bytes);
|
|
|
|
gCachedAttrData = this.parseAttributionCode(code);
|
|
|
|
} catch (ex) {
|
|
|
|
// The attribution file may already have been deleted,
|
|
|
|
// or it may have never been installed at all;
|
|
|
|
// failure to open or read it isn't an error.
|
|
|
|
}
|
|
|
|
} else if (AppConstants.platform == "macosx") {
|
|
|
|
try {
|
|
|
|
let appPath = Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent
|
|
|
|
.path;
|
|
|
|
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
|
|
|
|
Ci.nsIMacAttributionService
|
|
|
|
);
|
|
|
|
let referrer = attributionSvc.getReferrerUrl(appPath);
|
|
|
|
let params = new URL(referrer).searchParams;
|
|
|
|
for (let key of ATTR_CODE_KEYS) {
|
|
|
|
// We support the key prefixed with utm_ or not, but intentionally
|
|
|
|
// choose non-utm params over utm params.
|
|
|
|
for (let paramKey of [`utm_${key}`, `funnel_${key}`, key]) {
|
|
|
|
if (params.has(paramKey)) {
|
|
|
|
let value = params.get(paramKey);
|
|
|
|
if (value && ATTR_CODE_VALUE_REGEX.test(value)) {
|
|
|
|
gCachedAttrData[key] = value;
|
2018-07-23 17:01:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 01:06:46 +03:00
|
|
|
} catch (ex) {
|
|
|
|
// No attributions
|
2016-08-19 00:40:08 +03:00
|
|
|
}
|
2019-07-23 01:06:46 +03:00
|
|
|
}
|
|
|
|
return gCachedAttrData;
|
2016-08-19 00:40:08 +03:00
|
|
|
},
|
|
|
|
|
2017-10-09 13:03:38 +03:00
|
|
|
/**
|
|
|
|
* Return the cached attribution data synchronously without hitting
|
|
|
|
* the disk.
|
|
|
|
* @returns A dictionary with the attribution data if it's available,
|
|
|
|
* null otherwise.
|
|
|
|
*/
|
|
|
|
getCachedAttributionData() {
|
|
|
|
return gCachedAttrData;
|
|
|
|
},
|
|
|
|
|
2016-08-19 00:40:08 +03:00
|
|
|
/**
|
|
|
|
* Deletes the attribution data file.
|
|
|
|
* Returns a promise that resolves when the file is deleted,
|
|
|
|
* or if the file couldn't be deleted (the promise is never rejected).
|
|
|
|
*/
|
2019-07-23 01:06:46 +03:00
|
|
|
async deleteFileAsync() {
|
|
|
|
try {
|
|
|
|
await OS.File.remove(getAttributionFile().path);
|
|
|
|
} catch (ex) {
|
|
|
|
// The attribution file may already have been deleted,
|
|
|
|
// or it may have never been installed at all;
|
|
|
|
// failure to delete it isn't an error.
|
|
|
|
}
|
2016-08-19 00:40:08 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the cached attribution code value, if any.
|
|
|
|
* Does nothing if called from outside of an xpcshell test.
|
|
|
|
*/
|
|
|
|
_clearCache() {
|
|
|
|
let env = Cc["@mozilla.org/process/environment;1"].getService(
|
|
|
|
Ci.nsIEnvironment
|
|
|
|
);
|
|
|
|
if (env.exists("XPCSHELL_TEST_PROFILE_DIR")) {
|
|
|
|
gCachedAttrData = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|