Bug 1406097 - Use the cached attribution code in the environment. r=mhowell
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Mon, 09 Oct 2017 12:03:38 +0200
changeset 385345 5ad20a772a819d18e086abbe26b9ee33f81a2fd8
parent 385344 4eae12c81330175739f303189d82ddcb0fa434de
child 385346 8d9d8afdea0cbed8b0d82adfb85d3493c73bc077
push id32652
push userarchaeopteryx@coole-files.de
push dateTue, 10 Oct 2017 21:49:31 +0000
treeherdermozilla-central@f1ecd5c26948 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1406097
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1406097 - Use the cached attribution code in the environment. r=mhowell This adds a new sync function to AttributionCode.jsm that returns the cached attribution data. The cached data is then used to populate the environment instead of relying on the async function. MozReview-Commit-ID: F0se7kXspo2
browser/modules/AttributionCode.jsm
toolkit/components/telemetry/TelemetryEnvironment.jsm
--- a/browser/modules/AttributionCode.jsm
+++ b/browser/modules/AttributionCode.jsm
@@ -86,16 +86,26 @@ var AttributionCode = {
       }
 
       gCachedAttrData = parseAttributionCode(code);
       return gCachedAttrData;
     })();
   },
 
   /**
+   * 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;
+  },
+
+  /**
    * 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).
    */
   deleteFileAsync() {
     return (async function() {
       try {
         await OS.File.remove(getAttributionFile().path);
--- a/toolkit/components/telemetry/TelemetryEnvironment.jsm
+++ b/toolkit/components/telemetry/TelemetryEnvironment.jsm
@@ -862,17 +862,17 @@ function EnvironmentCache() {
 
   let p = [];
   this._addonBuilder = new EnvironmentAddonBuilder(this);
   p = [ this._addonBuilder.init() ];
 
   this._currentEnvironment.profile = {};
   p.push(this._updateProfile());
   if (AppConstants.MOZ_BUILD_APP == "browser") {
-    p.push(this._updateAttribution());
+    p.push(this._loadAttributionAsync());
   }
 
   for (const [id, {branch, options}] of gActiveExperimentStartupBuffer.entries()) {
     this.setExperimentActive(id, branch, options);
   }
   gActiveExperimentStartupBuffer = null;
 
   let setup = () => {
@@ -1358,23 +1358,17 @@ EnvironmentCache.prototype = {
    * Update the cached settings data.
    */
   _updateSettings() {
     let updateChannel = null;
     try {
       updateChannel = UpdateUtils.getUpdateChannel(false);
     } catch (e) {}
 
-    // Make sure to retain the attribution code across environment changes.
-    const attributionCode =
-      (this._currentEnvironment.settings &&
-       this._currentEnvironment.settings.attribution) || {};
-
     this._currentEnvironment.settings = {
-      attribution: attributionCode,
       blocklistEnabled: Services.prefs.getBoolPref(PREF_BLOCKLIST_ENABLED, true),
       e10sEnabled: Services.appinfo.browserTabsRemoteAutostart,
       e10sMultiProcesses: Services.appinfo.maxWebProcessCount,
       e10sCohort: Services.prefs.getStringPref(PREF_E10S_COHORT, "unknown"),
       telemetryEnabled: Utils.isTelemetryEnabled,
       locale: getBrowserLocale(),
       update: {
         channel: updateChannel,
@@ -1383,16 +1377,17 @@ EnvironmentCache.prototype = {
       },
       userPrefs: this._getPrefData(),
       sandbox: this._getSandboxData(),
     };
 
     this._currentEnvironment.settings.addonCompatibilityCheckEnabled =
       AddonManager.checkCompatibility;
 
+    this._updateAttribution();
     this._updateDefaultBrowser();
     this._updateSearchEngine();
   },
 
   _getSandboxData() {
     let effectiveContentProcessLevel = null;
     try {
       let sandboxSettings = Cc["@mozilla.org/sandbox/sandbox-settings;1"].
@@ -1420,28 +1415,52 @@ EnvironmentCache.prototype = {
       Utils.millisecondsToDays(creationDate);
     if (resetDate) {
       this._currentEnvironment.profile.resetDate =
         Utils.millisecondsToDays(resetDate);
     }
   },
 
   /**
-   * Update the cached attribution data object.
+   * Load the attribution data object and updates the environment.
    * @returns Promise<> resolved when the I/O is complete.
    */
-  async _updateAttribution() {
-    let data = await AttributionCode.getAttrDataAsync();
-    if (Object.keys(data).length > 0) {
-      this._currentEnvironment.settings.attribution = {};
-      for (let key in data) {
-        this._currentEnvironment.settings.attribution[key] =
-          limitStringToLength(data[key], MAX_ATTRIBUTION_STRING_LENGTH);
-      }
+  async _loadAttributionAsync() {
+    try {
+      await AttributionCode.getAttrDataAsync();
+    } catch (e) {
+      // The AttributionCode.jsm module might not be always available
+      // (e.g. tests). Gracefully handle this.
+      return;
     }
+    this._updateAttribution();
+  },
+
+  /**
+   * Update the environment with the cached attribution data.
+   */
+  _updateAttribution() {
+    let data = null;
+    try {
+      data = AttributionCode.getCachedAttributionData();
+    } catch (e) {
+      // The AttributionCode.jsm module might not be always available
+      // (e.g. tests). Gracefully handle this.
+    }
+
+    if (!data || !Object.keys(data).length) {
+      return;
+    }
+
+    let attributionData = {};
+    for (let key in data) {
+      attributionData[key] =
+        limitStringToLength(data[key], MAX_ATTRIBUTION_STRING_LENGTH);
+    }
+    this._currentEnvironment.settings.attribution = attributionData;
   },
 
   /**
    * Get the partner data in object form.
    * @return Object containing the partner data.
    */
   _getPartner() {
     let partnerData = {