Bug 1335349 - add survey link option to 'undo' for automigration, r=Dolske
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Tue, 31 Jan 2017 12:32:27 +0100
changeset 341117 9b7386fe607a3a05ad2c0cb8583a4e4b330c4e76
parent 341116 39a45dfe0be9abb8887a3cc555b8b2e3594c9d0e
child 341118 0afa9c6cd411c3c0b4abc62a21de8b344b271577
push id31326
push userkwierso@gmail.com
push dateTue, 07 Feb 2017 23:57:02 +0000
treeherdermozilla-central@c80c2c7bc043 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersDolske
bugs1335349
milestone54.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 1335349 - add survey link option to 'undo' for automigration, r=Dolske 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);
   }
 });