Bug 1335349 - Add survey link option to 'undo' for automigration. r=Dolske, a=jcristau
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Tue, 31 Jan 2017 12:32:27 +0100
changeset 354172 9c1abdef23a872d6c04a93f1de8be8fa73034617
parent 354171 aba7f4593fc97b90d7f1ceac0b7caabe2fd6dd6f
child 354173 8345541aea0bc794a1d238cde7aa0c7390f91087
push id6892
push userryanvm@gmail.com
push dateTue, 14 Feb 2017 16:13:02 +0000
treeherdermozilla-esr52@35e191e72900 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersDolske, jcristau
bugs1335349
milestone52.0
Bug 1335349 - Add survey link option to 'undo' for automigration. r=Dolske, a=jcristau MozReview-Commit-ID: BGsxAdsTu7X
browser/components/migration/AutoMigrate.jsm
browser/components/migration/tests/browser/browser_undo_notification.js
--- a/browser/components/migration/AutoMigrate.jsm
+++ b/browser/components/migration/AutoMigrate.jsm
@@ -11,16 +11,19 @@ const { classes: Cc, interfaces: Ci, res
 const kAutoMigrateEnabledPref = "browser.migrate.automigrate.enabled";
 const kUndoUIEnabledPref = "browser.migrate.automigrate.ui.enabled";
 
 const kAutoMigrateBrowserPref = "browser.migrate.automigrate.browser";
 
 const kAutoMigrateLastUndoPromptDateMsPref = "browser.migrate.automigrate.lastUndoPromptDateMs";
 const kAutoMigrateDaysToOfferUndoPref = "browser.migrate.automigrate.daysToOfferUndo";
 
+const kAutoMigrateUndoSurveyPref = "browser.migrate.automigrate.undo-survey";
+const kAutoMigrateUndoSurveyLocalePref = "browser.migrate.automigrate.undo-survey-locales";
+
 const kNotificationId = "automigration-undo";
 
 Cu.import("resource:///modules/MigrationUtils.jsm");
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
@@ -350,16 +353,17 @@ const AutoMigrate = {
           this._purgeUndoState(this.UNDO_REMOVED_REASON_OFFER_REJECTED);
           this._removeNotificationBars();
         },
       },
       {
         label: MigrationUtils.getLocalizedString("automigration.undo.dontkeep.label"),
         accessKey: MigrationUtils.getLocalizedString("automigration.undo.dontkeep.accesskey"),
         callback: () => {
+          this._maybeOpenUndoSurveyTab(win);
           this.undo();
         },
       },
     ];
     notificationBox.appendNotification(
       message, kNotificationId, null, notificationBox.PRIORITY_INFO_HIGH, buttons
     );
     let remainingDays = Preferences.get(kAutoMigrateDaysToOfferUndoPref, 0);
@@ -546,14 +550,57 @@ const AutoMigrate = {
           visitData.url = visitData.url.href;
         } catch (ignoredEx) {}
         Cu.reportError("Failed to remove a visit: " + JSON.stringify(visitData));
         Cu.reportError(ex);
       }
     }
   }),
 
+  /**
+   * Maybe open a new tab with a survey. The tab will only be opened if all of
+   * the following are true:
+   * - the 'browser.migrate.automigrate.undo-survey' pref is not empty.
+   *   It should contain the URL of the survey to open.
+   * - the 'browser.migrate.automigrate.undo-survey-locales' pref, a
+   *   comma-separated list of language codes, contains the language code
+   *   that is currently in use for the 'global' chrome pacakge (ie the
+   *   locale in which the user is currently using Firefox).
+   *   The URL will be passed through nsIURLFormatter to allow including
+   *   build ids etc. The special additional formatting variable
+   *   "%IMPORTEDBROWSER" is also replaced with the name of the browser
+   *   from which we imported data.
+   *
+   * @param {Window} chromeWindow   A reference to the window in which to open a link.
+   */
+  _maybeOpenUndoSurveyTab(chromeWindow) {
+    let canDoSurveyInLocale = false;
+    try {
+      let surveyLocales = Preferences.get(kAutoMigrateUndoSurveyLocalePref, "");
+      surveyLocales = surveyLocales.split(",").map(str => str.trim());
+      // Strip out any empty elements, so an empty pref doesn't
+      // lead to a an array with 1 empty string in it.
+      surveyLocales = new Set(surveyLocales.filter(str => !!str));
+      let chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"]
+                             .getService(Ci.nsIXULChromeRegistry);
+      canDoSurveyInLocale =
+        surveyLocales.has(chromeRegistry.getSelectedLocale("global"));
+    } catch (ex) {
+      /* ignore exceptions and just don't do the survey. */
+    }
+
+    let migrationBrowser = this.getBrowserUsedForMigration();
+    let rawURL = Preferences.get(kAutoMigrateUndoSurveyPref, "");
+    if (!canDoSurveyInLocale || !migrationBrowser || !rawURL) {
+      return;
+    }
+
+    let url = Services.urlFormatter.formatURL(rawURL);
+    url = url.replace("%IMPORTEDBROWSER%", encodeURIComponent(migrationBrowser));
+    chromeWindow.openUILinkIn(url, "tab");
+  },
+
   QueryInterface: XPCOMUtils.generateQI(
     [Ci.nsIObserver, Ci.nsINavBookmarkObserver, Ci.nsISupportsWeakReference]
   ),
 };
 
 AutoMigrate.init();
--- a/browser/components/migration/tests/browser/browser_undo_notification.js
+++ b/browser/components/migration/tests/browser/browser_undo_notification.js
@@ -33,26 +33,35 @@ add_task(function* autoMigrationUndoNoti
     let notification = getNotification(browser);
     let notificationBox = notification.parentNode;
     notification.querySelector("button.notification-button-default").click();
     ok(!undoCalled, "Undo should not be called when clicking the default button");
     is(notification, notificationBox._closedNotification, "Notification should be closing");
     yield BrowserTestUtils.removeTab(tab);
 
     undoCalled = false;
-    Services.prefs.setCharPref("browser.migrate.automigrate.browser", "someunknownbrowser");
+    Services.prefs.setCharPref("browser.migrate.automigrate.browser", "chrome");
     tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, url, false);
     browser = tab.linkedBrowser;
     if (!getNotification(browser)) {
       info(`Notification for ${url} not immediately present, waiting for it.`);
       yield BrowserTestUtils.waitForNotificationBar(gBrowser, browser, kExpectedNotificationId);
     }
 
     ok(true, `Got notification for ${url}`);
     notification = getNotification(browser);
     notificationBox = notification.parentNode;
+    // Set up the survey:
+    yield SpecialPowers.pushPrefEnv({set: [
+      ["browser.migrate.automigrate.undo-survey", "https://example.com/?browser=%IMPORTEDBROWSER%"],
+      ["browser.migrate.automigrate.undo-survey-locales", "en-US"],
+    ]});
+    let tabOpenedPromise = BrowserTestUtils.waitForNewTab(gBrowser, "https://example.com/?browser=Google%20Chrome");
     notification.querySelector("button:not(.notification-button-default)").click();
     ok(undoCalled, "Undo should be called when clicking the non-default (Don't Keep) button");
     is(notification, notificationBox._closedNotification, "Notification should be closing");
+    let surveyTab = yield tabOpenedPromise;
+    ok(surveyTab, "Should have opened a tab with a survey");
+    yield BrowserTestUtils.removeTab(surveyTab);
     yield BrowserTestUtils.removeTab(tab);
   }
 });