Bug 1343510 - Add createdWeeksAgo and resetWeeksAgo to appInfo. r=MattN, a=jcristau
authorGiorgos Logiotatidis <giorgos@mozilla.com>
Fri, 30 Jun 2017 08:52:08 -0700
changeset 414185 7bd174ec72487842c2f272fda7ef8896a3ac57ab
parent 414184 a7e869953401b3bb8addc58fa47dcf6560483107
child 414186 91c36f63a2f75563986340dd874c0cf4d8153d9f
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, jcristau
bugs1343510
milestone55.0
Bug 1343510 - Add createdWeeksAgo and resetWeeksAgo to appInfo. r=MattN, a=jcristau MozReview-Commit-ID: 9UDG1z1wV3R
browser/components/uitour/UITour-lib.js
browser/components/uitour/UITour.jsm
browser/components/uitour/test/browser_UITour.js
--- a/browser/components/uitour/UITour-lib.js
+++ b/browser/components/uitour/UITour-lib.js
@@ -476,16 +476,23 @@ if (typeof Mozilla == "undefined") {
    *                                                        without user interaction.
    * @property {Boolean} defaultBrowser - Whether the application is the default browser. Since Fx40.
    * @property {String} defaultUpdateChannel - Update channel e.g. 'release', 'beta', 'aurora',
    *                                           'nightly', 'default'
    *                                           (self-built or automated testing builds)
    * @property {String} distribution - Contains the distributionId property. This value will be
    *                                   "default" in most cases but can differ for repack or
    *                                   funnelcake builds. Since Fx48
+   * @property {Number} profileCreatedWeeksAgo - The number of weeks since the profile was created,
+   *                                             starting from 0 for profiles dating less than
+   *                                             seven days old. Since Fx56.
+   * @property {Number} profileResetWeeksAgo - The number of weeks since the profile was last reset,
+   *                                           starting from 0 for profiles reset less than seven
+   *                                           days ago. If the profile has never been reset it
+   *                                           returns null. Since Fx56.
    * @property {String} version - Version string e.g. "48.0a2"
    * @since 35
    */
 
   /**
    * @summary Search service configuration.
    *
    * @description From version 34 through 42 inclusive, a string was returned for the 'selectedSearchEngine'
--- a/browser/components/uitour/UITour.jsm
+++ b/browser/components/uitour/UITour.jsm
@@ -25,16 +25,18 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
   "resource:///modules/CustomizableUI.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry",
   "resource://gre/modules/UITelemetry.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "BrowserUITelemetry",
   "resource:///modules/BrowserUITelemetry.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
   "resource://gre/modules/PrivateBrowsingUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "ProfileAge",
+  "resource://gre/modules/ProfileAge.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "ReaderParent",
   "resource:///modules/ReaderParent.jsm");
 
 XPCOMUtils.defineLazyPreferenceGetter(this, "gPhotonStructure", "browser.photon.structure.enabled");
 
 // See LOG_LEVELS in Console.jsm. Common examples: "All", "Info", "Warn", & "Error".
 const PREF_LOG_LEVEL      = "browser.uitour.loglevel";
 const PREF_SEENPAGEIDS    = "browser.uitour.seenPageIDs";
@@ -1478,53 +1480,17 @@ this.UITour = {
     aPanel.hidden = true;
     aPanel.clientWidth; // flush
     aPanel.hidden = false;
   },
 
   getConfiguration(aMessageManager, aWindow, aConfiguration, aCallbackID) {
     switch (aConfiguration) {
       case "appinfo":
-        let props = ["defaultUpdateChannel", "version"];
-        let appinfo = {};
-        props.forEach(property => appinfo[property] = Services.appinfo[property]);
-
-        // Identifier of the partner repack, as stored in preference "distribution.id"
-        // and included in Firefox and other update pings. Note this is not the same as
-        // Services.appinfo.distributionID (value of MOZ_DISTRIBUTION_ID is set at build time).
-        let distribution =
-          Services.prefs.getDefaultBranch("distribution.").getCharPref("id", "default");
-        appinfo["distribution"] = distribution;
-
-        let isDefaultBrowser = null;
-        try {
-          let shell = aWindow.getShellService();
-          if (shell) {
-            isDefaultBrowser = shell.isDefaultBrowser(false);
-          }
-        } catch (e) {}
-        appinfo["defaultBrowser"] = isDefaultBrowser;
-
-        let canSetDefaultBrowserInBackground = true;
-        if (AppConstants.isPlatformAndVersionAtLeast("win", "6.2") ||
-            AppConstants.isPlatformAndVersionAtLeast("macosx", "10.10")) {
-          canSetDefaultBrowserInBackground = false;
-        } else if (AppConstants.platform == "linux") {
-          // The ShellService may not exist on some versions of Linux.
-          try {
-            aWindow.getShellService();
-          } catch (e) {
-            canSetDefaultBrowserInBackground = null;
-          }
-        }
-
-        appinfo["canSetDefaultBrowserInBackground"] =
-          canSetDefaultBrowserInBackground;
-
-        this.sendPageCallback(aMessageManager, aCallbackID, appinfo);
+        this.getAppInfo(aMessageManager, aWindow, aCallbackID);
         break;
       case "availableTargets":
         this.getAvailableTargets(aMessageManager, aWindow, aCallbackID);
         break;
       case "search":
       case "selectedSearchEngine":
         Services.search.init(rv => {
           let data;
@@ -1571,16 +1537,74 @@ this.UITour = {
         } catch (e) {}
         break;
       default:
         log.error("setConfiguration: Unknown configuration requested: " + aConfiguration);
         break;
     }
   },
 
+  getAppInfo(aMessageManager, aWindow, aCallbackID) {
+    (async() => {
+      let props = ["defaultUpdateChannel", "version"];
+      let appinfo = {};
+      props.forEach(property => appinfo[property] = Services.appinfo[property]);
+
+      // Identifier of the partner repack, as stored in preference "distribution.id"
+      // and included in Firefox and other update pings. Note this is not the same as
+      // Services.appinfo.distributionID (value of MOZ_DISTRIBUTION_ID is set at build time).
+      let distribution =
+          Services.prefs.getDefaultBranch("distribution.").getCharPref("id", "default");
+      appinfo["distribution"] = distribution;
+
+      let isDefaultBrowser = null;
+      try {
+        let shell = aWindow.getShellService();
+        if (shell) {
+          isDefaultBrowser = shell.isDefaultBrowser(false);
+        }
+      } catch (e) {}
+      appinfo["defaultBrowser"] = isDefaultBrowser;
+
+      let canSetDefaultBrowserInBackground = true;
+      if (AppConstants.isPlatformAndVersionAtLeast("win", "6.2") ||
+          AppConstants.isPlatformAndVersionAtLeast("macosx", "10.10")) {
+        canSetDefaultBrowserInBackground = false;
+      } else if (AppConstants.platform == "linux") {
+        // The ShellService may not exist on some versions of Linux.
+        try {
+          aWindow.getShellService();
+        } catch (e) {
+          canSetDefaultBrowserInBackground = null;
+        }
+      }
+
+      appinfo["canSetDefaultBrowserInBackground"] =
+        canSetDefaultBrowserInBackground;
+
+      // Expose Profile creation and last reset dates in weeks.
+      const ONE_WEEK = 7 * 24 * 60 * 60 * 1000;
+      let profileAge = new ProfileAge(null, null);
+      let createdDate = await profileAge.created;
+      let resetDate = await profileAge.reset;
+      let createdWeeksAgo = Math.floor((Date.now() - createdDate) / ONE_WEEK);
+      let resetWeeksAgo = null;
+      if (resetDate) {
+        resetWeeksAgo = Math.floor((Date.now() - resetDate) / ONE_WEEK);
+      }
+      appinfo["profileCreatedWeeksAgo"] = createdWeeksAgo;
+      appinfo["profileResetWeeksAgo"] = resetWeeksAgo;
+
+      this.sendPageCallback(aMessageManager, aCallbackID, appinfo);
+    })().catch(err => {
+      log.error(err);
+      this.sendPageCallback(aMessageManager, aCallbackID, {});
+    })
+  },
+
   getAvailableTargets(aMessageManager, aChromeWindow, aCallbackID) {
     (async () => {
       let window = aChromeWindow;
       let data = this.availableTargetsCache.get(window);
       if (data) {
         log.debug("getAvailableTargets: Using cached targets list", data.targets.join(","));
         this.sendPageCallback(aMessageManager, aCallbackID, data);
         return;
--- a/browser/components/uitour/test/browser_UITour.js
+++ b/browser/components/uitour/test/browser_UITour.js
@@ -3,16 +3,18 @@
 
 "use strict";
 
 var gTestTab;
 var gContentAPI;
 var gContentWindow;
 
 Components.utils.import("resource://testing-common/TelemetryArchiveTesting.jsm", this);
+Components.utils.import("resource://gre/modules/ProfileAge.jsm", this);
+
 
 function test() {
   UITourTest();
 }
 
 var tests = [
   function test_untrusted_host(done) {
     loadUITourTestPage(function() {
@@ -298,16 +300,31 @@ var tests = [
       gContentAPI.getConfiguration("appinfo", (result2) => {
         ok(typeof(result2.distribution) !== "undefined", "Check distribution isn't undefined.");
         is(result2.distribution, testDistributionID, "Should have the distribution as set in preference.");
 
         done();
       });
     });
   },
+  function test_getConfigurationProfileAge(done) {
+    gContentAPI.getConfiguration("appinfo", (result) => {
+      ok(typeof(result.profileCreatedWeeksAgo) === "number", "profileCreatedWeeksAgo should be number.");
+      ok(result.profileResetWeeksAgo === null, "profileResetWeeksAgo should be null.");
+
+      // Set profile reset date to 15 days ago.
+      let profileAccessor = new ProfileAge();
+      profileAccessor.recordProfileReset(Date.now() - (15 * 24 * 60 * 60 * 1000));
+      gContentAPI.getConfiguration("appinfo", (result2) => {
+        ok(typeof(result2.profileResetWeeksAgo) === "number", "profileResetWeeksAgo should be number.");
+        is(result2.profileResetWeeksAgo, 2, "profileResetWeeksAgo should be 2.");
+        done();
+      });
+    });
+  },
   function test_addToolbarButton(done) {
     let placement = CustomizableUI.getPlacementOfWidget("panic-button");
     is(placement, null, "default UI has panic button in the palette");
 
     gContentAPI.getConfiguration("availableTargets", (data) => {
       let available = (data.targets.indexOf("forget") != -1);
       ok(!available, "Forget button should not be available by default");